Docsity

Exercises from CMPSC 465, Spring 2009 Course, Assignments of Computer Science

Toggle sidebar
bg1
CMPSC 465, Spring 2009, Homework 8
Due: Friday,April 10, 7 pm. to Dr.Berman).
Page 292, exercise 7.
Iviewthe matrix as distances between nodes 0, 1, 2, 3, 4. Amatrix
Rkallows intermediate nodes 0, ... , k−1.
original
0
6
•
•
3
2
0
•
•
•
•
3
0
2
•
1
2
4
0
•
8
•
•
3
0
→
allow 0
0
6
•
•
3
2
0
•
•
5
•
3
0
2
•
1
2
4
0
4
8
14
•
3
0
→
allow 1
0
6
•
•
3
2
0
•
•
5
5
3
0
2
8
1
2
4
0
4
8
14
•
3
0
→
allow 2, 3
0
6
•
•
3
2
0
•
•
5
3
3
0
2
6
1
2
4
0
4
4
5
7
3
0
→
allow 4
0
6
10
6
3
2
0
12
8
5
3
3
0
2
6
1
2
4
0
4
4
5
7
3
0
Page 292, exercise 9.
Floyd’salgorithm givesincorrect 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 A1be 2000×1matrix, A2be 1×2000 matrix, and A3be 2000×1matrix.
Evaluating (A1×A2)×A3costs 2000×1×2000+2000×2000×1=8,000,000.
Evaluating A1×(A2×A3) costs 1×2000×1+2000×1×1=4,000.
b. A‘‘way’’tocompute the product nmatrices is a binary tree with nleaves. As dis-
cussed on page 294, this is the n-th Catalan number,
c(n)=
2n
n
1
n+1
with order of growth Θ(4nn−1.5).
c. Desig a dynamic programming solution.
The input will be the array of matrix dimentions, Aiis a D[i−1]×D[i]matrix.
We define a recursive subproblem: to find the minimum cost C[i,j]ofmultiplying
matrices Ai×...×Aj,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:
pf3
pf4

Partial preview of the text

Download Exercises from CMPSC 465, Spring 2009 Course and more Assignments Computer Science in PDF only on Docsity!

CMPSC 465, Spring 2009, Homework 8

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

EXTRA CREDIT

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.