1

Graph example:

ListDigraph G;

ListGraph::Node A = G.addNode();
ListGraph::Node B = G.addNode();
ListGraph::Node C = G.addNode();
ListGraph::Node D = G.addNode();

ListGraph::Edge AB = G.addEdge(A,B);
ListGraph::Edge AC = G.addEdge(A,C);
ListGraph::Edge AD = G.addEdge(A,D);
ListGraph::Edge BC = G.addEdge(B,C);
ListGraph::Edge BD = G.addEdge(B,C);
ListGraph::Edge CD = G.addEdge(C,D);

I need a method that takes 2 nodes (A and C) for example and returns the ID of the Edge that connects these 2 nodes (if it exists).

Artem Zankovich
  • 2,319
  • 20
  • 36
Philip Xenos
  • 33
  • 1
  • 6

2 Answers2

1

You could store your nodes in a 1D array and your edges in a 2D array. That is, instead of having nodes A, B, C, and D have an array with Nodes[0] through Nodes[3]. Then edge AB could be stored as Edge[0][1]. You are just storing the edges as an adjacency matrix. Then you could find the ID of the edges by using this call: G.id(Edge[0][1]). If you want to verify that an edge exists between those two nodes, then you just have to check that the absolute value of the ID of the edge is less than the total number of edges:

if (abs(G.id(Edge[0][1])) < numberOfEdges)
    return true;
else
    return false;
Seth
  • 11
  • 3
1

In case anyone (like me) is looking for this, Lemon has findEdge(). You could do:

ListGraph G;
ListGraph::Node A = G.addNode();
ListGraph::Node B = G.addNode();
ListGraph::Node C = G.addNode();
ListGraph::Edge AB = G.addEdge(A, B);
ListGraph::Edge BC = G.addEdge(B, C);

ListGraph::Edge FoundEdge = findEdge(G,A,B); // edge
// print existence:
cout << "Does edge A-B exist? " 
     << (FoundEdge!=INVALID ? "yes" : "no") 
     << endl; 
snooze_bear
  • 589
  • 6
  • 14