This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Cs445 — Homework #7 (optinal) Dynamic programming, Stable marriage, and Approximation algorithm If you choose to submit this homework, its grade would replace yout Due: 5/9/2005 at 11:59pm (deadline is firm). If you are not in Tucson at this date, you may submit via email to Rohit. 1. Show how to reduce the space requirement of FloydWarshall algorithm to O ( n 2 ). Answer: We use only two matrices, each of size n × n . At each iteration we read from one and write to the other. 2. How can you use FloydWarshall algorithm to find if the graph contains negative cycles ? Answer: There is a negative cycle if and only if a table entry of D n [ i,i ] < (for some i ). This implies that there is a path from v i to v i whose distance is negative. 3. Generate a sequence of 5 matrices A 1 ,A 2 ,A 3 ,A 4 ,A 5 , and show how to optimally compute the number of multiplication operations need to compute the product A 1 · A 2 · A 3 · A 4 · A 5 . The smallest matrix has 5 rows, and the largest matrix has at least 100 rows. Answer: Let us assume the dimensions of the matrices to be: A 1 : (5,25) A 2 : (25,75) A 3 : (75,10) A 4 : (10,100) A 5 : (100,1) We use the recursive formula m [ i,j ] = 0 if i = j m [ i,j ] = min i ≤ k<j { m [ i,k ] + m [ k + 1 ,j ] + p i 1 p k p j } if i < j 1 We use the dynamic programming approach, initialising m [ i,i ] = 0 for i = 1,2,...5. We then compute m [ i,i + 1] for i = 1,2,3,4. m [1 , 2] = 5 * 25 * 75 = 9375 m [2 , 3] = 25 * 75 * 10 = 18750 m [3 , 4] = 75 * 10 * 100 = 75000 m [4 , 5] = 10 * 100 * 1 = 1000 Computing m [ i,i + 2] for i = 1,2,3....
View
Full Document
 Spring '06
 Williams
 Algorithms, Graph Theory, C Programming, Vertex

Click to edit the document details