包含引用 Map 中其他键的值的 Map 是有向图的最简单形式吗?

发布于 2024-12-05 09:35:35 字数 992 浏览 0 评论 0原文

Map<K,V<List<K>> graph = new HashMap<K,V<List<K>>();

使用它来表示可以循环的有向图是否存在任何主要障碍?

编辑:

这比应有的更令人困惑。这是一个角色扮演游戏的对话图,这是我到目前为止所拥有的。我试图确定是否可以将其重构为更简单的形式:

为 NPC 初始化:

public interface ConversationGraphI {

    void init();

    Map<String, DialogueNodeI> getConversation();

    void setConversation(Map<String, DialogueNodeI> conversation);
}

一段对话,带有响应选项:

public interface DialogueNodeI {

    String getText();

    void setText(String text);

    List<ResponseChoiceI> getResponseChoices();

    void setResponseChoices(List<ResponseChoiceI> responseChoices);
}

一个响应选择,然后可以循环回地图中的另一段对话:

public interface ResponseChoiceI {

    String getResponseText();

    void setResponseText(String responseText);

    String getDialogueKey();

    void setDialogueKey(String dialogueKey);
}
Map<K,V<List<K>> graph = new HashMap<K,V<List<K>>();

Are there any major obstacles to using this to represent a directed graph that can be cyclical?

EDIT:

This was more confusing than it probably should have been. This is a conversation graph for an RPG, and here is what I have so far. I was trying to determine if I could refactor this into a simpler form:

Initialized for an NPC:

public interface ConversationGraphI {

    void init();

    Map<String, DialogueNodeI> getConversation();

    void setConversation(Map<String, DialogueNodeI> conversation);
}

A piece of dialogue, with response options:

public interface DialogueNodeI {

    String getText();

    void setText(String text);

    List<ResponseChoiceI> getResponseChoices();

    void setResponseChoices(List<ResponseChoiceI> responseChoices);
}

A response choice that can then loop back to another piece of dialogue in the map:

public interface ResponseChoiceI {

    String getResponseText();

    void setResponseText(String responseText);

    String getDialogueKey();

    void setDialogueKey(String dialogueKey);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一萌ing 2024-12-12 09:35:35

我认为它的主要问题是您可能无法轻松存储有关每个边缘的数据。这取决于 V 类型的对象是否会给你这个。不过,我同意 Andrei LED 的评论,即最好使用显式边缘类型:

Map<K,Collection<Edge<K>>>

如果您根本不需要存储边缘元数据,那么您可以更简单:

Map<K,Collection<K>>

作为 all-in- 的替代方案一种方法是,我看到由两个单独的集合表示的图,一个用于节点,一个用于边缘。如果 N 是一个节点,那么像这样:

Collection<N>  // nodes
Collection<Edge<N>>  // edges between nodes

I think the main issue with it is that you might not be able to store data about each edge easily. It depends on whether an object of type V will give you that. Still, I agree with Andrei LED's comment that it'd be better to use an explicit Edge type:

Map<K,Collection<Edge<K>>>

If you don't need to store edge metadata at all, then you could go even simpler:

Map<K,Collection<K>>

As an alternative to the all-in-one approach, I've seen graphs represented by two separate collections, one for nodes and one for edges. If N is a node, then something like this:

Collection<N>  // nodes
Collection<Edge<N>>  // edges between nodes
_蜘蛛 2024-12-12 09:35:35

好吧,我猜你必须使用 TreeMap 来让你的键按其自然顺序排序。如果不是,那么它现在将是一个非直接图,不是吗?除此之外,它是否会遵守直接图的所有其他标准将取决于您为放入其中的 Keys 和 Values 对象提供的实际实现。

Well I guess you'd have to use a TreeMap to have your keys ordered by their natural order. If not then it would be an undirect graph now wouldn't it? Other than that, whether it will respect all the other criterias of a direct graph will depend on what actual implementation you give to the Keys and Values objects you put in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文