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

Exercices - Chapter5

1. The document discusses algorithms covered in a design and analysis of algorithms class, including shortest paths in DAGs, matrix chain multiplication, longest common subsequence, and greedy algorithms. 2. Students are asked to provide pseudocode, traces, and analyze runtimes for several algorithms. Problems also ask students to prove or disprove claims about greedy algorithms. 3. The document covers dynamic programming techniques, including computing shortest paths and optimal parenthesization of matrix expressions. Students must analyze algorithms, prove claims, and develop solutions for optimization problems.

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)
11 views

Exercices - Chapter5

1. The document discusses algorithms covered in a design and analysis of algorithms class, including shortest paths in DAGs, matrix chain multiplication, longest common subsequence, and greedy algorithms. 2. Students are asked to provide pseudocode, traces, and analyze runtimes for several algorithms. Problems also ask students to prove or disprove claims about greedy algorithms. 3. The document covers dynamic programming techniques, including computing shortest paths and optimal parenthesization of matrix expressions. Students must analyze algorithms, prove claims, and develop solutions for optimization problems.

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 5

—–Material covered in class—–

1. In Section 5.1, we studied how to compute shortest paths in a directed acyclic graph.
Write the trace of this algorithm on two different examples.
2. We studied an algorithm to solve the matrix chain multiplication problem. We saw that
we need to compute a secondary table to keep track of the optimal order to multipy
the matrices. Modify the pseudocode that was presented in class such that it computes
this secondary table. What running time do you get?
3. Write the trace of the algorithm from Question 2 on the following two different exam-
ples.
(a) A1 is 10 × 4, A2 is 4 × 5, A3 is 5 × 20, A4 is 20 × 2 and A5 is 2 × 50.
(b) A1 is 13 × 5, A2 is 5 × 89, A3 is 89 × 3, A4 is 3 × 34.
4. Write the pseudocode we need to reconstruct the solution from the two tables built
in Question 2. If the two tables are given as input, what is the running time of your
pseudocode?
5. Write the trace of the algorithm for the longest common subsequence on two different
examples.
6. Write the pseudocode to build the table in the longest common subsequence problem.
7. Write the pseudocode to reconstruct a longest common subsequence from the table.
8. Once in your life, you should write the trace of Floyd-Warshall algorithm. Try the
following input...
4 6
v1 v2 v3

3
15
5 18 12

10 v4 2 v5

19 1
8 5

v6 v7
10
—–Design & analysis of algorithms—–

9. In Section 5.1, we studied how to compute shortest paths in a directed acyclic graph.
In the algorithm we came up with, there are the following two steps.
• k = indegree(vj )
• Let u1 , u2 , ..., vj be the vertices that have an edge to vj .
Explain how to implement these two steps such that at the end, the running time of
the algorithm is O(|V | + |E|).
10. This question is about the Rod-Cutting Problem. Find best way to cut a rod of length
n. Assume that each length rod i has a price pi (all lengths are integers). You can use
as many cuts as you want (each cut is free).
For instance, consider a rod of length 8 with the following values.

length i 1 2 3 4 5 6 7 8
price pi 1 5 8 9 10 17 17 20

If you cut the rod into one piece of length 1, one piece of length 3 and one piece of
length 4, you get a total value of 1 + 8 + 9 = 18. If you cut the rod into two pieces of
length 2 and one piece of length 4, you get a total value of 2 · 5 + 9 = 19. What is the
optimal cut?
11. Let a = (a1 , a2 , ..., an ) be a sequence of numbers. The sequence b = (b1 , b2 , ..., bk ) is a
subsequence of a if the numbers in b can be found, in order, in a. A subsequence b is
increasing if the numbers in b are strictly increasing.
For instance, (1, 1, 3, 2, 8, 9, 4) is a subsequence of (1, 0, 1, 0, 1, 4, 4, 3, 5, 2, 8, 9, 100, 4, −1)
and (0, 1, 4, 5, 9, 100) is an increasing subsequence of (1, 0, 1, 0, 1, 4, 4, 3, 5, 2, 8, 9, 100, 4, −1).
Given a sequence a1 , a2 , ..., an as input, compute the length of a longest increasing
subsequence (LIS), which we denote by LIS(a1 , a2 , ..., an ). For instance, the LIS of
(5, 2, 8, 6, 3, 6, 9, 7) is (2, 3, 6, 9), which has length 4.
In this question, we solve the problem in two different ways.
(a) i. Prove that LIS(a1 , a2 , ..., an ) = LIS(−∞, a1 , a2 , ..., an , ∞) − 2.
ii. Define a graph G = (V, E), where V = {v0 , v1 , ..., vn+1 } and each vertex vi
gets a key such that
key(v0 ) = −∞,
key(vi ) = ai (1 ≤ i ≤ n),
key(vn+1 ) = ∞.
Moreover, there is a directed edge (vi , vj ) ∈ E if and only if i < j and ai < aj .
Explain how to solve this problem using the algorithm from Section 5.1.
iii. What running time do you get?
(b) * Explain how to solve this problem in O(n log(n)) time.

12. Let A[1..n] be an array containing n distinct real numbers (some of which may be
negative). Design a O(n) time algorithm that computes two indices i and j (where
i ≤ j) for which the sum A[i] + A[i + 1] + ... + A[j − 1] + A[j] is maximum.

13. In class, we discussed the problem Making Change. We know that the greedy algorithm
cannot solve the problem for all sets of denominations.

(a) Using the dynamic programming approach, design an algorithm which solves the
problem for all sets of denominations.
Suppose that there are n different denominations and that you need to make
change for a value v, the running time of your algorithm should be O(nv).
(b) What if you are allowed to use at most one coin of each denomination?

14. Given two strings x = x1 x2 ...xn and y = y1 y2 ...ym , we wish to find the length of their
longest common substring (not to be confused with subsequence), that is, the largest
k for which there are indices i and j with xi xi+1 ...xi+k−1 = yj yj+1 ...yj+k−1 . Show how
to do this in O(mn) time.

15. We studied a dynamic programming algorithm in Chapter 1. Can you tell which one?

16. We hope to find a greedy algorithm to solve the Matrix Chain Multiplication problem.
To multiply A1 · A2 · ... · An , where Ai is pi−1 × pi (1 ≤ i ≤ n), we wish to use the
following greedy criterion.

First multiply the matrices Mi and Mi+1 whose common dimension pi is smallest,
and continue in the same way.

Prove or disprove: this approach works for every input.

17. We hope to find a greedy algorithm to solve the Matrix Chain Multiplication problem.
To multiply A1 · A2 · ... · An , where Ai is pi−1 × pi (1 ≤ i ≤ n), we wish to use the
following greedy criterion.

First multiply the matrices Mi and Mi+1 that minimize the product pi−1 pi pi+1 , and
continue in the same way.

Prove or disprove: this approach works for every input.


—–Proofs—–

18. Consider a sequence a1 , a2 , ..., an of numbers. Sort this sequence and denote the result
by b1 ≤ b2 ≤ ... ≤ bn . Prove that LIS(a1 a2 ...an ) = LCS(a1 a2 ...an , b1 b2 ...bn ).

19. Show that to fully parenthesize an expression having n matrices we need n − 1 pairs
of parentheses.

You might also like