SlideShare a Scribd company logo
CS 440 Theory of Algorithms /
CS 468 Al ith i Bi i f tiCS 468 Algorithms in Bioinformatics
Divide-and-Conquer
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Divide and Conquer
The most well known algorithm design strategy:
1. Divide instance of problem into two or more smaller
instances
2. Solve smaller instances recursively
3. Obtain solution to original (larger) instance by combining
these solutions
Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-1
Divide-and-conquer technique example
a problem of size n
b bl 2b bl 1
a problem of size n
subproblem 2
of size n/2
subproblem 1
of size n/2
a solution to
subproblem 1
a solution to
subproblem 2p p
a solution to
the original problem
Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-2
e o g p ob e
Divide and Conquer Examples
Ɣ Sorting: mergesort and quicksort
Ɣ Tree traversals
Ɣ Binary search
Ɣ Multiplication of large integers
Ɣ Matrix multiplication: Strassen’s algorithm
Ɣ Closest-pair and convex-hull algorithms
Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-3
General Divide and Conquer recurrence:
T(n) = aT(n/b) + f (n) where f (n) Ĭ(nd)T(n) aT(n/b) + f (n) where f (n) Ĭ(n )
Master Theorem
d d• a < bd T(n) Ĭ(nd)
• a = bd T(n) Ĭ(nd lg n )
• a > bd T(n) Ĭ(nlog b a)
Note: the same results hold with O instead of Ĭ.Note: the same results hold with O instead of Ĭ.
Examples: T(n) = 4T(n/3) + n Ÿ T(n)  ?Examples: T(n) 4T(n/3) + n Ÿ T(n)  ?
T(n) = 2T(n/2) + n2 Ÿ T(n)  ?
T(n) = 8T(n/2) + n3 Ÿ T(n)  ?
Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-4
T(n) = 8T(n/2) + n Ÿ T(n)  ?
Mergesort
Ɣ Split array A[0..n-1] in two about equal halves and make
copies of each half in arrays B and Ccopies of each half in arrays B and C
Ɣ Sort arrays B and C recursively
Ɣ Merge sorted arrays B and C into array A as follows:Ɣ Merge sorted arrays B and C into array A as follows:
• Repeat the following until no elements remain in one of
the arrays:y
– compare the first elements in the remaining
unprocessed portions of the arrays
– copy the smaller of the two into A, while
incrementing the index indicating the unprocessed
portion of that arrayportion of that array
• Once all elements in one of the arrays are processed,
copy the remaining unprocessed elements from the other
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
array into A.
Pseudocode of Mergesort
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Pseudocode of Merge
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Mergesort Example
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Analysis of Mergesort
Ɣ All cases have same efficiency: Ĭ(n log n)
• According to Master Theorem (why?)
Number of comparisons in the worst case is close toƔ Number of comparisons in the worst case is close to
theoretical minimum for comparison-based sorting:
Cworst(n) = 2 Cworst(n/2) + n – 1, Cworst(1) = 0Cworst(n) 2 Cworst(n/2) n 1, Cworst(1) 0
Æ Cworst(n) = n log2 n – n + 1
Th ti l l b d ªl !º § ª l 1 44 ºTheoretical lower bound: ªlog2 n!º § ªn log2 n - 1.44nº
Ɣ Space requirement: Ĭ(n) (not in place)Ɣ Space requirement: Ĭ(n) (not in-place)
Ɣ Can be implemented without recursion (bottom-up)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Ɣ Can be implemented without recursion (bottom-up)
Quicksort
Ɣ Select a pivot (partitioning element) – here, the first element
Ɣ Rearrange the list so that all the elements in the first s
positions are smaller than or equal to the pivot and all the
i i i i ielements in the remaining n-s positions are larger than or
equal to the pivot (see next slide for an algorithm)
p
A[i]dp A[i]tp
Ɣ Exchange the pivot with the last element in the first (i.e., d)
subarray — the pivot is now in its final position
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Ɣ Sort the two subarrays recursively
Partitioning Algorithm
dd
The index i can
go out of thego out of the
subarray bound
and it needs to
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
be taken care of.
Quicksort Example
5 3 1 9 8 2 4 7
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Analysis of Quicksort
Ɣ Best case: split in the middle — Ĭ(n log n)
Worst case: sorted array! Ĭ(n2)Ɣ Worst case: sorted array! — Ĭ(n2)
Ɣ Average case: random arrays — Ĭ(n log n)
Ɣ Improvements:
b tt i t l ti di f th titi i• better pivot selection: median of three partitioning
• switch to insertion sort on small subfiles
li i i f i• elimination of recursion
These combine to 20-25% improvement
Ɣ Considered the method of choice for internal sorting of large
fil ( • 10000)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
files (n • 10000)
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]and in A[m+1..n-1] if K > A[m]
l m 0; r m n-1
while l d r do
m m ¬(l+r)/2¼
if K = A[m] return mif K = A[m] return m
else if K < A[m] r m m-1
else l m m+1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
return -1
Analysis of Binary Search
Ɣ Time efficiency
• worst-case recurrence: Cw (n) = 1 + Cw( ¬n/2¼ ), Cw (1) = 1w ( ) w( ¬ ¼ ), w ( )
solution: Cw(n) = ªlog2(n+1)º
This is VERY fast: e g C (106) = 20This is VERY fast: e.g., Cw(106) = 20
Ɣ Optimal for searching a sorted arrayp g y
Ɣ Limitations: must be a sorted array (not linked list)
Ɣ Degenerate example of divide-and-conquer
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Binary Tree Algorithms
Binary tree is a divide-and-conquer ready structure!
Ex. 1: Classic traversals (preorder, inorder, postorder)
Algorithm Inorder(T)
if T z ‡ a a‡
Inorder(Tleft)
i t( t f T)
a a
b c b c
d ' ' dprint(root of T)
Inorder(Tright)
d e ' ' d e
' ' ' '
Efficiency: Ĭ(n)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
y ( )
Binary Tree Algorithms (cont.)
Ex. 2: Computing the height of a binary tree
T TL R
h(T) = max{h(TL), h(TR)} + 1 if T z ‡ and h(‡) = -1
Efficiency: Ĭ(n)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Multiplication of Large Integers
Consider the problem of multiplying two (large) n-digit integers
represented by arrays of their digits such as:p y y g
A = 12345678901357986429 B = 87654321284820912836
The grade-school algorithm:
a1 a2 … an1 2 n
b1 b2 … bn
(d10) d11d12 … d1n
(d ) d d d(d20) d21d22 … d2n
… … … … … … …
(d ) d d d(dn0) dn1dn2 … dnn
Efficiency: n2 one digit multiplications
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Efficiency: n2 one-digit multiplications
First Divide-and-Conquer Algorithm
A small example: A 
 B where A = 2135 and B = 4014
