final exam - design and analysis of algorithms - fall 2010 semester - sol

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:

Upload: abdul-rauf

Post on 21-Apr-2015

290 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Final Exam - Design and Analysis of Algorithms - Fall 2010 Semester - Sol

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 28th 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:

Page 2: Final Exam - Design and Analysis of Algorithms - Fall 2010 Semester - Sol

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

211

7

4

18

anil 0

bnil ∞

c nil ∞

dnil ∞

f nil ∞

enil ∞

8

6

211

7

4

18

a

b c

d

fe

8

Page 3: Final Exam - Design and Analysis of Algorithms - Fall 2010 Semester - Sol

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

211

7

4

18

aNil 0

ba 4

c d 2

d f 6

f e 1

ea 8

8

6

211

7

4

18

a Nil 0

b a 4

cd 2

d f 6

fe 1

ea 8

8

6

211

7

4

18

aNil 0

ba 4

c b 8

d f 6

f e 1

ea 8

8

6

211

7

4

18

a Nil 0

b a 4

cb 8

d e 7

fe 1

ea 8

8

6

211

7

4

18

aNil 0

ba 4

c b 8

d

f ea 8

8

6

211

7

4

18

a Nil 0

b a 4

c

d

fea 8

8

Page 4: Final Exam - Design and Analysis of Algorithms - Fall 2010 Semester - Sol

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)

Page 5: Final Exam - Design and Analysis of Algorithms - Fall 2010 Semester - Sol

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) 2

Solution

2, 2,

3 ,

b) 16 Solution

16, 4,

2 ,

lg lg

Page 6: Final Exam - Design and Analysis of Algorithms - Fall 2010 Semester - Sol

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)

Page 7: Final Exam - Design and Analysis of Algorithms - Fall 2010 Semester - Sol

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(V×V). (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.