The traversal of the graph and the traversal of the non-graph are similar, mainly DFS and BFS.
For Dags (Directed acyclic Graph), there is also a very important concept of topology, the reverse order of topological ordering can be generated with Depth-first search, similar to the second-post traversal of binary tree.
See Depthfirst Order in ALGS4
private void Dfs (Digraph G, int v) {
MARKED[V] = true;
PRE[V] = precounter++;
Preorder.enqueue (v);
for (int W:g.adj (v)) {
if (!marked[w]) {
DFS (G, W);
}
}
Postorder.enqueue (v);
POST[V] = postcounter++;
}
When the postorder is reversed, it is a topological sort of a non-circular graph. One of the real-world scenarios for topological sequencing is scheduling decisions when there are multiple tasks that can be performed at the same time and there is a pre-relationship between tasks.
Programming exercises
Wordnet
Although this project has a definition of shortest ancestor, but he is not a tree, and the side of the point is from the son---father, I use the method is divided into two input nodes to do a DFS, and note the distance from each node to the starting node, Get two array deptha,deptahb, representing the distance of each node to a and B, and then find min (Deptha[i] +deptahb[i]) (0<=i<n).
Princeton University's second week of Algorithm II study notes Directed Graph