You are on page 1of 3

CSE 431/531: Algorithm Analysis and Design Spring 2021

Homework 5
Instructor: Shi Li Deadline: 4/28/2021

Your Name: Your Student ID:

Problems 1 2 Total
Max. Score 15 25 40
Your Score

Problem 1 Consider the following graph G with non-negative edge weights.

a 13 s

8 11 20 6

b 1 c 18 e
14 10
d

(1a) Use Prim’s algorithm to compute the minimum spanning tree of G. Give the
minimum spanning tree and its weight.
You can use the following table to describe the execution of the algorithm. The algo-
rithm maintains a set S of vertices. The d value of a vertex v ∈
/ S is minu∈S:(u,v)∈E w(u, v).
The π value of a vertex v is the vertex u ∈ S such that d(v) = w(u, v); if d(v) = ∞,
then π(v) =“⊥”.

a b c d e
iteration vertex added to S
d π d π d π d π d π
1 s 13 s ∞ ⊥ 20 s ∞ ⊥ 6 s
2 e 13 s ∞ ⊥ 18 e 10 e
3 d 13 s ∞ ⊥ 14 d
4 a 8 a 11 a
5 b 1 b
6 c

Table 1: Prim’s Algorithm for Minimum Spanning Tree

The edges in the MST are (s, a), (a, b), (b, c), (e, d), (s, e), and the total weight is
13 + 8 + 1 + 10 + 6 = 38.

1
(1b) Use Dijkstra’s algorithm to compute the shortest paths from s to all other vertices
in G.
You can use the following table to describe the execution of the algorithm on the
instance. The algorithm maintains a set S of vertices. The d value of a vertex v ∈ /S
is minu∈S:(u,v)∈E (d(u) + w(u, v)). The π value of a vertex v is the vertex u ∈ S such
that d(v) = d(u) + w(u, v); if d(v) = ∞, then π(v) =“⊥”.

a b c d e
iteration vertex added to S
d π d π d π d π d π
1 s 13 s ∞ ⊥ 20 s ∞ ⊥ 6 s
2 e 13 s ∞ ⊥ 20 s 16 e
3 a 21 a 20 s 16 e
4 d 21 a 20 s
5 c 21 a
6 b

Table 2: Dijkstra’s algorithm for Shortest Paths

a b c d e
The parents of vertices are . Shortest paths to a, b, c, d, e
par: s a s e s
are s → a, s → a → b, s → c, s → e → d and s → e. The lengths are 13, 21, 20, 16
and 6.

Problem 2 We are given a directed graph G = (V, E) with positive weight function:
w : E → R>0 , and two vertices s, t ∈ V . Suppose we have already computed the d and π
array using the Dijkastra’s algorithm: d[v] is the length of the shortest path from s to v,
and π[v] is the vertex before v in the path.
Show that how to use the d and π array to count the number of shortest paths from
s to t in O(n log n + m) time. You do not need to worry about the integer overflow issue.
That means, you assume a word can hold a very big integer, and basic operations over
these big integers take O(1) time.
For every v ∈ V , we define num[v] be the number of shortest paths from s to v. We
say an edge (u, v) is good if d[u] + w(u, v) = d[v]. Then, the recursion for num[v] is as
follows:
(
0 if v = s
num[v] = P .
(u,v) is good f [u] if v 6
= s

Notice that all weights are positive, a good edge (u, v) must have d[u] < d[v]. So, we
can sort all vertices in V in non-decreasing order of d values, and then compute num[v]’s
in this order. The running time of the algorithm is O(n log n + m), where O(n log n) is
the running time for sorting, and O(m) is the running time for computing f [v]’s.

Problem 3 (You do not need to submit the solution for the problem.) Suppose there
is a negative cycle over a directed graph G = (V, E) with edge weights w : E → R. Then

2
show that for any array d : V → R over vertices, there exists some edge (u, v) ∈ E such
that d(u) + w(u, v) < d(v).
The problem says that if we do not put an upper bound on the number of iterations
the Bellman-Ford algorithm runs, it will run forever when there is a negative cycle.
Let C be the negative cycle and a ≥ 2 be the size of C, i.e, the number of vertices in C.
Let v1 , v2 , · · · , va be the set of vertices in C, so that (v1 , v2 ), (v2 , v3 ), · · · , (va−1 , va ), (va , v1 ) ∈
C.
Assuming towards to the contradiction that there exists d : V → R such that for every
edge (u, v) ∈ E we have d(u)+w(u, v) ≥ d(v), which is equivalent to d(v)−d(u) ≤ w(u, v).
Then, we have

the weight of C = w(v1 , v2 ) + w(v2 , v3 ) + · · · + w(va−1 , va ) + w(va , v1 )


≥ (d(v2 ) − d(v1 )) + (d(v3 ) − d(v2 )) + · · · + (d(va ) − d(va−1 )) + (d(v1 ) − d(va ))
= 0.

This contradicts the fact that C is a negative cycle.

You might also like