The following code snippets implement depth-first search and breadth-first search respectively.
void graphTraverse(Graph G) {
int v;
for (v=0; v<G.nodeCount(); v++)
G.setValue(v, null); // Initialize
for (v=0; v<G.nodeCount(); v++)
if (G.getValue(v) != VISITED)
doTraversal(G, v); // Replace with either DFS or BFS
}
void DFS(Graph G, int v) {
PreVisit(G, v);
G.setValue(v, VISITED);
int[] nList = G.neighbors(v);
for (int i=0; i< nList.length; i++)
if (G.getValue(nList[i]) != VISITED)
DFS(G, nList[i]);
PostVisit(G, v);
}
void BFS(Graph G, int v) {
LQueue Q = new LQueue(G.nodeCount());
Q.enqueue(v);
G.setValue(v, VISITED);
while (Q.length() > 0) { // Process each vertex on Q
v = (Integer)Q.dequeue();
PreVisit(G, v);
int[] nList = G.neighbors(v);
for (int i=0; i< nList.length; i++)
if (G.getValue(nList[i]) != VISITED) { // Put neighbors on Q
G.setValue(nList[i], VISITED);
Q.enqueue(nList[i]);
}
PostVisit(G, v);
}
}
Draw the in-memory representations of the undirected version of
the graph above. (In other words, every directed edge is replaced
by an undirected edge.)