Data Structures and Algorithms (II) Spring 2007
Voluntary Exercise 1 - Solutions
Mon, Mar 12 due Mon, Mar 26
Problem 1. A directed graph G = (V, E) is singly connected if there is at most one directed path from u to v for all vertices u, v ∈ V . Give an efficient algorithm to determine whether or not a directed graph is singly connected. ♣
Solution. For each vertex u ∈ V , perform a DFS on the given graph G. Check if there are any foward edges or cross edges (in the same component) 1 in any one of the searches. If no such edges exist, then the graph is singly connected, else not.
Time complexity: O(|V |(|V | + |E|)).
The graph is singly connected even with back edges existed. Since back edges implies there is a path u v and v u, which is consistent with the definition of single connectedness.
1Please refer to the classification of edges in CLRS textbook pp. 546
1
1
第二題
Yes, all min(u) can be computed in O(|V|+|E|) time.
Reason 方法一
1. 先將整張圖的所有 directed edges 全部反向
2. 從 label 為 1 的那個 node x 開始, 將 x 可以走到的所有 node v, 讓 min(v)=L(x) 3. 接著從 label 為 2 的那個 node y 開始, 先檢查自己的 min(y)有沒有被填值, 如果有, 就不做任何事, 如果沒有被填值, 做以下的事情: 對於 y 可以走到的所有 node u, 如果 min(u)還沒被填入值, 就讓 min(u)=L(y), 如果已經被填入了就不要更改他.
4. 依此類推, 每一個 label 都做相同的事情, 最後可以填完所有的 min(..)值.
(沒有被填入 min(..)值的 node 就代表在原圖中, 他的鄰邊都是 in_degree) Correctness: left to you
Time:
Step1: O(|E|)
Step2: O(|V|+|E|) (use DFS or BFS) 方法二
Dynamic programming: 要算自己可以走到的最小 label 是多少, 就先去問問所有鄰 居可以走到的最小 label, 再跟所有鄰居的 label 取最小值就是答案!
2
Problem 3. When an adjacency-matrix representation is used, most graph algorithms require time Ω(V 2 ), but there are some exceptions. Show that determining whether a directed graph G contains a universal sink-a vertex with in-degree |V| – 1 and out-degree 0-can be determined in time O(V), given an adjacency matrix of G.
Solution. If vertex i is a universal sink according to the definition, the i-th row of the adjacency-matrix will be all “0”, and the i-th column will be all “1” except the aii
entry, and clearly there is only one such vertex. We then describe an algorithm to find out if a universal sink really exist.
Starts from a11. If current entry aij = 0 then j = j + 1 (take one step right); if aij = 1 then i = i +1 (take one step down). In this way, it will stop at an entry akn of the last row or ank of the last column (n = |V|, 1 ≦ k ≦|V|). Check if vertex k satisfies the definition of universal sink, if yes then we found it, if no then there is no universal sink. Since we always make a step right or down, and checking if a vertex is a universal sink can be done in O(V), the total running time is O(V).
If there is no universal sink, this algorithm won’t return any vertex. If there is a universal sink u, the path starts from a11 will definitely meet u-th column or u-th row at some entry. Once it’s on track, it can’t get out of the track and will finally stop at the right entry.
u
u
3
Problem 4. Reading 22.3 in textbook. Show that edge (u, v) is a. a tree edge or forward edge if and only if d[u] < d[v] < f [v] < f [u].
b. a back edge if and only if d[v] < d[u] < f [u] < f [v].
c. a cross edge if and only if d[v] < f [v] < d[u] < f [u].
Solution. First, you have to show the two following lemma:
1. u is an ancestor of v ⇔ d[u] < d[v] < f [v] < f [u].
2. u is a decendant of v ⇔ d[v] < d[u] < f [u] < f [v].
Therefore,
a. (u, v) is a tree edge or forward edge
⇔ u is an ancestor of v ⇔ d[u] < d[v] < f [v] < f [u].
b. (u, v) is a back edge
⇔ u is a decendant of v ⇔ d[v] < d[u] < f [u] < f [v]
c. (u, v) is a cross edge
⇔ v has been finished when exploring (u, v) ⇔ d[v] < f [v] < d[u] < f [u]
1
4