A = (21 102 + 35) B = (40 102 + 14)A = (21·102 + 35), B = (40 ·102 + 14)
So, A 
 B = (21 ·102 + 35) 
 (40 ·102 + 14)
21 
 40 104 + (21 
 14 + 35 
 40) 102 + 35 
 14= 21 
 40 ·104 + (21 
 14 + 35 
 40) ·102 + 35 
 14
= 8569890
In general, if A = A1A2 and B = B1B2 (where A and B are n-digit,
A1, A2, B1, B2 are n/2-digit numbers),1 2 1 2 g
A 
 B = A1 
 B1·10n + (A1 
 B2 + A2 
 B1) ·10n/2 + A2 
 B2
R f h b f di i l i li i M( )Recurrence for the number of one-digit multiplications M(n):
M(n) = 4M(n/2), M(1) = 1
S l ti M( ) 2
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Solution: M(n) = n2
Second Divide-and-Conquer Algorithm
A 
 B = A1 
 B1·10n + (A1 
 B2 + A2 
 B1) ·10n/2 + A2 
 B2
The idea is to decrease the number of multiplications from 4 to 3:
(A1 + A2 ) 
 (B1 + B2 ) = A1 
 B1 + (A1 
 B2 + A2 
 B1) + A2 
 B2,
I.e., (A1 
 B2 + A2 
 B1) = (A1 + A2 ) 
 (B1 + B2 ) - A1 
 B1 - A2 
 B2,
which requires only 3 multiplications at the expense of (4-1) extra
add/sub.
Recurrence for the number of multiplications M(n):Recurrence for the number of multiplications M(n):
M(n) = 3M(n/2), M(1) = 1
Solution: M(n) = 3log 2n = nlog 23 § n1.585
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
So u o : ( ) 3
Example of Large-Integer Multiplication
2135 x 4014
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Strassen’s matrix multiplication
Ɣ Strassen observed [1969] that the product of two matrices
can be computed as follows:
C00 C01 A00 A01 B00 B01
= *
C C A A B BC10 C11 A10 A11 B10 B11
M + M M + M M + MM1 + M4 - M5 + M7 M3 + M5
=
M2 + M4 M1 + M3 - M2 + M62 4 1 3 2 6
Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-22
Submatrices:
Ɣ M1 = (A00 + A11) * (B00 + B11)
Ɣ M2 = (A10 + A11) * B00
Ɣ M3 = A00 * (B01 - B11)
M = A * (B B )Ɣ M4 = A11 * (B10 - B00)
Ɣ M5 = (A00 + A01) * B11
Ɣ M6 = (A10 - A00) * (B00 + B01)
Ɣ M7 = (A01 - A11) * (B10 + B11)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-23
Efficiency of Strassen’s algorithm
Ɣ If n is not a power of 2, matrices can be padded with zeros
Ɣ Number of multiplications:
M(n) = , n > 1
M(1) = 1
Î M(n) = ?
Ɣ Number of additions:
A(n) = , n > 1
Î A( ) ?
Ɣ Algorithms with better asymptotic efficiency are known
A(n) , n 1
A(1) = 1
Î A(n) = ?
g y p y
but they are even more complex.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-24
Closest-Pair Problem by Divide-and-Conquer
Step 1 Divide the points given into two subsets S1 and S2 by a
vertical line x = c so that half the points lie to the left or onvertical line x c so that half the points lie to the left or on
the line and half the points lie to the right or on the line.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Closest Pair by Divide-and-Conquer (cont.)
Step 2 Find recursively the closest pairs for the left and right
s bsetssubsets.
Step 3 Set d = min{d1, d2}Step 3 Set d min{d1, d2}
We can limit our attention to the points in the symmetric
vertical strip of width 2d as possible closest pair. Let C1
and C be the subsets of points in the left subset S and ofand C2 be the subsets of points in the left subset S1 and of
the right subset S2, respectively, that lie in this vertical
strip. The points in C1 and C2 are stored in increasing
order of their y coordinates which is maintained byorder of their y coordinates, which is maintained by
merging during the execution of the next step.
Step 4 For every point P(x,y) in C1, we inspect points in
C2 that may be closer to P than d. There can be no more
than 6 such points (because d ” d2)!
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
than 6 such points (because d ” d2)!
Closest Pair by Divide-and-Conquer: Worst Case
The worst case scenario is depicted below:
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
Efficiency of the Closest-Pair Algorithm
Running time of the algorithm is described by
T(n) = 2T(n/2) + M(n), where M(n)  O(n)
By the Master Theorem (with a = 2, b = 2, d = 1)
T(n)  O(n log n)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
QuickHull Algorithm
Inspired by Quicksort compute Convex Hull:
Ɣ Assume points are sorted by x coordinate valuesƔ Assume points are sorted by x-coordinate values
Ɣ Identify extreme points P1 and P2 (part of hull)
Compute upper hull:Ɣ Compute upper hull:
• find point Pmax that is farthest away from line P1P2
• compute the hull of the points to the left of line P1Pmaxcompute the hull of the points to the left of line P1Pmax
• compute the hull of the points to the left of line PmaxP2
Ɣ Compute lower hull Pma
p
in a similar manner
P2
Pmax
P1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-29
Efficiency of QuickHull algorithm
Ɣ Finding point farthest away from line P1P2 can be done in
li tilinear time
Ɣ Time efficiency:
Ĭ 2 i• worst case: Ĭ(n2) (as quicksort)
• average case: Ĭ(n logn) (under reasonable assumptions
b t di t ib ti f i t i )about distribution of points given)
Ɣ If points are not initially sorted by x-coordinate value, thisƔ If points are not initially sorted by x coordinate value, this
can be accomplished in Ĭ( n log n) — no increase in
asymptotic efficiency class
Ɣ Several O(n log n) algorithms for convex hull are known
• Graham’s scan
• DCHull
Copyright © 2007 Pearson Addison-Wesley. All rights reserved
• DCHull
Design and Analysis of Algorithms - Chapter 4 4-30

More Related Content

What's hot

Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
Mohammed Hussein
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
Krish_ver2
 
Chap05alg
Chap05algChap05alg
Chap05alg
Munkhchimeg
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Muhammad Sarfraz
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Amrinder Arora
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
Swati Kulkarni Jaipurkar
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
Traian Rebedea
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
Amrinder Arora
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
Edhole.com
 
08 decrease and conquer spring 15
08 decrease and conquer spring 1508 decrease and conquer spring 15
08 decrease and conquer spring 15
Hira Gul
 
Unit 3
Unit 3Unit 3
Unit 3
guna287176
 
RECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMRECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEM
Alpana Ingale
 
Unit i
Unit iUnit i
Unit i
guna287176
 
Algorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithmsAlgorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithms
SiddhantShelake
 
Unit 4
Unit 4Unit 4
Unit 4
guna287176
 
Unit 2
Unit 2Unit 2
Unit 2
guna287176
 

What's hot (20)

Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
Chap05alg
Chap05algChap05alg
Chap05alg
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
08 decrease and conquer spring 15
08 decrease and conquer spring 1508 decrease and conquer spring 15
08 decrease and conquer spring 15
 
Unit 3
Unit 3Unit 3
Unit 3
 
RECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEMRECURRENCE EQUATIONS & ANALYZING THEM
RECURRENCE EQUATIONS & ANALYZING THEM
 
Unit i
Unit iUnit i
Unit i
 
Algorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithmsAlgorithm designing using divide and conquer algorithms
Algorithm designing using divide and conquer algorithms
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 2
Unit 2Unit 2
Unit 2
 

Similar to Divide and conquer

dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
giridaroori
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
B.Kirron Reddi
 
lecture 15
lecture 15lecture 15
lecture 15
sajinsc
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
Advanced algebra
Advanced algebraAdvanced algebra
Advanced algebra
spark21
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
03 dc
03 dc03 dc
03 dc
Hira Gul
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
Pramit Kumar
 
Permutations and Combinations IIT JEE+Olympiad Lecture 4
Permutations and Combinations IIT JEE+Olympiad Lecture 4Permutations and Combinations IIT JEE+Olympiad Lecture 4
Permutations and Combinations IIT JEE+Olympiad Lecture 4
Parth Nandedkar
 
Brute force
Brute forceBrute force
Ayush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptxAyush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptx
AmanChoudhary329978
 
Bca3010 computer oreineted numerical methods
Bca3010   computer oreineted numerical methodsBca3010   computer oreineted numerical methods
Bca3010 computer oreineted numerical methods
smumbahelp
 
Ch04n
Ch04nCh04n
Ch04n
kpcs2010
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
Krish_ver2
 
Project management
Project managementProject management
Project management
Avay Minni
 
Lemh104
Lemh104Lemh104
TABREZ KHAN.ppt
TABREZ KHAN.pptTABREZ KHAN.ppt
TABREZ KHAN.ppt
TabrezKhan733764
 
Unit 3
Unit 3Unit 3
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
Trector Rancor
 
Indefinite Integration One shot Revision
Indefinite Integration One shot Revision Indefinite Integration One shot Revision
Indefinite Integration One shot Revision
CHANDHINIJAYARAMAN
 

