#include #include #include "book.h" #include "grlist.h" #include "aqueue.h" void PreVisit(Graph* G, int v) { cout << "PreVisit vertex " << v << "\n"; } void PostVisit(Graph* G, int v) { cout << "PostVisit vertex " << v << "\n"; } void BFS(Graph* G, int start, Queue* Q) { int v, w; Q->enqueue(start); // Initialize Q G->setMark(start, VISITED); while (Q->length() != 0) { // Process all vertices on Q Q->dequeue(v); PreVisit(G, v); // Take appropriate action for (w=G->first(v); wn(); w = G->next(v,w)) if (G->getMark(w) == UNVISITED) { G->setMark(w, VISITED); Q->enqueue(w); } PostVisit(G, v); // Take appropriate action } } // Test Depth First Search: // Version for Adjancency Matrix representation main(int argc, char** argv) { Graph* G; FILE *fid; if (argc != 2) { cout << "Usage: grdfsm \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); } Queue* Q = new AQueue(G->n()); BFS(G, 0, Q); return 0; }