cheatsheet
cheatsheet
Pseudocode:
Merge Sort:
MERGE_SORT(A, left, right)
1. if left < right:
2. mid = (left + right) / 2
3. MERGE_SORT(A, left, mid)
4. MERGE_SORT(A, mid + 1, right)
5. MERGE(A, left, mid, right)
Merge Function:
MERGE(A, left, mid, right)
1. Create temporary arrays for left and right halves.
2. Compare elements and merge them back into A.
Complexity Analysis:
Merge Sort follows the recurrence:
Base Case (Initial Call)
T (n) = 2T (n/2) + cn
Recursive Step
T (n/2) = 2T (n/4) + c(n/2)
Substituting in T (n):
1
2. [3] With the help of a diagram and proper explanation prove
that a complete binary tree has depth of log n and width of n/2.
Proof of Depth: log n
In a complete binary tree, each level doubles the number of nodes.
- The number of nodes at level h is:
2h
n = 1 + 2 + 4 + · · · + 2h = 2h+1 − 1
− > 2h+1 = n + 1
− > h + 1 = log2 (n + 1)
− > h ≈ log2 n
Thus, the depth of a complete binary tree is O(log n).
Proof of Width: n2
The width of a binary tree is the number of nodes at the level with the most
nodes.
- In a complete binary tree, the last level contains the most nodes. - The
last level is at depth h, and the number of nodes at this level is:
2h
- Since we know that:
n
2h+1 ≈ n− > 2h ≈
2
n
Thus, the maximum width of a complete binary tree is 2.
2
3. Find the smallest among A[i], A[left], and A[right]
4. If A[i] is not the smallest:
• Swap A[i] with the smaller child
• Recursively call Min-Heapify(A, smallest)
O(log n)
Complexity Analysis:
The recurrence relation for this algorithm is:
3
Since T (1) = O(1), we get:
T (n) = O(n)
This means the algorithm runs in linear time, which is optimal for finding
the minimum element in an unsorted array.
5. [5] Illustrate the operation of Radix Sort on the following list of numbers:
329, 457, 657, 839, 436, 720, 355.
Hint: Radix Sort uses a stable sort to sort the array on digit i. O(kn) =
O(n log n).
T (n) = Θ(n2 )
(b) T (n) = 3T (n/4) + 1
- a = 3, b = 4, and f (n) = O(1).
T (n) = Θ(n0.792 )
4
1. [6] Describe and analyze naı̈ve pattern search algorithm.
The Naı̈ve Pattern Search Algorithm (also called the Brute Force Algorithm)
is a straightforward method for finding occurrences of a pattern P of length m
in a text T of length n. It works by checking for P at every possible position in
T.
Algorithm Steps:
1. Start with the first character of T .
2. Compare each character of P with the corresponding character in T .
for i = 0 to n - m:
match = true
for j = 0 to m - 1:
if T[i + j] P[j]:
match = false
break
if match:
print "Pattern found at index", i
2. [3+3] Perform (a) Depth First and (b) Breadth First Traversals
on the given graph.
(a) Depth First Search (DFS) Traversal: Depth First Search (DFS)
explores as far as possible along each branch before backtracking. It uses a stack
(or recursion) to track nodes.
Algorithm Steps:
1. Start at Node 1 (mark as visited).
5
2. Move to Node 4 (first unvisited neighbor).
3. Move to Node 2 (next unvisited neighbor of 4).
4. Move to Node 5 (next unvisited neighbor of 2).
1→4→2→5→3
(b) Breadth First Search (BFS) Traversal: Breadth First Search (BFS)
explores all neighbors of a node before moving deeper. It uses a queue to track
nodes.
Algorithm Steps:
1. Start at Node 1, enqueue: [1].
1→4→2→5→3
6
4. Repeat until all nodes are visited or the destination node d has the shortest
path determined.
Step-by-Step Execution:
Iteration Visited Node Distance Updates
Initialization - d(a) = 0, d(b) = ∞, d(c) = ∞, d(d) = ∞
1 a d(b) = 1, d(c) = 4
2 b d(c) = min(4, 1 + 8) = 4, d(d) = 1 + 10 = 11
3 c d(d) = min(11, 4 + 3) = 7
4 d Shortest path to d is finalized: d(d) = 7
Shortest Path from a to d:
a → c → d (Total cost = 7)
Kruskal’s Algorithm
Kruskal’s algorithm builds the Minimum Spanning Tree (MST) by sorting all
edges by weight and selecting the smallest edges while avoiding cycles.
Steps:
1. Sort all edges in increasing order of weight.
2. Initialize an empty MST.
3. Add edges one by one, ensuring no cycles are formed.
4. Stop when MST contains (V − 1) edges.
Sorted Edges:
7
• Pick edge (1,2) → Forms a cycle, discard it.
• Pick edge (0,2) → Not needed.
Final MST using Kruskal’s Algorithm:
Prim’s Algorithm
Prim’s algorithm starts from a node and grows the MST by selecting the smallest
edge that connects an unvisited node.
Steps:
• Start at node 0.
• Pick edge (0,1) [weight 1].
• Pick edge (1,4) [weight 1].
Conclusion: Both Kruskal’s and Prim’s algorithms yield the same MST
with total weight **4**.
5. [5] Suppose you run a day care for an office building and there
are seven children A, B, C, D, E, F. You need to assign a locker where
each child’s parent can put the child’s food. The children come and
leave so they are not all there at the same time. You have 1 hour
time slots starting 7:00 a.m. to 9:00 a.m. A star in the table means a
child is present at that time. What is the minimum number of lockers
necessary? Show how you would assign the lockers.
Solution:
We determine the minimum number of lockers by analyzing overlapping time
slots.
Children present at each time:
8
• 7:00 AM: A, C, E
• 8:00 AM: B, C
• 9:00 AM: D, E, F
Locker Assignment:
• 7:00 AM: A → L1, C → L2, E → L3
• 8:00 AM: B → L1 (reuses A’s locker), C → L2 (same locker)