Docsity

Past Final Exam - Fall 2005 - Algorithm Analysis and Design | CPSC 5115G, Exams of Computer Science

Toggle sidebar
bg1
CPSC 5115 Algorithm Analysis and Design
Final Exam Fall 2005
DUE DATE: 6:00 PM on Friday, December 16
This exam must be handed in before 6:00 PM on Friday, December 16.
Distance learners: Submit as an attachment to e–mail.
This exam is open–book, open–notes, and not proctored. Do your own work.
The first two questions are based on the following data for the Knapsack Problem. This is a
list of ten items, in no particular order, with weights, values and “value density” given.
Item Weight Value Density
1 16.00 192.00 12.00
2 2.00 4.00 2.00
3 3.00 9.00 3.00
4 1.00 0.50 0.50
5 5.00 24.00 4.80
6 18.00 27.00 1.50
7 4.00 18.00 4.50
8 20.00 400.00 20.00
9 30.00 27.00 0.90
10 24.00 4 8.00 2.00
1. (10 points) You are given a knapsack with a capacity of 15 pounds. Solve this as
a continuous (fractional) Knapsack problem, giving the amount of each item placed
in the knapsack and the total value of the solution..
2. (10 points) You are again given a knapsack with a capacity of 15 pounds. Solve this
as a 0–1 (discrete) Knapsack problem, giving the amount of each item placed
in the knapsack and the total value of the solution..
3. (20 points) Consider the following directed graph on four vertices, represented by the
following edge weight matrix, with the entry N indicating no such edge. Thus there is no
edge (1, 4) from vertex 1 to vertex 4; although there is an edge from vertex 4 to vertex 1.
Vertex 1 2 3 4
1 0 5 1 N
2 9 0 6 4
3 N 2 0 8
4 3 N N 0
Use Floyd’s Algorithm to compute all shortest paths in this directed weighted graph. You
may use short–cuts to avoid computations when two vertices are identical.
Hint: You may want to replace the “N” (for no such edge) by a large value, such as 100.
Page 1 of 3
pf3

Partial preview of the text

Download Past Final Exam - Fall 2005 - Algorithm Analysis and Design | CPSC 5115G and more Exams Computer Science in PDF only on Docsity!

CPSC 5115 Algorithm Analysis and Design

Final Exam Fall 2005

DUE DATE: 6:00 PM on Friday, December 16

This exam must be handed in before 6:00 PM on Friday, December 16. Distance learners: Submit as an attachment to e–mail. This exam is open–book, open–notes, and not proctored. Do your own work. The first two questions are based on the following data for the Knapsack Problem. This is a list of ten items, in no particular order, with weights, values and “value density” given. Item Weight Value Density 1 16.00 192.00 12. 2 2.00 4.00 2. 3 3.00 9.00 3. 4 1.00 0.50 0. 5 5.00 24.00 4. 6 18.00 27.00 1. 7 4.00 18.00 4. 8 20.00 400.00 20. 9 30.00 27.00 0. 10 24.00 48.00 2.

  1. (10 points) You are given a knapsack with a capacity of 15 pounds. Solve this as a continuous (fractional) Knapsack problem, giving the amount of each item placed in the knapsack and the total value of the solution..
  2. (10 points) You are again given a knapsack with a capacity of 15 pounds. Solve this as a 0–1 (discrete) Knapsack problem, giving the amount of each item placed in the knapsack and the total value of the solution..
  3. (20 points) Consider the following directed graph on four vertices, represented by the following edge weight matrix, with the entry N indicating no such edge. Thus there is no edge (1, 4) from vertex 1 to vertex 4; although there is an edge from vertex 4 to vertex 1. Vertex 1 2 3 4 1 0 5 1 N 2 9 0 6 4 3 N 2 0 8 4 3 N N 0 Use Floyd’s Algorithm to compute all shortest paths in this directed weighted graph. You may use short–cuts to avoid computations when two vertices are identical. Hint: You may want to replace the “N” (for no such edge) by a large value, such as 100.

CPSC 5115 Final Exam Due Friday, December 16, 2005 The next two questions are based on the following algorithm, written in a strange variant of Java, in which all arrays are one–based ; thus the statement long[] A = new long [10] creates an array of 10 elements, indexed 1 through 10. I still find 0–based arrays confusing when doing complexity analysis. Also note that this version uses procedures, not classes, and really is so different from any language that it cannot be processed by any compiler. Procedure Doofus ( long [] A, N) // Array A has N elements, indexed 1 to N. // if (N < 4) return ; // Have to stop the recursion somewhere. // A // First create two new arrays, each of size N /2. Assume this takes O(N) time. long N1 = N / 2 // Integer division long N2 = N – N long [] A1 = new long [N1] // Create an array of size N1, long [] A2 = new long [N2] // Create an array of size N // B // Copy values into these two new arrays. for ( long index = 1; index  N1; index++) A1[index] = A[index] ; for ( long index = 1; index  N2; index++) A1[index] = A[index + N1] ; // C // Now do the recursive calls. Doofus (A1, N1) ; // Here you should assume that both N1 and N2 are exactly Doofus (A2, N2) ; // equal to N / 2. I am just being picky in my coding. // D // Reassemble the original array A in a decidedly strange fashion. for (long J = 1; J  N1; J++) { for (long K = 1; K  N2; K++) // Again, assume that N1 = = N2 = = (N / 2) { L = J + K ; A[L] = A[L] + A1[J] * A2[K] ; } } ; // E // Split the big array again. for ( long index = 1; index  N1; index++) A1[index] = A[index] ; for ( long index = 1; index  N2; index++) A1[index] = A[index + N1] ; // F // Now do the recursive calls again. Doofus (A1, N1) ; // Here you should assume that both N1 and N2 are exactly Doofus (A2, N2) ; // equal to N / 2. I am just being picky in my coding. // G // Now the final assembly of the big array. for ( long index = 1; index  N1; index++) A[index] = A1[index] ; for ( long index = 1; index  N2; index++) A[index + N1] = A2[index] ; // H // done return ;