Algorithm Solved Answers
Algorithm Solved Answers
ENGINEERING
CS3401 – Algorithms
Regulation 2021
Fourth semester
Note: This pdf contains answers for only specified problematic questions
1
November / December 2023 Question paper
Part-B
11(a). write the asymptotic notations used for best case, average case and
worst case analysis of algorithms. Also write the algorithm of finding
maximum element of an array and perform best, worst and average case
complexity with appropriate order notations.
Solution:
Algorithm to find maximum element of an array
Step 1: start
Step 2: Input: an array x and n is the total number of elements in the array
Step 3: Sort the elements in the ascending order of the array
Step 4: returns the last element in the sorted array.
Step 5: stop
Complexity:
Since the array is already sorted in order, we can get the largest element at
the last position on the sorting order of the array.
Time Complexity: Since we are not traversing the whole array, its
time complexity is O(1).
Space complexity: No space is utilized; hence space complexity is O(1).
Step 1: shift 1
0 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 1 1 1 0
1 1 1
2
Step 2: shift 2
0 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 1 1 1 0
1 1 1
Pattern is not matched.
Step 3: shift 3
0 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 1 1 1 0
1 1 1
Pattern matched.
Step 4: shift 4
0 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 1 1 1 0
1 1 1
Step 5: shift 5
0 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 1 1 1 0
1 1 1
Pattern is not matched.
3
Step 6: shift 6
0 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 1 1 1 0
Step 7: shift 7
0 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 1 1 1 0
Pattern matched. 1 1 1
Step 8: shift 8
0 1 2 3 4 5 6 7 8 9
1 0 1 1 1 0 1 1 1 0
1 1 1
Pattern is not matched.
Conclusion: the pattern is matched at shift 3 and 7.
Part – C
16(a). Write and explain the Dijikstra’s algorithm. Find the shortest path for the
following graph using Dijikstra’s algorithm.
5
b d 2
2
4
Step 1: choose a path from s
S -> a = 1 < ∞ = 1 is the chosen path
S -> b = 5< ∞ = 5
2
a c
1 1 ∞
1
S e
0 1 ∞
2 3
5
b d
5 ∞
2
2
b
2
a
c
1 1
2
1
S
0 e
2 1 ∞
3
5
b d
3 1
2
2
b
5
Step 3: choose a path from d
d -> e = 1+2= 3 < ∞
2
a
c
1 1
2
1
S
0
e
1 3
3
5
b d
3 1
2
2
---------------------------------------------------------------------------------------------------------
b
1 5 $40
2 7 $35
3 2 $18
4 4 $4
5 5 $10
6 1 $2
Solution:
ub = v + (W-w) (p(i+1) /w(i+1))
Step 1:
v=0, w=0, W=10, (p(i+1) /w(i+1))= (p1/w1)=18/2 = 9
ub=0+(15-0)(9)
ub=135
Step 2: select item 1
6
p=18,w=2,W=15, (p(i+1) /w(i+1))=40/5=8
ub=18+(15-2)(8)
ub=122
Step 3: Do not select item 1
p=0,w=0,W=15, (p2/w2)=(40/5)=8
ub=0+(15-0)(8)
ub=120
Step 4: select item 2
Here we assume that item 1 is already selected and now we are selecting
item 2.
p=p1+p2=58, w=w1+w2=7, p3/w3=35/7=5
ub=58+(15-7)(5)
ub=98
Step 5: Do not select item 2
Hence we assume that item 1 is selected but item 2 is not selected.
p=18, w=2, p3/w3=35/7=5
ub=18+(15-2)(5)
ub=83
Step 6: select item 3
Hence we assume that item 1, item 2 and item 3 are selected.
ub=93+(15-4)(2)
ub=95
Step 7: Do not select item 3
Hence we assume that item 1, item 2 are already selected.
but do not select item 3.
7
p=p1+p2=58, w= w1+w2=7 , p4/w4=10/5=2
ub=58+(15-7)(2)
ub=74
Step 8: select item 4
Hence we addume item 1, item 2 and item 3 are selected.
p= p1+p2+p3+p4=103, w= w1+w2+w3+w4=19
as w>W i.e. 19>15
This is not feasible choice.
Step 9: Do not select item 4
Here we assume item 1, item 2 and item 3 are already selected.
p=p1+p2+p3=93, w=w1+w2+w3=14,p5/w5=2/1=2
ub=93+(15-4)(2)
ub=95
Step 10: select item 5
Here we assume that item 1, item 2, item 3 are already selected.
The item 4 is not selected.
8
p=0
w=0
ub=135
w/o item 1
Item 1
p=18 p=0
w=2 w=0
ub=122 ub=120
w/o item 2
Item 2
p=58 p=18
w=2
w=7
ub=83
ub=98
w/0 item 3
Item 3
p=93 p=58
w=14 w=7
ub=95 ub=74
Item 4 w/o item 4
p=103
p=93
w=19
w=14
ub not feasible ub=95
Item 5
p=95
w=15
ub=95
solution
9
November / December 2024
12 (a). Write the Kruskal’s algorithm and to find the minimum spanning tree for the
following graph.
Weight Path
1 (F,E)
2 (A,F)
3 (A,B)
4 (D,I)
5 (E,H)
6 (E,G)
7 (F,G)
8 (C,D)
9 (I,J)
10 (I,E)
11 (D,E)
12 (I,H)
13 (H,J)
15 (G,H)
16 (B,D)
17 (B,C)
18 (C,I)
10
The Minimum spanning tree is as follows,
G
2
6
3 F
B
E 5
D H
8 4 10
C I
J
9
--------------------------------------------------------------------------------------------------------
The above graph is the MST .The edges 7,11,12,13,15,16,17,18 cannot be
done because it forms a loop.
Therefore the total weight =1+2+3+4+5+6+8+9+10=48.
12(b). In the given graph, the vertex represents the city and edge represents
the cost between the two vertices. Apply Dijikstra’s shortest algorithm and find
the optimal cost of reach the destination. Also determine the worst case time
complexity of the algorithm.
11
Solution:
Step 1:
Choose a path from s
S -> t = 0+10 = 10<∞ = 10
S-> y = 0+5 = 5 <∞ = 5 is the chosen path
t 1 X
10 ∞
10
9
2 3
6 4
s
0
5
y Z
5 ∞
2
Step 2:
Choose a path from y
y-> t = 5+3= 8<10 = 8
y->x= 5+9 =14<∞ = 14
y-> z= 5+2=7<∞ = 7 is the chosen shortest path
t 1 X
8 14
10
9
2 3
6 4
s
0
5
y Z
5 7
12 2
conclusion : Thus the shortest path is s-> y -> z.
The optimal cost to reach the destination is 5+2=7.
The worst case time complexity of the algorithm is O(n2)
13(a). Apply Merge sort algorithm to sort the given set of numbers
(40,25,69,65,31,53,86,24,55,57,19,21,16) and compute the worst case,
average case and best case time complexity of the algorithm.
Solution :
0 1 2 3 4 5 6 7 8 9 10 11 12
40 25 69 65 31 53 86 24 55 57 19 21 16
0 1 2 3 4 5 0 1 2 3 4 5 6
40 25 69 65 31 53 86 24 55 57 19 21 16
0 1 2 0 1 2
40 25 69 65 31 53
0 1 0 1
65 31 53
40 25 69
40 25 69 65 31 53
25 40 69 31 53 65
25 31 40 53 65 69
13
0 1 2 3 4 5 6
86 24 55 57 19 21 16
0 1 2 0 1 2 3
86 24 55 57 19 21 16
0 1
86 24 55
57 19
21 16
24 55
86
57 19
24 55 86
21 16
16 19 21 57
16 19 21 24 55 57 86
25 31 40 53 65 69
16 19 21 24 25 31 40 53 55 57 65 69 86
Time complexity :
Best case Average case Worst case
13(b). Produce Huffman tree for the following data and encode the data
abbcddeef.
14
Character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
Solution:
15
16
14(a) Apply Backtracking approach and determine whether the given graph can
be colored using 4 colors with graph colouring techniques.
1 2
5 6
3 4
7 8 9
10
11 12
Solution:
17
18
14(b). consider the following graph. The vertex represents the city and edge
represents the cost between the two vertices. A salesman starts from node 1,
visit all the cities exactly once and return to the starting node. Justify that the
algorithm that uses optimality principle produces an optimal tour cost to visit
all cities.
Solution:
19
20
Part – C
16(a). Apply Ford Fulkerson algorithm for the following graph and determine
the maximum flow in the graph.
Solution:
Step 1:
The selected path is s – v1 – v3 – t , the maximum flow is 12.
12/12
V V 12/2
12/16 1 3 0
0/4 t
s 0/10 0/7
0/9
0/13 V V 0/4
2 4
0/14
Step 2:
The next selected path is s – v2 – v4 – v3 – t, the maximum flow is 12+7=19.
21
12/12
V V 19/20
12/16 1 3
0/4
0/10 t
s 7/7
0/9
7/13 V V 0/4
2 4
7/14
Step 3:
The next selected path is s – v2 – v4 – t, the maximum flow is 19+4=23
12/12
V V 19/2
12/16 1 3 0
0/4 t
s 0/10 7/7
0/9
11/13 V V 4/4
2 4
11/14
Solution:
22
23
24
April / May 2024
Part – B
11(a). Use substitution method to show that T(n)= 2T (n/2) +n is O(n log(n)).
Solution:
T(n) = 2(2T (n/4)+n/2) + n
T(n) = 4T(n/2)+2n
T(n) = 4(2T (n/8) + (n/4))+2n
= 8T (n/8) + 3n
T(n) = 23T(n/23) + 3n
.
.
.
T(n) = 2kT (n/2k) + kn
T(n) = n.T(n/n) + logn.n
=n. T(1) + n.logn
T(n) = n+n log(n)
T(n) ≈ n log n
Hence in terms of big oh notation,
T(n) = O (n log n)
Hence proved.
11(b). Explain the working of naïve string matching algorithm with
ABCCDDAEFG as the text input and CDD as the search string.
Solution:
Shift 1
25
0 1 2 3 4 5 6 7 8 9
A B C C D D A E F G
C D D
C D D
C D D
C D D
Pattern found.
Shift 5:
26
0 1 2 3 4 5 6 7 8 9
A B C C D D A E F G
C D D
Pattern is not matched.
Shift 6:
0 1 2 3 4 5 6 7 8 9
A B C C D D A E F G
C D D
Pattern is not matched.
Shift 7:
0 1 2 3 4 5 6 7 8 9
A B C C D D A E F G
C D D
Pattern is not matched.
Shift 8:
0 1 2 3 4 5 6 7 8 9
A B C C D D A E F G
C D D
Pattern is not matched.
Conclusion: pattern found at shift 4.
12(a)(1). Write the pseudocode for BFS and DFS traversals on the graph given
below in fig and compare the time and space complexity of the two traversals.
27
1 3
2 4
solution:
DFS:
Step 1:
DFS uses stack implementation so in the stack insert the node 0.
0
Stack visited.
Step 2:
After the insertion of the previous node 0 in the stack it will get into the visited
content. And then insert the new node 1 in the stack.
0
1
Stack visited.
Step 3:
28
After the insertion of the previous node 1 in the stack it will get into the visited
content. And then insert the new node 2 in the stack.
0 1
2
Stack visited.
Step 4:
After the insertion of the previous node 2 in the stack it will get into the visited
content. And then insert the new node 3 in the stack.
0 1 2
4
Stack visited.
Step 5:
After the insertion of the previous node 2 in the stack it will get into the visited
content. And then insert the new node 3 in the stack.
0 1 2 4 3
3
Stack visited.
Step 6:
After the insertion of the previous node 2 in the stack it will get into the visited
content. And then the stack becomes empty.
29
0 1 2 4 3
Stack visited.
Conclusion : DFS = 0 1 2 4 3
BFS:
In the BFS we use queue implementation to represent.
Step 1:
Insert the new node 0 in the queue.
Queue visited.
Step 2:
Insert the new node 1 in queue so the previous node 0 get into the visited site.
1 0
Queue visited.
Step 3:
Insert the new node 3in queue so the previous node 1 get into the visited site.
3 0 1
Queue visited.
30
Step 3:
Insert the new node 2 in queue so the previous node 3 get into the visited site.
2 0 1 3
Queue visited.
Step 4:
Insert the new node 4 in queue so the previous node 2 get into the visited site.
4 0 1 3 2
Queue visited.
Step 5:
There is no new node. the previous node 4 is get into the visited site. And the
queue becomes empty.
0 1 3 2 4
Queue visited.
Conclusion: BFS = 0 1 3 2 4.
12(a)(2). Find the minimum spanning tree of the following graph in the fig
using Kruskal’s algorithm.
8
7
b c d
4 9
11 e
a 1
i 6
7 4
8 10
h f
g
1 2
31
Solution:
Weight Path
1 (h,g)
2 (g,f)
4 (a,b)
6 (i,g)
7 (c,d)
7 (h,i)
8 (b,c)
8 (a,h)
9 (d,e)
10 (f,e)
11 (b,h)
14 (d,f)
e
a
i 6
8 h f
g
1 2
12(b)(1). Given a graph and a source vertex in the graph, find the shortest
paths from the source vertex 0 to all vertices in the given graph.
32
Step 1:
Choose a path from vertex 0,
0 -> 1 = 0+4= 4<∞ = 4 is the chosen path
0 -> 7 = 0+8 = 8 <∞ =8
Vertex 0 1 2 3 4 5 6 7 8
Weight 0 4 ∞ ∞ ∞ ∞ ∞ 8 ∞
Step 2:
Choose a path from vertex 1
1 -> 7 = 4 + 11 = 15 < 8 = 8 is the chosen path
1 -> 2 = 12 < ∞ = 12
Vertex 0 1 2 3 4 5 6 7 8
Weight 0 4 12 ∞ ∞ ∞ ∞ 8 ∞
Step 3:
Choose a path from vertex 7
7->8 = 8+7 = 15 <∞ = 15
7 -> 6 = 8+1 =9<∞ = 9 is the chosen path
Vertex 0 1 2 3 4 5 6 7 8
Weight 0 4 12 ∞ ∞ ∞ 9 8 15
Step 4:
Choose a path from vertex 6
6 -> 8 = 9+6 = 15
6 -> 5 = 9+2 = 11 <∞ = 11 is the chosen path
33
Vertex 0 1 2 3 4 5 6 7 8
Weight 0 4 12 ∞ ∞ 11 9 8 15
Step 5:
Choose a path vertex from 5
5 -> 2=11+4=15>12 = 12 is the chosen path.
5 -> 3=11+14=25<∞ = 25
5 -> 4 =11+10=21<∞ = 21
Vertex 0 1 2 3 4 5 6 7 8
Weight 0 4 12 25 21 11 9 8 15
Step 6:
Choose a path from vertex 2
2-> 8 = 12+2= 14 < 15 = 14 is the chosen path.
2 -> 3= 12+7= 19 < 25 = 19
Vertex 0 1 2 3 4 5 6 7 8
Weight 0 4 12 19 21 11 9 8 14
Step 7:
Choose a path from vertex 8
8 has the visited vertices so backtrack to 2
2 -> 3 = 12+7= 19
Vertex 0 1 2 3 4 5 6 7 8
Weight 0 4 12 19 21 11 9 8 14
Step 8:
34
Choose a path from vertex 3
3 -> 4 = 19+9 = 28 >21 = 21
Vertex 0 1 2 3 4 5 6 7 8
Weight 0 4 12 19 21 11 9 8 14
7
1 2 3
9
4
4
2
0
11 8 4
7 6 5
1 2
12(b)(2). Using Ford Fulkerson algorithm find the maximum possible flow in
the network given below.
Solution:
35
36
13(a). Demonstrate divide and conquer approach for performing quick sort on
the following values.
44,33,11,55,77,90,40,60,99,22,88.
37
13(b). A character coding problem. A data file of 100,000 characters contains
only the characters a – f, with the frequencies indicated as below
a b c d e f
Characters
45 13 12 16 9 5
Frequency
(in thousands)
Show the steps in constructing the final Huffman tree representing the optimal
prefix code.
Solution:
38
14(a). Solve the following subset sum problem using backtracking. Let
S={3,7,9,13,26,41}; d(sum) = 51.
Solution:
Part - c
39
16(a).How many spurious hits does the Rabin Karp matcher encounter in the
text T = 314159265358793 when working modulo q = 11 and looking for the
pattern P = 26. Briefly write about the processing time, worst case running
time of Rabin Karp algorithm.
Solution:
T = 3141592653589793
P = 26
q = 11
Q = P mod q = 26 mod 11 = 4
shift 0:
31 mod 11 = 9 ≠ Q
31 ≠ 26
Shift 1:
14 mod 11 = 3 ≠ Q
14 ≠ 26
Shift 2:
41 mod 11 = 8 ≠ Q
14 ≠ 26
Shift 3:
15 mod 11 = 4 =Q
15 ≠ 26
Shift 4:
59 mod 11 = 4 = Q
59 ≠ 26
Shift 5:
92 mod 11 = 4 = Q
92 ≠ 26
40
Shift 6:
26 mod 11 = 4 = Q
26 = 26
Shift 7:
65 mod 11 = 10 ≠ Q
65 ≠ 26
Shift 8:
53 mod 11 = 9 ≠ Q
52 ≠ 26
Shift 9:
35 mod 11 = 2 ≠ Q
35 ≠ 26
Shift 10:
58 mod 11 = 3 ≠ Q
58 ≠ 26
Shift 11:
89 mod 11 = 1 ≠ Q
89 ≠ 26
Shift 12:
97 mod 11 = 9 ≠ Q
97 ≠ 26
Shift 13:
79 mod 11 = 2 ≠ Q
79 ≠ 26
Shift 14:
93 mod 11 = 5 ≠ Q
41
93 ≠ 26
Conclusion:
Spurious hit: shift = 3,4,5
Valid shift: shift = 6
16(b). Run the Bellman – Ford algorithm on the directed graph of figure below
using vertex s as the source and show the results after each pass of an
algorithm.
Solution:
42