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:
If you don’t need to store edge metadata at all, then you could go even simpler:
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: