0% found this document useful (0 votes)
3 views

daa notes

The document is a question bank for a course on algorithms, specifically focusing on network flow and asymptotic notation. It includes multiple-choice questions and problems related to algorithms' time complexity, Big-O notation, and other related concepts. The content is structured according to Bloom's Taxonomy, addressing different levels of understanding from remembering to analyzing.

Uploaded by

ghoshspandan108
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)
3 views

daa notes

The document is a question bank for a course on algorithms, specifically focusing on network flow and asymptotic notation. It includes multiple-choice questions and problems related to algorithms' time complexity, Big-O notation, and other related concepts. The content is structured according to Bloom's Taxonomy, addressing different levels of understanding from remembering to analyzing.

Uploaded by

ghoshspandan108
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/ 92

Design and Analysis of

Y
Algorithms

LOG
Questions Bank

Paper Code: PCC-CS404

NO
ECH
FT
YO
D EM

Dilip Kumar Maity


Computer Science and Engineering
ACA

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.

a) Symptotic analysis b) Asymptotic analysis

c) Posterior Analysis d) Priori Analysis

Ans : B
FT
2. Using asymptotic analysis, we can very well conclude the ............ scenario of an algorithm.

a) best case b) average case

c) worst case d) best case, average case, and worst case


YO

Ans : D

3. Which case indicate the minimum time required for program execution?
EM

a) best case b) average case c) worst case d) None of the above

Ans : A

4. ............ is the formal way to express the upper bound of an algorithm's running time.
D

a) Ω b) Θ c) O d) All of the above


ACA

Ans : C

5. Worst Case indicates maximum time required for program execution.

a) Yes b) No c) Can be yes or no d) Can not say

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?

a) constant asymptotic notations b) logarithmic asymptotic notations

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

c) Can be true or false d) Can not say

Ans : A
FT
9. The Theta notation is the formal way to express ......... of an algorithm's running time.

a) upper bound b) lower bound

c) lower bound and upper bound d) None of the above


YO

Ans : C

10. Asymptotic analysis is ............... bound.

a) output b) input c) outer d) inner


EM

Ans : B

11. What is time complexity of fun()?

1 int fun(int n)
2 {
D

3 int count = 0;
4 for (int i = n; i > 0; i /= 2)
ACA

5 for (int j = 0; j < i; j++)


6 count += 1;
7 return count;
8 }

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

a) (1510 ) × n + 12099 b) n1.98


FT
p
c) n3 /( (n)) d) (220 ) × n

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 }

a) O(n) b) O(n2 ) c) O(nlogn) d) O(n(logn)2 )

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

18. Consider the following functions:


f (n) = 2n
g(n) = n!
h(n) = nl ogn
Which of the following statements about the asymptotic behavior of f (n), g(n), and h(n)
is true?
EM

(A) f (n) = O(g(n)); g(n) = O(h(n))


(B) f (n) = Ω(g(n)); g(n) = O(h(n))
(C) g(n) = O(f (n)); h(n) = O(f (n))
(D) h(n) = O(f (n)); g(n) = Ω(f (n))
D

a) A b) B c) C d) D
ACA

Answer: d
19. Consider the following C code segment [ISRO CS2018]

1 int f (int x){


2 if (x < 1) return 1;

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 }

Of the following, which best describes the growth of f(x) as a function of x?

a) Linear b) Exponential c) Quadratic d) Cubic

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 }

a) Θ(n) b) Θ(nLogn) c) Θ(n2 ) d) Θ(n2 Logn)


EM

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

4 for (j = 1; j <= n; j = j*2)


5 count++;
6 }

The complexity of the program is

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

a) Θ(nlogn) b) Θ(n) c) Θ(logn) d) Θ(1)

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?

a) O(x2 ) b) O(x) c) O(x1.5 /3) d) O(1)


FT
Answer: a
26. If f (x) = 3x2 + x2 logx, then f (x) is?

a) O(x2 ) b) O(x3 ) c) O(x) d) O(1)


YO

Answer: b
27. The big-O notation for f (n) = (nlogn + n2 )(n3 + 2) is?

a) O(n2 ) b) O(n3 ) c) O(n4 ) d) O(n5 )


EM

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

a) n2 logn b) n2 c) logn d) nlog(n2 )

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

2. What are the characteristics of an algorithm?

3. What is time and space complexity?

4. Discuss Big − Oh(O) notation.

5. Discuss Big − Omega(Ω) notation.


EM

6. Discuss Big − T heta(Θ) notation.

7. Discuss Little − Oh(o) notation.

8. Discuss Little − omega(ω) notation.


D

9. Prove that if f (n) is Θ(g(n)) then g(n) is Θ(f (n)) .

f (n) = O(g(n)) g(n) = O(h(n)) f (n) = O(h(n))


ACA

10. Prove that if and then

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

Prove that 5n2 − 6n = Θ(n2 )

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

2. Prove that n! = O(nn )


3. Prove that 2n2 2n + nlogn = Θ(n2 2n )
Prove that
Pn
4. i2 = Θ(n3 )

NO
i=0

Prove that
Pn
5. i=0 i3 = Θ(n4 )

6. Prove that n2n + 6 × 2n = Θ(n2n )


Prove that n3 + 106 n2 = Θ(n3 )

ECH
7.

8. Prove that 6n3


(logn+1)
= O(n3 )

9. Prove that n1.001 + nlogn = Θ(n1.001 )


10. Prove that 10n3 + 15n4 + 100n2 2n = O(100n2 2n )
Prove that 33n3 + 4n2 = Ω(n2 )
FT
11.

12. Prove that 33n3 + 4n2 = Ω(n3 )


13. Prove that log2 (n! ) = Θ(nlog2 n).
YO

Answer:
From the Stirling's approximation
√ 1
n! = 2nπ( ne )n e 12n

taking log2 both side, we get


EM

log2 n! = 12 log2 n + 12 log2 2π + nlog2 gn − nlog2 e + 1


12n
log2 e

log2 n! = nlog2 n − nlog2 e + 12 log2 n + 1


12n
log2 e + 12 log2 (2π)
D

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

1. Why 10n2 + 9 6= O(n) ? Justify your answer.


2. Why n3 logn 6= Θ(n2 ) ? Justify your answer.
3. Why n2
logn
6= Θ(n2 ) ? Justify your answer.

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 }

6. Which of the following is not O(n2 )? Justify your answer.

a) (1510 ) × n + 12099 b) n1.98


YO

p
c) n3 /( (n)) d) (220 ) × n

7. Analyse the time complexity of f un().

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.

1 void fun(int n, int arr[]){


2 int i = 0, j = 0;
3 for(; i < n; ++i)

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?

1 a) for(i = 0; i < n; i++)


2 b) for(i = 0; i < n; i += 2)
3 c) for(i = 1; i < n; i *= 2)

NO
4 d) for(i = n; i > -1; i /= 2)

10. Consider the following C code segment

int f (int x){

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

1. Master's theorem is used for?

a)

b)

c)

d)
solving recurrences

solving iterative relations

analysing loops
ECH
calculating the time complexity of any code
FT
Answer: a

2. How many cases are there under Master's theorem?

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 ?

a) T (n) = O(nlogb a ) b) T (n) = O(nc logn)


EM

c) T (n) = O(f (n)) d) T (n) = O(n2 )

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

a) T (n) = O(nlogb a) b) T (n) = O(nc logn)


ACA

c) T (n) = O(f (n)) d) T (n) = O(n2 )

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

c) 3 d) No case can be extended

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 ?

a) T (n) = O(nlogb a) b) T (n) = O(nc logn)


c) T (n) = O(nc (logn)k+1 d) T (n) = O(n2 )

Answer:
YO

10. Under what case of Master's theorem will the recurrence relation of binary search fall?

a) 1 b) 2

c) 3 d) It cannot be solved using master's theorem


EM

Answer: b

11. What is the value of T (n) of the following recurrence T (n) = 4T (n/2) + n2

a) T (n) = O(n) b) T (n) = O(logn)


D

c) T (n) = O(n2 logn) d) T (n) = O(n2 )

Answer:
ACA

12. What is the value of T (n) of the following recurrence T (n) = T (n/2) + 2n

a) T (n) = O(n2 ) b) T (n) = O(n2 logn)


c) T (n) = O(2n ) d) cannot be solved

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

a) T (n) = O(n(logn)2 ) b) T (n) = O(nlogn)

NO
c) T (n) = O(n2 logn) d) cannot be solved

Answer: a

2.2 Questions for Blooms Taxonomy Level: Understand

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.

5. Write the algorithm of Tower of Hanoi.

6. Find the recurrence relation of Tower of Hanoi. And solve it.


YO

2.3 Questions for Blooms Taxonomy Level: Apply

1. Solve the following recurrences using Iterative method.


(
1 if n = 1
EM

(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

2. Solve the following recurrences using Substitution method.


(
1 if n=1
(a) T (n) =
2T ( n
2
)+n if n>1

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

3. Solve the following recurrences using Changing variables method.

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)

(d) T (n) = 16T ( n


4
) + n2 = Θ(n2 lg n)
EM

(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?

1 int xpowy(int x, int n){

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?

1 int sum(int n){


2 if(n==1) return 1;
3 else return n+sum(n-1);

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

Divide and Conquer

NO
3.1 Questions for Blooms Taxonomy Level: Remember

1. Merge sort uses which of the following technique to implement sorting?

a)

c)
backtracking

divide and conquer

Answer: c
ECH b)

d)
greedy algorithm

dynamic programming

2. What is the average case time complexity of merge sort?


FT
a) O(nlogn) b) O(n2 ) c) O(n2 logn) d) O(nlogn2 )

Answer: a

3. What is the auxiliary space complexity of merge sort?


YO

a) O(1) b) O(logn) c) O(n) d) O(nlogn)

Answer: c

4. Merge sort can be implemented using O(1) auxiliary space.


EM

a) true b) false

Answer: a

5. What is the worst case time complexity of merge sort?


D

a) O(nlogn) b) O(n2 ) c) O(n2 logn) d) O(nlogn2 )

Answer: a
ACA

6. Which of the following method is used for sorting in merge sort?

a) merging b) partitioning c) selection d) exchanging

Answer: a

21
7. What will be the best case time complexity of merge sort?

a) O(nlogn) b) O(n2 ) c) O(n2 logn) d) O(nlogn2 )

Y
Answer: a

LOG
8. Choose the incorrect statement about merge sort from the following?

a) it is a comparison based sort b) it is an adaptive algorithm

c) it is not an in place algorithm d) it is stable algorithm

Answer: b

NO
9. Which of the following is not in-place sorting algorithm by default?

a) merge sort b) quick sort c) heap sort d) insertion sort

Answer: a

Quick sort

Answer: a
b)
ECH
10. Which of the following is not a stable sorting algorithm?

a) Cocktail sort c) Bubble sort d) Merge sort

11. Which of the following stable sorting algorithm takes the least time when applied to an almost
FT
sorted array?

a) Quick sort b) Insertion sort c) Selection sort d) Merge sort

Answer:b
YO

12. Choose the correct code for merge sort.

a)

1 void merge_sort(int arr[], int left, int right){


2 if (left > right){
3 int mid = (right-left)/2;
EM

4 merge_sort(arr, left, mid);


5 merge_sort(arr, mid+1, right);
6 merge(arr, left, mid, right);
7 }
8 }
D
ACA

b)

1 void merge_sort(int arr[], int left, int right){


2 if (left < right){
3 int mid = left+(right-left)/2;
4 merge_sort(arr, left, mid);

22
5 merge_sort(arr, mid+1, right);
6 merge(arr, left, mid, right);
}

Y
7
8 }

LOG
c)

1 void merge_sort(int arr[], int left, int right){


2 if (left < right){
3 int mid = left+(right-left)/2;
4 merge(arr, left, mid, right);

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

13. Which of the following sorting algorithms is the fastest?

a) Merge sort b) Quick sort c) Insertion sort d) Shell sort

Answer: b b
EM

14. Quick sort follows Divide-and-Conquer strategy.

a) True b) False
D

Answer: a
15. What is the worst case time complexity of a quick sort algorithm?
ACA

a) O(N ) b) O(N logN ) c) O(N 2 ) d) O(logN )

Answer: c
16. Which of the following methods is the most eective for picking the pivot element?

23
a) rst element b) last element

c) median-of-three partitioning d) random 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) choosing a random element as pivot b) choosing the rst element as pivot

c) choosing the last element as pivot d) median-of-three partitioning method

a)
Answer: a

b)
ECH
19. What is the average running time of a quick sort algorithm?

O(N 2 ) O(N ) c) O(N logN ) d) O(logN )

Answer: c
FT
20. Which of the following sorting algorithms is used along with quick sort to sort the sub arrays?

a) Merge sort b) Shell sort c) Insertion sort d) Bubble sort

Answer: c
YO

It is used only at the end.

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?

a) one b) two c) three d) four


D

Answer: b
23. Which is the worst method of choosing a pivot element?
ACA

a) rst element as pivot b) last element as pivot

c) median-of-three partitioning d) random element as pivot

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 

a) greedy algorithm b) divide and conquer algorithm

c) dynamic programming algorithm d) backtracking algorithm

NO
Answer: b
26. What is the worst case time complexity of the Quick sort?

a) O(nlogn) b) O(n) c) O(n3 ) d) O(n2 )

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

-

a) n/2 : (n/2) − 1 b) n/2 : n/3 c) n/4 : 3n/2 d) n/4 : 3n/4

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

a) T (n) ≤ 2T (n/4) + cn b) T (n) ≤ T (n/4) + T (3n/4) + cn


c) T (n) ≤ 2T (3n/4) + cn d) T (n) ≤ T (n/3) + T (3n/4) + cn

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 -

a) 60.2 sec b) 45.54 sec c) 31.11 sec d) 20 sec

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 -

a) Bubble sort b) Selection sort


FT
c) Insertion sort d) Binary tree sort

Answer: d
35. QuickSort can be categorized into which of the following?
YO

a) Brute Force technique b) Divide and conquer

c) Greedy algorithm d) Dynamic programming

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)

1 void quickSort(int[] arr, int low, int high){


D

2 int pivot;
3 if(high>low){
ACA

4 pivot = partition(arr, low, high);


5 quickSort(arr, low, pivot-1);
6 quickSort(arr, pivot+1, high);
7 }
8 }

26
b)

1 void quickSort(int[] arr, int low, int high){

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)

1 void quickSort(int[] arr, int low, int high){


2 int pivot;
3 if(high>low){

ECH
4 pivot = partition(arr, low, high);
5 quickSort(arr, low, pivot);
6 quickSort(arr, pivot, high);
7 }
8 }
FT
d)

1 void quickSort(int[] arr, int low, int high){


2 int pivot;
3 if(high>low){
4 pivot = partition(arr, low, high);
YO

5 quickSort(arr, low, pivot);


6 quickSort(arr, pivot+2, high);
7 }
8 }
EM

Answer: a
37. What is the worst case complexity of QuickSort?

a) O(nlogn) b) O(logn) c) O(n) d) O(n2 )


D

Answer: d
38. What is a randomized QuickSort?
ACA

a) The leftmost element is chosen as the pivot

b) The rightmost element is chosen as the pivot

c) Any element in the array is chosen as the pivot

d) A random number is generated which is used as the pivot

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)

int partition(int[] arr, int low, int high){


YO

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

12 if(left < right){


13 swap(arr, left, right);
ACA

14 }
15 }
16 arr[low] = arr[right];
17 arr[right] = pivot_item;
18 return right;

28
19 }

Y
c)

1 int partition(int[] arr, int low, int high){

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)

1 int partition(int[] arr, int low, int high){


int left, right, pivot_item = arr[low];
YO

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

13 swap(arr, left, right);


14 }
ACA

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)

arr = {2, 6, 1}.


subsequent partitioning?
c) O(n) d) O(n2 )

What are the pivots that are returned as a result of


FT
a) 1 and 6 b) 6 and 1 c) 2 and 6 d) 1

Answer: d
44. Which of the following is not true about QuickSort?
YO

a) in-place algorithm b) pivot position can be changed

c) adaptive sorting algorithm d) can be implemented as a stable sort

Answer: b
EM

3.2 Questions for Blooms Taxonomy Level: Understand

1. What is divide and conquer approach of solving problem?

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.

4. Discuss the merge sort algorithm with an example.


ACA

5. Write the merge sort algorithm.

6. Is merge sort an in-place sorting algorithm? Explain?

7. Is merge sort stable sorting algorithm? Explain?

30
8. Discuss the quick sort algorithm with an example.

9. Write the quick sort algorithm.

Y
10. What is a randomized Quick Sort?

LOG
11. Is quick sort an in-place sorting algorithm? Explain?

12. Is quick sort stable sorting algorithm? Explain?

3.3 Questions for Blooms Taxonomy Level: Apply

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?

3.4 Questions for Blooms Taxonomy Level: Analyze

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.

3. Analyze the time complexity of Merge Sort Algorithm.

q Partition return when all elements in the array A[p..r] have


EM

4. Explain, what value of does


the same value?

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

7. Analyze the average-case time complexity of Quick sort Algorithm.


ACA

8. Analyze the worst-case time complexity of Quick sort Algorithm.

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

a) Stack b) Queue c) Linked List d) Tree