Similar to Divide and conquer (20)

dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
lecture 15
lecture 15lecture 15
lecture 15
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Advanced algebra
Advanced algebraAdvanced algebra
Advanced algebra
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
03 dc
03 dc03 dc
03 dc
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
Permutations and Combinations IIT JEE+Olympiad Lecture 4
Permutations and Combinations IIT JEE+Olympiad Lecture 4Permutations and Combinations IIT JEE+Olympiad Lecture 4
Permutations and Combinations IIT JEE+Olympiad Lecture 4
 
Brute force
Brute forceBrute force
Brute force
 
Ayush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptxAyush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptx
 
Bca3010 computer oreineted numerical methods
Bca3010   computer oreineted numerical methodsBca3010   computer oreineted numerical methods
Bca3010 computer oreineted numerical methods
 
Ch04n
Ch04nCh04n
Ch04n
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
Project management
Project managementProject management
Project management
 
Lemh104
Lemh104Lemh104
Lemh104
 
TABREZ KHAN.ppt
TABREZ KHAN.pptTABREZ KHAN.ppt
TABREZ KHAN.ppt
 
Unit 3
Unit 3Unit 3
Unit 3
 
Tree distance algorithm
Tree distance algorithmTree distance algorithm
Tree distance algorithm
 
Indefinite Integration One shot Revision
Indefinite Integration One shot Revision Indefinite Integration One shot Revision
Indefinite Integration One shot Revision
 

More from Emmanuel college

Public voice
Public voicePublic voice
Public voice
Emmanuel college
 
Inventory Management System
Inventory Management SystemInventory Management System
Inventory Management System
Emmanuel college
 
Pillcam
PillcamPillcam
High performance computing with accelarators
High performance computing with accelaratorsHigh performance computing with accelarators
High performance computing with accelarators
Emmanuel college
 
Mycasestudy
MycasestudyMycasestudy
Mycasestudy
Emmanuel college
 
Pill camera ppt
Pill camera pptPill camera ppt
Pill camera ppt
Emmanuel college
 

More from Emmanuel college (6)

Public voice
Public voicePublic voice
Public voice
 
Inventory Management System
Inventory Management SystemInventory Management System
Inventory Management System
 
Pillcam
PillcamPillcam
Pillcam
 
High performance computing with accelarators
High performance computing with accelaratorsHigh performance computing with accelarators
High performance computing with accelarators
 
Mycasestudy
MycasestudyMycasestudy
Mycasestudy
 
Pill camera ppt
Pill camera pptPill camera ppt
Pill camera ppt
 

Recently uploaded

UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 

Recently uploaded (20)

UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 

