Uploaded by abdo.yasser441

CSC340 OldBylaw Midterm 2021 Term1 [Model Answer]

advertisement
Name:
Sec:
Faculty of Computer and Information Sciences
Ain Shams University
Subject: CSC340 Analysis & Design of Algorithms
Exam: Midterm 23/11/2021
Year: 3rd undergrad – term1 (General)
ID:
Examiners: Prof. Dr. El-Sayed El-Horbaty
Dr. Ahmed Salah
Offering Dept.: Computer Science
Academic year: 2021-2022
Duration: 45 min
Answer the following questions:
(total marks: 10 (+1))
WRITE YOUR ANSWERS IN SAME EXAM PAPER
1st Question [Analysis]
marks: 5
A) Mark True or False
[1 Mark: 0.25/each]
1. If there're two algorithms that solve the same problem with the same order. Moreover, you are sure
that the problem always runs on large input size. Shall you pay attention to the constant factor when
comparing the two algorithms?
(T
)
2. An algorithm with running time log(N!) has an upper bound complexity of O((logN)N)?
(F
)
3. Cost of partitioning (i.e. divide) in Quick sort is O(N) while it’s O(1) in Merge sort?
(T
)
4. A quadratic algorithm to solve a problem can perform faster than a linear-time algorithm to solve the
same problem at some inputs?
(T
)
B) For the following code, chose the notation that BEST REPRESENTS its WORST and BEST case?
[2 Marks: 1/each]
Flag = true
1. WORST CASE
2. BEST CASE
I = 1
a. O(1)
a. O(1)
While (I < N AND Flag == true)
b. Θ(1)
b. Θ(1)
If (A[I] > A[I + 1])
c. O(N)
c. O(N)
Flag = false
d. Θ(N)
d. Θ(N)
End If
e. Ω(1)
e. Ω(1)
I = I + 1
End while
f. Ω(N)
f. Ω(N)
C) Analyze:
Given the following code, analyze it and answer the following questions:
sum = 0;
fun(N)
{
if (N == 1) return 0
for(I = 1 ; I < 4; I *= 2)
sum += fun(N/2);
}
1. Write the recurrence relation (T(N))?
Answer: T(N) = ……………………………………………………………………………………………………………. 2T(N/2) + O(1)
2. What is the final complexity of the code?
Answer: ……………………………………………………………………………. N
Page 2 of 2
[2 Marks: 1/each]
2nd Question [Divide & Conquer (D&C)]
marks: 5 (+1)
A) Trace & Analysis
[2 Marks: 1/each]
1. For the worst case linear time algorithm (ALG#2) with groups of 5, what’s the value of the first selected
pivot: {5, -1, 7, -4, 9, -3, 8, 0, 2, -6, 1, 2, 3, 4, 5, 9, 9, 9, 9, 9, -7, -8, 6, 6, 4}
Answer: …………………………………………………………. 4
2. In quicksort algorithm, suppose your smart friend gives you a technique for selecting the pivot as the 2nd
smallest element at EACH time. Write down the recurrence of the quick sort using this technique?
Answer: T(N) = ……………………………………………………………………………………………………………. T(N – 2) + O(N)
B) Problem: Position of Largest Element
[4 Marks]
Design a divide-and-conquer algorithm for finding the position of the largest element in an array of N
distinct numbers and answer the following questions:
1. Clearly specify (in words) what’s done at each of the three D&C steps?
[1 mark]
Divide: [0.25]
Trivial/Divide the array into 2 halves
Conquer:
[0.25]
Combine: [0.5]
by finding the position of the largest element in each of the two halves
Return position of the max of the 2 largest elements from the 2 halves
2. Write down the pseudo-code of your solution?
Pseudocode
int IndexOfLargest(A, L, R)
if L = R
return L
M = (L + R) / 2
temp1 = IndexOfLargest(A, L, M])
temp2 = IndexOfLargest(A, M + 1, R)
if A[temp1] ≥ A[temp2]
return temp1
else return temp2
3. What’s the recurrence relation of this efficient solution?
[2.5 marks]
[0.25 mark]
[0.25 mark]
[0.25 mark]
[0.5 mark]
[0.5 mark]
[0.25 mark]
[0.25 mark]
[0.25 mark]
[0.5 mark]
Answer: T(N) = ……………………………………………………………………….………………………………. 2 T(N/2) + O(1)
Wishing to you all the best isA ☺
Ahmed Salah
Page 2 of 2
Download