COL106: Data Structures and Algorithms: Ragesh Jaiswal, IIT Delhi
COL106: Data Structures and Algorithms: Ragesh Jaiswal, IIT Delhi
COL106: Data Structures and Algorithms: Ragesh Jaiswal, IIT Delhi
Introduction
Introduction
Digression: Binary Search Recursive Functions Divide and Conquer
Problem
Multiplying two n-bit numbers: Given two n-bit numbers, A and B,
Design an algorithm to output A B.
Algorithm
Karatsuba(A, B)
- If (|A| = |B| = 1) return(A B)
- Split A into AL and AR
- Split B into BL and BR
- P Karatsuba(AL , BL )
- Q Karatsuba(AR , BR )
- R Karatsuba(AL + AR , BL + BR )
- return(Combine(P, Q, R))
Recurrence relation: T (n) 3 T (n/2) + cn; T (1) c.
What is the solution of this recurrence relation?
T (n) O(nlog2 3 )
Ragesh Jaiswal, IIT Delhi
Introduction
Digression: Binary search Recursive functions Merge Sort
Problem
Given an array of unsorted integers, output a sorted array.
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
How do we argue correctness?
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
How do we argue correctness?
Proof of correctness of Divide and Conquer algorithms are usually
by induction.
Base case: This corresponds to the base cases of the algorithm.
For the MergeSort, the base case is that the algorithm correctly
sorts arrays of size 1.
Inductive step: In general, this corresponds to correctly combining
the solutions of smaller subproblems. For MergeSort, this is just
proving that the Merge routine works correctly. This may again be
done using induction and is left as an exercise.
Ragesh Jaiswal, IIT Delhi
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
What is the running time of MergeSort?
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Recurrence relation for running time: T (n) 2 T (n/2) + cn for
all n 2 and T (1) c for some constant c.
Obtain the solution to the above recurrence relation by unrolling
the recursion.
Introduction
Digression: Binary search Recursive functions Merge Sort
Algorithm
MergeSort(A)
- If (|A| = 1) return(A)
- Divide A into two equal parts AL and AR
- BL MergeSort(AL )
- BR MergeSort(AR )
- B Merge(BL , BR )
- return(B)
Recurrence relation for running time: T (n) 2 T (n/2) + cn for
all n 2 and T (1) c for some constant c.
Obtain the solution to the above recurrence relation by unrolling
the recursion. T (n) = O(n log n)
Introduction
Digression: Binary Search Recursive Functions Solving Recurrences
Let our guess be T (n) cn log n for all n 2. We will now prove
this by induction
Base case: T (n) cn log n when n = 2 since we are given that
T (2) c.
Inductive step: Suppose the bound holds for n = 2, ..., k 1, we
will show that the bound also holds for n = k.
We know T (k) 2T (k/2) + ck.
So, using induction hypothesis, we get:
T (k) 2c(k/2) log(k/2) + ck = ck log k.
Ragesh Jaiswal, IIT Delhi
Data Structures
Data Structures
Introduction
Data Structures
Introduction
Data Structures
Queue and Stack
Data Structures
Queue and Stack
Problem
Automate the queueing service at a Doctors office. As people
come to the Doctors office, they enter their names into the
computer. The Doctor asks the computer to return the name of
the next person as per the order in which they turned up.
The data structure that you design needs to support only the
following two operations:
1
2
Data Structures
Queue and Stack
Problem
Automate the queueing service at a Doctors office. As people
come to the Doctors office, they enter their names into the
computer. The Doctor asks the computer to return the name of
the next person as per the order in which they turned up.
The data structure that you design needs to support only the
following two operations:
1
2
Data Structures
Queue and Stack
Data Structures
Queue and Stack
Data Structures
Queue and Stack
Data Structures
Queue and Stack
Data Structures
Queue and Stack
Data Structures
Queue and Stack
Data Structures
Queue and Stack
Data Structures
Queue and Stack
Data Structures
Queue and Stack
End