0% found this document useful (0 votes)
15 views19 pages

Cs 702 Final Term Solved

The document contains solved final exam questions for the CS702 course on advanced algorithms, covering topics such as BFS, longest common subsequence, Naive string matching, Kruskal's algorithm, Dijkstra's algorithm, and various proofs related to graph theory and number theory. Each question includes a brief answer or pseudo code related to the algorithm or proof. The document is prepared by Aamir Shahzad and spans multiple pages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views19 pages

Cs 702 Final Term Solved

The document contains solved final exam questions for the CS702 course on advanced algorithms, covering topics such as BFS, longest common subsequence, Naive string matching, Kruskal's algorithm, Dijkstra's algorithm, and various proofs related to graph theory and number theory. Each question includes a brief answer or pseudo code related to the algorithm or proof. The document is prepared by Aamir Shahzad and spans multiple pages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CS702 – ADVANCED ALGORITHMS ANALYSIS AND DESIGN

SOLVED CURRENT FINAL PAPERS


Session: Fall 2015
Q.#. 1. How to print path in BFS algorithm
Ans. Shortest Paths
• The shortest-path-distance δ (s, v) from s to v as the minimum number of edges in
any path from vertex s to vertex v.
o if there is no path from s to v, then δ (s, v) = ∞
• A path of length δ (s, v) from s to v is said to be a shortest path from s to v.
• Breadth First search finds the distance to each reachable vertex in the graph G (V, E)
from a given source vertex s V.
• The field d, for distance, of each vertex is used.

Q.#. 2. Write down pseudo code of longest common subsequence?


Ans. Longest Common Subsequence Algorithm

Prepared By: Aamir Shahzad - 0300-681-9942 Page 1


Q.#. 3. Write down pseudo code of Naive String Matching problem?
Ans. Naive Approach
• The idea is based on Brute Force Approach.
• The naive algorithm finds all valid shifts using a loop that checks the condition P[1 ..
m] = T[s + 1 .. s + m] for each of the n - m + 1 possible values of s.
• It can be interpreted graphically as sliding a “template“ containing the pattern over
the text, noting for which shifts all of the characters on the template equal the
corresponding characters in the text.

Q.#. 4. Write the pseudo code of Optimal Binary Search Tree (10)

Prepared By: Aamir Shahzad - 0300-681-9942 Page 2


Q.#. 5. Kruskal’s Algorithm.

Q.#. 6. Prove that for all integers n, if n2is divisible by 7 then n is divisible by
Ans. Prove that for all integers n, if n2 is divisible by 7 then n is divisible by 7.
Proof :

Q.#. 7. Write down pseudo code of transition function?


Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 3


Q.#. 8. Prove that Z (+, *) ( the set of integers) is a ring.
Ans.

Q.#. 9. Prove that for all integers n, if n2 is even then n is also even.
Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 4


Q.#. 10. Use the Backtracking algorithm for the 0-1 Knapsack problem to maximize the
profit for the following problem instance. Show actions step by steps.
Maximum Capacity = 8
i 1 2 3 4
vi 3 5 6 10
wi 2 3 4 5
Ans.. (2,2,3;11) means that two elements of each weight 2 and one element of weight 3 is with
total value 11

Knapsack Algorithm: Backtracking

Q.#. 11. Write down the pseudo code of Huffman algorithm.


Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 5


Q.#. 12. Decrypt 0981 0461 if encrypted using RSA Public key = (e, n) = (13, 43.59 =
2537)
Ans. Solution

Q.#. 13. SAT is NP Complete


Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 6


Q.#. 14. Prove that in a graph there are even number of vertices of odd degree.(5)
Ans.

Q.#. 15. Prove that every connected graph has a spanning tree (5)
Ans.
If the graph has no cycles but is connected, it is a tree; and thus is its own
spanning tree. This makes a good base step for a proof by induction on the
number of cycles of the graph that every connected graph has a spanning tree.

Suppose inductively that when c>0 and a connected graph has fewer than c
cycles, then the graph have a spanning tree.
Suppose that G is a graph with c cycles. Choose a cycle of G and choose and edge
of that cycle. Deleting that edge (but not its endpoint) reduces the number of
cycles by at least one, and so our inductive hypothesis implies that the resulting
graph has a spanning tree. But then that spanning tree is also a spanning tree of G.

