COP 3503 – Final Exam Review

advertisement
COP 3503 – Final Exam Review
1) Given the following items and a max weight of 5, solve the Knapsack problem:
Item
Weight
Value
A
2
3
B
3
4
C
4
5
D
5
6
Fill in the table below:
Item/Weight
A
B
C
D
1
2
3
4
5
a. Algorithm in your own words:
b. How do you know which items were chosen?
2) Find the LENGTH longest common subsequence between AGCAT and GAC.
Fill in the table below:
T
C
G
A
G
T
G
C
C
T
A
G
A
C
T
T
A
G
A
T
C
A
A
C
Give one longest common subsequence: ________________________________________
A
3) Binary Heap
a. Show the result of inserting the value 7 into the heap shown below. (Note: The heap is shown in its
array representation.) For your work, show the final result as a tree, not an array:
index 1
value 1
2
2
3
8
4
17
5
6
6
14
7
20
8
31
9
19
10
11
11
9
12
20
b. Show the tree after deleteMin is run on the finished tree from above.
c. Describe how a priority queue would be implemented.
d. Describe how to do heap sort and the run time.
4) Draw the Disjoint Set that corresponds to the array representation shown below:
index
1
2
3
4
5
6
7
value
1
1
6
8
8
1
8
8
8
5) (12 pts) Run Dijkstra's Algorithm to find the shortest distances in the graph H described below from
vertex A to all other vertices.
H contains vertices A, B, C, D, E, F, and G.
H contains the following edges with the following weights:
AB 3
AC 15
AG 40
BC 8
BD 13
CD 4
DE 10
DF 6
DG 20
EG 4
FE 2
Fill in the chart below as shown in class to receive full credit:
Add to Set
A
A
B
C
D
E
F
G
Note: Each row stores the estimates at that point in the algorithm from A to each of the vertices in the
graph.
6) (10 pts) Consider doing a topological sort of ten items A, B, C, D, E, F, G, H, I and J with the following
constraints:
C before E
J before G
D before G
B before D
H before B
H before A
D before E
D before I
E before I
B before F
Start your Depth First Searches at A and go in alphabetical order. In any individual DFS, if you have a
choice, first explore the letter that comes first alphabetically. Show your work and give the final answer
of the topological sort with these rules.
7) Consider the world series problem with 5 games and 0.6 probability of winning a single game. Fill in the table
below and determine the probability that your team wins the series:
i/j
0
1
2
3
0
1
2
3
What is the formula for array[i][0]=____________________________
What is the formula for array[0][j]=____________________________
What is the formula for array[i][j] = ____________________________
8) Consider the dynamic programming version of Subset Sum Given 4 values: 1,2,3,4, and a target 6, fill in the
array isSubset after the algorithm is run. Then fill in the missing pseudocode below:
int values[] = {1,2,3,4}
Boolean[] isSubset = {true, false, false, false, false, false, false}
after algorithm
isSubset = {
}
// Determines if a subset of the elements of values adds to target
// exactly. It is guaranteed that all the elements of values are
// positive.
public static boolean subsetSum(int[] values, int target)
{
// isSubset[x] stores true iff there is a subset of values
// that adds up to x.
boolean[] isSubset = new boolean[target+1];
isSubset[0] = true; // The only subset we have at first.
// Consider each element in the array.
for (int index=0; index<values.length; index++)
{
// Update whether or not this element helps for a
// subset that adds upto check.
for (______________________________________________________)
{
if (_____________________________________)
isSubset[check] = true;
}
}
// This is what we're interested in.
return _____________________________;
}
Download