Exercices - Chapter4
Exercices - Chapter4
2. Show the trace of the greedy algorithm which solves the fractional knapsack problem
with the following inputs.
i 1 2 3 4 5 6
(a) vi 6 2 1 8 3 5
wi 6 10 3 5 1 3
Take W = 20 as the maximum weight allowed.
i 1 2 3 4 5 6
(b) vi 20 24 9 15 25 36
wi 2 4 1 5 5 6
Take W = 18 as the maximum weight allowed.
3. Find an input to the fractional knapsack problem for which there is more than one
optimal solution.
4. Show the trace of Kruskal’s algorithm on the following two inputs. Include the details
of the Union-Find data structure in your trace.
6 5 6
A B C D
1 2 2 5 5
4 7
E F G 3 H
(a) 1 3
1 2 3
A B C D
8 6 1
4 6 2 4
E F G 1 H
(b) 5 1
5. Show the trace of Prim’s algorithm on the two inputs of Question 4, starting at vertex
A.
—–Design & analysis of algorithms—–
If you think that the algorithm is correct, prove it. If you think it is not, find an input
for which it fails.
7. Donald Trump wants to drive his car from point A to point B. When the tank of the
car is full, he can drive up to distance D (at the beginning of the trip, the tank is full).
During his trip, he will cross n gaz stations where he can fill the tank of his car (the
n-th gaz station being at B). Let di (1 ≤ i ≤ n) be the distance between the (i − 1)-th
and the i-th gaz station. We denote by d1 the distance between A and the first gaz
station, and by dn the distance between the (n − 1)-th gaz station and the n-th gaz
station at B.
1 3 B
d1
A n−2 dn
d2 d3
2
n−1
(a) What greedy criterion should you use to solve this problem?
(b) Prove that your greedy criterion does lead to an optimal solution.
Hint: Start with an optimal solution S. Show that if S is not the greedy solution
S ∗ , then you can convert S into S ∗ without increasing the number of stops, then
conclude.
(c) Explain why your greedy criterion leads to an algorithm that takes O(n) time.
(d) Find an input to this problem for which there is an optimal solution that does
not satisfy your greedy criterion. Does this contradict anything?
8. Can Prim’s algorithm be used to find a minimum spanning tree in a graph with some
negative weights? Justify your answer.
9. Can Kruskal’s algorithm be used to find a minimum spanning tree in a graph with
some negative weights? Justify your answer.
10. Consider the fractional knapsack problem. In class, we studied a version where a
solution X = (x1 , x2 , ..., xn ) must satisfy 0 ≤ xi ≤ 1 for all 1 ≤ i ≤ n. In other words,
for each 1 ≤ i ≤ n we can bring at most one object i in the knapsack.
—–Proofs—–
11. Let G = (V, E) be an undirected and connected graph. Suppose that each edge {u, v} ∈
E has a weight wt(u, v) > 0.
In the section about minimum spanning trees, we are looking for a subgraph G0 (sub-
graph of G) such that
12. Assume that the set of coins available in a given Country is {5, 10} and prove that the
algorithm Making Change(x) leads to an optimal solution.
13. Consider an undirected graph G = (V, E) with distinct non-negative edge weights.
That is, for each {u, v} ∈ E, wt(u, v) ≥ 0. Suppose that you have computed a minimum
spanning tree of G using Kruskal, and that you have also computed shortest paths to
all nodes from a source node s ∈ V . Now suppose that each edge weight is increased
by 1: for each {u, v} ∈ E, the new weight of {u, v} is wt0 (u, v) = wt(u, v) + 1.
15. Consider an undirected graph G = (V, E) with distinct non-negative edge weights.
Assume that G has a cycle containing the heaviest edge eM . Prove or disprove: eM
cannot be part of any MST of G.
16. * Let G = (V, E) be an undirected graph. Prove that if all its edge weights are distinct,
then it has a unique minimum spanning tree.