PUT Sol
PUT Sol
Tail recursion is defined as a recursive function in which the recursive call is the last statement
that is executed by the function. So basically nothing is left to execute after the recursion call.
EXAMPLE
Recursion is applied to problems (situations) where you can break it up (reduce it) into smaller parts,
and each part(s) looks similar to the original problem. Good examples of where things that contain
smaller parts similar to itself are: tree structure (a branch is like a tree) lists (part of a list is still a list)
Comparison Table:
Q1 d)Convert the infix expression (a+b) *(c-d) /e*f to postfix. Give the
answer without any spaces
0((
1A(A
2+(+A
3B(+AB
4)AB+
5**AB+
6(*(AB+
7C*(AB+C
8-*(-AB+C
9D*(-AB+CD
10 ) * A B + C D -
11 / / A B + C D - *
12 E / A B + C D - * E
13 * * A B + C D - * E /
14 F * A B + C D - * E / F
15 A B + C D - * E / F *
Section B
Q2 a)i) Explain asymptotic notation . Define big-oh notation and find the
complexity of the following recursive function T(n)= 4T(n/2) +n logn.
Asymptotic Notation
Big-O notation represents the worst-case time complexity of an algorithm. It provides an upper
bound on the execution time in terms of input size n.
Mathematical Definition:
T(n)≤c⋅f(n)for all n≥n0T(n) \leq c \cdot f(n) \quad \text{for all } n \geq
n₀T(n)≤c⋅f(n)for all n≥n0
where:
Q2 a)ii) Write an algorithm to insert a node at the end in a circular linked list
Insertion at the end in circular linked list
Algorithm
o Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 1
[END OF IF]
o Step 2: SET NEW_NODE = PTR
o Step 3: SET PTR = PTR -> NEXT
o Step 4: SET NEW_NODE -> DATA = VAL
o Step 5: SET NEW_NODE -> NEXT = HEAD
o Step 6: SET TEMP = HEAD
o Step 7: Repeat Step 8 while TEMP -> NEXT != HEAD
o Step 8: SET TEMP = TEMP -> NEXT
[END OF LOOP]
START
IF disk == 1, THEN
Step 3 END IF
END Procedure
STOP
The Tower of Hanoi is a mathematical puzzle involving three pegs (A, B, and C) and multiple
discs of different sizes. The objective is to move all the discs from the source peg (A) to the
destination peg (C) using an auxiliary peg (B) while following these rules:
Recursive Algorithm
For n discs:
24−1=152^4 - 1 = 1524−1=15
Initial Configuration
A: 4 3 2 1 (Source)
B: - (Auxiliary)
C: - (Destination)
Moves
Final Configuration
A: - (Source)
B: 4 3 2 1 (Destination)
C: - (Auxiliary)
A heap is a complete binary tree, and the binary tree is a tree in which the node can have
the utmost two children. A complete binary tree is a binary tree in which all the levels except
the last level, i.e., leaf node, should be completely filled, and all the nodes should be left-
justified.
Algorithm
1. HeapSort(arr)
2. BuildMaxHeap(arr)
3. for i = length(arr) to 2
4. swap arr[1] with arr[i]
5. heap_size[arr] = heap_size[arr] ? 1
6. MaxHeapify(arr,1)
7. End
Q2 c)ii) Write an algorithm of merge sort
[a b f g]
No splitting is needed.
[ a b f g k ]
[ f ]
/ \
[ a b d ] [ g k ]
Step 4: Insert h, m, j, e
j moves up
Left child becomes [ g h ]
Right child becomes [ k m ]
[ f j ]
/ | \
[ a b d e ] [ g h ] [ k m ]
Step 5: Insert s, i, r, x
Since [ k m r s x ] overflows:
r moves up.
Left child [ k m ]
Right child [ s x ]
[ f j r ]
/ | | \
[ a b d e ] [ g h i ] [ k m ] [ s x ]
Step 6: Insert c, l, n, t, u, p
Splitting [ a b c d e ] at c:
c moves up.
Left [ a b ]
Right [ d e ]
j moves up.
New root: [ j ]
Left child [ c f ]
Right child [ r ]
[ j ]
/ \
[ c f ] [ r ]
/ | \ / \
[ a b ] [ d e ] [ g h i ] [ k l m n p ] [ s t u x ]
[ j ]
/ \
[ c f ] [ r ]
/ | \ / \
[ a b ] [ d e ] [ g h i ] [ k l m n p ] [ s t u x ]
Q2 d) ii) if the Inoder sequence of a binary tree is B,I,D,A,CG,E,H,F and Postorder sequence is
I,D,B,G,C,H,F,E,A .
Q2 e)i) Given Graph Representation
FromTo Weight
1 → 23
1 → 45
2 → 12
2 → 44
3→ 2 1
4 → 32
1 2 3 4
1 0 3 INF 5
2 2 0 INF 4
3 INF 1 0 INF
4 INF INF 2 0
1 2 3 4
1 0 3 INF 5
2 2 0 INF 4
3 INF 1 0 INF
4 INF INF 2 0
1 2 3 4
1 0 3 INF 5
2 2 0 INF 4
3 3 1 0 5
4 INF INF 2 0
1 2 3 4
1 0 3 INF 5
2 2 0 INF 4
3 3 1 0 5
4 5 3 2 0
1 2 3 4
1 0 3 7 5
2 2 0 6 4
3 3 1 0 5
4 5 3 2 0
1 2 3 4
1 0 3 7 5
2 2 0 6 4
3 3 1 0 5
4 5 3 2 0
Interpretation:
The shortest path from vertex 1 to vertex 3 is 7.
The shortest path from vertex 2 to vertex 3 is 6.
The shortest path from vertex 4 to vertex 1 is 5.
Data
Uses Stack (or recursion). Uses Queue.
Structure
Time
O(V + E) O(V + E)
Complexity
Path Does not guarantee the shortest Guarantees the shortest path in
Finding path in unweighted graphs. an unweighted graph.
Evidence Analysis
1.
DFS Characteristics:
2.
Explores as far as possible along each branch before backtracking
Path: 1→2→4→8→5→7→9→6→3
3.
BFS Characteristics:
4.
Explores all vertices at the present depth before moving to vertices at the next depth level
Path: 1→2→3→5→4→6→7→8→9
1.
BFS Tree for the Given Graph:
/|\
2 3 5
/ /\
4 6 7
| /\
8 9 8
Q3 a) (i) Find the Length of Each Dimension and Number of Elements in X and Y
X(-2:2, 2:22) → The first dimension ranges from -2 to 2, and the second dimension from 2 to
22.
Y(1:8, -5:5) → The first dimension ranges from 1 to 8, and the second from -5 to 5.
1.
For X:
2.
3.
For Y:
4.
o First dimension: 8 - 1 + 1 = 8
o Second dimension: 5 - (-5) + 1 = 11
o Total Elements in Y = 8 × 11 = 88
Given:
where:
Substituting Values
Offset=(2−1)×11+(2−(−5))\text{Offset} = (2 - 1) \times 11 + (2 - (-
5))Offset=(2−1)×11+(2−(−5)) =(1×11)+(2+5)=11+7=18= (1 \times 11) + (2 + 5) =
11 + 7 = 18=(1×11)+(2+5)=11+7=18 Address=400+(18×4)=400+72=472\text{Address}
= 400 + (18 \times 4) = 400 + 72 = 472Address=400+(18×4)=400+72=472
Coefficient Exponent
4 3
3 2
5 1
6 0
printf("\n");
}
int main() {
printList(head);
return 0;
}
We need to convert this to Reverse Polish Notation (Postfix Notation) using a stack-based
algorithm.
Step-by-Step Conversion
1. Identify Operators and Operands
1. Operators: +, -, ^, √
2. Operands: b, 4, a, c
2. Follow Operator Precedence
1. b^2 → b 2 ^
2. 4ac → 4 a * c *
3. b^2 - 4ac → b 2 ^ 4 a * c * -
4. √(b^2 - 4ac) → b 2 ^ 4 a * c * - √
5. -b → b -
6. -b + √(b^2 - 4ac) → b - b 2 ^ 4 a * c * - √ +
b−b24a∗c∗−√+b - b 2 ^ 4 a * c * - √ +b−b24a∗c∗−√+
Implementation Idea:
1. Push Operation:
o push1(value): Insert at top1++.
o push2(value): Insert at top2--.
o Check if top1 and top2 cross to prevent overflow.
2. Pop Operation:
C Code Implementation:
struct TwoStacks {
int arr[MAX];
};
ts->top1 = -1;
ts->top2 = MAX;
ts->arr[++ts->top1] = val;
} else {
printf("Stack Overflow\n");
}
// Push to Stack 2void push2(struct TwoStacks* ts, int val) {
ts->arr[--ts->top2] = val;
} else {
printf("Stack Overflow\n");
if (ts->top1 >= 0) {
return ts->arr[ts->top1--];
} else {
printf("Stack Underflow\n");
return -1;
return ts->arr[ts->top2++];
} else {
printf("Stack Underflow\n");
return -1;
}
// Main functionint main() {
init(&ts);
push1(&ts, 10);
push2(&ts, 20);
push1(&ts, 30);
push2(&ts, 40);
return 0;
int top;
char arr[MAX];
};
s->arr[++s->top] = ch;
if (s->top >= 0) {
return s->arr[s->top--];
return '\0';
struct Stack s;
init(&s);
int n = strlen(str);
push(&s, str[i]);
}
// Pop characters to get reversed string
str[i] = pop(&s);
int main() {
reverseString(str);
return 0;
Best Case (O(n log n)) → When pivot divides the array equally.
Average Case (O(n log n)) → Most cases, balanced partitioning.
Worst Case (O(n²)) → If the pivot is always the smallest or largest element.
Q5b) Binary Search is an efficient search algorithm for sorted arrays that follows:
#include <stdio.h>
// Binary Search functionint binarySearch(int arr[], int left, int right, int
key) {
if (arr[mid] == key)
return mid;
if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
int arr[] = {2, 3, 5, 8, 13, 13, 15, 19, 28, 62}; // Sorted array
if (result != -1)
else
return 0;
3 \
1. Insert 71 → Root.
2. Insert 41 → Left of 71.
3. Insert 91 → Right of 71.
4. Insert 56 → Right of 41.
5. Insert 60 → Right of 56.
6. Insert 30 → Left of 41.
7. Insert 40 → Right of 30.
8. Insert 80 → Left of 91.
9. Insert 50 → Left of 56 (AVL Rotation needed to balance).
10. Insert 55 → Right of 50 (AVL Rotation needed).
56
/ \
41 71
/ \ / \
30 50 60 91
\ \ /
40 55 80
a 000
b 001
c 01
d 10
e 110
f 111
Start Point Starts from any vertex. Starts with the smallest edge.
Works well for dense graphs Works well for sparse graphs
Graph Type
(many edges). (fewer edges).
We need to find the shortest path from vertex 1 to all other vertices using Dijkstra's
Algorithm.
Step 1: Given Graph Representation
Vertices: {1, 2, 3, 4, 5, 6}
Edge Weight
1 → 29
1 → 34
2 → 4 12
2 → 55
3 → 24
3 → 5 13
4 → 62
5 → 43
5 → 6 15
1 0 -
2 ∞ -
3 ∞ -
4 ∞ -
5 ∞ -
Vertex Distance from 1 Previous Node
6 ∞ -
1.
2.
1. Distance to 2 = 9
2. Distance to 3 = 4
3. Updated Table:
1 0 -
2 9 1
3 4 1
4 ∞ -
5 ∞ -
6 ∞ -
4.
3.
4.
1 0 -
2 8 3
3 4 1
4 ∞ -
5 17 3
6 ∞ -
4.
5.
6.
1. Distance to 4 via 2 = 8 + 12 = 20
2. Distance to 5 via 2 = 8 + 5 = 13 (Less than 17 → Update)
3. Updated Table:
1 0 -
2 8 3
3 4 1
4 20 2
5 13 2
6 ∞ -
4.
7.
8.
1 0 -
2 8 3
3 4 1
4 16 5
5 13 2
6 28 5
4.
9.
10.
1 0 -
2 8 3
Vertex Distance Previous Node
3 4 1
4 16 5
5 13 2
6 18 4
3.
11.
12.
1 0 -
2 8 1 → 3 → 2
3 4 1 → 3
4 16 1 → 3 → 2 → 5 → 4
5 13 1 → 3 → 2 → 5
6 18 1 → 3 → 2 → 5 → 4 → 6
Final Answer
1. Shortest paths from vertex 1 to all vertices:
1. Vertex 2: 1 → 3 → 2 (Cost: 8)
2. Vertex 3: 1 → 3 (Cost: 4)
3. Vertex 4: 1 → 3 → 2 → 5 → 4 (Cost: 16)
4. Vertex 5: 1 → 3 → 2 → 5 (Cost: 13)
5. Vertex 6: 1 → 3 → 2 → 5 → 4 → 6 (Cost: 18)