Answer: b
4. The Breadth First Search traversal of a graph will result into?
EM

a) Linked List b) Tree

c) Graph with back edges d) Arrays

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

a) Depth First Search b) Breadth First Search

c) Trim's algorithm d) Kruskal's algorithm

Answer: b
33
6. Which of the following is not an application of Breadth First Search?

a) Finding shortest path between two nodes

Y
b) Finding bipartiteness of a graph

c) GPS navigation system

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

a) linked list b) tree c) stack d) queue

Answer: c
11. Which of the following traversal in a binary tree is similar to depth rst traversal?

a) level order b) post order c) pre order d) in order


EM

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

a) O(V + E) b) O(V ) c) O(E) d) O(V × E)

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

16 for (auto i = adjacent[s].begin(); i != adjacent[s].end(); ++i)


17 if (discovered[*i])
18 st.push(*i);
19 }
20 }
EM

Answer: b
17. What is the space complexity of standard DFS(V: no. of vertices E: no. of edges)?

a) O(V + E) b) O(V ) c) O(E) d) O(V × E)


D

Answer: b
ACA

18. Which of the following data structure is used to implement BFS?

a) linked list b) tree c) stack d) queue

Answer: d
38
19. Choose the incorrect statement about DFS and BFS from the following?

a) BFS is equivalent to level order traversal in trees

Y
b) DFS is equivalent to post order traversal in trees

LOG
c) DFS and BFS code has the same time complexity

d) BFS is implemented using queue

Answer: b
20. Depth First Search is equivalent to which of the traversal in the Binary Trees?

a) Pre-order Traversal b) Post-order Traversal

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 )

ECH c) O(E) d) O(V × E)

22. The Data structure used in standard implementation of Breadth First Search is?

a) Stack b) Queue c) Linked List d) Tree


FT
Answer: a
23. The Depth First Search traversal of a graph will result into?

a) Linked List b) Tree


YO

c) Graph with back edges d) Array

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

vertex. What algorithm he should use?

a) Depth First Search b) Breadth First Search

c) Trim's algorithm d) Kruskal's Algorithm

Answer: a
D

25. Which of the following is not an application of Depth First Search?


ACA

a) For generating topological sort of a graph

b) For generating Strongly Connected Components of a directed graph

c) Detecting cycles in the graph

d) Peer to Peer Networks

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

c) At most 1 d) Insucient Information

Answer: a

ECH
28. In Depth First Search, how many times a node is visited?

a) Once

b) Twice

c) Equivalent to number of in-degree of the node


FT
d) Thrice

Answer: c
29. Topological sort can be applied to which of the following graphs?
YO

a) Undirected Cyclic Graphs b) Directed Cyclic Graphs

c) Undirected Acyclic Graphs d) Directed Acyclic Graphs

Answer: d
30. Most Ecient Time Complexity of Topological Sorting is? (V - number of vertices, E - number
EM

of edges)

a) O(V + E) b) O(V ) c) O(E) d) O(V × E)

Answer: a
D

31. In most of the cases, topological sort starts from a node which has -

a) Maximum Degree b) Minimum Degree


ACA

c) Any degree d) Zero Degree

Answer: d
32. Which of the following is not an application of topological sorting?

40
a) Finding prerequisite of a task

b) Finding Deadlock in an Operating System

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

b) Always Not unique

NO
c) Sometimes unique and sometimes not unique

d) Always unique if graph has even number of vertices

Answer: c

ECH
34. Topological sort can be implemented by?

a) Using Depth First Search

b) Using Breadth First Search

c) Using Depth and Breadth First Search

d) Using level ordered search


FT
Answer: c
35. Topological sort is equivalent to which of the traversals in trees?

a) Pre-order traversal b) Post-order traversal

c) In-order traversal d) Level-order traversal


YO

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

a) Depth First Search b) Breadth First Search

c) Topological Sorting d) Dijkstra's Shortest path algorithm

Answer: c
D

37. When the topological sort of a graph is unique?

a) When there exists a hamiltonian path in the graph


ACA

b) In the presence of multiple nodes with indegree 0

c) In the presence of single node with indegree 0

d) In the presence of single node with outdegree 0

Answer: a
41
4.2 Questions for Blooms Taxonomy Level: Understand

Y
1. What is a graph? Explain its key terms.

2. How are graphs represented inside a computer's memory?

LOG
3. Explain the breadth rst traversal algorithms in detail with an example.

4. Explain the depth rst traversal algorithms in detail with an example.

5. Explain the topological sorting algorithms in detail with an example.

NO
6. Write the Breadth First Algorithm.

7. Write the Depth First Algorithm.

8. Write the Topological sort Algorithm.

4.3
ECH
9. Explain the term Tree, Back, Edge and Cross Edges in DFS of Graph.

Questions for Blooms Taxonomy Level: Apply

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?

2. Analyze the time complexity of BFS Algorithm.


YO

3. Analyze the time complexity of DFS Algorithm.

4. Analyze the time complexity of Topological Sorting Algorithm.

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.

7. Dierentiate between depth-rst search and breadth-rst search traversal of a graph.

8. How to check given a directed graph has a cycle or not.


D

9. When to use DFS or BFS to solve a Graph problem?


ACA

44
Y
LOG
Chapter 5

Greedy Algorithm

NO
5.1 Questions for Blooms Taxonomy Level: Remember

1. Fractional knapsack problem is also known as-

a)

c)
0/1 knapsack problem

Divisible knapsack problem

Answer: b
ECH b)

d)
Continuous knapsack problem

Non 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

c) Greedy algorithm d) Backtracking

Answer: c
YO

3. What is the objective of the knapsack problem?

a) To get maximum total value in the knapsack

b) To get minimum total value in the knapsack

c) To get maximum weight in the knapsack


EM

d) To get minimum weight in the knapsack

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

b) Both are the same

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 

a) O(nlogn) b) O(n) c) O(n2 ) d) O(nW )

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

Breaking items into fraction


ECH
8. The main time taking step in fractional knapsack problem is 

b) Adding items into knapsack


FT
c) Sorting d) Looping through sorted items

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.

a) 100, 80 b) 110, 70 c) 130, 110 d) 110, 80

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?

a) J2, J4, J1, J3 b) J4, J1, J2, J3


D

c) J4, J2, J1, J3 d) J4, J2, J3, J1


ACA

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?

a) All tasks are completed b) T1 and T6 are left out

c) T1 and T8 are left out d) T4 and T6 are left out

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.

What is the maximum prot earned?

a) 150 b) 147 c) 165 d) 175


YO

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

a) All tasks are completed b) J1 and J5 are left out

c) J2 and J4 are left out d) J2 and J3 are left out

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?

What is the maximum prot earned?

a) 140 b) 142 c) 161 d) 152

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 

a) nd minimum spanning tree

NO
b) nd single source shortest path

c) nd all pair shortest path algorithm

ECH
d) traverse the graph

Answer: a
17. Kruskal's algorithm is a 

a) divide and conquer algorithm b) dynamic programming algorithm


FT
c) greedy algorithm d) approximation algorithm

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?

a) O(logV ) b) O(ElogV ) c) O(E 2 ) d) O(V logE)

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

c) Prim's algorithm is simpler than Kruskal's algorithm

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

b) It constructs MST by selecting edges in increasing order of their weights

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

S1. Kruskal's algorithm might produce a non-minimal spanning tree.


S2. Kruskal's algorithm can eciently implemented using the disjoint-set data structure.

a) S1 is true but S2 is false b) Both S1 and S2 are false


EM

c) Both S1 and S2 are true d) S2 is true but S1 is false

Answer: d
27. Which of the following is true?
D

a) Prim's algorithm initialises with a vertex

b) Prim's algorithm initialises with a edge


ACA

c) Prim's algorithm initialises with a vertex which has smallest edge

d) Prim's algorithm initialises with a forest

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?

O(V 2 ) c) O(E 2 ) d) O(V logE)


FT
Answer: b
30. Prim's algorithm is a 

a) Divide and conquer algorithm b) Greedy algorithm


YO

c) Dynamic Programming d) Approximation algorithm

Answer: b
EM

31. Prim's algorithm resembles Dijkstra's algorithm.

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 

a) Dijkstra-Scholten algorithm b) Bor·vka's algorithm


FT
c) Floyd-Warshall algorithm d) DJP Algorithm

Answer: d
35. Prim's algorithm can be eciently implemented using-for graphs with greater density.
YO

a) d-ary heap b) linear search

c) bonacci heap d) binary search

Answer: a
36. Which of the following is false about Prim's algorithm?
EM

a) It is a greedy algorithm

b) It constructs MST by selecting edges in increasing order of their weights

c) It never accepts cycles in the MST


D

d) It can be implemented using the Fibonacci heap

Answer: b
ACA

37. Dijkstra's Algorithm is used to solve  problems.

a) All pair shortest path b) Single source shortest path

c) Network ow d) Sorting

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

c) Circular queue d) Min priority queue

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

Undirected and unweighted graphs

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

2 if(T[v].Dist + C(v,w) < T[w].Dist) {


3 Increase (T[w].Dist to T[v].Dist +C(v,w));
4 T[w].path=v;
5 }
D

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

c) Number of vertices - 1 d) Number of edges - 1

Answer: b
YO

45. What is running time of Dijkstra's algorithm using Binary min- heap method?

a) O(V ) b) O(V logV ) c) O(E) d) O(ElogV )

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)

What happens when While Q 6= 0 is changed to while Q > 1?

NO
a) While loop gets executed for v times

b) While loop gets executed for v-1 times

c) While loop gets executed only once

d) While loop does not get executed

Answer: b
49. Consider the following graph.
ECH
FT
YO

If b is the source vertex, what is the minimum cost to reach f vertex?

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

c) Back tracking d) Dynamic programming

Answer: a

NO
5.2 Questions for Blooms Taxonomy Level: Understand

1. What is greedy algorithm?

2. Dene spanning tree.

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.

5. Briey discuss fractional knapsack problem with an example.

6. Briey discuss Job sequencing with deadline with an example.


FT
7. Briey discuss Prim's algorithm with an example.

8. Briey discuss Kruskal's algorithm with an example.

9. Briey discuss Dijkstra's algorithm with an example.


YO

10. Write the algorithm for fractional knapsack problem.

11. Write the algorithm for Job sequencing with deadline.

12. Write the Prim's algorithm.

13. Write the Kruskal's algorithm.


EM

14. Write the Dijkstra's algorithm.

5.3 Questions for Blooms Taxonomy Level: Apply


D

6 Find the optimal solution for the fractional knapsack problem given below:
I = I1, I2, I3, I4, I5
ACA

W = 5, 10, 20, 30, 40


P = 30, 20, 100, 90, 160
T heknapsackcapacityM = 60.

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

5.4 Questions for Blooms Taxonomy Level: Analyze

1. Dierentiate between Dijkstra's algorithm and minimum spanning tree algorithm.

2. Analyze the time complexity of fractionl knapsack problem.


EM

3. Analyze the time complexity of Job Sequencing with Deadline algorithm.

4. Analyze the time complexity of Prim's Algorithm.

5. Analyze the time complexity of Kruskal's Algorithm.


D

6. Analyze the time complexity of Dijkstra's Algorithm.


ACA

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?

18000 c) 24000 d) 32000


FT
5. Consider the matrices P, Q, R and S which are 20 × 15, 15 × 30, 30 × 5 and 5 × 40 matrices
respectively. What is the minimum number of multiplications required to multiply the four
matrices?
YO

a) 6050 b) 7500 c) 7750 d) 12000

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?

a) O(n! ) b) O(n3 ) c) O(n2 ) d) Exponential

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?

a) arr[row][k]˘arr[k + 1][col] + mat[row˘1] ∗ mat[k] ∗ mat[col];


b) arr[row][k] + arr[k + 1][col]˘mat[row˘1] ∗ mat[k] ∗ mat[col];
arr[row][k] + arr[k + 1][col] + mat[row˘1] ∗ mat[k] ∗ mat[col];
YO

c)

d) arr[row][k]˘arr[k + 1][col]˘mat[row˘1] ∗ mat[k] ∗ mat[col];

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.

a) Boolean b) Integer c) String d) Double

Answer: a
YO

10. Bellmann ford algorithm provides solution for ____________ problems.

a) All pair shortest path b) Sorting


EM

c) Network ow d) Single source shortest path

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?

a) O(V ) b) O(V 2 ) c) O(ElogV ) d) O(V E)

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

16. Identify the correct Bellmann Ford Algorithm. a)


YO

1 for i=1 to V[g]-1 do{


2 for each edge (u,v) in E[g] do
3 Relax(u,v,w)
4 }
5 for each edge (u,v) in E[g] do
EM

6 if d[v]>d[u]+w(u,v) then return False


7 return True

b)
D

1 for i=1 to V[g]-1 do{


2 for each edge (u,v) in E[g] do
ACA

3 if d[v]>d[u]+w(u,v) then return False


4 }
5 return True

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)

for i=1 to V[g]-1 do

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

18. Bellmann Ford Algorithm can be applied for _____________

a) Undirected and weighted graphs b) Undirected and unweighted graphs

c) Directed and weighted graphs d) All directed graphs


EM

Answer: c

19. Bellmann Ford algorithm was rst proposed by ________


D

a) Richard Bellmann b) Alfonso Shimbe

c) Lester Ford Jr d) Edward F. Moore


ACA

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

22. Bellmann Ford Algorithm is an example for ____________

a) Dynamic Programming b) Greedy Algorithms


EM

c) Linear Programming d) Branch and Bound

Answer: a
D

23. A graph is said to have a negative weight cycle when?


ACA

a) The graph has 1 negative weighted edge

b) The graph has a cycle

c) The total weight of the graph is negative

d) The graph has 1 or more negative weighted edges

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

c) Network ow problems d) Sorting problems

Answer: a

25. Floyd Warshall's Algorithm can be applied on __________

NO
a) Undirected and unweighted graphs b) Undirected graphs

c) Directed graphs d) Acyclic 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?

a) Greedy technique b) Dynamic Programming

c) Linear Programming d) Backtracking


YO

Answer: b

28. Floyd Warshall Algorithm can be used for nding _____________


EM

a) Single source shortest path b) Topological sort

c) Minimum spanning tree d) Transitive closure

Answer: d
D

29. What procedure is being followed in Floyd Warshall Algorithm?


ACA

a) Top down b) Bottom up c) Big bang d) Sandwich

Answer: b

30. Complete the program.

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)

a) dij (k) = min(dij (k − 1), dik (k − 1) − dkj (k − 1))

NO
b) dij (k) = max(dij (k − 1), dik (k − 1) − dkj (k − 1))

c) dij (k) = min(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

c) N intermediate vertices d) N −1 intermediate vertices


YO

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

36. What is the formula to compute the transitive closure of a graph?


D

a) tij (k) = tij (k − 1) AN D (tik (k − 1) OR tkj (k − 1))


b) tij (k) = tij (k − 1) OR (tik (k − 1) AN D tkj (k − 1))
ACA

c) tij (k) = tij (k − 1) AN D (tik (k − 1) AN D tkj (k − 1))


d) tij (k) = tij (k − 1) OR (tik (k − 1) OR tkj (k − 1))

Answer: b

68
6.2 Questions for Blooms Taxonomy Level: Understand

Y
1. What is dynamic programming?

2. Write the characteristics of 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.

5. Write Bellman-Ford algorithm for single source shortest path problem.

NO
6. Write Floyd-Warshall algorithm for all pair of shortest path problem.

6.3 Questions for Blooms Taxonomy Level: Apply

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.

2. Analyze the time complexity of Bellman-Ford 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

1. Which of the problems cannot be solved by backtracking method?

a)

c)
n-queen problem

hamiltonian circuit problem

Answer: d
ECH b)

d)
subset sum problem

travelling salesman problem

2. Backtracking algorithm is implemented by constructing a tree of choices called as?


FT
a) State-space tree b) State-chart tree

c) Node tree d) Backtracking tree

Answer: a
YO

3. What happens when the backtracking algorithm reaches a complete solution?

a) It backtracks to the root

b) It continues searching for other possible solutions

c) It traverses from a dierent route


EM

d) Recursively traverses through the same route

Answer: b

4. A node is said to be ____________ if it has a possibility of reaching a complete solution.


D

a) Non-promising b) Promising c) Succeeding d) Preceding

Answer:
ACA

5. In what manner is a state-space tree for a backtracking algorithm constructed?

a) Depth-rst search b) Breadth-rst search

c) Twice around the tree d) Nearest neighbour rst

71
Answer: a

6. The leaves in a state-space tree represent only complete solutions.

Y
a) true b) false

LOG
Answer: b

7. In general, backtracking can be used to solve?

a) Numerical problems b) Exhaustive search

c) Combinatorial problems d) Graph coloring problems

NO
Answer: c

8. Which one of the following is an application of the backtracking algorithm?

ECH
a) Finding the shortest path b) Ecient quantity to shop

c) Ludo d) Crossword

Answer: d

9. Backtracking algorithm is faster than the brute force technique


FT
a) true b) false

Answer: a

10. Which of the following logical programming languages is not based on backtracking?
YO

a) Icon b) Prolog c) Planner d) Fortran

Answer: d

11. The problem of nding a list of integers in a given specic range that meets certain conditions
is called?
EM

a) Subset sum problem b) Constraint satisfaction problem

c) Hamiltonian circuit problem d) Travelling salesman problem

Answer: b
D

12. Who coined the term `backtracking' ?


ACA

a) Lehmer b) Donald c) Ross d) Ford

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

c) Backtracking d) Divide and conquer

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?

a) n- queen problem b) subset sum problem

c) knapsack problem d) hamiltonian circuit problem

Answer: b

NO
15. The problem of placing n queens in a chessboard such that no two queens attack each other
is called as?

a) n-queen problem b) eight queens puzzle

c)

a)
four queens puzzle

Answer:

1
a

b) 2
ECH d)

c)
1-queen problem

16. In how many directions do queens attack each other?

3 d) 4
FT
Answer: c
17. Placing n-queens so that no two queens attack each other is called?

a) n-queen's problem b) 8-queen's problem


YO

c) Hamiltonian circuit problem d) subset sum problem

Answer: a
18. Where is the n-queens problem implemented?
EM

a) carom b) chess c) ludo d) cards

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?

a) greedy algorithm b) divide and conquer

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?

a) (3,1,4,2) b) (2,3,1,4) c) (4,3,2,1) d) (4,2,3,1)

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?

a) 850 c) 842 d) 724

Answer: d
FT
25. If n=1, an imaginary solution for the problem exists.

a) true b) false

Answer: b
YO

26. What is the domination number for 8-queen's problem?

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

28. Which of the following is not a type of graph in computer science?

a) undirected graph b) bar graph

c) directed graph d) weighted graph

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

c) A condition where all vertices should have a dierent color

d) A condition where all vertices should 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

34. Which of the following is an NP complete problem?

a) Hamiltonian cycle b) Travelling salesman problem

c) Calculating chromatic number of graph d) Finding maximum element in an array

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

a) O(mn ) b) O(nmn ) c) O(mnm ) d) O(nmm )

Answer: b
EM

7.2 Questions for Blooms Taxonomy Level: Understand

1. What do you mean by Backtracking algorithm?

2. How many unique colors will be required for proper vertex coloring of a bipartite graph having
D

n vertices?

3. Explain m-coloring problem.


ACA

4. What is chromatic number? Give an example.

5. Write the m-coloring Algorithm.

6. Write the backtracking algorithm for n-queen problem.

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.

3. Show all possible solution for 4-queen problem.

7.4 Questions for Blooms Taxonomy Level: Analyze


FT
1. Evaluate the time complexity of m-coloring backtracking Algorithm.

2. Evaluate the time complexity of nQueen backtracking Algorithm.

3. Analyze the time complexity of Floyd-Warshall algorithm.


YO
D EM
ACA

78
Y
LOG
Chapter 8

Network Flow

NO
8.1 MCQs

1. What is the goal of a network ow algorithm?

ECH
A) To maximize the ow through a network

B) To minimize the ow through a network

C) To nd the shortest path in a network

D) To identify all nodes in a network


FT
Answer: A) To maximize the ow through a network

2. Which algorithm is commonly used to solve the maximum ow problem?

A) Dijkstra's algorithm
YO

B) Bellman-Ford algorithm

C) Ford-Fulkerson algorithm

D) Prim's algorithm

Answer:
EM

C) Ford-Fulkerson algorithm

3. In the Ford-Fulkerson algorithm, how is the residual capacity of an edge determined?

A) By subtracting the ow from the capacity


D

B) By adding the ow to the capacity

C) By dividing the ow by the capacity


ACA

D) By multiplying the ow by the capacity

Answer: A) By subtracting the ow from the capacity

4. What does the capacity of an edge in a network represent?

79
A) The maximum ow that can pass through the edge

B) The minimum ow that must 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

Answer: D) Adjacency list

6. Which of the following is NOT a variant of the Ford-Fulkerson algorithm?

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?

A) A path from source to sink with maximum ow

B) A path from source to sink with minimum ow


EM

C) A path with residual capacity greater than zero

D) A path with residual capacity equal to zero

Answer:
D

C) A path with residual capacity greater than zero

8. Which technique is used to eciently nd augmenting paths in the Ford-Fulkerson algorithm?
ACA

A) Breadth-rst search (BFS)

B) Depth-rst search (DFS)

C) Dijkstra's algorithm

80
D) Bellman-Ford algorithm

Answer: A) Breadth-rst search (BFS)

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

B) There is an augmenting path with maximum capacity

C) The ow through every edge is equal to its capacity

D) The ow through the source node is maximum

Answer:
FT
A) There are no more augmenting paths

8.2 Remember
YO

1. What is the objective of a network ow algorithm?

2. Name two classic network ow problems.

3. What does the capacity of an edge represent in the context of network ow?

4. Recall the Ford-Fulkerson algorithm. What is its basic operation?


EM

5. Identify the key components of a residual graph in network ow algorithms.

8.3 Understand
D

1. Explain the concept of maximum ow in a network.

2. Describe the signicance of the augmenting path in the Ford-Fulkerson algorithm.


ACA

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

1. Which of the following is true about the complexity class P?

ECH
A) Problems that can be solved in polynomial time

B) Problems that cannot be solved in polynomial time

C) Problems that are solved by non-deterministic algorithms


FT
D) Problems that are solved by quantum algorithms

Answer: A) Problems that can be solved in polynomial time


Explanation: The complexity class P consists of decision problems that can be solved in polynomial
time by a deterministic Turing machine.
2. NP stands for:
YO

A) Non-Polynomial

B) Non-Practical

C) Nondeterministic Polynomial
EM

D) No Problem

Answer: C) Nondeterministic Polynomial


Explanation: NP stands for "nondeterministic polynomial time." It consists of decision problems
for which solutions can be veried in polynomial time.
D

3. Which of the following is true regarding NP-complete (NPC) problems?

A) They are the easiest problems in NP


ACA

B) They are the hardest problems in NP

C) They are problems solvable in polynomial time

D) They are problems with non-deterministic algorithms

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) It is as hard as any problem in NP

C) It has only one solution

D) It has an exponential time complexity


FT
Answer: B) It is as hard as any problem in NP
Explanation: The Subset Sum Problem being NP-complete implies that it is as hard as any
problem in NP, meaning that there is no known polynomial-time algorithm for it and it is at least
as hard as any other problem in NP.
YO

6. Which of the following problems is NP-hard but not NP-complete?

A) Traveling Salesman Problem (TSP)

B) Knapsack Problem

C) Hamiltonian Cycle Problem


EM

D) Boolean Satisability Problem (SAT)

Answer: B) Knapsack Problem


Explanation: NP-hard problems are at least as hard as the hardest problems in NP, but they may
not be in NP themselves. The Knapsack Problem is NP-hard but not NP-complete.
D

7. The problem of nding the shortest path in a weighted graph is in:

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?

A) Factoring large integers


YO

B) Graph isomorphism

C) Finding the minimum spanning tree of a graph

D) Finding the shortest path in a directed acyclic graph

Answer: B) Graph isomorphism


EM

Explanation: Graph isomorphism is a problem that is not known to be in P or NP-complete. Its


complexity is still an open question in theoretical computer science.

9.2 Remember:
D

1. Recall the denition of NP-Completeness and briey explain its signicance in


computational theory.
ACA

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)

(b) The Knapsack Problem

LOG
(c) The Boolean Satisability Problem (SAT)

3. Name the mathematician who rst introduced the concept of NP-Completeness.


Answer: Stephen Cook and Leonid Levin independently introduced the concept of NP-
completeness in the early 1970s.

4. Identify the key characteristic that distinguishes NP-Complete problems from

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

Answer: A polynomial-time reduction is a way of transforming one problem (the source


problem) into another problem (the target problem) in polynomial time while preserving the
solution. By demonstrating that a known NP-Complete problem can be polynomial-time
reduced to a new problem, one can prove that the new problem is also NP-Complete.
D

9. Name one problem that is known to be NP-Hard but not NP-Complete.


Answer: The Halting Problem is an example of a problem that is NP-Hard but not NP-
ACA

Complete. It cannot be solved by a Turing machine in general, let alone in polynomial time.

10. Identify the class of problems to which NP-Complete problems belong.


Answer: NP-Complete problems belong to the complexity class NP (nondeterministic poly-
nomial time). This class includes problems for which a proposed solution can be veried in
polynomial time.

88
9.3 Understand:

Explain the concept of non-deterministic polynomial time and its relevance to

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.

2. Describe the process of reducing one NP-Complete problem to another to prove


NP-Completeness.

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.

Dierentiate between NP-Complete and NP-Hard problems, providing examples

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.

5. Discuss why nding a polynomial-time algorithm for any NP-Complete problem


would imply P = NP.
Answer: If a polynomial-time algorithm were found for any NP-Complete problem, then it
EM

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.

6. Explain how the concept of NP-Completeness impacts real-world problem-solving


D

and algorithm design.


Answer: The concept of NP-Completeness helps identify problems that are likely to be in-
ACA

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.

7. Describe the importance of the concept of NP-Completeness in cryptography and


security.
89
Answer: NP-Completeness plays a crucial role in cryptography and security by helping
identify problems that are suitable for cryptographic purposes, such as generating secure

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

(a) Show that the problem is in NP by providing a polynomial-time verier.

(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.

Apply the concept of polynomial-time reduction to prove that a new problem is


ACA

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.

4. Develop a reduction from the Boolean Satisability Problem (SAT) to a new


decision problem to demonstrate NP-Completeness.
Answer: To demonstrate NP-Completeness of a new problem using reduction from SAT:

ECH
(a) Choose an instance of SAT.

(b) Construct a polynomial-time reduction from SAT to the new problem.

(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.

5. Apply the concept of NP-Completeness to assess the diculty of solving a new


computational problem.
FT
Answer: If a problem can be shown to be NP-Complete, it suggests that nding exact solu-
tions eciently is likely to be challenging. NP-Completeness provides a framework for under-
standing the inherent diculty of computational problems and helps guide the development
of approximation algorithms or heuristic approaches.
YO

6. Develop a strategy for solving an NP-Complete problem using approximation


algorithms.
Answer: A strategy for solving an NP-Complete problem using approximation algorithms
involves sacricing optimality for eciency. This may include techniques such as greedy
algorithms, local search algorithms, or randomized algorithms, aiming to nd solutions that
EM

are close to the optimal within a reasonable amount of time.

7. Propose a heuristic approach to tackle an NP-Complete optimization problem


and evaluate its eectiveness.
Answer: A heuristic approach for an NP-Complete optimization problem could involve tech-
niques like genetic algorithms, simulated annealing, or ant colony optimization. The eec-
D

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:

1. Analyze the implications of NP-Completeness on the scalability of algorithms in


practical applications.
91
Answer: NP-Completeness implies that certain problems may not have ecient algorithms to
solve them exactly. As the input size grows, the time required to solve NP-Complete problems

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.

3. Analyze the dierences between deterministic and non-deterministic algorithms


in the context of NP-Completeness.
Answer: Deterministic algorithms follow a predened sequence of steps to solve a problem,

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.

Evaluate the limitations of approximation algorithms in solving NP-Complete op-


timization problems.
Answer: Approximation algorithms sacrice optimality for eciency by providing near-
FT
optimal solutions within a guaranteed approximation factor. While they oer practical solu-
tions for NP-Complete optimization problems, their limitations include the inability to guar-
antee the quality of solutions or their performance on all problem instances.

5. Analyze the conditions under which a problem can be proven to be NP-Complete


using reduction techniques.
YO

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

problem instances due to their exponential time complexity.

92

You might also like