Docsity

Quiz with Solution for Algorithm Design | ECS 122A, Quizzes of Computer Science

Toggle sidebar
bg1
ECS 122A Quiz 2 (60 points) Solutions
1. True or false (no justification required)
(a) For any graph G= (V, E ), Θ(Elg E) = Θ(Elg V).
False. It’s true for a connected graph. First, it’s easy to see |E|=O(|V|2). If the graph is
connected, |E| ≥ |V| − 1 = Ω(|V|). Therefore, Θ(lg E) = Θ(lg V).
(b) In BFS, the vertices are processed in a LIFO order (stack).
False. BSF uses FIFO queue.
(c) The toplogical sort of a directed graph G= (V, E ) can be computed in linear time.
False. It’s true for a DAG (Directed Acyclic Graph).
(d) Kruskal’s MST algorithm will work correctly even if weights may be negative.
True. Kruskal’s algorithm does not depend on the weight being positive.
2. Given a connected undirected graph G= (V, E ) with weight function w:E→R.
(a) Define a spanning tree for G
Answer: A tree that connects all vertices.
(b) Define a minimum spanning tree for G
Answer: The spanning tree of minimum weight.
(c) State the optimal substructure property of a minimum spanning tree.
Answer: An optimal tree has optimal subtrees.
(d) Complete the following pseudo-code for finding a minimum spanning tree T:
MST-Prim(G, w, r)
Q= Ø ⊲ Q: priority queue
for each u∈V
key[u] = ∞
π[u] = nil
Q←− u
endfor
key[r] = 0
while Q6= Ø
u←− Extract-Min(Q)
for each v∈Adj[u]
if v∈Qand w(u, v)<key[v]then
key[v] = w(u, v)
π[v] = u
endif
endfor
endwhile
return A={(v, π[v]) : v∈V− {r}}

Partial preview of the text

Download Quiz with Solution for Algorithm Design | ECS 122A and more Quizzes Computer Science in PDF only on Docsity!

ECS 122A Quiz 2 (60 points) Solutions

  1. True or false (no justification required) (a) For any graph G = (V, E), Θ(E lg E) = Θ(E lg V ). False. It’s true for a connected graph. First, it’s easy to see |E| = O(|V |^2 ). If the graph is connected, |E| ≥ |V | − 1 = Ω(|V |). Therefore, Θ(lg E) = Θ(lg V ). (b) In BFS, the vertices are processed in a LIFO order (stack). False. BSF uses FIFO queue. (c) The toplogical sort of a directed graph G = (V, E) can be computed in linear time. False. It’s true for a DAG (Directed Acyclic Graph). (d) Kruskal’s MST algorithm will work correctly even if weights may be negative. True. Kruskal’s algorithm does not depend on the weight being positive.
  2. Given a connected undirected graph G = (V, E) with weight function w : E → R. (a) Define a spanning tree for G Answer: A tree that connects all vertices. (b) Define a minimum spanning tree for G Answer: The spanning tree of minimum weight. (c) State the optimal substructure property of a minimum spanning tree. Answer: An optimal tree has optimal subtrees. (d) Complete the following pseudo-code for finding a minimum spanning tree T :

MST-Prim(G, w, r) Q = Ø ⊲ Q: priority queue for each u ∈ V key[u] = ∞ π[u] = nil Q ←− u endfor key[r] = 0 while Q 6 = Ø u ←− Extract-Min(Q) for each v ∈ Adj[u] if v ∈ Q and w(u, v) < key[v] then key[v] = w(u, v) π[v] = u endif endfor endwhile return A = {(v, π[v]) : v ∈ V − {r}}