Sie sind auf Seite 1von 7

COMSATS Institute of Information Technology

Department of Computer Science



Final Exam Fall 2010 Semester
Design and Analysis of Algorithms (CSC201)
Teacher: Dr. M. G. Abbas Malik
Date: Friday January 28
th
2011
Instructions:
Do not open this exam booklet until you are directed to do so.
The final exam will end in one and half hour.
This exam is closed book.
Write your name and Student ID clearly on this first page.
When the exam is started, write your complete Student ID clearly on the top
of *EVERY* page.
Write your solution in the space provided. If you need more space, write on
the back of the sheet containing the problem. Do not put part of the answer to
one problem on the back of the sheet for another problem.
Plan your time wisely. Do not spend too much time on any one problem.
Read through all of them first and attack them in order that allows you to
make the most progress.
Show your work, as partial credit will be given. You will be graded not only
on the correctness of your answer, but also on the clarity with which you
express it. Be neat.
Use Pseudocode only, when you are asked to write an algorithm for a
problem.
Good luck.
Name of Student:

Student ID:

Final Exam Fall 2010 Semester BSCS Student ID:_________________
2

Question 1: (30)
Consider the following graph algorithm where
G graph with V Set of vertices and E Set of edges
weight function that maps v E to a real number
r a randomly selected root
Q a Min Heap
MST-Prim (G, , r)
1 For each u V[G]{
2 Key[u] =
3 [u] = nil
5 }
6 Key[r] = 0
7 Q = Build-Min-Heap (V[G])
8 While Q {
9 u = Extract-Minimum (Q)
10 For each v Adj[u] {
11 If v Q and (u, v) < Key[v] then{
12 [v] = u
13 Key[v] = (u, v)
14 Min-Heapify(Q)
15 }
16 }
17 }
The data part of each node in the input graph contain following information:
shows the parent relation
Vertex name contains the name of the node like a, b, c, etc.
Key contains the weight of an edge connecting the node with another node in the
graph
The input graph is shown in Figure 1 (a) with weights marked along its edges. The
randomly selected root r is the node a.

r = a
a b
Figure 1
After running the instruction 1 to 6, the input graph is initialized with different
information and it is shown in Figure 1 (b). Line # 7 initializes the Min-Heap object
and it looks like
Q = {a, b, c, d, e, f}
6
2
11
7
4
1
8
a
nil
0
b
nil

c
nil

d
nil

f
nil

e
nil

8
6
2
11
7
4
1
8
a

b c
d
f
e
8
Final Exam Fall 2010 Semester BSCS Student ID:_________________
3

a) Now you will run the instruction 8 to 17 and fill the following table of
figures and once you have finished running the algorithm and filling the
table, you will fill the output graph with final values, showing the
minimal spanning tree. (25)

u = a
Q = {b,c,d,e,f}

v = b, e

u = b
Q = {e,c,d,f}

v = a, e, c

u = e
Q = {c,d,f}, {d,c,f}, {f,d,c}

v = b, d, f

u = f
Q = {d, c}

v = e, d

u = d
Q = {c}

v = e, f, c

u = c
Q = {}

v = b, d


6
2
11
7
4
1
8
a
Nil
0
b
a
4
c
d
2
d
f
6
f
e
1
e
a
8
8
6
2
11
7
4
1
8
a
Nil
0
b
a
4
c
d
2
d
f
6
f
e
1
e
a
8
8
6
2
11
7
4
1
8
a
Nil
0
b
a
4
c
b
8
d
f
6
f
e
1
e
a
8
8
6
2
11
7
4
1
8
a
Nil
0
b
a
4
c
b
8
d
e
7
f
e
1
e
a
8
8
6
2
11
7
4
1
8
a
Nil
0
b
a
4
c
b
8
d
f
e
a
8
8
6
2
11
7
4
1
8
a
Nil
0
b
a
4
c
d
f
e
a
8
8
Final Exam Fall 2010 Semester BSCS Student ID:_________________
4

b) Discuss and give the detailed analysis of the algorithm MST-PRIM given
above. (5)
O(V+E)

Final Exam Fall 2010 Semester BSCS Student ID:_________________
5

Question 2 (10)
Apply Master Theorem to following recurrences, if possible and justify your answer.
a) I(n) = 2I [
n
2
+n
3

Solution
o = 2, b = 2, (n) = n
3

n
Iog
b
u
= n
Iog
2
2
= n
= (n) > n
Iog
b
u

it mcons cosc S o Hostcr
i
sIbcorcm is opplicoblc,
Complcxity o I(n) = 0((n)) = 0(n
3
)



b) I(n) = 16I [
n
4
+n
2


Solution
o = 16, b = 4, (n) = n
2

n
Iog
b
u
= n
Iog
4
16
= n
2

= (n) = n
Iog
b
u

it mcons cosc 2 o Hostcr
i
sIbcorcm is opplicoblc,
Complcxity o I(n) = 0(n
Iog
b
u
lg n) = 0(n
2
lg n)

Final Exam Fall 2010 Semester BSCS Student ID:_________________
6

Question 3 ()
a) Suppose we have a graph G = (V, E). There is non-overlapping partition of set
of vertices V, i.e. S and V-S. Identify all crosses and the light edge in the
following diagram. (4)
S V S

Crosses: (b,c) , (h,i), (h,g)
Light Edge: (h,g)

b) Build both the Adjacency List Representation and Adjacency Matrix
Representation of the following graph. (18)














Final Exam Fall 2010 Semester BSCS Student ID:_________________
7

c) Adjacency Matrix Representation of a Graph is Time efficient. (1)
d) Adjacency List Representation of a Graph is Space efficient. (1)
e) Space Complexity of Adjacency Matrix Representation is O(VV). (1)
f) Space Complexity of Adjacency List Representation is O(V+E). (1)
g) Given a graph G(V, E) and distinguished source vertex s, Breadth First Search
algorithm explores the edges of G to discover every vertex that is reachable
from the source s. (1)
h) Breadth First search algorithm produces a Breadth First Tree. (1)
i) Depth First search algorithm produces a Depth First Forest. (1)
j) Give two elements of Dynamic Programming. (2)
Optimal Structure
Overlapping sub-problem space
k) Give two elements of Greedy Algorithms. (2)
Optimal Structure
Greedy Choice
l) What is difference between Quick Sort Algorithm and Randomized Quick Sort?
(2)
In Quick Sort, first element is selected as a pivot, on the other hand, in
Randomized Quick Sort, a random number from the input array is selected as a
pivot.

Das könnte Ihnen auch gefallen