answers are in italics
Start the DFS at vertex A, and, given a choice, visit neighbors of each vertex in alphabetical order.
Recall that the dfs-numbering of the vertices gives the order in which the vertices are first encountered during the DFS.
Define the dfs post-order numbering of the vertices so that it gives the order in which the vertices are first finished with during the DFS.
For example, a vertex v has post-order number 1 if it is the first one such that the call DFS(v) returns. (Such a vertex will always be a leaf in the dfs tree.) The vertex with post-order number N (the number of vertices) will always be the root of the DFS tree, provided all vertices are reachable from the root.
1A. What are the dfs-numbers of the vertices?
1B. What are the dfs-post-order numbers of the vertices?
1C. If you run BFS (breadth-first search, starting at A) what are the distance labels of the vertices?
vertex: | A | B | C | D | E | F | G | H | J |
dfs-number: | 1 | 2 | 7 | 3 | 6 | 8 | 9 | 4 | 5 |
post-order: | 9 | 5 | 8 | 3 | 4 | 6 | 7 | 1 | 2 |
distance: | 0 | 1 | 1 | 2 | 2 | 2 | 1 | 3 | 3 |
Recall that DFS on a directed graph classifies edges into tree, back, cross, and forward edges.
2A. List the edges of each type in the example you did above.
2B. In general, if an edge (u,v) is of the type in the first column below, which conditions are possible?
type | pre[u] < pre[v] | pre[u] > pre[v] | post[u] < post[v] | post[u] > post[v] |
tree: | ? | ? | ? | ? |
back: | ? | ? | ? | ? |
forward: | ? | ? | ? | ? |
cross: | ? | ? | ? | ? |
Above pre[u] denotes the dfs-number of u, post[u] denotes the dfs post-order number, defined in problem 1.
2C. In terms of the post-order numbers, what is unique about back edges? Use this to give another argument that, if the DFS yields no back edges, then the graph has no cycles.
2A.
2B.
type | pre[u] < pre[v] | pre[u] > pre[v] | post[u] < post[v] | post[u] > post[v] |
tree: | yes | no | yes | no |
back: | no | yes | no | yes |
forward: | yes | no | yes | no |
cross: | no | yes | yes | no |
2C. (u,v) is a back edge if and only if post[u] > post[v].
If the DFS yields no back edges, then every edge (u,v) in the graph has post[u] < post[v]. This means there can't be a cycle, because as you follow any path in the graph, the post-order numbers of the vertices must strictly increase.
Describe an algorithm that, given a directed graph G with no cycles, outputs the vertices of the graph in some order v1, v2, ..., vn so that any directed edge in the graph goes backward in the ordering. That is, if there is an edge from vi to vj, then i ≥ j.
Your algorithm should run in time O(N+M) where N is the number of vertices and M is the number of edges. Argue that your algorithm does this.
Use either of the methods described in class or the book for topologically sorting the graph in linear time (but then output the vertices in reverse order).
For example, run DFS on the graph, then output the vertices in order of increasing post-order number.
(A more complete answer would describe the details of this.)
In class we claimed but did not prove that in a DFS of an undirected graph, the root vertex v is a cut vertex if and only if it has more than one child in the DFS tree. (Recall that a cut vertex is a vertex whose removal disconnects the graph.)
Prove this claim. Argue carefully that
4B. Suppose the root vertex R has at least two children U and W in the DFS tree. Since the graph is undirected, the DFS yields no cross edges. Thus, there are no edges between U or its descendants and W or its descendants. Thus, all paths between U and W go through R. Thus, removing R from the graph disconnects U and W. Therefore R is a cut vertex.