BFS(G, s)
Input: G, a graph; s, the source vertex from which to begin BFS
Purpose: visits all vertices of G reachable from s
for each vertex v in Vertices(G) color[v] = white color[s] = gray enqueue s into Q while Q is not empty v = Q.peek() for each u adjacent to v if color[u] = white // Found a new node color[u] = gray // Mark it as discovered enqueue u into Q // Enqueue for later exploration dequeue from Q color[v] = black // Mark v as fully explored
DFS(G, s)
Input: A graph, G; a source vertex s
Purpose: visits all vertices of G reachable from s
for each vertex v in Vertices(G) color[v] = white DFS_Visit(s)DFS_Visit(v)
color[v] = gray // Mark v as discovered for each u adjacent to v if color[u] == white DFS_Visit(u) // Found a new node; explore it color[v] = black // Mark v as fully explored
DFS(G, s)
Input: A Graph, G; a source node s
Purpose: visits all nodes of G reachable from s
time = 0 for each vertex v in Vertices(G) color[v] = white for each vertex v in Vertices(G) if color[v] == white DFS_Visit(v)DFS_Visit(v)
color[v] = gray // Mark v as discovered time = time + 1 d[v] = time for each u adjacent to v if color[u] == white DFS_Visit(u) // Found a new node; explore it color[v] = black // Mark v as fully explored time = time + 1 f[v] = time