#include #include #include "book.h" #define INFINITY 65535 // Big enough for simple tests #include "grlist.h" #include "minheap.h" int minVertex(Graph*, int*); class DijkElem { public: int vertex, distance; DijkElem() { vertex = -1; distance = -1; } DijkElem(int v, int d) { vertex = v; distance = d; } }; class DDComp { public: static bool lt(DijkElem x, DijkElem y) { return x.distance < y.distance; } static bool eq(DijkElem x, DijkElem y) { return x.distance == y.distance; } static bool gt(DijkElem x, DijkElem y) { return x.distance > y.distance; } }; // Dijkstra's shortest paths algorithm w/ priority queue void Dijkstra(Graph* G, int* D, int s) { int i, v, w; // v is current vertex DijkElem temp; DijkElem E[G->e()]; // Heap array with lots of space temp.distance = 0; temp.vertex = s; E[0] = temp; // Initialize heap array minheap H(E, 1, G->e()); // Create heap for (i=0; in(); i++) { // Now, get distances do { if(!H.removemin(temp)) return; // Nothing to remove v = temp.vertex; } while (G->getMark(v) == VISITED); G->setMark(v, VISITED); if (D[v] == INFINITY) return; // Unreachable vertices for (w=G->first(v); wn(); w = G->next(v,w)) if (D[w] > (D[v] + G->weight(v, w))) { // Update D D[w] = D[v] + G->weight(v, w); temp.distance = D[w]; temp.vertex = w; H.insert(temp); // Insert new distance in heap } } } // Test Depth First Search: // Version for Adjancency Matrix representation main(int argc, char** argv) { Graph* G; FILE *fid; if (argc != 2) { cout << "Usage: grdijk1m \n"; exit(-1); } if ((fid = fopen(argv[1], "rt")) == NULL) { cout << "Unable to open file |" << argv[1] << "|\n"; exit(-1); } G = createGraph(fid); if (G == NULL) { cout << "Unable to create graph\n"; exit(-1); } int D[G->n()]; for (int i=0; in(); i++) // Initialize D[i] = INFINITY; D[0] = 0; Dijkstra(G, D, 0); for(int k=0; kn(); k++) cout << D[k] << " "; cout << endl; return 0; }