ClassS04CS141/Hwk2Soln

ClassS04CS141 | ClassS04CS141 | recent changes | Preferences

Difference (from prior major revision) (no other diffs)

Changed: 79,82c79,82
|| tree: || yes || no || yes || no ||
|| back: || no || yes || no || yes ||
|| forward: || yes || no || yes || no ||
|| cross: || no || yes || yes || no ||
|| tree: || yes || no || no || yes ||
|| back: || no || yes || yes || no ||
|| forward: || yes || no || no || yes ||
|| cross: || no || yes || no || yes ||

Changed: 84c84
2C. (u,v) is a back edge if and only if post[u] > post[v].
2C. (u,v) is a back edge if and only if post[u] < post[v].

Changed: 137c137
</i>

</i>


hwk 2 solutions

answers are in italics

1. Simulate DFS on the directed graph with the following edge and vertex set:

vertex set = {A, B, C, D, E, F, G, H, J}

directed edges = { (A,B), (A,C), (B,D), (B,E), (D,H), (D,J), (C,F), (C,G), (H,B), (D,A), (F,E), (A,G) }

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?


Answers:
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


2. Classification of edges

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.

tree: (A,B) (A,C) (B,D) (B,E) (C,F) (C,G) (D,H) (D,H)
back: (D,A) (H,B)
cross: (F,E)
forward: (A,G)

2B.

type pre[u] < pre[v] pre[u] > pre[v] post[u] < post[v] post[u] > post[v]
tree: yes no no yes
back: no yes yes no
forward: yes no no yes
cross: no yes no yes

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.


3. Algorithm for topologically sorting a directed acyclic graph

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.)


4. Prove that the root vertex of the DFS tree is a cut vertex iff it has more than one child in the DFS tree

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


4A. Suppose the root vertex R in the DFS tree is a cut vertex. Suppose for contradiction that the root has only one child in the DFS tree. Then even if the root vertex is removed from the graph, all other vertices would still be connected to each other using the remaining tree edges. So R would not be a cut vertex.

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.



ClassS04CS141 | ClassS04CS141 | recent changes | Preferences
This page is read-only | View other revisions
Last edited May 5, 2004 10:34 pm by Neal (diff)
Search: