


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Various exercises from the cmpsc 465, spring 2009 course, covering topics such as matrix theory, graph algorithms, and dynamic programming. Students are expected to solve these exercises, which may involve implementing algorithms, analyzing their complexity, and solving mathematical problems.
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Due: Friday, April 10, 7 pm. to Dr. Berman).
Page 292, exercise 7. I view the matrix as distances between nodes 0, 1, 2, 3, 4. A matrix Rk^ allows intermediate nodes 0, ... , k − 1. original
allow 0
allow 1
allow 2, 3
allow 4
Page 292, exercise 9. Floyd’s algorithm gives incorrect results if there exists a negative cost cycle. In that case, the minimum cost path does not exist, but Floyd returns an answer. E.g. we may have two nodes, d (0, 1) = 1, d (1, 0) = − 2.
Page 298, exercise 5. False. For example, when the nodes have very similar search probability then the optimum shape is not changed. We may have nodes 0, 1, 2 with probabilities 0. 34, 0. 33, 0. 33. With 0 at the root the average search cost is 1.99 and with 1 at the root the average is 1.67.
Page 299, exercise 10.
a. Let A 1 be 2000×1 matrix, A 2 be 1×2000 matrix, and A 3 be 2000×1 matrix. Evaluating ( A 1 × A 2 )× A 3 costs 2000× 1 ×2000+2000× 2000 ×1 = 8,000,000. Evaluating A 1 ×( A 2 × A 3) costs 1× 2000 ×1+2000× 1 ×1 = 4,000.
b. A ‘‘way’’ to compute the product n matrices is a binary tree with n leaves. As dis- cussed on page 294, this is the n -th Catalan number,
c ( n ) = ^2 n n
n + 1 with order of growth Θ(4 n^ n −1.5). c. Desig a dynamic programming solution.
The input will be the array of matrix dimentions, Ai is a D [ i − 1]× D [ i ] matrix. We define a recursive subproblem: to find the minimum cost C [ i , j ] of multiplying matrices Ai ×...^ × A (^) j , where i ≤ j. The basic case is C [ i , i ] = 0, no cost to multiply when there is only one matrix. The recursive relationship holds when i < j :
C [ i , j ] =
j − 1 min k = i C [ i , k ] + C [ k + 1, j ] + D [ i − 1] * D [ k ] * D [ j ]
Now we can fill a matrix iteratively, in order of increasing j − i : for (i = 1; i <= n; i++) C[i][i] = 0; for (s = 1; s <= n; s++) for (i = 1; i+s <= n; i++) { j = i+s; C[i][j] = infty; for (k = i; k < j; k++) { new = C[i][k]+C[k+1][j]+D[i-1]D[k]D[j]; if (new < C[i][j]) B[i][j] = k, C[i][j] = new; } } We can use array B to establish the actual optimal order of multiplications.
Page 314, exercise 7.
a.
Black numbers are priorities, 99 is infinity, red numbers are deleted from the queue a b
c d
7^ e 6
4
5
4
2
5
3
0 99
99 99
99
a b
c d
7^ e 6
4
5
4
2
5
3
0 5
7 99
2
a b
c d
7^ e 6
4
5
4
2
5
3
0
2
3
4 5
a b
c d
7^ e 6
4
5
4
2
5
3
0
2
3
4 5
a b
c d
7^ e 6
4
5
4
2
5
3
0
2
3
4 4
a b
c d
7^ e 6
4
5
4
2
5
3
0
2
3
4 4
Page 292, exercise 5.
Warshall’s algorithm can be written as follows. We initialize array R with the adjacency matrix of the graph and then we run this iteration: for (k = 0; k < n; k++) for (i = 0; i < n; i++) // innermost loop for (j = 0; j < n; j++) R[i][j] ||= R[i][k]&&R[k][j]; In the innermost loop the value of R[i][k] is fixed, so we can skip doing it if it is 0 (false), and ‘‘ignore it’’ if it is 1 (true): for (k = 0; k < n; k++) for (i = 0; i < n; i++) { if (!R[i][k]) continue; // innermost loop for (j = 0; j < n; j++) R[i][j] ||= R[k][j]; } Now the innermost loop replaces i -th row with a position-wise ‘‘or’’ with the k -th row. If we represent rows as bit strings, then the task of the innermost loop can be achieved using bitwise and.