You are on page 1of 12

Algorithm Final Exam (Spring 2009)

四技二年級
四技二年級
總分:
總分:110 %

Name:

Student ID #:

Question Score

1 (20%)

2 (5%)

3 (10%)

4 (10%)

5 (10%)

6 (15%)

7 (20%)

8 (20%)

Total
1. [Simple questions: 20%] Only simple answers are necessary for the following
questions.
(a) [2%] (Yes/no) Ω(n lg n) ⊆ ω(n lg n), yes or no?

(b) [2%] (Yes/no) There is a possibility that the decision model of a sorting
method has its longest path longer than Θ(n lg n), yes or no?

(c) [3%] (Multiple Choices) Disjoint set is necessary in which of the following
situation(s)? (i) Prim’s algorithm, (ii) Kruskal’s algorithm, (iii) finding
connected components, (iv) finding strongly connected components.

(d) [3%] (Simple Answer) DFS can help us to find the topological sort for a
DAG. Can you name any application for BFS?
(e) [5%] (Simple Answer) State as brief as possible about why we use
complexity (such as this is a Θ(n lg n) algorithm) instead of “number of
steps” (such as this algorithm needs 100 steps to obtain the answer) to
describe how fast an algorithm is.

(f) [5%] (Simple Answer) Someone claims that counting sort should always be
favored over quicksort because its time complexity is Θ(n + k), which is less
than Θ(n lg n) in most cases. What is your comment on this statement?
2. [Recurrence equation: 5%] Solve the following recurrence equation.
T(n) = 2T( n) + 1

Your answer should be as tight as possible. You can use master theorem when
there is a need.
3. [Sorting: 10%] A sorting is executed half way and suddenly the server crashed.
Fortunately some partial result of the sorting was automatically saved just that we
do not know how much has been done or what method was used for the sorting.
Name any efficient sorting method which is appropriate to continue the sorting at
this case until it is finished. Explain your reason.
4. [Heap: 10%] If someone suggests the following algorithm for heap deletion

HEAP-DELETE(A, i)
1 HEAP-INCREASE-KEY(A, i, +∞)
2 HEAP-EXTRACT-MAX(A)

would you agree with him/her? What is the complexity of this algorithm?
5. [Dynamic Programming: 10%] Suppose a shortest path from u to v which goes
through a vertex w. Explain why the path from u to w and the path from w to v are
also the shortest paths in each part.
6. [Dynamic Programming: 15%] Find an optimal parenthesization of a
matrix-chain product whose sequence of dimensions is 〈6, 4, 3, 1, 2〉. Can this
solution be found by a greedy algorithm, say trying to multiply the matrices with
the largest dimension in the middle? For instance, in this case maybe we may
want to multiply matrices A1 (with dimension 6×4) and A2 (with dimension 4×3)
first.
7. [DFS: 20%] Answer the following two questions related to DFS. The code of
DFS is included here for your reference.

DFS(G)
1 for each vertex u∈V[G]
2 do color[u] ← WHITE
3 π[v] ← NIL
4 time ← 0
5 for each vertex u∈V[G]
6 do if color[u] = WHITE
7 then DFS-VISIT(u)

DFS-VISIT(u)
1 color[u] = GRAY
2 d[u] ← time ← time +1
3 for each v∈Adj[u]
4 do if color[v] = WHITE
5 then π[v] ← u
6 DFS-VISIT(v)
7 color[u] = BLACK
8 f[u] ← time ← time + 1

(a) Starting from the vertex r, give the result of DFS for the following graph. In
your visit, when there is more than one possible choice, try the alphabetic
order. (8%)

s u

x
v
y
w
z
(b) Name any edges which are not tree edges. (5%)

(c) Write down the lifetime (the parenthesis structure) for all the vertices in the
graph. (7%)
8. [Minimum Spanning Tree: 20%] Given the following graph, answer three
questions related to minimum spanning tree. The Prim’s algorithm is given below
for your reference.

MST-PRIM(G, w, r)
1 for each u ∈ V[G]
2 do key[u] ← ∞
3 π[u] ← NIL
4 key[r] ← 0
5 Q ← V[G]
6 while Q ≠ ∅
7 do u ← EXTRACT-MIN(Q)
8 for each v ∈ Adj[u]
9 do if v ∈ Q and w(u, v) < key[v]
10 then π[v] ← u
11 key[v] ← w(u, v)

(a) Starting from the vertex a, find the MST via the Prim’s algorithm. Other than
giving the MST, you should also list the edges in order in discovering your
MST. (7%)

4 8
f d a
8
7
2 3 7
5

14
10 8
h g 5 e 12 c b
(b) Write down the min-heap when you got four edges already in your MST.
(5%)

(c) Argue that if all edge weights of a graph are positive, then any subset of
edges that connects all vertices and has minimum total weight must be a tree.
Hint: a tree must not contain a cycle in it. Modify some of the edges in the
graph of (a) to show that the same conclusion does not follow if we allow
some weights to be nonpositive. (8%)

You might also like