/* BreadthFirst: breadth-first traversal of a graph. Pre: The graph G has been created. Post: The function Visit has been performed at each vertex of G, where the vertices are chosen in breadth-first order. Uses: Queue functions. */ void BreadthFirst(Graph G, void (*Visit)(Vertex)) { Queue Q; /* QueueEntry defined to be Vertex. */ Boolean visited[MAXVERTEX]; Vertex v, w; for (all v in G) visited[v] = FALSE; CreateQueue(Q); for (all v in G) if (!visited[v]) { Append(v, Q); do { Serve(v,Q); if (!visited[v]) { visited[v] = TRUE; Visit(v); } for (all w adjacent to v) if (!visited[w]) Append(w, Q); } while (!QueueEmpty(Q)); } }