DAA Module 2
DAA Module 2
🌐Website
[Link]
Contact us
✉︎ ktubbabcaupdates@[Link]
✆ 9544136946
Chapter 2
1. Divide
• Break the main problem into smaller subproblems.
Example: In Merge Sort, the array is divided into two halves. In Quick Sort, the array
is divided around a pivot element.
2. Conquer
• Solve each smaller subproblem individually.
33
34 2.1. DIVIDE AND CONQUER
• When a subproblem becomes simple enough, solve it directly without further recursion.
3. Merge
• Combine the solutions of the subproblems to form the final solution.
Example: In Merge Sort, the two sorted halves are merged to form a completely sorted
array.
4. Combining Solutions: The solutions of the subproblems are combined to form the
final answer to the original problem.
• Quick Sort
• Binary Search
Conditions
• The data structure must be sorted.
Conquer:
Compare the key with the middle element and select the half to continue searching.
Merge:
Trivial — no nontrivial merge is required; the result is either the element found or not
found.
high − low
$ %
mid = low + .
2
Compare A[mid] with the key and update low or high accordingly until the key is found or
the interval is empty.
36 2.1. DIVIDE AND CONQUER
Complexity analysis
Let C(n) denote the worst-case number of comparisons on an array of size n.
The recurrence halves n each time. The number k of halvings to reach 1 is k = ⌊log2 n⌋.
Hence
C(n) = ⌊log2 n⌋ + 1.
Thus time complexity is Θ(log n) (worst and average). Space complexity is O(1) for the
iterative version, and O(log n) for the recursive version (recursion stack).
CHAPTER 2. DIVIDE AND CONQUER AND GRAPH ALGORITHMS 37
Pseudocode (iterative)
• Combine: Merge the two sorted halves into a single sorted array.
Worked Example
Sort the array {38, 27, 43, 10}.
38 2.1. DIVIDE AND CONQUER
3. Merge pairs:
• Merge [27, 38] and [10, 43] ⇒ [10, 27, 38, 43].
CHAPTER 2. DIVIDE AND CONQUER AND GRAPH ALGORITHMS 39
Recurrence Relation
Θ(1), n = 1,
T (n) = n
2T + Θ(n), n > 1.
2
nlogb a = nlog2 2 = n.
Complexity Analysis
• Time Complexity:
Advantages
• Stable and guarantees O(n log n) even in worst case.
Disadvantages
Quick Sort is a highly efficient sorting algorithm that follows the Divide and Conquer
approach. It works by selecting a pivot element and partitioning the array such that all
elements less than or equal to the pivot are moved to its left and those greater than the
pivot are moved to its right. The process is recursively applied to both subarrays until the
entire array is sorted.
CHAPTER 2. DIVIDE AND CONQUER AND GRAPH ALGORITHMS 41
Algorithm (Pseudocode)
Step-by-Step Example
Let us sort the array [11, 9, 12, 7, 3] using Quick Sort.
Step 2: Choose Pivot: Last element 3 as pivot. All other values are greater than 3, so
they move to the right. Swap 3 with 11:
Step 3: Sort Right Subarray: Subarray [9, 12, 7, 11] is selected. Choose pivot 11.
42 2.1. DIVIDE AND CONQUER
Step 4: Partition:
Step 5: Sort Left Subarray: Consider subarray [9, 7] to the left of 11. Choose pivot 7.
Recurrence Relation
Let T (n) represent the time required to sort an array of size n. Then:
n
T (n) = 2T + Θ(n)
2
CHAPTER 2. DIVIDE AND CONQUER AND GRAPH ALGORITHMS 43
By Master’s Theorem:
Average Case:
T (n) = O(n log n)
Complexity Analysis
Advantages
• Efficient for large datasets with average-case time complexity of O(n log n).
Disadvantages
• Worst-case complexity of O(n2 ) if pivot selection is poor.
called links). Graphs are used to model pairwise relations between objects, making them a
powerful tool for representing and analyzing complex systems in various fields.
Definition of a Graph
A graph G can be defined as an ordered pair:
G = (V, E)
where:
• V is a set of vertices.
Undirected Graph
An undirected graph is a type of graph in which the edges have no direction. This means
that the relationship between any pair of connected vertices is mutual. In an undirected
graph, the edge (u, v) is identical to the edge (v, u).
Example:
V = {A, B, C, D}, E = {{A, B}, {A, C}, {B, D}, {C, D}}
CHAPTER 2. DIVIDE AND CONQUER AND GRAPH ALGORITHMS 45
A B
C D
V = {A, B, C, D}, E = {(A, B), (A, C), (B, D), (C, D)}
A B
C D
Weighted Graph
A weighted graph assigns a weight to each edge representing cost, distance, or capacity.
V = {A, B, C, D}, E = {(A, B, 3), (A, C, 5), (B, D, 2), (C, D, 1)}
3
A B
5 2
C D
1
46 2.3. GRAPH TRAVERSAL
• Searching a node
• Detecting cycles
Step-by-Step Explanation
1. Start at a vertex v. 2. Mark it as visited. 3. For each unvisited neighbor, recursively
perform DFS. 4. Backtrack when no unvisited neighbors remain. 5. Repeat until all vertices
are visited.
DFS Example
Graph:
A
B C
D E
BFS Algorithm
BFS(graph, start):
create a visited set
create a queue and enqueue start
mark start as visited
48 2.3. GRAPH TRAVERSAL
Step-by-Step Explanation
1. Start at a vertex v. 2. Mark it visited and enqueue it. 3. While queue is not empty: -
Dequeue a vertex u - Visit all unvisited neighbors, mark them, and enqueue them 4. Repeat
until all vertices are visited
BFS Example
Graph:
A
B C
D E
5. Summary
• DFS uses recursion or stack → explores deep paths first
• Both DFS and BFS have time complexity O(V + E) for adjacency list representation
CHAPTER 2. DIVIDE AND CONQUER AND GRAPH ALGORITHMS 49
• Traversal is fundamental for searching, cycle detection, path finding, and connected
components
2. Initialize a stack and a visited array of size n with all values set to false.
4. Once all vertices are visited, pop elements from the stack and append them to the
output list.
2.5 Pseudocode
TopologicalSort(Graph G):
stack = empty
visited = [False]*n
50 2.6. EXAMPLE AND VISUALIZATION
topological_order = []
while stack is not empty:
topological_order.append([Link]())
return topological_order
1 2 3
4 5
• Visit 1, then 2.
1, 4, 5, 2, 3
2.7 Algorithm
Dijkstra’s algorithm follows a greedy approach. It always picks the unvisited vertex with
the smallest known distance to the source and marks it as visited. Then, it updates the
distances of its unvisited neighbors. This process continues until all vertices are visited.
The steps of the algorithm are as follows:
1. Initialization:
2. Iteration:
3. Mark as Visited: When we are done considering all of the unvisited neighbors of the
current node, mark the current node as visited and remove it from the unvisited set.
A visited node will not be checked again.
4. Select Next Node: If the destination node has been marked visited (when planning
a route between two specific nodes) or if the smallest tentative distance among the
nodes in the unvisited set is infinity (when planning a complete traversal), then stop.
The algorithm has finished. Otherwise, select the unvisited node that is marked with
the smallest tentative distance, set it as the new "current node", and go back to step
2.
B
4 5
A 1 D
2
2 8
C E
10
We will use a table to keep track of the distances and the previous node in the shortest
path.
Initialization
• Current node: A
CHAPTER 2. DIVIDE AND CONQUER AND GRAPH ALGORITHMS 53
Iteration 1
• Neighbors of A: B, C
• Mark A as visited.
Iteration 2
• Neighbors of C: B, D, E
• Mark C as visited.
Iteration 3
• Neighbors of B: D
• Mark B as visited.
Iteration 4
• Neighbors of D: E
• Mark D as visited.
Iteration 5
• No unvisited neighbors.
• Mark E as visited.
• A to A: 0
• A to C: 2 (Path: A -> C)
2.9 Visualization
The final shortest path tree can be visualized as follows. The red edges indicate the shortest
paths from the source node A to all other nodes.
CHAPTER 2. DIVIDE AND CONQUER AND GRAPH ALGORITHMS 55
A D
C E
2.11 Properties
• It is a binary search tree.
• The heights of the two child subtrees of any node differ by at most one.
• A node’s balance factor can be -1, 0, or 1. If it becomes something else, the tree needs
to be rebalanced.
2.12 Rotations
When an insertion or deletion causes the tree to become unbalanced, we perform rotations
to restore the AVL property. There are four types of rotations:
56 2.12. ROTATIONS
C B
Right Rotate(C)
B A C
A B
Left Rotate(A)
B A C
C C B
Unbalanced
B Tree A Balanced Tree
CHAPTER 2. DIVIDE AND CONQUER AND GRAPH ALGORITHMS 57
A A B
Unbalanced
B Tree C Balanced Tree
• Insert 20: 20 is greater than 10, so it becomes the right child. The tree is balanced.
• Insert 30: 30 is greater than 20. The tree becomes unbalanced at node 10 (balance
factor = -2). This is an RR case. We perform a left rotation on 10.
10 20
Left Rotate(10)
20 10 30
30
• Insert 40: Inserted as the right child of 30. The tree remains balanced.
• Insert 50: Inserted as the right child of 40. The tree becomes unbalanced at node 30
(balance factor = -2). This is another RR case. We perform a left rotation on 30. The
final tree after this step and inserting 40 is:
58 2.13. WORKED EXAMPLE: INSERTION
20
10 40
30 50
• Insert 25: Inserted as the left child of 30. Now, the tree is unbalanced at node 20
(balance factor = -2). The path is 20(right) -> 40(left) -> 30(right) -> 25. This is an
RL case. We first perform a right rotation on 40, and then a left rotation on 20.
30
20 40
10 25 50
Final Tree after inserting 25: