daa notes
daa notes
Y
Algorithms
LOG
Questions Bank
NO
ECH
FT
YO
D EM
Academy of Technology
[email protected]
ACA
DEM
YO
2
FT
ECH
NO
LOG
Y
Y
LOG
Contents
1 Network Flow 5
NO
1.1 MCQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Understand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 Apply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
ECH
1.5 Analyze . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
FT
YO
D EM
ACA
3
ACA
DEM
YO
4
FT
ECH
NO
LOG
Y
Y
LOG
Chapter 1
Asymptotic Notation
NO
1.1 Questions for Blooms Taxonomy Level: Remember
1. ..... of an algorithm refers to dening the mathematical bound/framing of its run-time per-
ECH
formance.
Ans : B
FT
2. Using asymptotic analysis, we can very well conclude the ............ scenario of an algorithm.
Ans : D
3. Which case indicate the minimum time required for program execution?
EM
Ans : A
4. ............ is the formal way to express the upper bound of an algorithm's running time.
D
Ans : C
5
Ans : A
Y
6. Which of the following is linear asymptotic notations?
LOG
a) O(1) b) O(logn) c) O(n) d) O(nlog2 n)
Ans : C
7. O(logn) is?
NO
c) polynomial asymptotic notations d) quadratic asymptotic notations
Ans : B
ECH
8. Omega Notation is the formal way to express the lower bound of an algorithm's running time.
a) TRUE b) FALSE
Ans : A
FT
9. The Theta notation is the formal way to express ......... of an algorithm's running time.
Ans : C
Ans : B
1 int fun(int n)
2 {
D
3 int count = 0;
4 for (int i = n; i > 0; i /= 2)
ACA
6
a) O(n2 ) b) O(nLogn) c) O(n) d) O(nLognLogn)
Y
Answer: c
12. What is the time complexity of fun()?
LOG
1 int fun(int n)
2 {
3 int count = 0;
4 for (int i = 0; i < n; i++)
5 for (int j = i; j > 0; j--)
6 count = count + 1;
NO
7 return count;
8 }
ECH
a) Θ(n) b) Θ(n2 ) c) Θ(n × Logn) d) Θ(nLognLogn)
Answer: b
13. Which of the following is not O(n2 )?
Answer: c
YO
14. Which of the given options provides the increasing order of asymptotic complexity of functions
f 1, f 2, f 3 and f 4?
f 1(n) = 2n
f 2(n) = n3/2
f 3(n) = nLogn
f 4(n) = nLogn
EM
a) f 3, f 2, f 4, f 1 b) f 3, f 2, f 1, f 4 c) f 2, f 3, f 1, f 4 d) f 2, f 3, f 4, f 1
Answer: a
D
15. What is the worst case time complexity of insertion sort where position of the data to be
inserted is calculated using binary search?
ACA
a) N b) N logN c) N2 d) N (logN )2
Answer: c
16. What is the time complexity of the below function?
7
1 void fun(int n, int arr[])
2 {
Y
3 int i = 0, j = 0;
4 for(; i < n; ++i)
LOG
5 while(j < n && arr[i] < arr[j])
6 j++;
7 }
NO
Answer: a
17. In a competition, four dierent functions are observed. All the functions use a single for loop
and within the for loop, same set of statements are executed. Consider the following for loops:
ECH
1 for(i = 0; i < n; i++)
2 for(i = 0; i < n; i += 2)
3 for(i = 1; i < n; i *= 2)
4 for(i = n; i > -1; i /= 2)
If n is the size of input(positive), which function is most ecient(if the task to be performed
FT
is not an issue)?
a) A b) B c) C d) D
Answer: c
YO
a) A b) B c) C d) D
ACA
Answer: d
19. Consider the following C code segment [ISRO CS2018]
8
3 else return (f(x-1) + g(x))
4 }
Y
5
6 int g (int x){
7 if (x < 2) return 2;
LOG
8 else return (f(x-1) + g(x/2));
9 }
NO
Answer: b
20. Let f (n) and g(n) be asymptotically non-negative functions. Which of the following is
correct? [UGC NET CS 2015]
a)
c)
Answer: d
ECH
Θ(f (n) × g(n)) = min(f (n), g(n))
Θ(f (n) + g(n)) = min(f (n), g(n))
b)
d)
Θ(f (n) × g(n)) = max(f (n), g(n))
Θ(f (n) + g(n)) = max(f (n), g(n))
21. What is the time complexity of following function fun()? Assume that log(x) returns log
FT
value in base 2.
1 void fun(){
2 int i, j;
3 for (i=1; i<=n; i++)
4 for (j=1; j<=log(i); j++)
YO
5 printf("GeeksforGeeks");
6 }
Answer: b
22. Consider the program [ISRO CS2017]
1 void function(int n) {
D
2 int i, j, count=0;
3 for (i=n/2; i <= n; i++)
ACA
9
a) O(logn) b) O(n2 ) c) O(n2 logn) d) O(nlogn)
Answer: d
Y
23. An unordered list contains n distinct elements. The number of comparisons to nd an element
LOG
in this list that is neither maximum nor minimum is
Answer: d
24. Arrange the following functions in increasing asymptotic order:
A. n1/3
NO
B. e
n
C. n
7/4
D. nlog n
9
E. 1.0000001
n
ECH
a) A, D, C, E, B b) D, A, C, E, B c) A, C, D, E, B d) A, C, D, B, E
Answer: a
25. If f (x) = (x3 − 1)/(3x + 1) then f (x) is?
Answer: b
27. The big-O notation for f (n) = (nlogn + n2 )(n3 + 2) is?
Answer: d
28. The Θ notation for function f (n) = 2n3 + n − 1 is?
a) n b) n2 c) n3 d) n4
D
Answer: c
29. The Θ notation for f (n) = nlog(n2 + 1) + n2 logn is?
ACA
Answer: a
30. The ω notation for f (x, y) = x5 y 3 + x4 y 4 + x3 y 5 is?
10
a) x5 y 3 b) x5 y 5 c) x3 y 3 d) x4 y 4
Answer: c
Y
31. The little − o notation for f (x) = xlogx is?
LOG
a) x b) 1 c) x2 d) xlogx
Answer: c
32. The big-O notation for f (n) = 2log(n! ) + (n2 + 1)logn is?
a) n b) n2 c) nlogn d) n2 logn
NO
Answer: d
33. The Ω notation for f (x) = 5logx is?
a) 1 b) x c) x2 d) x3
a)
Answer: a
34. The
x2
O
Answer: d
notation for
b) x3
ECH
f (x) = 2x4 + x2 − 4 is?
c) x d) x4
FT
1.2 Questions for Blooms Taxonomy Level: Understand
1. What is an algorithm.
YO
11. Prove that if f (n) = Θ(g(n)) and g(n) = Θ(h(n)) then f (n) = Θ(h(n))
12. Prove that if f (n) = Ω(g(n)) and g(n) = Ω(h(n)) then f (n) = Ω(h(n))
13. Prove that if f (n) = am nm + am−1 nm−1 + ... + a1 n + a0 then f (n) = O(nm )
11
1.3 Questions for Blooms Taxonomy Level: Apply
Y
1.
Answer:
0 ≤ 4n2 ≤ 5n2 − 6n ≤ 5n2 ∀ n ≥ 6
LOG
=⇒ 0 ≤ c1 g(n) ≤ f (n) ≤ c2 g(n)
=⇒ f (n) = Θ(g(n)) = Θ(n2 ) for c1 = 4, c2 = 5 and n0 = 6
NO
i=0
Prove that
Pn
5. i=0 i3 = Θ(n4 )
ECH
7.
Answer:
From the Stirling's approximation
√ 1
n! = 2nπ( ne )n e 12n
Now,
ACA
1
2
nlog2 n ≤ nlog2 n − nlog2 e + 12 log2 n + 1
12n
log2 e + 12 log2 (2π) ≤ 2nlog2 n ∀ n ≥ 4
1
∴ c1 g(n) ≤ f (n) ≤ c2 g(n) for n0 = 4, c1 = 2
and c2 = 2
12
∴ log2 (n! ) = Θ(nlog2 n)
Y
x2
Note: ex = 1 + x + 2!
+ ... where x ≤ 1, if x = 1, value of e lies between 2 ≤ e < 3.
LOG
1.4 Questions for Blooms Taxonomy Level: Analyze
NO
4. Why n3 2n + 6n2 3n 6= O(n3 2n ) ? Justify your answer.
5. Analyse the time complexity of f un()?
1 int fun(int n)
ECH
2 {
3 int count = 0;
4 for (int i = n; i > 0; i /= 2)
5 for (int j = 0; j < i; j++)
6 count += 1;
7 return count;
FT
8 }
p
c) n3 /( (n)) d) (220 ) × n
1 int fun(int n)
2 {
EM
3 int count = 0;
4 for (int i = 0; i < n; i++)
5 for (int j = i; j > 0; j--)
6 count = count + 1;
7 return count;
D
8 }
ACA
8. "The time complexity of the below function is O(n)", justify the statement.
13
4 while(j < n && arr[i] < arr[j])
5 j++;
}
Y
6
LOG
9. In a competition, four dierent functions are observed. All the functions use a single for loop
and within the for loop, same set of statements are executed. Which loop is ecient? and
why?
NO
4 d) for(i = n; i > -1; i /= 2)
ECH
1
2 if (x < 1) return 1;
3 else return (f(x-1) + g(x))
4 }
5
6 int g (int x){
7 if (x < 2) return 2;
FT
8 else return (f(x-1) + g(x/2));
9 }
Of the Linear, Exponential, Quadratic and Cubic, which one describes the growth of f (x) as
a function of x?
YO
D EM
ACA
14
Y
LOG
Chapter 2
Recurrence Relation:
NO
2.1 Questions for Blooms Taxonomy Level: Remember
a)
b)
c)
d)
solving recurrences
analysing loops
ECH
calculating the time complexity of any code
FT
Answer: a
a) 2 b) 3 c) 4 d) 5
Answer:
YO
3. What is the result of the recurrences which fall under rst case of Master's theorem (let the
recurrence be given by T (n) = aT (n/b) + f (n) and f (n) = nc ?
Answer: a
4. What is the result of the recurrences which fall under second case of Master's theorem (let
the recurrence be given by T (n) = aT (n/b) + f (n) and f (n) = nc ?
D
Answer: b
5. What is the result of the recurrences which fall under third case of Master's theorem (let the
recurrence be given by T (n) = aT (n/b) + f (n) and f (n) = nc ?
15
a) T (n) = O(nlogb a) b) T (n) = O(nc logn)
c) T (n) = O(f (n)) d) T (n) = O(n2 )
Y
Answer: c
LOG
6. We can solve any recurrence by using Master's theorem.
a) true b) false
Answer: b
7. Under what case of Master's theorem will the recurrence relation of merge sort fall?
NO
a) 1 b) 2 c) 3 d) It cannot be solved us-
ing master's theorem
Answer: b
ECH
8. Which case of master's theorem can be extended further?
a) 1 b) 2
Answer: b
9. What is the result of the recurrences which fall under the extended second case of Master's
FT
theorem (let the recurrence be given by T (n) = aT (n/b) + f (n) and f (n) = nc (logn)k ?
Answer:
YO
10. Under what case of Master's theorem will the recurrence relation of binary search fall?
a) 1 b) 2
Answer: b
11. What is the value of T (n) of the following recurrence T (n) = 4T (n/2) + n2
Answer:
ACA
12. What is the value of T (n) of the following recurrence T (n) = T (n/2) + 2n
16
Answer: c
13. What is the value of T (n) of the following recurrence T (n) = 16T (n/4) + n
Y
a) T (n) = O(n) b) T (n) = O(logn)
LOG
c) T (n) = O(n2 logn) d) T (n) = O(n2 )
Answer: d
14. What is the value of T (n) of the following recurrence T (n) = 4T (n/4) + nlogn
NO
c) T (n) = O(n2 logn) d) cannot be solved
Answer: a
1. What is recurrence?
ECH
2. What is the meaning of solution of a recurrence?
3. Write a recursive algorithm for nding factorial of a number and derive its space and time
complexities.
FT
4. Write a recursive algorithm for nding xn and derive its space and time complexities.
(a) T (n) = n
2T ( 2 ) + n if n > 1
(
1 if n = 1
(b) T (n) =
2T (n − 1) + 1 if n > 1
D
(
1 if n = 1
(c) T (n) = n
T ( 2 ) + 1 if n > 1
ACA
17
(
1 if n = 1
(b) T (n) = n
T ( 2 ) + 1 if n > 1
Y
(
1 if n = 1
(c) T (n) = n
LOG
2T ( 2 ) + 1 if n > 1
(
1 if n = 1
(d) T (n) =
2T (n − 1) + 1 if n > 1
(
1 if n = 1
(e) T (n) = n 2
2T ( 2 ) + n if n > 1
NO
(
1 if n = 1
(f ) T (n) =
T (n − 1) + n if n > 1
ECH
√
(a) T (n) = T ( n) + 1 and T (1) = 1.
√
(b) T (n) = 2T ( n) + logn and T (1) = 1.
(c) T (n) = 2T (n − 1) + 2n and T (1) = 1.
n
(d) T (2 ) = T (2n−1 )
+ 1 and T (1) = 1.
√ √
(e) T (n) = nT ( n) + n and T (1) = 1.
FT
4. Solve the following recurrences using Masters method.
(a) T (n) = 2T ( n
2
) + n2
YO
(b) T (n) = 2T ( n
2
) + n3 = Θ(n3 )
9
(c) T (n) = T ( 10 n) + n = Θ(n)
(e) T (n) = 7T ( n
3
) + n2 = Θ(n2 )
(f ) T (n) = 7T ( n
2
) + n2 = Θ(nlg7 )
D
√ √
(g) T (n) = 2T ( n
4
)+ n = Θ( nlgn)
ACA
(h) T (n) = 9T ( n
3
) + n = Θ(n2 )
(i) T (n) = T ( 2n
3
) + 1 = Θ(lgn)
18
2.4 Questions for Blooms Taxonomy Level: Analyze
Y
1. Explain the time complexity of the following code?
LOG
2 if (n==0) return 1;
3 if (n==1) return x;
4 if ((n % 2) == 0) return xpowy(x*x, n/2);
5 else return xpowy(x*x, n/2) * x;
6 }
NO
2. Explain the time complexity of the following code?
ECH
4 }
3. Can you solve the following recurrence using Master's theorem. Justify your answer. T (n) =
4T (n/2) + n!
4. Can you solve the following recurrence using Master's theorem. Justify your answer.
FT
T (n) = 0.7T (n/2) + 1/n
5. Given two recurrence relation T1 and T2 . Explain the asymptotic relationship between these
functions?(
1 if n = 1
T1 (n) = n
YO
2T1 ( 2 ) + n if n > 1
(
1 if n = 1
T2 (n) =
T2 (n − 1) + n if n > 1
D EM
ACA
19
ACA
DEM
YO
20
FT
ECH
NO
LOG
Y
Y
LOG
Chapter 3
NO
3.1 Questions for Blooms Taxonomy Level: Remember
a)
c)
backtracking
Answer: c
ECH b)
d)
greedy algorithm
dynamic programming
Answer: a
Answer: c
a) true b) false
Answer: a
Answer: a
ACA
Answer: a
21
7. What will be the best case time complexity of merge sort?
Y
Answer: a
LOG
8. Choose the incorrect statement about merge sort from the following?
Answer: b
NO
9. Which of the following is not in-place sorting algorithm by default?
Answer: a
Quick sort
Answer: a
b)
ECH
10. Which of the following is not a stable sorting algorithm?
11. Which of the following stable sorting algorithm takes the least time when applied to an almost
FT
sorted array?
Answer:b
YO
a)
b)
22
5 merge_sort(arr, mid+1, right);
6 merge(arr, left, mid, right);
}
Y
7
8 }
LOG
c)
NO
5 merge_sort(arr, left, mid);
6 merge_sort(arr, mid+1, right);
7 }
8 }
1
2
3
4
d)
ECH
void merge_sort(int arr[], int left, int right){
if (left < right){
int mid = (right-left)/2;
merge(arr, left, mid, right);
merge_sort(arr, left, mid);
FT
5
6 merge_sort(arr, mid+1, right);
7 }
8 }
YO
Answer: b
Answer: b b
EM
a) True b) False
D
Answer: a
15. What is the worst case time complexity of a quick sort algorithm?
ACA
Answer: c
16. Which of the following methods is the most eective for picking the pivot element?
23
a) rst element b) last element
Y
Answer: c
LOG
17. Find the pivot element from the given input using median-of-three partitioning method.
8, 1, 4, 9, 6, 3, 5, 2, 7, 0.
a) 8 b) 7 c) 9 d) 6
Answer: d
NO
18. Which is the safest method to choose a pivot element?
a)
Answer: a
b)
ECH
19. What is the average running time of a quick sort algorithm?
Answer: c
FT
20. Which of the following sorting algorithms is used along with quick sort to sort the sub arrays?
Answer: c
YO
21. Quick sort uses join operation rather than merge operation.
a) true b) false
EM
Answer: a
22. How many sub arrays does the quick sort algorithm divide the entire array into?
Answer: b
23. Which is the worst method of choosing a pivot element?
ACA
Answer: a
24
24. Which among the following is the best cut-o range to perform insertion sort within a quick
sort?
Y
a) N =0−5 b) N = 5 − 20 c) N = 20 − 30 d) N > 30
LOG
Answer: b
25. Quick sort is a
NO
Answer: b
26. What is the worst case time complexity of the Quick sort?
a)
Answer: d
6 4 3 7 11 9 14 12
ECH
27. Apply Quick sort on a given sequence 7 11 14 6 9 4 3 12. What is the sequence after rst
phase, pivot is rst element?
b) 6 3 4 7 9 14 11 12
FT
c) 7 6 14 11 9 4 3 12 d) 7 6 4 3 9 14 11 12
Answer: b
28. The best case behaviour occurs for quick sort is, if partition splits the array of size n into
YO
-
Answer: a
29. Quick sort is a stable sorting algorithm.
EM
a) True b) False
Answer: b
D
30. Consider the Quick sort algorithm in which the partitioning procedure splits elements into
two sub-arrays and each sub-array contains at least one-fourth of the elements. Let T(n) be
the number of comparisons required to sort array of n elements. Then T (n) <=?
ACA
Answer: b
25
31. Consider the Quick sort algorithm which sorts elements in ascending order using the rst
element as pivot. Then which of the following input sequence will require a maximum number
Y
of comparisons when this algorithm is applied on it?
a) 22 25 56 67 89 b) 52 25 76 67 89 c) 22 25 76 67 50 d) 52 25 89 67 76
LOG
Answer: a
32. A machine needs a minimum of 200 sec to sort 1000 elements by Quick sort. The minimum
time needed to sort 200 elements will be approximately -
NO
Answer: c
33. Which one of the following sorting algorithm is best suited to sort an array of 1 million
elements?
ECH
a) Bubble sort b) Insertion sort c) Merge sort d) Quick sort
Answer: d
34. Quick sort is a space-optimised version of -
Answer: d
35. QuickSort can be categorized into which of the following?
YO
Answer: b
36. Select the appropriate recursive call for QuickSort.(arr is the array, low is the starting index
EM
and high is the ending index of the array, partition returns the pivot element, we will see the
code for partition very soon)
a)
2 int pivot;
3 if(high>low){
ACA
26
b)
Y
2 int pivot;
3 if(high<low){
LOG
4 pivot = partition(arr, low, high);
5 quickSort(arr, low, pivot-1);
6 quickSort(arr, pivot+1, high);
7 }
8 }
NO
c)
ECH
4 pivot = partition(arr, low, high);
5 quickSort(arr, low, pivot);
6 quickSort(arr, pivot, high);
7 }
8 }
FT
d)
Answer: a
37. What is the worst case complexity of QuickSort?
Answer: d
38. What is a randomized QuickSort?
ACA
27
Answer: c
Y
39. Which of the following code performs the partition operation in QuickSort?
a)
LOG
1 int partition(int[] arr, int low, int high){
2 int left, right, pivot_item = arr[low];
3 left = low;
4 right = high;
5 while(left > right){
6 while(arr[left] <= pivot_item){
NO
7 left++;
8 }
9 while(arr[right] > pivot_item){
10 right--;
11 }
ECH
12 if(left < right){
13 swap(arr, left, right);
14 }
15 }
16 arr[low] = arr[right];
17 arr[right] = pivot_item;
FT
18 return right;
19 }
b)
1
2 int left, right, pivot_item = arr[low];
3 left = low;
4 right = high;
5 while(left <= right){
6 while(arr[left] <= pivot_item){
EM
7 left++;
8 }
9 while(arr[right] > pivot_item){
10 right--;
11 }
D
14 }
15 }
16 arr[low] = arr[right];
17 arr[right] = pivot_item;
18 return right;
28
19 }
Y
c)
LOG
2 int left, right, pivot_item = arr[low];
3 left = low;
4 right = high;
5 while(left <= right){
6 while(arr[left] > pivot_item){
7 left++;
NO
8 }
9 while(arr[right] <= pivot_item){
10 right--;
11 }
12 if(left < right){
13
14
15
16
17
18
}
}
ECH
swap(arr, left, right);
arr[low] = arr[right];
arr[right] = pivot_item;
return right;
FT
19 }
d)
2
3 left = low;
4 right = high;
5 while(left > right){
6 while(arr[left] > pivot_item){
7 left++;
EM
8 }
9 while(arr[right] <= pivot_item){
10 right--;
11 }
12 if(left < right){
D
15 }
16 arr[low] = arr[right];
17 arr[right] = pivot_item;
18 return right;
19 }
29
Y
Answer: b
40. What is the best case complexity of QuickSort?
LOG
a) O(nlogn) b) O(logn) c) O(n) d) O(n2 )
Answer:a
41. The given array is arr = {2, 3, 4, 1, 6}. What are the pivots that are returned as a result
of subsequent partitioning?
NO
a) 1 and 3 b) 3 and 1 c) 2 and 6 d) 6 and 2
Answer: a
42. What is the average case complexity of QuickSort?
a) O(nlogn)
Answer: a
43. The given array is
b)
ECH
O(logn)
Answer: d
44. Which of the following is not true about QuickSort?
YO
Answer: b
EM
2. Discuss the Max-Min algorithm using divide and conquer with an example.
D
3. Write the algorithm for Max-Min algorithm using divide and conquer.
30
8. Discuss the quick sort algorithm with an example.
Y
10. What is a randomized Quick Sort?
LOG
11. Is quick sort an in-place sorting algorithm? Explain?
NO
1. The given array is arr[] = {2, 3, 4, 1, 6}. What are the pivots that are returned as a
result of subsequent partitioning?
2. If the following sequence of numbers is to be sorted using quick sort, then show the iterations
of the sorting process. 42, 34, 75, 23, 21, 18, 90, 67, 78
ECH
3. Given the following sequence 95, 46, 32, 13, 15, 19, 99, 64, 32. Show the steps to nd the max
and min applying Max-Min algorithm using divide and conquer.
4. If the following sequence of numbers is to be sorted using merge sort, then show the iterations
of the sorting process. 42, 34, 75, 23, 21, 18, 90, 67, 78
5. Apply Quick sort on a given sequence 7 11 14 6 9 4 3 12. Find the sequence after rst phase,
FT
pivot is rst element?
1. Analyze the time complexity of Max-Min Algorithm using divide and conquer.
YO
2. Compare Max-Min Algorithm using divide and conquer with naive method with respect to
the number of comparison.
5. Give a brief argument that the running time of Partition on a sub-array of size n is Θ(n).
6. Analyze the best-case time complexity of Quick sort Algorithm.
D
9. A machine needs a minimum of 200 sec to sort 1000 elements by Quick sort. Find the
approximate minimum time needed to sort 200 elements.
31
ACA
DEM
YO
32
FT
ECH
NO
LOG
Y
Y
LOG
Chapter 4
Graph Traversal
NO
4.1 Questions for Blooms Taxonomy Level: Remember
1. Breadth First Search is equivalent to which of the traversal in the Binary Trees?
a)
c)
Pre-order Traversal
Level-order Traversal
Answer: c
ECH b)
d)
Post-order Traversal
In-order Traversal
2. Time Complexity of Breadth First Search is? (V - number of vertices, E - number of edges)
FT
a) O(V + E) b) O(V ) c) O(E) d) O(V × E)
Answer: a
3. The Data structure used in standard implementation of Breadth First Search is?
YO
Answer: b
4. The Breadth First Search traversal of a graph will result into?
EM
Answer: b
D
5. A person wants to visit some places. He starts from a vertex and then wants to visit every
place connected to this vertex and so on. What algorithm he should use?
ACA
Answer: b
33
6. Which of the following is not an application of Breadth First Search?
Y
b) Finding bipartiteness of a graph
LOG
d) Path Finding
Answer: d
7. When the Breadth First Search of a graph is unique?
a) When the graph is a Binary Tree b) When the graph is a Linked List
NO
c) When the graph is a n-ary Tree d) When the graph is a Ternary Tree
Answer: b
8. Regarding implementation of Breadth First Search using queues, what is the maximum dis-
tance between two nodes present in the queue? (considering each edge length 1)
a)
c)
Can be anything
At most 1
Answer: c
ECH
9. In BFS, how many times a node is visited?
b)
d)
0
Insucient Information
FT
a) Once b) Twice c) in-degree d) Thrice
Answer: c
10. Which of the following data structure is used to implement DFS?
YO
Answer: c
11. Which of the following traversal in a binary tree is similar to depth rst traversal?
Answer: c
12. What will be the result of depth rst traversal in the following tree?
D
1
ACA
2 3
4 5
34
a) 4 2 5 1 3 b) 1 2 4 5 3 c) 4 5 2 3 1 d) 1 2 3 4 5
Answer: b
Y
13. Which of the following is a possible result of depth rst traversal of the given graph(consider
LOG
1 to be source element)?
5 1 2
NO
4 3
ECH
a) 1 2 3 4 5 b) 1 2 3 1 4 5 c) 1 4 5 3 2 d) 1 4 5 1 2 3
Answer: a
14. Which of the following represent the correct pseudo code for non recursive DFS algorithm?
a)
FT
1 Algorithm DFS-non_recursive(G,v):
2 //let St be a stack
3 St.push(v)
4 while St is not empty
5 v = St.pop()
if v is not discovered:
YO
6
7 label v as discovered
8 for all adjacent vertices of v do
9 St.push(\task //a being the adjacent vertex
b)
EM
1 Algorithm DFS-non_recursive(G,v):
2 //let St be a stack
3 St.pop()
4 while St is not empty
D
5 v = St.push(v)
6 if v is not discovered:
ACA
7 label v as discovered
8 for all adjacent vertices of v do
9 St.push(\task //a being the adjacent vertex
c)
35
1 Algorithm DFS-non_recursive(G,v):
2 //let St be a stack
Y
3 St.push(v)
4 while St is not empty
LOG
5 v = St.pop()
6 if v is not discovered:
7 label v as discovered
8 for all adjacent vertices of v do
9 St.push(v)
NO
d)
1 Algorithm DFS-non_recursive(G,v):
2 //let St be a stack
3 St.pop(v)
ECH
4 while St is not empty
5 v = St.pop()
6 if v is not discovered:
7 label v as discovered
8 for all adjacent vertices of v do
9 St.push(\task //a being the adjacent vertex
FT
Answer: a
15. What will be the time complexity of the iterative depth rst traversal code(V=no. of vertices
E=no.of edges)?
YO
Answer: a
16. Which of the following functions correctly represent iterative DFS?
EM
a)
1 void DFS(int s)
2 {
3 vector<bool> discovered(V, true);
4 stack<int> st;
D
5 st.push(s);
6 while (!st.empty())
ACA
7 {
8 s = st.top();
9 st.pop();
10 if (!discovered[s])
11 {
36
12 cout << s << " ";
13 discovered[s] = true;
}
Y
14
15 for (auto i = adjacent[s].begin(); i != adjacent[s].end(); ++i)
16 if (!discovered[*i])
LOG
17 st.push(*i);
18 }
19 }
b)
NO
1
2 void DFS(int s)
3 {
4 vector<bool> discovered(V, false);
5 stack<int> st;
6
7
8
9
10
11
st.push(s);
while (!st.empty())
{
s = st.top();
st.pop();
ECH
if (!discovered[s])
FT
12 {
13 cout << s << " ";
14 discovered[s] = true;
15 }
16 for (auto i = adjacent[s].begin(); i != adjacent[s].end(); ++i)
YO
17 if (!discovered[*i])
18 st.push(*i);
19 }
20 }
EM
c)
1
2 void DFS(int s)
3 {
4 vector<bool> discovered(V, false);
D
5 stack<int> st;
6 st.push(s);
ACA
7 while (!st.empty())
8 {
9 st.pop();
10 s = st.top();
11 if (!discovered[s])
37
12 {
13 cout << s << " ";
discovered[s] = true;
Y
14
15 }
16 for (auto i = adjacent[s].begin(); i != adjacent[s].end(); ++i)
LOG
17 if (!discovered[*i])
18 st.push(*i);
19 }
20 }
d)
NO
1
2 void DFS(int s)
3 {
4 vector<bool> discovered(V, false);
5
6
7
8
9
10
stack<int> st;
st.push(s);
while (!st.empty())
{
s = st.top();
st.pop();
ECH
FT
11 if (!discovered[s])
12 {
13 cout << s << " ";
14 discovered[s] = false;
15 }
YO
Answer: b
17. What is the space complexity of standard DFS(V: no. of vertices E: no. of edges)?
Answer: b
ACA
Answer: d
38
19. Choose the incorrect statement about DFS and BFS from the following?
Y
b) DFS is equivalent to post order traversal in trees
LOG
c) DFS and BFS code has the same time complexity
Answer: b
20. Depth First Search is equivalent to which of the traversal in the Binary Trees?
NO
c) Level-order Traversal d) In-order Traversal
Answer: a
21. Time Complexity of DFS is? (V number of vertices, E number of edges)
a) O(V + E)
Answer: a
b) O(V )
22. The Data structure used in standard implementation of Breadth First Search is?
Answer: b
24. A person wants to visit some places. He starts from a vertex and then wants to visit every
vertex till it nishes from one vertex, backtracks and then explore other vertex from same
EM
Answer: a
D
39
Answer: d
26. When the Depth First Search of a graph is unique?
Y
a) When the graph is a Binary Tree b) When the graph is a Linked List
LOG
c) When the graph is a n-ary Tree d) When the graph is a ternary Tree
Answer: b
27. Regarding implementation of Depth First Search using stacks, what is the maximum distance
between two nodes present in the stack? (considering each edge length 1)
NO
a) Can be anything b) 0
Answer: a
ECH
28. In Depth First Search, how many times a node is visited?
a) Once
b) Twice
Answer: c
29. Topological sort can be applied to which of the following graphs?
YO
Answer: d
30. Most Ecient Time Complexity of Topological Sorting is? (V - number of vertices, E - number
EM
of edges)
Answer: a
D
31. In most of the cases, topological sort starts from a node which has -
Answer: d
32. Which of the following is not an application of topological sorting?
40
a) Finding prerequisite of a task
Y
c) Finding Cycle in a graph
d) Ordered Statistics
LOG
Answer: d
33. Topological sort of a Directed Acyclic graph is?
a) Always unique
NO
c) Sometimes unique and sometimes not unique
Answer: c
ECH
34. Topological sort can be implemented by?
Answer: a
36. A man wants to go dierent places in the world. He has listed them down all. But there are
some places where he wants to visit before some other places. What application of graph can
he use to determine that?
EM
Answer: c
D
Answer: a
41
4.2 Questions for Blooms Taxonomy Level: Understand
Y
1. What is a graph? Explain its key terms.
LOG
3. Explain the breadth rst traversal algorithms in detail with an example.
NO
6. Write the Breadth First Algorithm.
4.3
ECH
9. Explain the term Tree, Back, Edge and Cross Edges in DFS of Graph.
1. Consider the graph given below and nd out the degree of each node.
FT
YO
EM
2. Consider the graph given below. State all the simple paths from A to D, B to D, and C to D.
Also, nd out the in-degree and out-degree of each node. Is there any source or sink in the
graph?
D
ACA
42
3. Consider the graph given below. Find out its depth-rst and breadth-rst traversal scheme
(Start vertex is E).
Y
LOG
NO
4. Consider the graph given below. Find out its depth-rst and breadth-rst traversal scheme.
ECH
FT
YO
5. Consider the graph given below. Find out its depth-rst and breadth-rst traversal scheme.
D EM
ACA
6. Consider the graph given below. s be the start node. Mark tree edge(s), back edge(s), forward
edge(s) and cross edge(s) of the DFS tree.
43
Y
LOG
7. Find the topological sorted order of the following graph?
NO
ECH
4.4 Questions for Blooms Taxonomy Level: Analyze
FT
1. Which method of adjacency matrix and adjacency list do you prefer for graph representation
and why?
5. You are given an adjacency matrix. How to nd the in-degrees of the corresponding vertices.
Analyze the time complexity. How to improve the running time.
EM
6. Can a graph have multiple topological sort order? Explain with an example.
44
Y
LOG
Chapter 5
Greedy Algorithm
NO
5.1 Questions for Blooms Taxonomy Level: Remember
a)
c)
0/1 knapsack problem
Answer: b
ECH b)
d)
Continuous knapsack problem
2. Fractional knapsack problem is solved most eciently by which of the following algorithm?
FT
a) Divide and conquer b) Dynamic programming
Answer: c
YO
Answer: a
4. Which of the following statement about 0/1 knapsack and fractional knapsack problem is
D
correct?
a) In 0/1 knapsack problem items are divisible and in fractional knapsack items are indivisible
ACA
c) 0/1 knapsack is solved using a greedy algorithm and fractional knapsack is solved using dy-
namic programming
d) In 0/1 knapsack problem items are indivisible and in fractional knapsack items are divisible
45
Answer: d
Y
5. Time complexity of fractional knapsack problem is
LOG
Answer: a
6. Given items as value,weight pairs {{40, 20}, {30, 10}, {20, 5}}. The capacity of knap-
sack=20. Find the maximum value output assuming items to be divisible.
a) 60 b) 80 c) 100 d) 40
NO
Answer: a
7. The result of the fractional knapsack is greater than or equal to 0/1 knapsack.
a) True b) False
a)
Answer: a
Answer: c
9. Given items as value,weight pairs {{60, 20}, {50, 25}, {20, 5}}. The capacity of knap-
sack=40. Find the maximum value output assuming items to be divisible and non divisible
YO
respectively.
Answer: d
EM
10. Consider a job scheduling problem with 4 jobs J1, J2, J3, J4 and with corresponding deadlines:
(d1, d2, d3, d4) = (4, 2, 4, 2). Which of the following is not a feasible schedule without
violating any job schedule?
Answer: b
11. We are given 9 tasks Tl , T2 T9 . The execution of each task requires one unit of time.
... We
can execute one task at a time. Ti has a prot Pi and a deadline di prot Pi is earned if the
task is completed before the
th
end of the di unit of time.
46
Y
LOG
Are all tasks completed in the schedule that gives maximum prot?
NO
Answer: d
12. We are given 9 tasks Tl , T2 T9 . The execution of each task requires one unit of time.
... We
can execute one task at a time. Ti has a prot Pi and a deadline di prot Pi is earned if the
ECH
task is completed before the FT th
end of the di unit of time.
Answer: b
13. Consider a job scheduling problem with 5 jobs J1, J2, J3, J4, J5 and with corresponding
deadlines: (d1, d2, d3, d4, d5) = (2, 1, 2, 1, 3) and prot (p1, p2, p3, p4, p5)=(100, 19, 27,
25, 15). Are all tasks completed in the schedule that gives maximum prot?
EM
Answer: c
D
14. Consider a job scheduling problem with 5 jobs J1, J2, J3, J4, J5 and with corresponding
ACA
deadlines: (d1, d2, d3, d4, d5) = (2, 1, 2, 1, 3) and prot (p1, p2, p3, p4, p5)=(100, 19, 27,
25, 15). Are all tasks completed in the schedule that gives maximum prot?
47
Answer: b
Y
15. Time complexity of greedy job scheduling problem is
n3 n2
LOG
a) b) n c) d) nlog2 n
Answer: c
16. Kruskal's algorithm is used to
NO
b) nd single source shortest path
ECH
d) traverse the graph
Answer: a
17. Kruskal's algorithm is a
Answer: c
18. Consider the given graph.
YO
D EM
What is the weight of the minimum spanning tree using the Kruskal's algorithm?
ACA
a) 24 b) 23 c) 15 d) 19
Answer: d
19. Consider the given graph.
48
Y
LOG
So, the weight of the MST is 19.
NO
20. What is the time complexity of Kruskal's algorithm?
Answer: b
ECH
21. Consider the following graph. Using Kruskal's algorithm, which edge will be selected rst?
FT
YO
a) GF b) DE c) BE d) BG
Answer: c
22. Which of the following edges form minimum spanning tree on the graph using kruskals algo-
rithm?
D EM
ACA
a) (B-E)(G-E)(E-F)(D-F) b) (B-E)(G-E)(E-F)(B-G)(D-F)
c) (B-E)(G-E)(E-F)(D-E) d) (B-E)(G-E)(E-F)(D-F)(D-G)
49
Answer: a
23. Which of the following is true?
Y
a) Prim's algorithm can also be used for disconnected graphs
LOG
b) Kruskal's algorithm can also run on the disconnected graphs
d) In Kruskal's sort edges are added to MST in decreasing order of their weights
Answer: b
NO
24. Which of the following is false about the Kruskal's algorithm?
a) It is a greedy algorithm
c)
d)
It can accept cycles in the MST
ECH
It uses union-nd data structure
Answer: c
25. Kruskal's algorithm is best suited for the dense graphs than the prim's algorithm.
FT
a) True b) False
Answer: b
26. Consider the following statements.
YO
Answer: d
27. Which of the following is true?
D
Answer: a
50
28. Consider the given graph.
Y
LOG
NO
What is the weight of the minimum spanning tree using the Prim's algorithm,starting from
vertex a?
a) 23 b) 28 c) 27 d) 11
a)
Answer: c
O(logV ) b)
ECH
29. Worst case is the worst case time complexity of Prim's algorithm if adjacency matrix is used?
Answer: b
EM
a) True b) False
Answer: a
D
32. Kruskal's algorithm is best suited for the sparse graphs than the prim's algorithm.
ACA
a) True b) False
Answer: a
33. Consider the graph shown below.
51
Y
LOG
NO
Which of the following edges form the MST of the given graph using Prim'a algorithm, starting
from vertex 4.
a) (4-3)(5-3)(2-3)(1-2) b) (4-3)(3-5)(5-1)(1-2)
ECH
c) (4-3)(3-5)(5-2)(1-5) d) (4-3)(3-2)(2-1)(1-5)
Answer: d
34. Prim's algorithm is also known as
Answer: d
35. Prim's algorithm can be eciently implemented using-for graphs with greater density.
YO
Answer: a
36. Which of the following is false about Prim's algorithm?
EM
a) It is a greedy algorithm
Answer: b
ACA
52
Answer: b
Y
38. Which of the following is the most commonly used data structure for implementing Dijkstra's
Algorithm?
LOG
a) Max priority queue b) Stack
Answer: d
39. What is the time complexity of Dijikstra's algorithm?
NO
a) O(N ) b) O(N 3 ) c) O(N 2 ) d) O(logN )
Answer: c
40. Dijkstra's Algorithm cannot be applied on
a)
c)
Directed and weighted graphs
Unweighted graphs
Answer: b
ECH b)
d)
Graphs having negative weight function
41. What is the pseudo code to compute the shortest path in Dijkstra's algorithm?
FT
a)
1 if(!T[w].Known)
2 if(T[v].Dist + C(v,w) < T[w].Dist){
3 Decrease(T[w].Dist to T[v].Dist +C(v,w));
T[w].path=v;
YO
4
5 }
b)
1 if(T[w].Known)
EM
c)
ACA
1 if(!T[w].Known)
2 if(T[v].Dist + C(v,w) > T[w].Dist) {
3 Decrease(T[w].Dist to T[v].Dist +C(v,w);
4 T[w].path=v;
5 }
53
d)
1 if(T[w].Known)
Y
2 if(T[v].Dist + C(v,w) < T[w].Dist) {
3 Increase(T[w].Dist to T[v].Dist);
LOG
4 T[w].path=v;
5 }
Answer: a
42. How many priority queue operations are involved in Dijkstra's Algorithm?
NO
a) 1 b) 3 c) 2 d) 4
Answer: b
43. How many times the insert and extract min operations are invoked per vertex?
a) 1
Answer: a
b) 2
ECH c) 3 d) 0
44. The maximum number of times the decrease key operation performed in Dijkstra's algorithm
will be equal to
FT
a) Total number of vertices b) Total number of edges
Answer: b
YO
45. What is running time of Dijkstra's algorithm using Binary min- heap method?
Answer: d
EM
46. The running time of Bellmann Ford algorithm is lower than that of Dijkstra's Algorithm.
a) True b) False
Answer: b
D
47. Dijkstra's Algorithm run on a weighted, directed graph G=V,E with non-negative weight
function w and source s, terminates with d[u]=delta(s,u) for all vertices u in V.
ACA
a) True b) False
Answer: a
48. Given pseudo code of Dijkstra's Algorithm.
54
1 //Initialise single source(G,s)
2 S=0
Y
3 Q=V[G]
4 While Q != 0
LOG
5 Do u=extract-min(Q)
6 S=S union {u}
7 For each vertex v in adj[u]
8 Do relax(u,v,w)
NO
a) While loop gets executed for v times
Answer: b
49. Consider the following graph.
ECH
FT
YO
a) 8 b) 9 c) 4 d) 6
EM
Answer: d
50. In the given graph, identify the shortest path having minimum cost to reach vertex E if A is
the source vertex.
D
ACA
55
a) a-b-e b) a-c-e c) a-c-d-e d) a-c-d-b-e
Answer: b
Y
51. Dijkstra's Algorithm is the prime example for
LOG
a) Greedy algorithm b) Branch and bound
Answer: a
NO
5.2 Questions for Blooms Taxonomy Level: Understand
ECH
3. When is a spanning tree called a minimum spanning tree?
4. Take a weighted graph of your choice and nd out its minimum spanning tree.
6 Find the optimal solution for the fractional knapsack problem given below:
I = I1, I2, I3, I4, I5
ACA
1. Write the algorithm for job sequencing with deadline problem. Find the time complexity.
56
2. Consider the below table for jobs given with prot and dead line. Find the maximum prot
earned. (Explain each step of your solution)
Y
Job J1 J2 J3 J4 J5 J6 J7 J8 J9
LOG
Prot 15 20 30 18 18 10 23 16 25
Deadline 7 2 5 3 4 5 2 7 3
3. Consider 10 jobs for A to J. The execution of each job takes 1 unit of time. One job is executed
at a time. Each job is related with prot and a deadline. If job is completed before deadline
then the corresponding prot is earned. Find the maximum prot. (Explain each step of your
NO
solution)
Job A B C D E F G H I J
Prot 25 35 40 30 18 25 33 43 19 25
Deadline 4 7 4 7 1 4 6 5
ECH
3 6
Find the maximum prot earned. (Explain each step of your solution)
4. Write the Prim's Algorithm for minimum spanning tree. Explain the time complexity.
FT
5. Show steps of Prim's algorithm to nd a minimum spanning tree (consider vertex 0 as root)
of the graph shown in the gure:
YO
D EM
ACA
6. Write Kruskal's algorithm for minimum spanning tree. Explain the time complexity.
7. Show each steps of Kruskal's algorithm to nd a minimum spanning tree of the graph shown
in the gure:
57
Y
LOG
8. Write Dijkstra's Algorithm for single source shortest path problem. Explain the time com-
NO
plexity of the algorithm.
9. Dijkstra's Algorithm is used to solve the single source shortest path problem. Can this algo-
rithm support negative edge weight? Justify your answer with some example.
10. Show each steps of Dijkstra's algorithm to nd a single source shortest path of the graph
ECH
shown in the following gure (Consider
FT S is the source vertex):
YO
58
Y
LOG
Chapter 6
Dynamic Algorithm
NO
6.1 Questions for Blooms Taxonomy Level: Remember
1. Which of the following methods can be used to solve the matrix chain multiplication problem?
a)
b)
c)
d)
Dynamic programming
Brute force
Recursion
ECH
Dynamic Programming, Brute force, Recursion
FT
Answer: d
2. Which of the following is the recurrence relation for the matrix chain multiplication problem
where mat[i − 1] ∗ mat[i] gives the dimension of the ith matrix?
YO
a)
dp[i, j] = 1 if i = j
dp[i, j] = min{dp[i, k] + dp[k + 1, j]}
EM
b)
dp[i, j] = 0 if i = j
dp[i, j] = min{dp[i, k] + dp[k + 1, j]}
D
ACA
c)
dp[i, j] = 1 if i = j
dp[i, j] = min{dp[i, k] + dp[k + 1, j]} + mat[i − 1] ∗ mat[k] ∗ mat[j].
59
d)
Y
dp[i, j] = 0 if i = j
dp[i, j] = min{dp[i, k] + dp[k + 1, j]} + mat[i − 1] ∗ mat[k] ∗ mat[j].
LOG
Answer: d
3. Consider the two matrices P and Q which are 10 × 20 and 20 × 30 matrices respectively.
What is the number of multiplications required to multiply the two matrices?
NO
a) 10*20 b) 20*30 c) 10*30 d) 10*20*30
Answer: d
a)
Answer: a
b) 12000
ECH
4. Consider the matrices P, Q and R which are 10×20, 20×30 and 30×40 matrices respectively.
What is the minimum number of multiplications required to multiply the three matrices?
Answer: c
6. Consider the brute force implementation in which we nd all the possible ways of multiplying
EM
the given set of n matrices. What is the time complexity of this implementation?
Answer: d
D
7. Consider the following dynamic programming implementation of the matrix chain problem:
ACA
1 #include<stdio.h>
2 #include<limits.h>
3 int mat_chain_multiplication(int *mat, int n){
4 int arr[n][n];
5 int i,k,row,col,len;
60
6 for(i=1;i<n;i++)
7 arr[i][i] = 0;
for(len = 2; len < n; len++){
Y
8
9 for(row = 1; row <= n - len + 1; row++){
10 col = row + len - 1;
LOG
11 arr[row][col] = INT_MAX;
12 for(k = row; k <= col - 1; k++){
13 int tmp = ________________________;
14 if(tmp < arr[row][col])
15 arr[row][col] = tmp;
16 }
NO
17 }
18 }
19 return arr[1][n - 1];
20 }
21 int main(){
22
23
24
25
26 }
printf("%d",ans);
return 0;
ECH
int mat[6] = {20,5,30,10,40};
int ans = mat_chain_multiplication(mat,5);
FT
Which of the following lines should be inserted to complete the above code?
c)
Answer: c
EM
8. What is the time complexity of the following dynamic programming implementation of the
matrix chain problem?
1 #include<stdio.h>
2 #include<limits.h>
3 int mat_chain_multiplication(int *mat, int n){
D
4 int arr[n][n];
5 int i,k,row,col,len;
ACA
6 for(i=1;i<n;i++)
7 arr[i][i] = 0;
8 for(len = 2; len < n; len++){
9 for(row = 1; row <= n - len + 1; row++){
10 col = row + len - 1;
61
11 arr[row][col] = INT_MAX;
12 for(k = row; k <= col - 1; k++){
int tmp = arr[row][k] + arr[k + 1][col] + mat[row -
Y
13
1] * mat[k] * mat[col];
14 if(tmp < arr[row][col])
LOG
15 arr[row][col] = tmp;
16 }
17 }
18 }
19 return arr[1][n - 1];
20 }
NO
21 int main(){
22 int mat[6] = {20,5,30,10,40};
23 int ans = mat_chain_multiplication(mat,5);
24 printf("%d",ans);
25 return 0;
26
a)
}
O(1)
Answer: d
b) O(n) ECH c) O(n2 ) d) O(n3 )
FT
9. The Bellmann Ford algorithm returns _______ value.
Answer: a
YO
Answer: d
D
11. Bellmann Ford algorithm is used to indicate whether the graph has negative weight cycles or
not.
ACA
a) True b) False
Answer: a
12. How many solution/solutions are available for a graph having negative weight cycle?
62
a) One solution b) Two solutions
Y
c) No solution d) Innite solutions
Answer: c
LOG
13. What is the running time of Bellmann Ford Algorithm?
Answer: d
NO
14. How many times the for loop in the Bellmann Ford Algorithm gets executed?
a) V times b) V −1 c) E d) E−1
Answer: b
ECH
15. Dijikstra's Algorithm is more ecient than Bellmann Ford Algorithm.
a) True b) False
FT
Answer: a
b)
D
c)
63
1 for i=1 to V[g]-1 do{
2 for each edge (u,v) in E[g] do
Y
3 Relax(u,v,w)
4 }
LOG
5 for each edge (u,v) in E[g] do
6 if d[v]<d[u]+w(u,v) then return true
7 return True
d)
NO
1
2 for each edge (u,v) in E[g] do
3 Relax(u,v,w)
4 return True
a)
Answer: a
Interpolation
ECH
17. What is the basic principle behind Bellmann Ford Algorithm?
b) Extrapolation
FT
c) Regression d) Relaxation
Answer: d
YO
Answer: c
Answer: b
20. Consider the following graph. What is the minimum cost to travel from node A to node C?
64
Y
LOG
a) 5 b) 2 c) 1 d) 3
NO
Answer: b
21. In the given graph, identify the path that has minimum cost to travel from node a to node f.
ECH
FT
a) a-b-c-f b) a-d-e-f c) a-d-b-c-f d) a-d-b-c-e-f
YO
Answer: d
Answer: a
D
65
Answer: c
Y
24. Floyd Warshall's Algorithm is used for solving ____________
LOG
a) All pair shortest path problems b) Single Source shortest path problems
Answer: a
NO
a) Undirected and unweighted graphs b) Undirected graphs
Answer: c
a) O(V )
Answer: d
b) Θ(V 2 ) ECH
26. What is the running time of the Floyd Warshall Algorithm?
c) O(V E) d) Θ(V 3 )
FT
27. What approach is being followed in Floyd Warshall Algorithm?
Answer: b
Answer: d
D
Answer: b
66
1 n=rows[W]
2 D(0)=W
Y
3 for k=1 to n do
4 for i=1 to n do
LOG
5 for j=1 to n do
6 ____________________
7 return D(n)
NO
b) dij (k) = max(dij (k − 1), dik (k − 1) − dkj (k − 1))
ECH
d) dij (k) = max(dij (k − 1), dik (k − 1) + dkj (k − 1))
Answer: c
31. What happens when the value of k is 0 in the Floyd Warshall Algorithm?
FT
a) 1 intermediate vertex b) 0 intermediate vertex
Answer: b
32. Using logical operator's instead arithmetic operators saves time and space.
a) True b) False
EM
Answer: a
D
33. The time taken to compute the transitive closure of a graph is Theta(n2).
a) True b) False
ACA
Answer: b
34. In the given graph, what is the minimum cost to travel from vertex 1 to vertex 3?
67
Y
LOG
NO
a) 3 b) 2 c) 10 d) -3
Answer: d
ECH
35. In the given graph, how many intermediate vertices are required to travel from node a to node
e at a minimum cost?
FT
YO
a) 2 b) 0 c) 1 d) 3
EM
Answer: c
Answer: b
68
6.2 Questions for Blooms Taxonomy Level: Understand
Y
1. What is dynamic programming?
LOG
3. What the dierence between top-down and bottom-up approach?
4. Write matrix chain multiplication algorithm. What is the time complexity of the algorithm.
NO
6. Write Floyd-Warshall algorithm for all pair of shortest path problem.
ECH
1. Find an optimal parenthesization of a matrix chain product, where the dimension of the
sequence of matrices are < 2, 3, 5, 2, 4 >.
2. Show each steps of Bellman-Ford's algorithm to nd a single source shortest path of the graph
shown in the following gure (Consider A is the source vertex):
FT
YO
EM
3. Show each steps of Floyd's algorithm to nd all pair of source shortest path of the graph
shown in the following gure. Find the path from the vertex 1 to 4.
D
ACA
69
6.4 Questions for Blooms Taxonomy Level: Analyze
Y
1. Analyze the time complexity of matrix chain multiplication algorithm.
LOG
3. Analyze the time complexity of Floyd-Warshall algorithm.
NO
ECH
FT
YO
D EM
ACA
70
Y
LOG
Chapter 7
Back Tracking
NO
7.1 Questions for Blooms Taxonomy Level: Remember
a)
c)
n-queen problem
Answer: d
ECH b)
d)
subset sum problem
Answer: a
YO
Answer: b
Answer:
ACA
71
Answer: a
Y
a) true b) false
LOG
Answer: b
NO
Answer: c
ECH
a) Finding the shortest path b) Ecient quantity to shop
c) Ludo d) Crossword
Answer: d
Answer: a
10. Which of the following logical programming languages is not based on backtracking?
YO
Answer: d
11. The problem of nding a list of integers in a given specic range that meets certain conditions
is called?
EM
Answer: b
D
Answer: a
13. ___________ enumerates a list of promising nodes that could be computed to give the
possible solutions of a given problem.
72
a) Exhaustive search b) Brute force
Y
Answer: c
LOG
14. The problem of nding a subset of positive integers whose sum is equal to a given positive
integer is called as?
Answer: b
NO
15. The problem of placing n queens in a chessboard such that no two queens attack each other
is called as?
c)
a)
four queens puzzle
Answer:
1
a
b) 2
ECH d)
c)
1-queen problem
3 d) 4
FT
Answer: c
17. Placing n-queens so that no two queens attack each other is called?
Answer: a
18. Where is the n-queens problem implemented?
EM
Answer: b
19. Not more than 2 queens can occur in an n-queens problem.
D
a) true b) false
Answer: b
ACA
20. In n-queen problem, how many values of n does not provide an optimal solution?
a) 1 b) 2 c) 3 d) 4
Answer: b
73
21. Which of the following methods can be used to solve n-queen's problem?
Y
c) iterative improvement d) backtracking
LOG
Answer: d
22. Of the following given options, which one of the following is a correct option that provides an
optimal solution for 4-queens problem?
NO
Answer: a
23. How many possible solutions exist for an 8-queen problem?
a) 100 b) 98 c) 92 d) 88
Answer: c
b) 742
ECH
24. How many possible solutions occur for a 10-queen problem?
Answer: d
FT
25. If n=1, an imaginary solution for the problem exists.
a) true b) false
Answer: b
YO
a) 8 b) 7 c) 6 d) 5
Answer: d
EM
27. Of the following given options, which one of the following does not provides an optimal solution
for 8-queens problem?
a) (5,3,8,4,7,1,6,2) b) (4,1,5,8,6,3,7,2)
D
c) (1,6,3,8,5,2,7,4) d) (6,2,7,1,4,8,5,3)
Answer: c
ACA
74
Answer: b
Y
29. What is vertex coloring of a graph?
LOG
a) A condition where any two vertices having a common edge should not have same color
b) A condition where any two vertices having a common edge should always have same color
Answer: a
NO
30. How many edges will a tree consisting of N nodes have?
a) Log(N) b) N c) N 1 d) N + 1
a)
Answer: c
vertex matching
ECH
31. Minimum number of unique colors required for vertex coloring of a graph is called?
b) chromatic index
FT
c) chromatic number d) color number
Answer: c
YO
32. How many unique colors will be required for proper vertex coloring of an empty graph having
n vertices?
a) 0 b) 1 c) 2 d) n
Answer: b
EM
33. How many unique colors will be required for proper vertex coloring of a bipartite graph having
n vertices?
a) 0 b) 1 c) 2 d) n
D
Answer: c
ACA
75
Answer: c
Y
35. How many unique colors will be required for proper vertex coloring of a line graph having n
vertices?
LOG
a) 0 b) 1 c) 2 d) n
Answer: d
36. How many unique colors will be required for proper vertex coloring of a complete graph having
n vertices?
NO
a) 0 b) 1 c) n d) n!
Answer: c
a)
c)
Chromatic color
Edge matching
Answer: b
ECH
37. Minimum number of colors required for proper edge coloring of a graph is called?
b)
d)
Chromatic index
Color number
FT
38. What will be the chromatic number of the following graph?
YO
EM
a) 1 b) 2 c) 3 d) 4
Answer: b
39. How many unique colors will be required for vertex coloring of the following graph?
D
ACA
76
a) 2 b) 3 c) 4 d) 5
Y
Answer: c
LOG
40. How many unique colors will be required for vertex coloring of the following graph?
NO
a) 2 b) 3 c) 4 d) 5
ECH
Answer: b
41. Vertex coloring and chromatic number are one and the same.
a) True b) False
FT
Answer: b
42. What is the time complexity of backtracking m-Coloring algorithm for a graph having n
vertices?
YO
Answer: b
EM
2. How many unique colors will be required for proper vertex coloring of a bipartite graph having
D
n vertices?
77
7. Write Floyd-Warshall algorithm for all pair of shortest path problem.
8. How many unique colors will be required for proper vertex coloring of an empty graph having
Y
n vertices?
LOG
7.3 Questions for Blooms Taxonomy Level: Apply
1. Draw the state space of all possible 3 coloring of the following graph.
NO
ECH
2. How many unique colors will be required for proper vertex coloring of a complete graph having
n vertices? Draw the diagram if n = 4.
78
Y
LOG
Chapter 8
Network Flow
NO
8.1 MCQs
ECH
A) To maximize the ow through a network
A) Dijkstra's algorithm
YO
B) Bellman-Ford algorithm
C) Ford-Fulkerson algorithm
D) Prim's algorithm
Answer:
EM
C) Ford-Fulkerson algorithm
79
A) The maximum ow that can pass through the edge
Y
C) The distance between two nodes in the network
LOG
D) The weight of the edge in the network
Answer: A) The maximum ow that can pass through the edge
5. Which data structure is commonly used to implement a graph for network ow algorithms?
A) Array
NO
B) Linked list
C) Queue
ECH
D) Adjacency list
A) Edmonds-Karp algorithm
FT
B) Dinic's algorithm
C) Bellman-Ford algorithm
D) Push-relabel algorithm
Answer:
YO
C) Bellman-Ford algorithm
7. In the context of network ow algorithms, what does the term "augmenting path" refer to?
Answer:
D
8. Which technique is used to eciently nd augmenting paths in the Ford-Fulkerson algorithm?
ACA
C) Dijkstra's algorithm
80
D) Bellman-Ford algorithm
Y
9. What is the time complexity of the Ford-Fulkerson algorithm with Edmonds-Karp implemen-
LOG
tation?
A) O(V)
B) O(V^2)
C) O(V^3)
D) O(E)
NO
Answer: C) O(V^3)
10. Which condition indicates that the maximum ow has been achieved in a network using the
Ford-Fulkerson algorithm?
ECH
A) There are no more augmenting paths
Answer:
FT
A) There are no more augmenting paths
8.2 Remember
YO
3. What does the capacity of an edge represent in the context of network ow?
8.3 Understand
D
3. Compare and contrast the Ford-Fulkerson algorithm with the Edmonds-Karp algorithm.
4. Discuss the role of the bottleneck capacity in augmenting path methods for nding maximum
ow.
5. Explain how network ow algorithms can be applied to solve transportation problems.
81
8.4 Apply
Y
1. Given a network with specic edge capacities, apply the Ford-Fulkerson algorithm to nd the
maximum ow.
LOG
2. Apply the concept of residual capacities to update the ow in a network using the Edmonds-
Karp algorithm.
3. Apply the concept of minimum cut to nd the maximum ow in a network.
4. Design a network ow model to optimize the assignment of tasks to workers in a project
management scenario.
NO
5. Apply Ford-Fulkerson Algorithm to nd the maximum ow of the of the following network.
ECH
FT
6. Apply Ford-Fulkerson Algorithm to nd the maximum ow of the of the following network.
YO
D EM
8.5 Analyze
ACA
1. Analyze the time complexity of the Ford-Fulkerson algorithm and identify its limitations.
2. Compare and contrast the complexity of network ow algorithms with dierent augmenting
path strategies.
3. Evaluate the impact of modifying edge capacities on the maximum ow of a network.
82
4. Analyze the eciency of network ow algorithms in solving large-scale transportation or as-
signment problems.
Y
5. Critically assess the applicability of network ow algorithms in real-world scenarios such as
resource allocation in computer networks or optimizing supply chain logistics.
LOG
NO
ECH
FT
YO
D EM
ACA
83
ACA
DEM
YO
84
FT
ECH
NO
LOG
Y
Y
LOG
Chapter 9
NPC
NO
9.1 MCQ
ECH
A) Problems that can be solved in polynomial time
A) Non-Polynomial
B) Non-Practical
C) Nondeterministic Polynomial
EM
D) No Problem
85
Answer: B) They are the hardest problems in NP
Explanation: NP-complete problems are the hardest problems in NP, meaning that if any NP-
Y
complete problem can be solved in polynomial time, then all problems in NP can be solved in
polynomial time.
4. The Traveling Salesman Problem (TSP) is an example of:
LOG
A) A problem in P
B) A problem in NP
C) A problem in NPC
D) A problem in NP-hard
NO
Answer: C) A problem in NPC
Explanation: The Traveling Salesman Problem (TSP) is an NP-complete problem, meaning that
it is both in NP and any problem in NP can be reduced to it in polynomial time.
5. The Subset Sum Problem is NP-complete. What does this mean?
ECH
A) It can be solved in polynomial time
B) Knapsack Problem
A) P
ACA
B) NP
C) NPC
D) NP-hard
86
Answer: A) P
Explanation: The problem of nding the shortest path in a weighted graph, such as Dijkstra's
Y
algorithm, is in P as it can be solved in polynomial time.
8. The problem of nding the longest simple path in an undirected graph is:
LOG
A) In P
B) In NP
C) In NPC
D) In NP-hard
Answer: D) In NP-hard
NO
Explanation: Finding the longest simple path in an undirected graph is NP-hard, meaning that
it is at least as hard as the hardest problems in NP.
9. The problem of checking if a given graph is bipartite is:
ECH
A) In P
B) In NP
C) In NPC
D) In NP-hard
Answer: A) In P
FT
Explanation: Checking if a given graph is bipartite can be done in polynomial time using algo-
rithms like breadth-rst search (BFS), placing it in the complexity class P.
10. Which of the following problems is not known to be in P or NP-complete?
B) Graph isomorphism
9.2 Remember:
D
Answer: NP-completeness refers to a class of decision problems that are both in NP (nonde-
terministic polynomial time) and at least as hard as the hardest problems in NP. Signicantly,
if any NP-complete problem can be solved in polynomial time, then all problems in NP can
be solved in polynomial time, which would imply P = NP. Thus, NP-completeness serves as
a cornerstone in understanding the inherent diculty of certain computational problems.
87
2. List three well-known NP-Complete problems.
Answer:
Y
(a) The Traveling Salesman Problem (TSP)
LOG
(c) The Boolean Satisability Problem (SAT)
NO
other computational problems.
Answer: The key characteristic of NP-Complete problems is that they are among the hard-
est problems in NP, meaning any problem in NP can be reduced to them in polynomial
time. Additionally, verifying a potential solution to an NP-Complete problem can be done in
polynomial time.
5.
NP-complete. ECH
State the Cook-Levin theorem and its importance in the context of NP-Completeness.
Answer: The Cook-Levin theorem states that the Boolean Satisability Problem (SAT) is
This theorem is crucial because it was the rst to prove the existence of an
NP-complete problem, providing a foundation for understanding the complexity of other prob-
lems.
FT
6. What is the time complexity of verifying a solution to an NP-Complete problem?
Answer: The time complexity of verifying a solution to an NP-Complete problem is polyno-
mial time. This means that given a solution, it can be veried in polynomial time, but nding
the solution itself might not be achievable in polynomial time.
7. Name the problem that is often considered as the "poster child" for NP-Completeness.
YO
Answer: The Boolean Satisability Problem (SAT) is often considered the "poster child" for
NP-Completeness due to its importance in proving the NP-completeness of many other prob-
lems.
8. Recall the denition of a polynomial-time reduction and its role in proving NP-
Completeness.
EM
Complete. It cannot be solved by a Turing machine in general, let alone in polynomial time.
88
9.3 Understand:
Y
1.
NP-Completeness.
Answer: Non-deterministic polynomial time (NP) refers to the class of decision problems for
LOG
which a proposed solution can be veried in polynomial time by a non-deterministic Turing
machine. NP-Completeness arises from the fact that certain problems in NP are so di-
cult that if a polynomial-time algorithm exists for any one of them, then polynomial-time
algorithms exist for all problems in NP, implying P = NP.
NO
Answer: To prove that a new problem is NP-Complete, one must demonstrate that it is
in NP and that an existing NP-Complete problem can be reduced to it in polynomial time.
This reduction process typically involves transforming instances of the known NP-Complete
problem into equivalent instances of the new problem while preserving the solution.
ECH
3.
of each.
Answer: NP-Complete problems are a subset of NP-Hard problems. NP-Complete problems
are those for which a proposed solution can be veried in polynomial time, while NP-Hard
problems are at least as hard as the hardest problems in NP, but they may not be in NP
themselves. Examples of NP-Complete problems include the Traveling Salesman Problem
(TSP), while examples of NP-Hard problems include the Halting Problem.
FT
4. Explain the signicance of the polynomial-time verication property in NP-Complete
problems.
Answer: The polynomial-time verication property in NP-Complete problems means that
given a solution, it can be veried in polynomial time. This property allows for a potential so-
YO
lution to be eciently checked, even though nding the solution itself might not be achievable
in polynomial time.
would imply that all problems in NP could be solved in polynomial time. This is because NP-
Complete problems are the hardest problems in NP, and nding a polynomial-time algorithm
for any of them would mean that all problems in NP could be reduced to polynomial time,
implying P = NP.
herently dicult to solve eciently. It guides researchers in focusing their eorts on nding
approximate or heuristic solutions for these problems and understanding the limits of compu-
tational feasibility.
Y
encryption schemes and digital signatures. The hardness of NP-Complete problems forms the
basis for many cryptographic techniques and protocols.
LOG
8. Discuss the implications of the P vs. NP problem on computational complexity
theory and algorithm design.
Answer: The P vs. NP problem is one of the most signicant open questions in computer
science. Resolving it would have profound implications for computational complexity theory
and algorithm design. If P = NP, it would imply that many computationally dicult problems
have ecient solutions, revolutionizing elds such as optimization, cryptography, and articial
intelligence.
NO
9. Explain why proving a problem to be NP-Complete requires demonstrating both
membership in NP and NP-Hardness.
Answer: Proving a problem to be NP-Complete requires demonstrating that it is in NP by
showing that a proposed solution can be veried in polynomial time. Additionally, it must
ECH
be demonstrated that the problem is NP-Hard by showing that every problem in NP can be
reduced to it in polynomial time.
10. Describe how NP-Complete problems are used in complexity theory to classify
the diculty of computational problems.
Answer: NP-Complete problems serve as benchmarks for measuring the inherent diculty of
computational problems. They provide a standard against which other problems can be com-
FT
pared in terms of complexity. Problems that are polynomial-time reducible to NP-Complete
problems are also considered NP-Complete, thereby helping classify the diculty of a wide
range of problems.
9.4 Apply:
YO
1. Given a decision problem, explain the steps you would take to demonstrate its
NP-Completeness.
Answer: To demonstrate the NP-Completeness of a decision problem, one typically follows
these steps:
EM
(b) Choose a known NP-Complete problem and reduce it to the given problem in polynomial
time.
(c) Prove that the reduction is correct, meaning that a "yes" instance of the known NP-
D
Complete problem maps to a "yes" instance of the given problem, and a "no" instance
of the known problem maps to a "no" instance of the given problem.
2.
NP-Complete.
Answer: To prove that a new problem is NP-Complete using polynomial-time reduction:
(a) Select a known NP-Complete problem, preferably one that is closely related to the new
problem.
90
(b) Construct a polynomial-time reduction from the known NP-Complete problem to the
new problem.
Y
(c) Show that the reduction is correct by demonstrating that a "yes" instance of the known
problem can be transformed into a "yes" instance of the new problem, and a "no" instance
LOG
of the known problem can be transformed into a "no" instance of the new problem.
3. Design an algorithm to solve a known NP-Complete problem and analyze its time
complexity.
Answer: Designing an algorithm for an NP-Complete problem often involves developing
approximation algorithms or heuristic approaches due to the inherent diculty of nding exact
solutions. Analyzing the time complexity of such algorithms typically involves determining
NO
their worst-case or average-case performance, often resulting in exponential time complexity.
ECH
(a) Choose an instance of SAT.
(c) Prove that the reduction is correct by showing that the truth assignment satisfying the
SAT instance also satises the new problem, and vice versa.
tiveness of the heuristic approach can be evaluated by comparing its solutions with known
optimal solutions or by assessing its performance on benchmark instances.
ACA
9.5 Analyze:
Y
typically grows exponentially. This scalability issue poses signicant challenges in practical
applications, where large datasets or complex problem instances need to be processed within
reasonable time constraints.
LOG
2. Compare and contrast the concepts of NP-Complete, NP-Hard, and polynomial-
time solvable problems.
Answer: NP-Complete problems are both in NP and NP-Hard, meaning they are among
the hardest problems in NP. NP-Hard problems are at least as hard as the hardest problems
in NP but may not be in NP themselves. Polynomial-time solvable problems can be solved
in polynomial time by deterministic algorithms, implying they are less complex than NP-
NO
Complete problems.
4.
ECH
while non-deterministic algorithms can explore multiple paths simultaneously. In the context
of NP-Completeness, non-deterministic algorithms can eciently verify potential solutions,
but nding these solutions in the rst place is typically NP-hard for deterministic algorithms.
Answer: To prove a problem NP-Complete using reduction techniques, one must demonstrate
that it is in NP by providing a polynomial-time verier and show that it is NP-Hard by
reducing a known NP-Complete problem to it in polynomial time. This typically involves
constructing a transformation that maps instances of the known problem to instances of the
new problem while preserving the solution.
EM
6. Compare and contrast the time complexity of brute-force algorithms and polynomial-
time algorithms for NP-Complete problems.
Answer: Brute-force algorithms exhaustively search through all possible solutions, result-
ing in exponential time complexity for NP-Complete problems. In contrast, polynomial-time
D
algorithms aim to nd solutions more eciently, although they may not guarantee optimal-
ity. While brute-force algorithms provide exact solutions, they are often impractical for large
ACA
92