Divide and conquer

  • 1. CS 440 Theory of Algorithms / CS 468 Al ith i Bi i f tiCS 468 Algorithms in Bioinformatics Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original (larger) instance by combining these solutions Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-1
  • 2. Divide-and-conquer technique example a problem of size n b bl 2b bl 1 a problem of size n subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to subproblem 2p p a solution to the original problem Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-2 e o g p ob e Divide and Conquer Examples Ɣ Sorting: mergesort and quicksort Ɣ Tree traversals Ɣ Binary search Ɣ Multiplication of large integers Ɣ Matrix multiplication: Strassen’s algorithm Ɣ Closest-pair and convex-hull algorithms Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-3
  • 3. General Divide and Conquer recurrence: T(n) = aT(n/b) + f (n) where f (n) Ĭ(nd)T(n) aT(n/b) + f (n) where f (n) Ĭ(n ) Master Theorem d d• a < bd T(n) Ĭ(nd) • a = bd T(n) Ĭ(nd lg n ) • a > bd T(n) Ĭ(nlog b a) Note: the same results hold with O instead of Ĭ.Note: the same results hold with O instead of Ĭ. Examples: T(n) = 4T(n/3) + n Ÿ T(n)  ?Examples: T(n) 4T(n/3) + n Ÿ T(n)  ? T(n) = 2T(n/2) + n2 Ÿ T(n)  ? T(n) = 8T(n/2) + n3 Ÿ T(n)  ? Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-4 T(n) = 8T(n/2) + n Ÿ T(n)  ? Mergesort Ɣ Split array A[0..n-1] in two about equal halves and make copies of each half in arrays B and Ccopies of each half in arrays B and C Ɣ Sort arrays B and C recursively Ɣ Merge sorted arrays B and C into array A as follows:Ɣ Merge sorted arrays B and C into array A as follows: • Repeat the following until no elements remain in one of the arrays:y – compare the first elements in the remaining unprocessed portions of the arrays – copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that arrayportion of that array • Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other Copyright © 2007 Pearson Addison-Wesley. All rights reserved array into A.
  • 4. Pseudocode of Mergesort Copyright © 2007 Pearson Addison-Wesley. All rights reserved Pseudocode of Merge Copyright © 2007 Pearson Addison-Wesley. All rights reserved
  • 5. Mergesort Example Copyright © 2007 Pearson Addison-Wesley. All rights reserved Analysis of Mergesort Ɣ All cases have same efficiency: Ĭ(n log n) • According to Master Theorem (why?) Number of comparisons in the worst case is close toƔ Number of comparisons in the worst case is close to theoretical minimum for comparison-based sorting: Cworst(n) = 2 Cworst(n/2) + n – 1, Cworst(1) = 0Cworst(n) 2 Cworst(n/2) n 1, Cworst(1) 0 Æ Cworst(n) = n log2 n – n + 1 Th ti l l b d ªl !º § ª l 1 44 ºTheoretical lower bound: ªlog2 n!º § ªn log2 n - 1.44nº Ɣ Space requirement: Ĭ(n) (not in place)Ɣ Space requirement: Ĭ(n) (not in-place) Ɣ Can be implemented without recursion (bottom-up) Copyright © 2007 Pearson Addison-Wesley. All rights reserved Ɣ Can be implemented without recursion (bottom-up)
  • 6. Quicksort Ɣ Select a pivot (partitioning element) – here, the first element Ɣ Rearrange the list so that all the elements in the first s positions are smaller than or equal to the pivot and all the i i i i ielements in the remaining n-s positions are larger than or equal to the pivot (see next slide for an algorithm) p A[i]dp A[i]tp Ɣ Exchange the pivot with the last element in the first (i.e., d) subarray — the pivot is now in its final position Copyright © 2007 Pearson Addison-Wesley. All rights reserved Ɣ Sort the two subarrays recursively Partitioning Algorithm dd The index i can go out of thego out of the subarray bound and it needs to Copyright © 2007 Pearson Addison-Wesley. All rights reserved be taken care of.
  • 7. Quicksort Example 5 3 1 9 8 2 4 7 Copyright © 2007 Pearson Addison-Wesley. All rights reserved Analysis of Quicksort Ɣ Best case: split in the middle — Ĭ(n log n) Worst case: sorted array! Ĭ(n2)Ɣ Worst case: sorted array! — Ĭ(n2) Ɣ Average case: random arrays — Ĭ(n log n) Ɣ Improvements: b tt i t l ti di f th titi i• better pivot selection: median of three partitioning • switch to insertion sort on small subfiles li i i f i• elimination of recursion These combine to 20-25% improvement Ɣ Considered the method of choice for internal sorting of large fil ( • 10000) Copyright © 2007 Pearson Addison-Wesley. All rights reserved files (n • 10000)
  • 8. 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]and in A[m+1..n-1] if K > A[m] l m 0; r m n-1 while l d r do m m ¬(l+r)/2¼ if K = A[m] return mif K = A[m] return m else if K < A[m] r m m-1 else l m m+1 Copyright © 2007 Pearson Addison-Wesley. All rights reserved return -1 Analysis of Binary Search Ɣ Time efficiency • worst-case recurrence: Cw (n) = 1 + Cw( ¬n/2¼ ), Cw (1) = 1w ( ) w( ¬ ¼ ), w ( ) solution: Cw(n) = ªlog2(n+1)º This is VERY fast: e g C (106) = 20This is VERY fast: e.g., Cw(106) = 20 Ɣ Optimal for searching a sorted arrayp g y Ɣ Limitations: must be a sorted array (not linked list) Ɣ Degenerate example of divide-and-conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved
  • 9. Binary Tree Algorithms Binary tree is a divide-and-conquer ready structure! Ex. 1: Classic traversals (preorder, inorder, postorder) Algorithm Inorder(T) if T z ‡ a a‡ Inorder(Tleft) i t( t f T) a a b c b c d ' ' dprint(root of T) Inorder(Tright) d e ' ' d e ' ' ' ' Efficiency: Ĭ(n) Copyright © 2007 Pearson Addison-Wesley. All rights reserved y ( ) Binary Tree Algorithms (cont.) Ex. 2: Computing the height of a binary tree T TL R h(T) = max{h(TL), h(TR)} + 1 if T z ‡ and h(‡) = -1 Efficiency: Ĭ(n) Copyright © 2007 Pearson Addison-Wesley. All rights reserved
  • 10. Multiplication of Large Integers Consider the problem of multiplying two (large) n-digit integers represented by arrays of their digits such as:p y y g A = 12345678901357986429 B = 87654321284820912836 The grade-school algorithm: a1 a2 … an1 2 n b1 b2 … bn (d10) d11d12 … d1n (d ) d d d(d20) d21d22 … d2n … … … … … … … (d ) d d d(dn0) dn1dn2 … dnn Efficiency: n2 one digit multiplications Copyright © 2007 Pearson Addison-Wesley. All rights reserved Efficiency: n2 one-digit multiplications First Divide-and-Conquer Algorithm A small example: A B where A = 2135 and B = 4014 A = (21 102 + 35) B = (40 102 + 14)A = (21·102 + 35), B = (40 ·102 + 14) So, A B = (21 ·102 + 35) (40 ·102 + 14) 21 40 104 + (21 14 + 35 40) 102 + 35 14= 21 40 ·104 + (21 14 + 35 40) ·102 + 35 14 = 8569890 In general, if A = A1A2 and B = B1B2 (where A and B are n-digit, A1, A2, B1, B2 are n/2-digit numbers),1 2 1 2 g A B = A1 B1·10n + (A1 B2 + A2 B1) ·10n/2 + A2 B2 R f h b f di i l i li i M( )Recurrence for the number of one-digit multiplications M(n): M(n) = 4M(n/2), M(1) = 1 S l ti M( ) 2 Copyright © 2007 Pearson Addison-Wesley. All rights reserved Solution: M(n) = n2
  • 11. Second Divide-and-Conquer Algorithm A B = A1 B1·10n + (A1 B2 + A2 B1) ·10n/2 + A2 B2 The idea is to decrease the number of multiplications from 4 to 3: (A1 + A2 ) (B1 + B2 ) = A1 B1 + (A1 B2 + A2 B1) + A2 B2, I.e., (A1 B2 + A2 B1) = (A1 + A2 ) (B1 + B2 ) - A1 B1 - A2 B2, which requires only 3 multiplications at the expense of (4-1) extra add/sub. Recurrence for the number of multiplications M(n):Recurrence for the number of multiplications M(n): M(n) = 3M(n/2), M(1) = 1 Solution: M(n) = 3log 2n = nlog 23 § n1.585 Copyright © 2007 Pearson Addison-Wesley. All rights reserved So u o : ( ) 3 Example of Large-Integer Multiplication 2135 x 4014 Copyright © 2007 Pearson Addison-Wesley. All rights reserved
  • 12. Strassen’s matrix multiplication Ɣ Strassen observed [1969] that the product of two matrices can be computed as follows: C00 C01 A00 A01 B00 B01 = * C C A A B BC10 C11 A10 A11 B10 B11 M + M M + M M + MM1 + M4 - M5 + M7 M3 + M5 = M2 + M4 M1 + M3 - M2 + M62 4 1 3 2 6 Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-22 Submatrices: Ɣ M1 = (A00 + A11) * (B00 + B11) Ɣ M2 = (A10 + A11) * B00 Ɣ M3 = A00 * (B01 - B11) M = A * (B B )Ɣ M4 = A11 * (B10 - B00) Ɣ M5 = (A00 + A01) * B11 Ɣ M6 = (A10 - A00) * (B00 + B01) Ɣ M7 = (A01 - A11) * (B10 + B11) Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-23
  • 13. Efficiency of Strassen’s algorithm Ɣ If n is not a power of 2, matrices can be padded with zeros Ɣ Number of multiplications: M(n) = , n > 1 M(1) = 1 Î M(n) = ? Ɣ Number of additions: A(n) = , n > 1 Î A( ) ? Ɣ Algorithms with better asymptotic efficiency are known A(n) , n 1 A(1) = 1 Î A(n) = ? g y p y but they are even more complex. Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-24 Closest-Pair Problem by Divide-and-Conquer Step 1 Divide the points given into two subsets S1 and S2 by a vertical line x = c so that half the points lie to the left or onvertical line x c so that half the points lie to the left or on the line and half the points lie to the right or on the line. Copyright © 2007 Pearson Addison-Wesley. All rights reserved
  • 14. Closest Pair by Divide-and-Conquer (cont.) Step 2 Find recursively the closest pairs for the left and right s bsetssubsets. Step 3 Set d = min{d1, d2}Step 3 Set d min{d1, d2} We can limit our attention to the points in the symmetric vertical strip of width 2d as possible closest pair. Let C1 and C be the subsets of points in the left subset S and ofand C2 be the subsets of points in the left subset S1 and of the right subset S2, respectively, that lie in this vertical strip. The points in C1 and C2 are stored in increasing order of their y coordinates which is maintained byorder of their y coordinates, which is maintained by merging during the execution of the next step. Step 4 For every point P(x,y) in C1, we inspect points in C2 that may be closer to P than d. There can be no more than 6 such points (because d ” d2)! Copyright © 2007 Pearson Addison-Wesley. All rights reserved than 6 such points (because d ” d2)! Closest Pair by Divide-and-Conquer: Worst Case The worst case scenario is depicted below: Copyright © 2007 Pearson Addison-Wesley. All rights reserved
  • 15. Efficiency of the Closest-Pair Algorithm Running time of the algorithm is described by T(n) = 2T(n/2) + M(n), where M(n)  O(n) By the Master Theorem (with a = 2, b = 2, d = 1) T(n)  O(n log n) Copyright © 2007 Pearson Addison-Wesley. All rights reserved QuickHull Algorithm Inspired by Quicksort compute Convex Hull: Ɣ Assume points are sorted by x coordinate valuesƔ Assume points are sorted by x-coordinate values Ɣ Identify extreme points P1 and P2 (part of hull) Compute upper hull:Ɣ Compute upper hull: • find point Pmax that is farthest away from line P1P2 • compute the hull of the points to the left of line P1Pmaxcompute the hull of the points to the left of line P1Pmax • compute the hull of the points to the left of line PmaxP2 Ɣ Compute lower hull Pma p in a similar manner P2 Pmax P1 Copyright © 2007 Pearson Addison-Wesley. All rights reserved Design and Analysis of Algorithms - Chapter 4 4-29
  • 16. Efficiency of QuickHull algorithm Ɣ Finding point farthest away from line P1P2 can be done in li tilinear time Ɣ Time efficiency: Ĭ 2 i• worst case: Ĭ(n2) (as quicksort) • average case: Ĭ(n logn) (under reasonable assumptions b t di t ib ti f i t i )about distribution of points given) Ɣ If points are not initially sorted by x-coordinate value, thisƔ If points are not initially sorted by x coordinate value, this can be accomplished in Ĭ( n log n) — no increase in asymptotic efficiency class Ɣ Several O(n log n) algorithms for convex hull are known • Graham’s scan • DCHull Copyright © 2007 Pearson Addison-Wesley. All rights reserved • DCHull Design and Analysis of Algorithms - Chapter 4 4-30