Therefore by the principle of mathematical induction, every finite connected


graph has a spanning tree.

Q.#. 16. Write the pseudo code of DIJKSTRA’S algorithm. (5)


Ans. Dijkstra’s Algorithm

Prepared By: Aamir Shahzad - 0300-681-9942 Page 7


Q.#. 17. Write the Prim’s algorithm of minimum spanning tree.(10)

Q.#. 18. Write the pseudo code of Fast Fourier Transform (FFT) recursive algorithm.
Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 8


Q.#. 19. Give a counter example of conjucture that if there is a path from u to v in a
directed graph g then any depth first search must result d[u]< f[u] 5
Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 9


Q.#. 20. Relatively prime
Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 10


Relatively prime
Ans.

Q.#. 21. Encrypt message STOP using RSA cryptosystem with


p = 43, q = 59 and e = 13, n = pq = 2537, from lecture 41
Ans.

Q.#. 22. Show that CIRCUIT-SAT is NP complete. 10


Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 11


Q.#. 23. Steps Designing Greedy Algorithms
Ans.

Q.#. 24. CLIQUE


Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 12


Q.#. 25. Dynamic algo and greedy ka difference
Ans.

Q.#. 26. With the help of Chinese remainder theorm solve following (10 marks)

Q.#. 27. DFS algo ( 5 markx )


Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 13


Q.#. 28. Huffman optimal coding given frequencies and we find the solution by drawing
t tree
Ans.

Q.#. 29. Write the pseudo code of Naïve String Matching Algorithm. (5)
Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 14


Q.#. 30. GCD
Ans.

Q.#. 31. A binary tree is not fully corresponding with optimal prefix prove it? ( 5 markx)
Ans.

Let T be a binary tree corresponds to prefix code such that T is not full. Then there must exist an
internal node, say x, such that x has only one child, y. Construct another binary tree, T`, which has
save leaves as T and have same depth as T except for the leaves which are in the sub tree rooted at
y in T. These leaves will have depth in T`, which implies T cannot correspond to an optimal prefix
code.
To obtain T`, simply merge x and y into a single node, z is a child of parent of x (if a parent exists)
and z is a parent to any children of y. Then T` have the desired properties: it corresponds to a code
on the same alphabet as the code which are obtained, in the sub tree rooted at y in T have depth in
T` strictly less (by one) than their depth in T.

Completes the proof. □

T
h a b c d e F
iFrequency 45,000 13,000 12,000 16,000 9,000 5,000
s
Fixed Length code 000 001 010 011 100 101
cVariable-length Code 0 101 100 111 1101 1100

Note:

 Fixed-length code is not optimal since binary tree is not full.


 Optimal prefix code because tree is full binary

Prepared By: Aamir Shahzad - 0300-681-9942 Page 15


Q.#. 32. Prove that for all prime p and all integers a,b if p|ab then p|a or p|b (or provides
both a and b)
Ans.

Q.#. 33. Proof a|b and a|c then a|(b-c)


Ans.

Q.#. 34. Prove that if a|b, b|c then a|c, a, b, c Z


Ans.

Q.#. 35. Prove if a | b , a | c then a | (b +c) for all a, b, c belongs to Z ( 5 mrkx )


Ans.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 16


Q.#. 36. BFS algo
Ans.

Q.#. 37. Algorithm for print screen BFS

Q.#. 38. Prim’s algorithm for minimum spanning tree.

Prepared By: Aamir Shahzad - 0300-681-9942 Page 17


Q.#. 39. The Rabin-Karp Algorithm for any integer n and any prime number p, if p
divides n then p dose not divide n+1

Q.#. 40. Explain the sitiation in which greedy algorithm does not work?

Q.#. 41. Write steps of topological sort algorithm?

Prepared By: Aamir Shahzad - 0300-681-9942 Page 18


Q.#. 42. Euclid’s Algoritm

Q.#. 43. Compute (1,2) if m1=3 and m2=4

Q.#. 44. Summary of Different Algorithms

Prepared By: Aamir Shahzad - 0300-681-9942 Page 19

You might also like