0% found this document useful (0 votes)
17 views

Exercices - Chapter4

The document provides material covered in class and design and analysis of algorithms problems. It includes: 1. Tracing algorithms like Making Change, greedy fractional knapsack, Kruskal's, and Prim's on sample inputs. 2. Analyzing variations of algorithms like reversing Kruskal's, allowing fractional values other than 1 in knapsack, and minimum spanning trees with negative edges. 3. Proving properties of minimum spanning trees like being acyclic/connected graphs and the effect of increasing all edge weights by 1.

Uploaded by

Myummie e
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Exercices - Chapter4

The document provides material covered in class and design and analysis of algorithms problems. It includes: 1. Tracing algorithms like Making Change, greedy fractional knapsack, Kruskal's, and Prim's on sample inputs. 2. Analyzing variations of algorithms like reversing Kruskal's, allowing fractional values other than 1 in knapsack, and minimum spanning trees with negative edges. 3. Proving properties of minimum spanning trees like being acyclic/connected graphs and the effect of increasing all edge weights by 1.

Uploaded by

Myummie e
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 4

—–Material covered in class—–

1. Show the trace of Making Change(3.65).

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—–

6. * If we reverse Kruskal algorithm, do we get a correct algorithm to compute MSTs?


More precisely, what do you think of the following algorithm?

• Start with graph G = (V, E)


• Go through E in decreasing order of edge weights.
• For each edge, check if deleting the edge will further disconnect the graph.
• Perform any deletion that does not lead to additional disconnection.

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

We suppose that di ≤ D for all 1 ≤ i ≤ n. Donald wants to minimize the number of


stops he needs to make. He knows the location of all gaz stations before leaving from
A.

(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.

(a) What if a solution X = (x1 , x2 , ..., xn ) must satisfy 0 ≤ xi ≤ 2 for all 1 ≤ i ≤ n?


3
(b) What if a solution X = (x1 , x2 , ..., xn ) must satisfy 0 ≤ xi ≤ 2
for all 1 ≤ i ≤ n?

—–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

• the vertex set of G0 is V ,


• G0 is connected
• and weight(G0 ) is minimum.

Prove that G0 is a tree (i.e. G0 is acyclic and connected).

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.

(a) Prove or disprove: the minimum spanning tree changes.


(b) Prove or disprove: the shortest paths do not change.
14. Consider an undirected graph G = (V, E) with non-negative edge weights. Prove or
disprove: the shortest path between two nodes is necessarily part of some MST of G.

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.

You might also like