Decrease and Conquer
Decrease and Conquer
Decrease-and-Conquer
1. Reduce problem instance to smaller instance of the same problem
2. Solve smaller instance
3. Extend solution of smaller instance to obtain solution to original
instance
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved.
Different Types
• Decrease by a constant (usually by 1):
• Insertion sort
• Topological sorting
• Algorithms for generating permutations, subsets
• Variable-size decrease
• Euclid’s algorithm
• Selection by partition
• Nim-like games
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved.
Computing an, Different strategies
• Brute Force:
• Multiply a, n times.
• Decrease by one:
• Compute an-1 and then multiply by a.
Example: Sort 6, 4, 1, 8, 5
6|4 1 8 5
4 6|1 8 5
1 4 6|8 5
1 4 6 8|5
1 4 5 6 8
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. 5
All Rights Reserved.
Pseudocode of Insertion Sort
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 4 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. A
ll Rights Reserved. 6
Analysis of Insertion Sort
• Time efficiency
Cworst(n) = n(n-1)/2 Θ(n2)
Cavg(n) ≈ n2/4 Θ(n2)
Cbest(n) = n - 1 Θ(n) (also fast on almost sorted arrays)
• Stability: yes
c d c d
human
fish
sheep
shrimp
plankton wheat
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 9
Topological Sort Example
Source Removal Algorithm
Source removal algorithm
Repeatedly identify and remove a source (a vertex with no
incoming edges) and all the edges incident to it until either no
vertex is left (problem is solved) or there is no source among
remaining vertices (not a DAG)
Example:
a b c d
e f g h
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved.
Decrease-by-Constant-Factor Algorithms
Examples:
• binary search and the method of bisection
• exponentiation by squaring
• fake-coin puzzle
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 15
General Divide-and-Conquer
Recurrence
T(n) = aT(n/b) + f (n) where f(n) (nd), d 0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. 16
Upper Saddle River, NJ. All Rights Reserved.
Binary Search
Very efficient algorithm for searching in sorted
array:
K
vs
A[0] . . . A[m] . . . A[n-1]
If K = A[m], stop (successful search);
otherwise, continue
searching by the same method in A[0..m-1] if K
< A[m]
and in A[m+1..n-1] if K > A[m]
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 17
Binary Search -Analysis
• Basic Operation Using Master Theorem,
a=1, b=2, d=0
• Three way comparison Case of a = bd , So, the run time is :
• Recurrence Relation (Worst case) C(n)= O(nlog b a ), where logba = 0,
So, C(n) = O(log n)
• Analysis
•
Analysis of Binary Search
• This is VERY fast: e.g., Cw(106) = 20
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 19
Exponentiation by Squaring
The problem: Compute an where n is a nonnegative integer
Base case
a 0= 1
For even values of n, (n >1)
a n = (a n/2 )2
For odd values of n, (n >1)
a n = (a (n-1)/2 )2.a
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 20
Exponentiation by Squaring-Analysis
• Basic Operation
• Multiplication
• Recurrence Relation
• = 1 or 2
• Analysis
• (for n= 2k for some k >0)
= 1 (Base case)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 22
Example of Russian Peasant
Multiplication
Compute 20 * 26
n m
20 26
10 52
5 104
2 208 + 104
1 416 416
520
Note: Method reduces to adding m’s values corresponding to odd n’s.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 23
Russian Peasant Multiplication-
Analysis
• Basic Operation
• Shift operators + Addition (in some cases)
• Recurrence Relation
• Analysis
•
Fake-Coin Puzzle (simpler version)
There are n identically looking coins one of which is fake.
There is a balance scale but there are no weights; the scale can tell whether
two sets of coins weigh the same and, if not, which of the two sets is heavier
(but not by how much). Design an efficient algorithm for detecting the fake
coin. Assume that the fake coin is known to be lighter than the genuine ones.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 25
Divide into two Algorithm
• Divide the pile of n coins into two sets (set one coin aside if n is odd)
• Weigh them
• If equal, then the coin set aside is fake
• If not equal, solve the same problem with the lighter pile, until you reach the
end.
Examples:
• Euclid’s algorithm for greatest common divisor
• partition-based algorithm for selection problem (Quickselect)
• interpolation search
• some algorithms on binary search trees
• Nim and Nim-like games
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 27
Euclid’s Algorithm
Euclid’s algorithm is based on repeated application of
equality
gcd(m, n) = gcd(n, m mod n)
• median: k = n/2
Example: 4, 1, 10, 9, 7, 12, 8, 2, 15 median = ?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 31
Algorithms for the Selection Problem
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 33
Lomuto’s Partitioning Algorithm
Scans the array left to right maintaining the array’s partition into
three contiguous sections: < p, p, and unknown, where p is the
value of the first element (the partition’s pivot).
l s i r
p <p >= p ?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 34
Tracing Lomuto’s Partioning Algorithm
s i
4 1 10 8 7 12 9 2 15
s i
4 1 10 8 7 12 9 2 15
s i
4 1 10 8 7 12 9 2 15
s i
4 1 2 8 7 12 9 10 15
s
4 1 2 8 7 12 9 10 15
2 1 4 8 7 12 9 10 15
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 4 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. A
ll Rights Reserved. 35
Quickselect (based on Lomuto Partitioning)
Tracing Quickselect (Partition-based
Algorithm)
7 8 12 9 10 15
The median is A[4]= 8
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 4 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. A
ll Rights Reserved. 37
Efficiency of Quickselect
Basic Operation
Comparison + Swaps
Average case (average split in the middle):
C(n) = C(n/2)+(n+1) C(n) Θ(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved. 38
Summary
• Three variations of Decrease and Conquer
• Decrease by a constant
• Insertion Sort
• Topological Sort (Source Removal Algorithm)
• Decrease by a constant factor
• Binary Search
• Exponentiation by Squaring
• Fake coin example
• Russian Pheasant Multiplication
• Decrease by variable size
• Euclid Algorithm for GCD
• Quickselect