Maharishi University of Management: CS 435 - Design and Analysis of Algorithms
Maharishi University of Management: CS 435 - Design and Analysis of Algorithms
FINAL - ANSWERS
Course Title and Code: CS 435 - Design and Analysis of Algorithms
Instructo: Dr. Emdad Khan
Date: Friday 07/16/2015
Duration: 10am - 12 pm
Student Name: Total Mark
46 + 4 (Bonus)
a. Use the following Heap-Insert algorithm to insert 10 in the array A = {15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2 ,
1}. Make sure you take care of needed heapification as appropriate. Show all the steps starting at i
and moving i to the final step.
Ans. Starting point is i=10 as after executing lines 1 and 2, the heap size becomes 10. The steps are
shown below:
b. Insert the following sequence of numbers 23, 46, 12, 21, 75, 5, 3 into a hash table of size 9
using h(x) = x%9 as a hash function, where % mean “mod”. Use Chaining with Linked List to
avoid collision.
Ans.
0 1 2 3 4 5 6 7 8
46 12,21, 23,5
75,3
c. Use Pre-Order, In-Order and Post-Order Tree-Walk to print all keys in the following Tree:
Ans.
Question 2: 11 points (6 + 5)
a. Consider a 0-1 Knapsack problem with 4 items and with a max weight of 8. The benefits and
weight values are shown below.
Item 2 3 4 5
Benefit 3 4 5 6
Weight 2 3 4 5
Find an optimal solution using Dynamic Programming. Show all Table values. Be sure to state
both the value of the maximum benefit as well as the item(s) selected. Explain how you
determine the items selected (you can also use a column for Keep-items / selected items).
Ans.
1 2 3 4 5 6 7 8 Items
Selected
2 0 3 3 3 3 3 3 3 2
3 0 3 4 4 7 7 7 7 2,3
4 0 3 4 5 7 8 9 9 3,4
5 0 3 4 5 7 8 9 10 3,5
(For example, the third line in this table is the result we get when we try to find the maximum benefit
that is achievable using the set of items {2, 3, 4}.) We see from the table that the maximum benefit
obtainable (over the set of all 4 items) is 10. Tracing backwards through the table (e.g 10 -6 = 4 in row 2),
we can find that the set of items that gives us this benefit of 10 is the set (in the order we discover
them) {3,5}.
b. Show that a Red-Black tree with n-internal nodes has height at most 2 lg(n+1).
Ans.
Let h be the height of the tree. According to property 4 of Red Black tree, at least half the nodes on any simple path
from the root to a leaf, not including the root must be black. Thus, the black height of the root must be at least h/2.
Hence, we have,
Moving the 1 to the left side, and then taking log on both sides, we get,
Question 3: 12 points (6 + 4 + 2)
Using Dijkstra’s algorithm, determine the shortest path from node A to I. Show the steps, your
tables and the resulting path.
Ans. The min path is A->B->D->G->I for a cost of 16. Start at node A. Then MinQ operation will
yield node B instead of C as the enxt node as |AB| < |AC|. Using the same method, the next
node will be D etc.
b. Let G = (V, E) be a weighted, directed graph with exactly one negative weight edge and no
negative-weight cycles. Give an algorithm to find the shortest distance from s to all other
vertices in V that has the same running time as Dijkstra.
Ans.
Let’s say the negative-weight edge is (u; v). First, remove the edge and run Dijkstra from s. Then, check if
ds[u] + w(u,v) < ds[v]. If not, then we’re done. If yes, then run Dijkstra from v, with the negative-weight
edge still removed. Then, for any node t, its shortest distance from s will be min(d s[t], ds[u] + w(u, v) +
dv[t]).
c. Can you use Bellman-Ford shortest path algorithm on undirected graph? Explain your
answer. If your answer is yes, will you be able to deal with negative weight?
Ans.
Yes, we can use Bellman-Ford shortest path algorithm on undirected graph. This is because an
undirected graph is a directed graph with arrows in both direction. However, we need to ensure
that there is a no negative cycle. A negative cycle in a directed graph means a negative weight
in an undirected graph. So, to use Bellman-Ford algorithm on an undirected graph, we need to
make sure all weights are >= 0.
Question 4: 12 points (7 + 5)
a. Assume that Subset-Sum problem is NP-Complete. Show that the Knapsack problem is NP-
Complete by reducing the Subset-Sum problem to Knapsack problem. Show all the key details.
Use W for the weight and V for values for the Knapsack problem. Use {si} as the set, and "t" as
the total desired sum for the Subset-Sum problem.
You just need to explain the reduction concept well with specifics - no need to do a formal
proof. Assume that Knapsack belongs to NP. [Hint: see that both problems are very similar].
Ans.
The first step is to show that Knapsack belongs to NP. Given an input set, it is easy to see if the
total weight is at most W, and if the corresponding profit is at least V. It takes only linear time to
add all profits and weights to find true / false result of the decision problem. Thus, we can verify
a solution in polynomial time.
The 2nd step is to use Subset-sum problem and reduce it to Knapsack problem.
Knapsack problem
Instance: Non-negative weights w1, w2, , , wn, W, and profits v1, v2 , , vn, V.
Question: Is there a subset of weights with total weight at most W, such that the corresponding
profit is at least V?
It is very easy to reduce an instance of Subset Sum problem to an instance of Knapsack problem.
wi = ci = si and
W=V=t
The Yes/No answer to the new problem corresponds to the same answer to the original
problem. This can be shown with a few extra lines of reasoning (omitted for simplicity as we are
just testing whether the students could map the Subset-sum problem to Knapsack problem).
b. Define NP problems, NP-Complete problems and NP-Hard problems? Can all NP-Hard
problems be reduced to NP problems? Can all NP problems be reduced to NP-Hard problems?
Explain your answers.
Ans.
NP Problems: The class NP consists of all those decision problems whose positive
solutions can be verified in polynomial time given the right information, or equivalently,
whose solution can be found in polynomial time on a non-deterministic machine.
NP-complete problems are a set of problems that any other NP-problem can be
reduced to in polynomial time, but retain the ability to have their solution verified in
polynomial time. Alternatively, A problem (in NP) is NP-complete if any problem in NP is
reducible to it
NP-Hard Problem: NP-hard problems are those at least as hard as NP-complete
problems, meaning all NP-problems can be reduced to them, but not all NP-hard
problems are in NP, meaning not all of them have solutions verifiable in polynomial
time.
Yes, all NP problems can be reduced to NP-Hard problems. This is because NP-Hard
problems are at least as hard as the NP-Complete problems (where all NP problems can
be reduced to).
Consider the unweighted directed graph shown below. Does this graph has optimal substructure to find
the longest simple path, say between q and r or q and t? Explain your answer. Can you use Dynamic
Programming for this problem. Explain why or why not.
Ans.
Since the graph does not have optimal substructure, we cannot use Dynamic Programming for this
problem.