Assignment 5 Solutions - Dalhousie University CSCI 3110
Assignment 5 Solutions - Dalhousie University CSCI 3110
December 5, 2012
4.4 (2 pts) Here’s a proposal for how to find the length of the shortest cycle in an undirected graph with
unit edge lengths.
When a back edge, say (v, w) , is encountered during a depth-first search, it forms a cycle with the
tree edges from w to v . The length of the cycle is level[v] − level[w] + 1 , where the level of a vertex
is its distance in the DFS tree from the root vertex. This suggests the following algorithm:
Show that this strategy does not always work by providing a counterexample as well as a brief (one
or two sentence) explanation.
a 1 b 2 c -1 d
2
This figure shows a graph of 4 vertices a, b, c, d, e. A DFS tree is indicated by marking non-tree edges
with dashed lines. The proposed algorithm will only find the length 4 cycle a → b → c → d → a, but
will miss the length 3 cycle a → d → e → a because it consists of two back edges.
4.8 (2 pts) Professor F. Lake suggests the following algorithm for finding the shortest path from node s
to node t in a directed graph with some negative edges: add a large constant to each edge weight so
that all the weights become positive, then run Dijkstra’s algorithm starting at node s, and return the
shortest path found to node t.
Is this a valid method? Either prove that it works correctly, or give a counterexample.
𝑎+2
This figure shows a counterexample graph, where a is the constant that we add. When a = 0 (ie, the
original graph), the shortest path from a to d is a → b → c → d. However, when we add a ≥ 2 to this
graph, we penalize longer paths, so the shortest path from a to d is a → b → d.
4.14 (4 pts) You are given a strongly connected directed graph G = (V, E) with positive edge weights
along with a particular node v0 ∈ V . Give an efficient algorithm for finding shortest paths between
all pairs of nodes, with the one restriction that these paths must all pass through v0 .
Any shortest path from two vertices s to t must pass through v0 . Thus, any such path is composed of
a path from s to v0 and a path from v0 to t. We first use Dijkstra’s algorithm to find the shortest
path length from v0 to any other vertex, B[·]. We then reverse the graph and find the shortest path
length to v0 from any vertex, A[·]. The shortest path from s to t that passes through v0 has length
A[s] + B[t]. This takes O(|V | + |E|) log |V |) time. Note that we do not store all of the shortest path
lengths directly, as that would require O(n2 ) time.
4.20 (4 pts) There is a network of roads G = (V, E) connecting a set of cities V . Each road in E has an
associated length le . There is a proposal to add one new road to this network, and there is a list E 0 of
pairs of cities between which the new road can be built. Each such potential road e0 ∈ E 0 has an
associated length. As a designer for the public works department you are asked to determine the road
e0 ∈ E 0 whose addition to the existing network G would result in the maximum decrease in the
driving distance between two fixed cities s and t in the network. Give an efficient algorithm for
solving this problem.
A newly added edge e0 = (u, v) can only decrease the length of the shortest path from s to t if we
follow e0 . Thus, we would follow the paths s → u → v → t. We can solve this problem by first using
Dijkstra’s algorithm to compute all shortest paths from s to any vertex, S[·] and to t from any vertex,
T [·] (using GR if the graph is directed). For each possible new edge e0 = (u, v), the new shortest path
length from s to t is S[u] + le0 + T [v]. We choose the edge e0 that minimizes this (Or none, if S[t] is
never improved upon).
5.7 (2pts) Show how to find the maximum spanning tree of a graph, that is, the spanning tree of largest
total weight.
Multiply all the edge weights by -1 and find the minimum spanning tree by any of the standard
algorithms: Prim’s, Kruskal’s etc
5.9 (10 pts) The following statements may or may not be correct. In each case, either prove it (if it is
correct) or give a counterexample (if it isn’t correct). Always assume that the graph G = (V, E) is
undirected. Do not assume that edge weights are distinct unless this is specifically stated.
(a) If graph G has more than |V | − 1 edges, and there is a unique heaviest edge, then this edge
cannot be part of a minimum spanning tree.
FALSE. Any unique heaviest edge that is not part of a cycle must be in the MST. A graph with
one edge is a counterexample.
(b) If G has a cycle with a unique heaviest edge e, then e cannot be part of any MST.
TRUE. An MST has no cycles, so at least one edge of the cycle e0 is not in an MST T . If e0 6= e
then we could swap e0 for e in T and get a lighter spanning tree.
(c) Let e be any edge of minimum weight in G. Then e must be part of some MST.
TRUE. An edge of minimum weight is trivially the minimum weight edge of some cut.
(d) If the lightest edge in a graph is unique, then it must be part of every MST.
TRUE. If the lightest edge is unique, then it is the lightest edge of any cut that separates its
endpoints.
(e) If e is part of some MST of G, then it must be a lightest edge across some cut of G.
TRUE. If there were a lighter edge e0 across some cut of G, then we could replace e with e0 and
obtain a smaller MST.
6 1
6 1
(f) If G has a cycle with a unique lightest edge e, then e must be part of every MST.
FALSE. The dashed edge with weight 5 is not part of the MST, but is the lightest edge in the
left cycle.
(g) The shortest-path tree computed by Dijkstra’s algorithm is necessarily an MST.
1 1 1
s t
2
FALSE. Dijkstra’s algorithm will use the heaviest edge of a cycle if it is on the shortest path
from the start s to a node t.
(h) The shortest path between two nodes is necessarily part of some MST.
FALSE. The shortest path between s and t in (g) is not part of any MST.
(i) Prim’s algorithm works correctly when there are negative edges.
TRUE. Prim’s algorithm always adds the lightest edge between the visited vertices and the
unvisited vertices, which is the lightest edge of this cut. Negative weights do not affect this.
(j) (For any r > 0 , define an r-path to be a path whose edges all have weight < r.) If G contains
an r-path from node s to t, then every MST of G must also contain an r-path from node s to
node t.
TRUE. Suppose that a graph G contains an r-path from a node s to t but that there is an MST
T of G that does not contain an r-path from s to t. Then T contains a path from s to t with an
edge e of weight we ≥ r. Consider the partition (S|V − S) of vertices made by removing e from
T . One vertex e0 of the r-path must be along this cut, as the r-path connects s and t. Since
we0 < r, we can swap e0 for e to get a spanning tree that is lighter than T , a contradiction.
5.10 (NOT ASSIGNED- FOR PRACTICE) Let T be an MST of graph G. Given a connected
subgraph H of G, show that T ∩ H is contained in some MST of H.
Suppose not. Then there is an edge e ∈ T ∩ H across some cut (S|V − S) of H such that another
edge across the cut, e0 , is lighter. However, H is a subgraph of G, so e0 is lighter than e across the cut
(S|V − S) of G. We could thus swap e0 for e in T to get a lighter spanning tree, contradicting that T
is an MST.
5.22 (NOT ASSIGNED - FOR PRACTICE) In this problem, we will develop a new algorithm for
finding minimum spanning trees. It is based upon the following property:
Pick any cycle in the graph, and let e be the heaviest edge in that cycle. Then there is a minimum
spanning tree that does not contain e.
(a) Prove this property carefully.
Suppose that the property is not true. Then there is an edge e = (u, v) of a graph G such that e
is the heaviest edge in a cycle and e is part of any MST T of G. Let e0 = (w, x) be the lightest
edge of the cycle and consider a cut (S|V − S) such that S contains the vertices of the cycle
from v to w and V − S contains the vertices of the cycle from u to x. Then the weight of e0 is
less than or equal to the weight of e across this cut and we can swap e0 for e to get an MST T 0 ,
a contradiction.
(b) Here is the new MST algorithm. The input is some undirected graph G = (V, E) (in adjacency
list format) with edge weights {we }.