Question

I have a undirected graph that is represented by an adjacency list. I'm trying to read unique edges from the graph(because its undirected, if 0-1 is considered, 1-0 shouldnt be). Right now, I'm thinking i'd use a field in the adjacency list to denote that an edge has already been read, but even to set it, I have to traverse the list of a vertex. Is there a good way to do this?

Était-ce utile?

La solution

Consider storing in an unordered_map what you have already traversed, populating this as you traverse. You can have they key be a pair of (node1, node2) and the entry be whatever auxiliary information you need to keep about that edge, and then when traversing, look up if you have (node1, node2) or (node2, node1) in the unordered_map. If you do, the edge if not unique.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top