Whatever-First Search, Depth-first search, Breadth-first search topological sort
When studying certain properties of graphs, one general concept is to consider the
Depth-first search
Here is the recursive pseudocode for DFSAlgorithm 1 Recursice depth-first search
procedure RecursiveDFS()
if then
mark
for do
RecursiveDFS()
end for
end if
end procedure
Algorithm 2 Iterative depth-first search
procedure IterativeDFS()
Push()
while the stack is not empty do
Pop
if is unmarked then
mark
for each edge do
Push()
end for
end if
end while
end procedure
Given the description and algortihm for depth-first search we can turn to what we used through out the class for most graph problems is
Algorithm 3 Whatever-first search
procedure WhateverFirstSearch()
put into the bag
while the bag is not empty do
take from the bag
if is unmarked then
mark
for each edge do
put into the bag
end for
end if
end while
end procedure
Looking back to
Algorithm 4 Depth-first search
procedure DFS()
if is unmarked then
mark
for each edge do
DFS()
end for
end if
end procedure
One natural question is to consider how every vertex is being marked and ask ourselves could we do better and not mark vertices that have been marked already. But we also would like to consider other useful information that we could possibly obtain when traversing the vertices and edges in a black-box subroutine that for now is left without clarificaiton.
Algorithm 5 Depth-first search
procedure DFS()
mark
PreVisit
for each edge do
if is unmarked then
DFS()
end if
end for
PostVisit
end procedure
We know that any node
Algorithm 6 Depth-first search
procedure DFSAll()
Preprocess
for all vertices do
unmark
end for
for all vertices do
if is unmarked then
DFS()
end if
end for
end procedure
Algorithm 7 Depth-first search
procedure DFSAll()
Preprocess
add vertex
for all vertices do
add edge
unmark
end for
DFS()
end procedure
When considering the input graph
DFS (Intro to Algorithms 4th ed.)
Algorithm 8 DFS
procedure DFS()
for each vertex do
.color = white
= NIL
end for
time =
for each vertex do
if .color == white then
DFS-Visit()
end if
end for
end procedure
Algorithm 9 DFS-Visit
procedure DFS-Visit()
time = time + 1
time
.color = gray
for each vertex in do
if .color == white then
DFS-Visit()
end if
end for
time = time + 1
time
color = black
end procedure
Preorder and Postorder
Recollecting
Algorithm 10 Preorder
procedure PreOrder()
if then
return
end if
Visit
PreOder()
PreOder()
end procedure
Algorithm 11 Postorder
procedure PostOrder()
if then
return
end if
Postorder()
Postorder()
Visit
end procedure
Breadth-first search
The representation of the algorithm is provided by Introduction to Algortihms Fourth Edition textbook.
Algorithm 12 Breadth-first search
procedure BreadthFirstSearch()
for each vertex do
.color = White
NIL
end for
.color = Gray
= 0
NIL
Enqueue
while do
Dequeue
for each vertex in do
if color == White then
color = Gray
Enqueue
end if
end for
color = Black
end while
end procedure
One thing we might wanna do is view the path created by BFS on a given source vertex
Algorithm 13 Print-Path
procedure Print-Path()
if then
else if NIL then
print no path from to exists
else
Print-Path()
end if
end procedure
Topological sort
Topological sort (Intro to Algorithms 4th ed.)
Algorithm 14 Topological sort
procedure Topological-Sort()
call DFS to compute finish times for each vertex
as each vertex is finished, insert it onto the front of a linked list
return the linked list of vertices
end procedure