CS502 Fundamentals of Algorithms 2013 Final Term Questions Answers Solved With References by Moaaz
CS502 Fundamentals of Algorithms 2013 Final Term Questions Answers Solved With References by Moaaz
1
Q No.5 In the solution of edit distance technique, please describe two solution given
(i) MATHS (ii) ARTS
Answer: (Page 77)
Q No.7 Explain the following two basic cases according to Floyd-Warshall Algorithm,
1. Don‟t go through vertex k at all.
2. Do go through vertex k.
2
Q No.6Variants of shortest path solution briefly?
Answer: (Page 153)
There are a few variants of the shortest path problem.
Single-source shortest-path problem: Find shortest paths from a given (single) source vertex s 2 V to
every other vertex v 2 V in the graph G.
Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from each
vertex v. We can reduce the this problem to a single-source problem by reversing the direction of each edge
in the graph.
Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v. If we solve
the single-source problem with source vertex u, we solve this problem also. No algorithms for this problem
are known to run asymptotically faster than the best single-source algorithms in the worst case.
All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and v.
Although this problem can be solved by running a single-source algorithm once from each vertex, it can
usually be solved faster.
Define Forward Edge from ancestor to descendent (u, v) where v is a proper descendent of u in the tree.
Answer: (Page 130)
For a forward edge (u, v), v is a descendent of u and so v’s start-finish interval is contained within u’s
implying that v has an earlier finish time. For a cross edge (u, v) we know that the two time intervals are
disjoint. When we were processing u, v was not white (otherwise (u, v) would be a tree edge), implying that
v was started before u. Because the intervals are disjoint, v must have also finished before u
3
Q pseudo code of timestamp DFS
Answer: (Page 126)
DFS(G)
1 for (each u 2 V)
2 do color[u] white
3 pred[u] nil
4 time 0
5 for each u 2 V
6 do if (color[u] = white)
7 then DFSVISIT(u)
4
CS502 – Final term (Fall 2012)
Q what is free tree of 2 marks
Answer: (Page 142)
A free tree is a tree with no vertex designated as the root vertex.
5
Q: Differentiate between back edge and forward edge
Answer: (Page 128)
Back edge: (u, v) where v is an ancestor of u in the tree.
Forward edge: (u, v) where v is a proper descendent of u in the tree.
Suppose you could prove that an NP-complete problem cannot be solved in polynomial time. What
would be the consequence?
Answer: Rep
Q:comment whether the computational powers RAM sequential machine are less than that the
parallel machine (3)
Answer: Page 10
RAM seems to go a good job of describing the computational power of most modern (non parallel)
machines. It does not model some elements, such as efficiency due to locality of reference.There are some
“loop-holes” (or hidden ways of subverting the rules)
Q: given a graph G(V,E) any DFS forest of G and consider edge (u,v)E E. prove that if this edge is
tree, forward or back edge then f[u]>f[v] and if this edge is backedge then f[u] ≤ [v]. (3)
Answer: Rep
6
CS502 – Final term (Fall 2012)
Q4 - free tree
Answer: Rep
Q5- reduction and example
Answer: NP-complete problem, then ever problem in NPC will be solvable in polynomial time. For this, we
need the concept of reductions.
7
EXAMPLE:
3-color: Given a graph G, can each of its vertices be labelled with one of 3 different colors such that two
adjacent vertices have the same label (color).
Coloring arises in various partitioning problems where there is a constraint that two objects cannot be
assigned to the same set of partitions. The term “coloring” comes from the original application which was
in map drawing. Two countries that share a common border should be colored with different colors.
It is well known that planar graphs can be colored (maps) with four colors. There exists a polynomial time
algorithm for this. But determining whether this can be done with 3 colors is hard and there is no
polynomial time algorithm for it. In Figure 9.3, the graph on the left can be colored with 3 colors while the
graph on the right cannot be colored.
8
Q:4 Kruskal's algorithm can return different spanning trees for the same input graph G, depending
on how ties are broken when the edges are sorted into order. Show that for each minimum spanning
tree T of G, there is a way to sort the edges of G in Kruskal's algorithm so that the algorithm returns
T.
Solution:
We would start by sorting the edges in of G in non-descending order. In addition, we would want that
among the edges of same weight, the edges which are contained in T are placed in first positions.
The claim here is that Kruskal’s algorithm will return T when run on E if sorted in the above mentioned
manner.
Proof:
T: e1 ≤ e2 ≤ , … , ≤ em
T’: e’1 ≤ e’2 ≤ , … , ≤ e’m
Weight of ei = Weight of e’i where 1 ≤ i ≤ m
Since the algorithm always places edges of T first, the edges of T will be chosen to T’.
Q:5 How to get Knapsack optimal solution with dynamic programming algorithm table ? (5)
Answer: (Page 96)
The algorithm for computing V[i, j] does not keep record of which subset of items gives the optimal
solution. To compute the actual subset, we can add an auxiliary boolean array keep [i, j] which is 1 if we
decide to take the ith item and 0 otherwise. We will use all the values keep[i, j] to determine the optimal
subset T of items to put in the knapsack as follows:
• If keep[n,W] is 1, then n 2 T. We can now repeat this argument for keep [n − 1,W − wn].
• If kee[n,W] is 0, the n 62 T and we repeat the argument for keep[n − 1,W].
9
Q:6 Define the following in Kruskal algorithm
Create Set-u
Find Set-u
Union(u,v)
Answer: Rep
Q:8 Q No.7 Explain the following two basic cases according to Floyd-War shall Algorithm?
Answer: Rep
Q:9:
Chain matrix multiplication? (2)
Answer: (Page 85)
Matrix multiplication is an associative but not commutative operation. We are free to add parenthesis the
above multiplication but the order of matrices cannot be changed. The Chain Matrix Multiplication
Problem is stated as follows:
Given a sequence A1,A2, . . . ,An and dimensions p0, p1, . . . , pn where Ai is of dimension
pi−1 × pi, determine the order of multiplication that minimizes the number of operations.
10
Q: How Plagiarism detection can be done with edit distance?
Answer: Page 76
Plagiarism Detection
If someone copies, say, a C program and makes a few changes here and there, for example, change variable
names, add a comment of two, the edit distance between the source and copy may be small. The edit
distance provides an indication of similarity that might be too close in some situations.
Can an adjacency matrix for a directed graph ever not be square in shape? Why or why not?
Answer: click here 4 detail
No. since we want to describe the relationship between each node and each other node, we need precisely
n^2 matrix entries.
Q: What do you mean by polynomial time algorithm? Explain what kind of problems can be solved
by using polynomial time algorithm?
Answer: (Page 169)
A polynomial time algorithm is any algorithm that runs in O(nk) time.
A problem is solvable in polynomial time if there is a polynomial time algorithm for it.
Q: Run Radix sort on the following array of integers, show first two passes of sorting:
8081, 7342, 9287, 9583, 3202, 5215, 8397, 8001, 972, 5315, 1983, 283, 1664, 8107
Answer :Page 71
8081 808[1] 32[0]2
7342 800[1] 80[0]1
9287 734[2] 81[0]7
9583 320[2] 52[1]5
3202 198[3] 53[1]5
5215 958[3] 97[2]
8397 => 166[4] => 28[3]
8001 521[5] 73[4]2
972 531[5] 16[6]4
5315 928[7] 19[8]3
1983 839[7] 80[8]1
283 810[7] 92[8]7
1664 283 95[8]3
8107 972 83[9]7
11
CS502 – Final term (Spring 2012)
How can we make it possible for an array of “n” elements that every element has equal probability of
„1/n‟ to be selected as pivot elements?
Answer: (Page 50)
To analyze the average running time, we let T(n) denote the average running time of QuickSort on a list of
size n. It will simplify the analysis to assume that all of the elements are distinct. The algorithm has n
random choices for the pivot element, and each choice has an equal probability of 1/n of occurring. So we
can modify the above recurrence to compute an average rather than a max, giving:
12
CS502 – Final term (Spring 2012)
Difference b/w back ward and forward 2 marks
Answer: Rep
13
8. How Dijkstra‟s algorithm works? (3)
Answer: Rep
3. Prim's Algorithm
Answer: Page 151
PRIM((G,w, r))
1 for ( each u 2 V)
2 do key [u] 1; pq.insert (u, key[u])
3 color [u] white
4 key [r] 0; pred [r] nil; pq.decrease key (r, key [r]);
5 while ( pq.not empty ())
6 do u pq.extract min ()
7 for ( each u 2 adj [u])
8 do if ( color [v] == white )and( w (u, v) < key [v])
9 then key [v] = w (u, v)
10 pq.decrease key (v, key [v])
11 pred [v] = u
12 color [u] = black
14
7. Prove that the generic TRAVERSE (S) marks every vertex in any connected graph exactly once
and the set of edges (v, parent (v)) with parent (v) ¹F form a spanning tree of the graph.
Answer: Page 125
15
CS502 – Final term (Spring 2012)
Q1) answer the follwing according to Floyd Warshall 1) runing time 2) space use
Answer: Page 164
the running time is theta (n3). The space used by the algorithm is Theta (n2).
16
5) how generic algorithms work with minimum spaning tree 3marks
Answer: Page 143
7) Which points should be supposed to prove the correctness of the Dijkstra's Algorithm 3marks
Answer: (Page 158 – 159)
17
CS502 – Final term (Spring 2012)
4- What are the minimum and maximum number of elements in a heap of height h (5 marks)
Answer: Page 44
At level h, there will be 2^h or less nodes.
7- Compare bellman ford algorithm with dijikstr's algorithm. Also give the time complexity of
bellman ford algorithm (3 marks)
Answer: Page 159
Dijkstra’s single-source shortest path algorithm works if all edges weights are non-negative and there are no
negative cost cycles. Bellman-Ford allows negative weights edges and no negative cost cycles. The
algorithm is slower than Dijkstra’s, running in _(VE) time.
18
Total running time of edit distance
Answer: (Page 84)
The total running time is (n2).
19