#include #include #include "book.h" #include "grmat.h" void PreVisit(Graph* G, int v) { cout << "PreVisit vertex " << v << "\n"; } void PostVisit(Graph* G, int v) { cout << "PostVisit vertex " << v << "\n"; } void DFS(Graph* G, int v) { // Depth first search PreVisit(G, v); // Take appropriate action G->setMark(v, VISITED); for (int w=G->first(v); wn(); w = G->next(v,w)) if (G->getMark(w) == UNVISITED) DFS(G, 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); } DFS(G, 0); return 0; }