Algorithm Notes
Algorithm Notes
Techniques
Dr M Muzammal
Divide and Conquer
The most-well known algorithm design strategy:
Divide instance of problem into two or more smaller
instances
Examples:
T(n) = 4T(n/2) + n T(n) ?
Let’s Solve it
Analysis:
Divide and Conquer Algorithm
Examples
T(n) = 9T(n/3) + n
T(n) = 8T(n/2) + n2
Divide and Conquer:
Merge Sort
Divide: Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
Divide and Conquer:
Analysis of Merge Sort
Divide: Computing the middle takes (1)
– copying two n/2 elements arrays take 2*n/2 = (n) time
Conquer: Solving 2 sub-problems takes 2T(n/2)
Combine: Merging n elements takes (n)
A[i]p A[i]p
Divide and Conquer:
Quick Sort
Exchange the pivot with the last element in the first (i.e., )
subarray — the pivot is now in its final position
Sort the two subarrays recursively
Efficiency:
Best case: split in the middle — Θ(n log n)
Worst case: sorted array! — Θ(n2)
Average case: random arrays — Θ(n log n)
Divide and Conquer:
Binary Tree Traversal
Graph algorithms: binary tree traversals
Inorder traversal:
traverse left subtree of current vertex
visit current vertex
traverse right subtree of current vertex
Preorder traversal similar, but visit current vertex first
Postorder traversal similar, but visit current vertex last
All three take O(n) time, where n is number of nodes in tree
Note difference from searching in a binary tree
Divide and Conquer:
Closest Pair Problem
Recall the problem: Given n points in the plane, find two that
are the minimum distance apart.
Brute force algorithm took Θ(n2) time.
Try to do better with divide and conquer:
― divide points into two disjoint subsets
― recursively find closest pairs in the two subsets
― somehow combine results to get final answer
Divide and Conquer:
Closest Pair Problem
Separate points into two equal-sized groups on either side of a
vertical line
Recursively compute closest pair for left group and for right
group
– what should base of the recursion be?
Check if there is a smaller distance between two points on
opposite sides of the vertical line
– This is the tricky part
Divide and Conquer:
Closest Pair Problem
d is min. of min. distance on right and min.
distance on left
any pair with distance < d must be in this
strip of width 2d centered around dividing
line
consider points in strip from bottom to top
for each such point, compare it against
other points in the strip that could possibly
be closer
there are only a constant number of these
other points!
Divide and Conquer:
Closest Pair Problem
Each box is d/2 by d/2
No point in comparing p
against points in red area –
more than d away
X= a b
Y= c d
X a10n / 2 b, Y c10n / 2 d
X *Y a * c10n (a * d b * c)10n / 2 b * d
Divide and Conquer:
Multiplication of Large Integers
1 if n = 1
T ( n)
4T (n / 2) (n) if n > 1
T (n) n 2
Divide and Conquer:
Large Matrix Multiplication
Input:
Output:
Divide and Conquer:
Large Matrix Multiplication
Idea:
n×n matrix = 2×2 matrix of (n/2)×(n/2) submatrices
Recurrence:
T(n) = 8 T(n/2) + 4n2
O(n3) solution
Divide and Conquer:
Strassen’s Matrix Multiplication
Reduce Multiplications
Divide and Conquer:
Strassen’s Matrix Multiplication
Recurrence:
T(n) = 7 T(n/2) + 18n2
Presorting-based algorithm:
― Stage 1 Sort the array by an efficient sorting algorithm
― Stage 2 Apply binary search
Example:
Polynomial Evaluation
Transform and Conquer:
Representation Change
Polynomial Evaluation: