CSE101: Design and Analysis of Algorithms (CSE, UCSD, Fall-2022) Homework-3
• Homework solutions should be neatly written or typed and turned in through Gradescope
by 11:59pm on the due date. No late homeworks will be accepted for any reason. You will be
able to look at your scanned work before submitting it. Please ensure that your submission
is legible (neatly written and not too faint) or your homework may not be graded.
• Students may consult their textbook, class notes, lecture slides, instructors, TAs, and tutors
when they need help with homework. Students should not look for answers to homework
problems in other texts or sources, including the internet. Only post about graded homework
questions on Piazza if you suspect a typo in the assignment, or if you don’t understand what
the question is asking you to do. Other questions are best addressed in office hours.
• Your assignments in this class will be evaluated not only on the correctness of your answers,
but on your ability to present your ideas clearly and logically. You should always explain
how you arrived at your conclusions, using mathematically sound reasoning. Whether you
use formal proof techniques or write a more informal argument for why something is true,
your answers should always be well-supported. Your goal should be to convince the reader
that your results and methods are sound.
• For questions that require pseudocode, you can follow the same format as the textbook, or
you can write pseudocode in your own style, as long as you specify what your notation means.
For example, are you using “=” to mean assignment or to check equality? You are welcome
to use any algorithm from class as a subroutine in your pseudocode.
• Students can work in a group of 1-4. Each group should submit only one homework.
• For all the questions in this homework, we will use as a cycle a closed walk that does not
repeat edges or vertices except for the starting/ending vertex.
There are 4, questions for a total of 50, points.
1. (20 points)
(a) (10 points) Let e be a maximum weight edge on some cycle of an undirected connected graph
G = (V, E). Prove that there is a minimum spanning tree of G that does not include e.
Solution:
We approach this using a proof by contradiction. Suppose that there is an MST T such that e ∈ T .
We want to show that T is not an MST, which will contradict our assumption. Consider a cycle
C which contains e. Then there exists an edge e′ in C which is not in T since trees do not contain
cycles. Then, construct a new tree T ′ by removing e from T and adding e′ . This new tree has
weights whose sum is less than that of T since we > we′ (because e is the maximum weight edge),
and so because T ′ has a smaller sum of weights of edges than T , T is not an MST so we have a
contradiction and there is a minimum spanning tree of G that does include e.
(b) (10 points) Use answer to the above to prove that the following algorithm, Reverse Delete finds an
optimal minimum spanning tree.
Reverse Delete Sort the edges in non-increasing order of weights. Start with T = G. Process
each edge according to the sorted order and remove it from T if that does not disconnect T . Return
T as the minimum spanning tree when all edges have been processed.
Solution:
Suppose by contradiction that the reverse delete algorithm does not produce an optimal MST.
Then, when we sort the edges in non-increasing order by weights and while the algorithm is running,
each time we remove the maximum edge if it does not disconnect T , there exists an edge e with
maximum edge weight w(e) such that e is not in the MST produced by the reverse delete algorithm
but it is contained in a sub-optimal MST. By part (a), there is an MST in G which does not
1 of 6
CSE101: Design and Analysis of Algorithms (CSE, UCSD, Fall-2022) Homework-3
include e. However, this is a contradiction, as e has the maximum weight out of all the edges
currently connected that is not in the MST produced by the reverse-delete algorithm. So by proof
of contradiction, the reverse-delete algorithm finds an optimal MST.
2. (10 points) Consider an undirected, connected and weighted graph G = (V, E) which has all distinct
edge weights. Show that Kruskal’s and Prim’s algorithm will return the same minimum spanning tree.
Solution:
We will prove that an undirected, connected, and weighted graph G with all distinct edge weights has a
unique MST, which will prove that Kruskal’s and Prim’s algorithm will return the same MST.
We will use a proof by contradiction and exchange argument. Assume by contradiction that in G,
there exists two distinct MSTs T1 and T2 . Now consider the minimum edge weights which are contained
in exactly one of T1 or T2 . Let one of these edges be in T1 and denote it e1 . Add e1 to T2 . Then, T2 must
contain a cycle now and one of these edges must not be in T1 or else we would have a cycle in T1 . So
now we have an edge in T2 but not in T1 . Call this edge e2 . By definition, we must have w(e1 ) < w(e2 ).
Now, exchange e2 with e1 in T2 , which decreases the sum of the edge weights in T2 . Then, the sum of
the weights of T2 is less than the sum of the weights in T1 , which is a contradiction since T2 is an MST,
so G must have a unique MST.
Then, since Kruskal’s and Prim’s algorithm is always guaranteed to return an MST, we have shown
that applying these algorithms to G will yield the same MST since G only has a unique MST.
3. (10 points) In the class you have learnt Dijkstra’s algorithm that returns the shortest path from a source
vertex to all vertices in a graph. Give a modification to Dijkstra’s algorithm to produce count of the
number of different minimum paths from the source to all vertices in the graph.
Rubric: 7 points for correct algorithm with the best running time and 3 points for correctness.
Solution:
High level Description: Essentially, we are running Dijkstra’s algorithm but maintaining an extra
value N (v) representing the total number of minimum paths from a source to a destination in addition
to D(v), the distance of the shortest path from the source to the node.
1. Store the graph G in an adjacency list.
2. Given a source node s, for every other vertex v in G, perform the following steps:
(a) Initialize all distances to infinity and the number of paths to 0. However, initialize the distance
of the source node to 0 and the number of paths for the source 1 (distance from source to itself
is 0 and a node has a single shortest path to itself).
(b) Initialize a priority queue sorted in ascending order with the source node in it.
(c) Perform Dijkstra’s as normal, traversing the popped node’s adjacency list edges from the pri-
ority queue. Update the weights accordingly to minimize the distance value of the neighboring
nodes to be the current node distance plus to the current edge weight if the neighboring nodes
has a greater distance than this sum.
(d) In addition to weight updates, perform the number of minimum path updates based on these
conditions when adding some vertex u in some edge (u, v):
i. If D(v) > D(u) + l(u, v), set N (v) = N (u).
ii. If D(v) = D(u) + l(u, v), set N (v) = N (u) + N (v).
iii. If D(v) < D(u) + l(u, v), do nothing.
(e) The number of paths obtained at the destination vertex d, denoted by N (d) is the number of
unique minimum paths from s to d obtained by this method.
3. Add up all the counts computed by the result of 2e and return that as the count of the number of
different minimum paths from source to all vertices in the graph.
2 of 6
CSE101: Design and Analysis of Algorithms (CSE, UCSD, Fall-2022) Homework-3
Algorithm 1 Total Number of Shortest Paths From Given Source Node to All Other Nodes
1: procedure FindAll(G = (V, E), le = positive edge lengths, s = source)
2: Inputs: G, source s, edge lengths le
3: Output: number of unique minimum paths from s to all v ∈ V | v ̸= s
4: total ← 0
5: list ← [[]] ▷ Initialize adjacency list
6: for v ∈ V do
7: add(list, e = (v, u))
8: end for
9: for all (v ̸= s) ∈ E do
10: count ← NumPaths(G, s, v)
11: total ← total + count
12: end for
13: return total
14: end procedure
Algorithm 2 Total Number of Shortest Paths
1: procedure NumPaths(G = (V, E), le = positive edge lengths, s = source, d = dest)
2: Inputs: G, source s, destination d, edge lengths le
3: Output: number of unique minimum paths from s to d
4: D[] ← ∞ ▷ Initialize all node distances to ∞
5: N [] ← 0 ▷ Initialize all unique path counts to 0
6: pq ← [] ▷ Initialize a priority queue
7: pq.add(0, s)
8: D[s] ← 0
9: N [s] ← 1
10: while pq is not empty do
11: current ← pq.top()
12: length ← current.length()
13: pq.pop()
14: for all e = (v, u) ∈ list[current] do
15: if D[v] > D[current] + l(v, u) then
16: pq.push(length + l(v, u), v)
17: D[v] ← D[current] + l(v, u)
18: N [v] ← N [current]
19: else if D[v] = D[current] + l(v, u) then
20: N (v) ← N [v] + N [current]
21: end if
22: end for
23: end while
24: return N [d]
25: end procedure
3 of 6
CSE101: Design and Analysis of Algorithms (CSE, UCSD, Fall-2022) Homework-3
See Algorithm 1 and 2 for the pseudocode.
Runtime:
The runtime of this algorithm is the same as Dijkstra’s assuming we are using a Fibonacci heap, which
is just O(|V | log |V | + |E|) since we are simply adding another variable to keep track of the number of
paths, but this does not add an additional order of runtime.
Correctness:
We know that Dijkstra’s algorithm is correct from the lecture slides. Thus, we use Dijkstra’s distance
update conditions to determine our minimum number of paths. Note that when the distance update
rule executes in Dijkstra’s algorithm, an edge is added to the minimum path. Therefore, we have two
cases if we want to count the total number of minimum paths from a source node to a particular vertex.
Case 1: When we are popping the current node from the priority queue, if D[v] > D[current] + l(v, u)
as in line 15 of Algorithm 2, we want to minimize the distance value of the neighboring node to be equal
to the current node distance plus the cost of the current node’s neighbor edge, as in Dijkstra’s. This is
equivalent to saying that a new shortest path has been discovered, making all previous shortest paths
invalid, so all the shortest paths to neighbor (the vertex to be added) is the same as all the shortest
paths to v (the current vertex). This yields all the current shortest paths in this case.
Case 2: If D[v] = D[current] + l(v, u), we have found multiple equivalent paths to get to the neighboring
node by definition so we increment the number of paths of the vertex to be added since all shortest paths
from the source to this vertex neighbor with previous edge (v, neighbor) gives a shortest path to neighbor.
This updates all the current shortest paths in this case.
4. (10 points) Given an unweighted and undirected graph G = (V, E), design an algorithm that runs in
O(|V | + |E|) time and determines whether G has a cycle of odd length.
Rubric: 7 points for correct algorithm with O(|V | + |E|) running time and 3 points for correctness.
Solution:
(Use BFS algorithm and Disjoint Set definition)
High-level description:
Step 1: Construct an adjacency list for all vertices. Runtime: O(|V | + |E|)
Step 2: Starting at any vertex s in G and run BFS to determine the odd depth versus even depth of G
and store each level in an array of linked lists. Runtime: O(|V | + |E|)
Step 3: Using makeset() operation from disjoint sets to put s and vertices that have even distances
from s into the same set, and the vertices of odd distances from s would be in another set. Continue to
separate level accordingly to two different sets until all vertices have been visited. Runtime: O(|V |) for
all vertices and O(1) for adding each vertex in a set
Step 4: If all vertices have not been visited (disconnected graph), choose another starting vertex and run
BFS again separately to determine if there is a cycle of odd length in that subroutine graph. Runtime:
O(|V |)
Step 5: Checking the two completed sets in G, for each of them, based on the adjacency list, if the
vertices in the same set are neighbors (an edge to connect them), the graph G contains a cycle of odd
length. Otherwise, no such exist. Runtime: O(|V | + |E|) since we have at most |V | vertices and every
vertex has at most |E| edges for each set
The algorithm takes at most O(|V | + |E|) time
Proof of correctness:
Forward: If two vertices v and w are in the same set and w appears in the adjacency list of v (which
means there is an edge to connect them in G), there exists a cycle of odd length in G.
Toward contradiction, assume v and w are connected but there is no cycle of odd length in G. By the
correctness of BFS, the level of vertices are determined accurately and correctly indicates the list of
vertices that have the odd or even distances from starting vertex s to separate them into two different
sets. Furthermore, by the correctness of makeset() using disjoint sets definition, we successfully put the
corresponding lists into the right place. Since v and w are in the same disjoint set, there exists a path
4 of 6
CSE101: Design and Analysis of Algorithms (CSE, UCSD, Fall-2022) Homework-3
from starting node s to v and w with the same type of length (odd or even distance from s), and if there
is an edge that connects v and w, it would definitely create a cycle, moreover, to determine the length
type, we separate into 2 cases:
Case 1: v is odd + w is odd + 1 (the edge between them) = odd length
Case 2: v is even + w is even + 1 (the edge between them) = odd length
Therefore, the forward approach is correct.
Backward: If no vertices in the same disjoint set are connected, no cycle of odd length exist in G.
Using v and w in the forward approach to represent any pair of vertices in the same set but in case there
is no edge between them, by the correctness of BFS and disjoint sets definition, the elements in the same
set would have the same type of distances from starting node s that no cycle has been detected yet in
the path from s to v and w.
Case 1: The edges from v and w are connected to a vertex x in the different disjoint set, the length
of the cycle are separated into two situations, both are odd or both are even, but the sum of them are
always even, which means no cycle of odd length could be detected.
Case 2: The edges from v and w are not connected to a vertex x in the different set, then there is no
cycle in G.
5 of 6
CSE101: Design and Analysis of Algorithms (CSE, UCSD, Fall-2022) Homework-3
Algorithm 3 Find cycle of odd length in graph
1: Input: graph G = (V, E) and a starting node s in V
2: Output: True if cycle of odd length exist, false otherwise
3:
4: Initialize an adjacency list of size |V | to store G
5: procedure BFS(G = (V, E), s (node to explore from))
6: for u ∈ V do
7: dist[u] = ∞
8: end for
9: dist[u] = 0
10: Q = [s]
11: while Q is not empty do:
12: u = eject(Q)
13: for each edge (u, v) in E do:
14: if dist[v] = ∞ then
15: inject(Q, v)
16: dist[v] = dist[u] + 1
17: end if
18: end for
19: end while
20: end procedure
21: set A = Makeset(s)
22: for u ∈ V do
23: if dist[u] is odd then
24: Make a new set B of all vertices with odd distances
25: else
26: Add vertices with even distances in set A
27: end if
28: end for
29: for every pair (x, y) in set A do
30: if x is in the adjacency list of y then
31: return true
32: else
33: return false
34: end if
35: end for
36: for every pair (x, y) in set B do
37: if x is in the adjacency list of y then
38: return true
39: else
40: return false
41: end if
42: end for
6 of 6