Whatever-First Search, Depth-first search, Breadth-first search topological sort

May 18, 2023

When studying certain properties of graphs, one general concept is to consider the reachability problem. Given a graph G=(V,E) and an additional vertex sV, we want to know which vertices are reachable from s. We want to find the vertices where there is a path from s to v (sv). One natural tool in any computer science student’s toolbox is the depth-first search algorithm. Two forms of methods can be constructed: recursively or iteratively. Either method works; under the hood, it doesn’t really matter. Just go with whatever you’re more comfortable with.

Preorder and Postorder

Recollecting Preorder and Postorder of rooted trees. If we have a tree noted as T and subtress as T1,...,Tk and whose root is label r the two methods are described as follows

Preorder: We visit the root r, and then recursively do a preorder traversal of T1,...Tk
Postorder We first recursively perform a postorder traversal of the T1,...,Tk subtrees and then visit the root r.

Algorithm 10 Preorder

procedure PreOrder(v)

if vV then

return

end if

Visit(v)

PreOder(v.left)

PreOder(v.right)

end procedure

Algorithm 11 Postorder

procedure PostOrder(v)

if vV then

return

end if

Postorder(v.left)

Postorder(v.right)

Visit(v)

end procedure

Topological sort

Topological sort (Intro to Algorithms 4th ed.)

Algorithm 14 Topological sort

procedure Topological-Sort(G)

call DFS(G) to compute finish times v.f for each vertex v

as each vertex is finished, insert it onto the front of a linked list

return the linked list of vertices

end procedure