One way to store the graph is to implement nodes as structures, like
struct node {
int vertex; node* next;
};
where vertex stores the vertex number and next contains link to the other node.
Another way I can think of is to implement it as vectors, like
vector<vector< pair<int,int> > G;
Now, while applying Dijkstra's algorithm for shortest path, we need to build priority queue and other required data structures and so as in case 2 (vector implementation). Will there be any difference in complexity in above two different methods of applying graph? Which one is preferable?
EDIT: In first case, every node is associated with a linked list of nodes which are directly accessible from the given node. In second case, G.size() is the number of vertices in our graph G[i].size() is the number of vertices directly reachable from vertex with index i G[i][j].first is the index of j-th vertex reachable from vertex i G[i][j].second is the length of the edge heading from vertex i to vertex G[i][j].first
vector<list<int> >)? For the second, will every pair inG[x]containx(otherwise you'll need to elaborate)? - Bernhard Barker