Is a Map that contains values that have references to other keys in the Map the simplest form of a directed graph?

StackOverflow https://stackoverflow.com/questions/7476002

  •  23-01-2021
  •  | 
  •  

문제

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);
}
도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top