Homework3
Homework3
1. Starting with array S[] = {123, 34, 89, 56, 150, 12, 9} indexed from 1 to 7:
a) (15 pts) Sort S[] using Merge Sort. Show the recursive tree and indicate order of
execution, i.e., label steps in tree nodes.
b) (5 pts) For 𝑛 = 32, in the best case Merge Sort would be expected to perform
how many comparisons?
2. Starting with array S[] = {123, 34, 189, 56, 150, 12, 9, 240} indexed from 1 to 8:
a) (15 pts) Assuming that Quick Sort uses the first item in the list as the pivot item,
sort S[] using Quick Sort. Show the recursive tree. Show the recursive tree and
indicate order of execution, i.e., label steps in tree nodes.
b) (5pts) Give a list of 8 items that represent the worst case scenario. Describe the
reason why it represents a worst case as well as its time complexity with n inputs.
4. For a certain directed graph with 4 vertices, Floyd’s Algorithm has produced the
following arrays D(3) and P(3). Using these values, compute the missing values in the
arrays D(4) and P(4). Show how you computed.
a) (3 pts) D(4)[1][2] =
b) (3 pts) D(4)[2][3] =
c) (3 pts) P(4)[2][3] =
d) (3 pts) P(4)[3][2] =
© Kaz Okada
e) (5 pts) Using the array 𝑃(4) , find the path from v1 to v2 that is shortest. Show how
you apply the information in 𝑃 (4) to arrive at your answer.
f) (3 pts) True or False: In general for a directed graph with n vertices, the Floyd’s
Algorithm guarantees that for all vertices 𝑣𝑖 , 𝑣𝑗 , 𝑣𝑘 , 𝐷 (𝑛) [𝑖][𝑗] = 𝐷(𝑛) [𝑖][𝑘] +
𝐷(𝑛) [𝑘][𝑗]
5. For the 0-1 Knapsack Problem, the item #1 has weight of 4 and the value of $12; the
item #2 has weight of 1 and the value of $1; the item #3 has weight of 5 and the value of
$10; and the item #4 has weight of 2 and the value of $8. The capacity of the knapsack’s
weight is 10. The Dynamic Programming algorithm has produced the following array. Fill
the last two entries in the array and show how they are calculated.
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 12 12 12 12 12 12 12
2 0 1 1 1 12 13 13 13 13 13 13
3 0 1 1 1 12 13 13 13 13 22 23
4 0 1 8 9 12 13 20 21 21
a) (6 pts) D[4][9] =
b) (6 pts) D[4][10] =
c) (8 pts) Which items should be put in the knapsack to achieve the maximum profit?
Show how you determined this result.
© Kaz Okada