SlideShare uma empresa Scribd logo
1 de 33
Chapter 4Chapter 4
Divide-and-ConquerDivide-and-Conquer
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
4-2Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Divide-and-ConquerDivide-and-Conquer
The most-well known algorithm design strategy:The most-well known algorithm design strategy:
1.1. Divide instance of problem into two or more smallerDivide instance of problem into two or more smaller
instancesinstances
2.2. Solve smaller instances recursivelySolve smaller instances recursively
3.3. Obtain solution to original (larger) instance by combiningObtain solution to original (larger) instance by combining
these solutionsthese solutions
4-3Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Divide-and-Conquer Technique (cont.)Divide-and-Conquer Technique (cont.)
subproblem 2
of size n/2
subproblem 1
of size n/2
a solution to
subproblem 1
a solution to
the original problem
a solution to
subproblem 2
a problem of size n
(instance)
It general leads to a
recursive algorithm!
4-4Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Divide-and-Conquer ExamplesDivide-and-Conquer Examples
Sorting: mergesort and quicksortSorting: mergesort and quicksort
Binary tree traversalsBinary tree traversals
Binary search (?)Binary search (?)
Multiplication of large integersMultiplication of large integers
Matrix multiplication: Strassen’s algorithmMatrix multiplication: Strassen’s algorithm
Closest-pair and convex-hull algorithmsClosest-pair and convex-hull algorithms
4-5Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
General Divide-and-Conquer RecurrenceGeneral Divide-and-Conquer Recurrence
TT((nn) =) = aTaT((n/bn/b) +) + ff ((nn)) wherewhere ff((nn)) ∈∈ ΘΘ((nndd
),), dd ≥≥ 00
Master TheoremMaster Theorem: If: If a < ba < bdd
,, TT((nn)) ∈∈ ΘΘ((nndd
))
IfIf a = ba = bdd
,, TT((nn)) ∈∈ ΘΘ((nndd
loglog nn))
IfIf a > ba > bdd
,, TT((nn)) ∈∈ ΘΘ((nnloglog bb aa
))
Note: The same results hold with O instead ofNote: The same results hold with O instead of ΘΘ..
Examples:Examples: TT((nn) = 4) = 4TT((nn/2) +/2) + nn ⇒⇒ TT((nn)) ∈∈ ??
TT((nn) = 4) = 4TT((nn/2) +/2) + nn22
⇒⇒ TT((nn)) ∈∈ ??
TT((nn) = 4) = 4TT((nn/2) +/2) + nn33
⇒⇒ TT((nn)) ∈∈ ??
ΘΘ((n^2n^2))
ΘΘ((n^2log nn^2log n))
ΘΘ((n^3n^3))
4-6Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
MergesortMergesort
Split array A[0..Split array A[0..nn-1] into about equal halves and make-1] into 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 recursivelySort 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 ofRepeat the following until no elements remain in one of
the arrays:the arrays:
– compare the first elements in the remainingcompare the first elements in the remaining
unprocessed portions of the arraysunprocessed portions of the arrays
– copy the smaller of the two into A, whilecopy the smaller of the two into A, while
incrementing the index indicating the unprocessedincrementing the index indicating the unprocessed
portion of that arrayportion of that array
• Once all elements in one of the arrays are processed,Once all elements in one of the arrays are processed,
copy the remaining unprocessed elements from the othercopy the remaining unprocessed elements from the other
array into A.array into A.
4-7Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Pseudocode of MergesortPseudocode of Mergesort
4-8Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Pseudocode of MergePseudocode of Merge
Time complexity: ΘΘ((p+qp+q)) = ΘΘ((nn)) comparisons
4-9Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Mergesort ExampleMergesort Example
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 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
The non-recursive
version of Mergesort
starts from merging
single elements into
sorted pairs.
4-10Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Analysis of MergesortAnalysis of Mergesort
All cases have same efficiency:All cases have same efficiency: ΘΘ((nn loglog nn))
Number of comparisons in the worst case is close toNumber of comparisons in the worst case is close to
theoretical minimum for comparison-based sorting:theoretical minimum for comparison-based sorting:
loglog22 nn!! ≈≈ nn loglog22 nn - 1.44- 1.44nn
Space requirement:Space requirement: ΘΘ((nn) () (notnot in-place)in-place)
Can be implemented without recursion (bottom-up)Can be implemented without recursion (bottom-up)
T(n) = 2T(n/2) +T(n) = 2T(n/2) + ΘΘ(n), T(1) = 0(n), T(1) = 0
4-11Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
QuicksortQuicksort
Select aSelect a pivotpivot (partitioning element) – here, the first element(partitioning element) – here, the first element
Rearrange the list so that all the elements in the firstRearrange the list so that all the elements in the first ss
positions are smaller than or equal to the pivot and all thepositions are smaller than or equal to the pivot and all the
elements in the remainingelements in the remaining n-sn-s positions are larger than orpositions are larger than or
equal to the pivot (see next slide for an algorithm)equal to the pivot (see next slide for an algorithm)
Exchange the pivot with the last element in the first (i.e.,Exchange the pivot with the last element in the first (i.e., ≤≤))
subarray — the pivot is now in its final positionsubarray — the pivot is now in its final position
Sort the two subarrays recursivelySort the two subarrays recursively
p
A[i]≤p A[i]≥p
4-12Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Partitioning AlgorithmPartitioning Algorithm
Time complexity: ΘΘ((r-lr-l)) comparisons
or i > r
or j = l<
4-13Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Quicksort ExampleQuicksort Example
5 3 1 9 8 2 4 75 3 1 9 8 2 4 7
2 3 1 4 5 8 9 7
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
4-14Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Analysis of QuicksortAnalysis of Quicksort
Best case: split in the middleBest case: split in the middle —— ΘΘ((nn loglog nn))
Worst case: sorted array! —Worst case: sorted array! — ΘΘ((nn22
))
Average case: random arraysAverage case: random arrays —— ΘΘ((nn loglog nn))
Improvements:Improvements:
• better pivot selection: median of three partitioningbetter pivot selection: median of three partitioning
• switch to insertion sort on small subfilesswitch to insertion sort on small subfiles
• elimination of recursionelimination of recursion
These combine to 20-25% improvementThese combine to 20-25% improvement
Considered the method of choice for internal sorting of largeConsidered the method of choice for internal sorting of large
files (files (nn ≥≥ 10000)10000)
T(n) = T(n-1) +T(n) = T(n-1) + ΘΘ((nn))
4-15Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Binary SearchBinary Search
Very efficient algorithm for searching inVery efficient algorithm for searching in sorted arraysorted array::
KK
vsvs
A[0] . . . A[A[0] . . . A[mm] . . . A[] . . . A[nn-1]-1]
IfIf K =K = A[A[mm], stop (successful search); otherwise, continue], stop (successful search); otherwise, continue
searching by the same method in A[0..searching by the same method in A[0..mm-1] if-1] if K <K < A[A[mm]]
and in A[and in A[mm+1..+1..nn-1] if-1] if K >K > A[A[mm]]
ll ←← 0;0; rr ←← nn-1-1
whilewhile ll ≤≤ rr dodo
mm ←← ((ll++rr)/2)/2
ifif K =K = A[A[mm] return] return mm
else ifelse if K <K < A[A[mm]] rr ←← mm-1-1
elseelse ll ←← mm+1+1
return -1return -1
4-16Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Analysis of Binary SearchAnalysis of Binary Search
Time efficiencyTime efficiency
• worst-case recurrence:worst-case recurrence: CCww ((nn) = 1 +) = 1 + CCww(( nn/2/2 ),), CCww (1) = 1(1) = 1
solution:solution: CCww((nn) =) = loglog22((nn+1)+1)
This is VERY fast:This is VERY fast: e.g., Ce.g., Cww(10(1066
) = 20) = 20
Optimal for searching a sorted arrayOptimal for searching a sorted array
Limitations: must be a sorted array (not linked list)Limitations: must be a sorted array (not linked list)
Bad (degenerate) example of divide-and-conquerBad (degenerate) example of divide-and-conquer
because only one of the sub-instances is solvedbecause only one of the sub-instances is solved
Has a continuous counterpart calledHas a continuous counterpart called bisection methodbisection method for solvingfor solving
equations in one unknownequations in one unknown ff((xx)) == 0 (see Sec. 12.4)0 (see Sec. 12.4)
4-17Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Binary Tree AlgorithmsBinary Tree Algorithms
Binary tree is a divide-and-conquer ready structure!Binary tree is a divide-and-conquer ready structure!
Ex. 1: Classic traversals (preorder, inorder, postorder)Ex. 1: Classic traversals (preorder, inorder, postorder)
AlgorithmAlgorithm InorderInorder((TT))
ifif TT ≠≠ ∅∅ aa aa
InorderInorder((TTleftleft)) b c b cb c b c
print(root ofprint(root of TT)) d ed e   d ed e
InorderInorder((TTrightright))    
Efficiency:Efficiency: ΘΘ((nn). Why?). Why? Each node is visited/printed once.
4-18Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Binary Tree Algorithms (cont.)Binary Tree Algorithms (cont.)
Ex. 2: Computing the height of a binary treeEx. 2: Computing the height of a binary tree
T TL R
hh((TT) = max{) = max{hh((TTLL),), hh((TTRR)} + 1 if)} + 1 if TT ≠≠ ∅∅ andand hh((∅∅) = -1) = -1
Efficiency:Efficiency: ΘΘ((nn). Why?). Why?
4-19Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Multiplication of Large IntegersMultiplication of Large Integers
Consider the problem of multiplying two (large)Consider the problem of multiplying two (large) nn-digit integers-digit integers
represented by arrays of their digits such as:represented by arrays of their digits such as:
A = 12345678901357986429 B = 87654321284820912836A = 12345678901357986429 B = 87654321284820912836
The grade-school algorithm:The grade-school algorithm:
aa11 aa22 …… aann
bb11 bb22 …… bbnn
((dd1010))dd1111dd1212 …… dd11nn
((dd2020))dd2121dd2222 …… dd22nn
… … … … … … …… … … … … … …
((ddnn00))ddnn11ddnn22 …… ddnnnn
Efficiency:Efficiency: ΘΘ((nn22
) single-digit multiplications) single-digit multiplications
4-20Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
First Divide-and-Conquer AlgorithmFirst Divide-and-Conquer Algorithm
A small example: AA small example: A ∗∗ B where A = 2135 and B = 4014B where A = 2135 and B = 4014
A = (21·10A = (21·1022
+ 35), B = (40 ·10+ 35), B = (40 ·1022
+ 14)+ 14)
So, ASo, A ∗∗ B = (21 ·10B = (21 ·1022
+ 35)+ 35) ∗∗ (40 ·10(40 ·1022
+ 14)+ 14)
= 21= 21 ∗∗ 40 ·1040 ·1044
+ (21+ (21 ∗∗ 14 + 3514 + 35 ∗∗ 40) ·1040) ·1022
+ 35+ 35 ∗∗ 1414
In general, if A = AIn general, if A = A11AA22 and B = Band B = B11BB22 (where A and B are(where A and B are nn-digit,-digit,
AA11, A, A22, B, B11,,BB22 areare n/n/2-digit numbers),2-digit numbers),
AA ∗∗ B = AB = A11 ∗∗ BB11·10·10nn
+ (A+ (A11 ∗∗ BB22 + A+ A22 ∗∗ BB11) ·10) ·10n/n/22
+ A+ A22 ∗∗ BB22
Recurrence for the number of one-digit multiplications M(Recurrence for the number of one-digit multiplications M(nn):):
M(M(nn) = 4M() = 4M(nn/2), M(1) = 1/2), M(1) = 1
Solution: M(Solution: M(nn) =) = nn22
4-21Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Second Divide-and-Conquer AlgorithmSecond Divide-and-Conquer Algorithm
AA ∗∗ B = AB = A11 ∗∗ BB11·10·10nn
+ (A+ (A11 ∗∗ BB22 + A+ A22 ∗∗ BB11) ·10) ·10n/n/22
+ A+ A22 ∗∗ BB22
The idea is to decrease the number of multiplications from 4 to 3:The idea is to decrease the number of multiplications from 4 to 3:
(A(A11 + A+ A22 )) ∗∗ (B(B11 + B+ B22 ) = A) = A11 ∗∗ BB11 + (A+ (A11 ∗∗ BB22 + A+ A22 ∗∗ BB11) + A) + A22 ∗∗ BB2,2,
I.e., (AI.e., (A11 ∗∗ BB22 + A+ A22 ∗∗ BB11) = (A) = (A11 + A+ A22 )) ∗∗ (B(B11 + B+ B22 ) - A) - A11 ∗∗ BB11 - A- A22 ∗∗ BB2,2,
which requires only 3 multiplications at the expense of (4-1) extrawhich requires only 3 multiplications at the expense of (4-1) extra
add/sub.add/sub.
Recurrence for the number of multiplications M(Recurrence for the number of multiplications M(nn):):
M(M(nn) = 3) = 3MM((nn/2), M(1) = 1/2), M(1) = 1
Solution: M(Solution: M(nn)) = 3= 3loglog 22nn
== nnloglog 2233
≈≈ nn1.5851.585
What if we count
both multiplications
and additions?
4-22Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Example of Large-Integer MultiplicationExample of Large-Integer Multiplication
21352135 ∗∗ 40144014
= (21*10^2 + 35) * (40*10^2 + 14)
= (21*40)*10^4 + c1*10^2 + 35*14
where c1 = (21+35)*(40+14) - 21*40 - 35*14, and
21*40 = (2*10 + 1) * (4*10 + 0)
= (2*4)*10^2 + c2*10 + 1*0
where c2 = (2+1)*(4+0) - 2*4 - 1*0, etc.
This process requires 9 digit multiplications as opposed to 16.
4-23Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Conventional Matrix MultiplicationConventional Matrix Multiplication
Brute-force algorithmBrute-force algorithm
cc0000 cc0101 aa0000 aa0101 bb0000 bb0101
= *= *
cc1010 cc1111 aa1010 aa1111 bb1010 bb1111
aa0000 * b* b0000 + a+ a0101 * b* b1010 aa0000 * b* b0101 + a+ a0101 * b* b1111
==
aa1010 * b* b0000 + a+ a1111 * b* b1010 aa1010 * b* b0101 + a+ a1111 * b* b1111
8 multiplications
4 additions
Efficiency class in general: Θ (n3
)
4-24Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Strassen’s Matrix MultiplicationStrassen’s Matrix Multiplication
Strassen’s algorithm for two 2x2 matrices (1969):Strassen’s algorithm for two 2x2 matrices (1969):
cc0000 cc0101 aa0000 aa0101 bb0000 bb0101
= *= *
cc1010 cc1111 aa1010 aa1111 bb1010 bb1111
mm11 + m+ m44 - m- m55 + m+ m77 mm33 + m+ m55
==
mm22 + m+ m44 mm11 + m+ m33 - m- m22 + m+ m66
mm11 = (a= (a0000 + a+ a1111)) ** (b(b0000 + b+ b1111))
mm22 = (a= (a1010 + a+ a1111)) ** bb0000
mm33 = a= a0000 ** (b(b0101 - b- b1111))
mm44 = a= a1111 ** (b(b1010 - b- b0000))
mm55 = (a= (a0000 + a+ a0101)) ** bb1111
mm66 = (a= (a1010 - a- a0000)) ** (b(b0000 + b+ b0101))
mm = (a= (a - a- a )) ** (b(b + b+ b ))
7 multiplications
18 additions
4-25Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Strassen’s Matrix MultiplicationStrassen’s Matrix Multiplication
Strassen observed [1969] that the product of two matrices canStrassen observed [1969] that the product of two matrices can
be computed in general as follows:be computed in general as follows:
CC0000 CC0101 AA0000 AA0101 BB0000 BB0101
= *= *
CC1010 CC1111 AA1010 AA1111 BB1010 BB1111
MM11 + M+ M44 - M- M55 + M+ M77 MM33 + M+ M55
==
MM22 + M+ M44 MM11 + M+ M33 - M- M22 + M+ M66
4-26Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Formulas for Strassen’s AlgorithmFormulas for Strassen’s Algorithm
MM11 = (A= (A0000 + A+ A1111)) ∗∗ (B(B0000 ++ BB1111))
MM22 = (A= (A1010 + A+ A1111)) ∗∗ BB0000
MM33 = A= A0000 ∗∗ (B(B0101 -- BB1111))
MM44 = A= A1111 ∗∗ (B(B1010 -- BB0000))
MM55 = (A= (A0000 + A+ A0101)) ∗∗ BB1111
MM66 = (A= (A1010 - A- A0000)) ∗∗ (B(B0000 ++ BB0101))
MM77 = (A= (A0101 - A- A1111)) ∗∗ (B(B1010 ++ BB1111))
4-27Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Analysis of Strassen’s AlgorithmAnalysis of Strassen’s Algorithm
IfIf nn is not a power of 2, matrices can be padded with zeros.is not a power of 2, matrices can be padded with zeros.
Number of multiplications:Number of multiplications:
M(M(nn) = 7M() = 7M(nn/2), M(1) = 1/2), M(1) = 1
Solution: M(Solution: M(nn)) = 7= 7loglog 22nn
== nnloglog 2277
≈≈ nn2.8072.807
vs.vs. nn33
of brute-force alg.of brute-force alg.
Algorithms with better asymptotic efficiency are known but theyAlgorithms with better asymptotic efficiency are known but they
are even more complex and not used in practice.are even more complex and not used in practice.
What if we count both
multiplications and additions?
4-28Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Closest-Pair Problem by Divide-and-ConquerClosest-Pair Problem by Divide-and-Conquer
Step 0 Sort the points by x (list one) and then by y (list two).Step 0 Sort the points by x (list one) and then by y (list two).
Step 1 Divide the points given into two subsets SStep 1 Divide the points given into two subsets S11 and Sand S22 by aby a
vertical linevertical line xx == cc so that half the points lie to the left or onso that half the points lie to the left or on
the line and half the points lie to the right or on the line.the line and half the points lie to the right or on the line.
4-29Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Closest Pair by Divide-and-Conquer (cont.)Closest Pair by Divide-and-Conquer (cont.)
Step 2 Find recursively the closest pairs for the left and rightStep 2 Find recursively the closest pairs for the left and right
subsets.subsets.
Step 3 SetStep 3 Set dd = min{= min{dd11,, dd22}}
We can limit our attention to the points in the symmetricWe can limit our attention to the points in the symmetric
vertical strip of width 2vertical strip of width 2dd as possible closest pair. Let Cas possible closest pair. Let C11
and Cand C22 be the subsets of points in the left subset Sbe the subsets of points in the left subset S11 and ofand of
the right subset Sthe right subset S22, respectively, that lie in this vertical, respectively, that lie in this vertical
strip. The points in Cstrip. The points in C11 and Cand C22 are stored in increasingare stored in increasing
order of theirorder of their yy coordinates, taken from the second list.coordinates, taken from the second list.
Step 4 For every pointStep 4 For every point PP((xx,,yy) in C) in C11, we inspect points in, we inspect points in
CC22 that may be closer tothat may be closer to PP thanthan dd. There can be no more. There can be no more
thanthan 6 such points6 such points (because(because dd ≤≤ dd22)!)!
4-30Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Closest Pair by Divide-and-Conquer: Worst CaseClosest Pair by Divide-and-Conquer: Worst Case
The worst case scenario is depicted below:The worst case scenario is depicted below:
4-31Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Efficiency of the Closest-Pair AlgorithmEfficiency of the Closest-Pair Algorithm
Running time of the algorithm (without sorting) is:Running time of the algorithm (without sorting) is:
T(T(nn) = 2T() = 2T(nn/2) + M(/2) + M(nn), where M(), where M(nn)) ∈∈ ΘΘ((nn))
By the Master Theorem (withBy the Master Theorem (with aa = 2,= 2, bb = 2,= 2, dd = 1)= 1)
T(T(nn)) ∈∈ ΘΘ((nn loglog nn))
So the total time isSo the total time is ΘΘ((nn loglog nn).).
4-32Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Quickhull AlgorithmQuickhull Algorithm
Convex hullConvex hull: smallest convex set that includes given points. An: smallest convex set that includes given points. An
O(n^3) bruteforce time is given in Levitin, Ch 3.O(n^3) bruteforce time is given in Levitin, Ch 3.
Assume points are sorted byAssume points are sorted by xx-coordinate values-coordinate values
IdentifyIdentify extreme pointsextreme points PP11 andand PP22 (leftmost and rightmost)(leftmost and rightmost)
ComputeCompute upper hullupper hull recursively:recursively:
• find pointfind point PPmaxmax that is farthest away from linethat is farthest away from line PP11PP22
• compute the upper hull of the points to the left of linecompute the upper hull of the points to the left of line PP11PPmaxmax
• compute the upper hull of the points to the left of linecompute the upper hull of the points to the left of line PPmaxmaxPP22
ComputeCompute lower hulllower hull in a similar mannerin a similar manner
P1
P2
Pmax
4-33Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd
ed., Ch. 4
Efficiency of Quickhull AlgorithmEfficiency of Quickhull Algorithm
Finding point farthest away from lineFinding point farthest away from line PP11PP22 can be done incan be done in
linear timelinear time
Time efficiency:Time efficiency: T(n) = T(x) + T(y) + T(z) + T(v) + O(n),T(n) = T(x) + T(y) + T(z) + T(v) + O(n),
where x + y + z +v <= n.where x + y + z +v <= n.
• worst case:worst case: ΘΘ((nn22
)) T(n) = T(n-1) + O(n)T(n) = T(n-1) + O(n)
• average case:average case: ΘΘ((nn) (under reasonable assumptions about) (under reasonable assumptions about
distribution of points given)distribution of points given)
If points are not initially sorted byIf points are not initially sorted by xx-coordinate value, this-coordinate value, this
can be accomplished incan be accomplished in O(O(nn loglog nn) time) time
SeveralSeveral O(O(nn loglog nn)) algorithms for convex hull are knownalgorithms for convex hull are known

Mais conteúdo relacionado

Mais procurados

Strong convergence of an algorithm about strongly quasi nonexpansive mappings
Strong convergence of an algorithm about strongly quasi nonexpansive mappingsStrong convergence of an algorithm about strongly quasi nonexpansive mappings
Strong convergence of an algorithm about strongly quasi nonexpansive mappingsAlexander Decker
 
Stability of Iteration for Some General Operators in b-Metric
Stability of Iteration for Some General Operators in b-MetricStability of Iteration for Some General Operators in b-Metric
Stability of Iteration for Some General Operators in b-MetricKomal Goyal
 
A common fixed point theorem for two random operators using random mann itera...
A common fixed point theorem for two random operators using random mann itera...A common fixed point theorem for two random operators using random mann itera...
A common fixed point theorem for two random operators using random mann itera...Alexander Decker
 
A lattice-based consensus clustering
A lattice-based consensus clusteringA lattice-based consensus clustering
A lattice-based consensus clusteringDmitrii Ignatov
 
Internal workshop jub talk jan 2013
Internal workshop jub talk jan 2013Internal workshop jub talk jan 2013
Internal workshop jub talk jan 2013Jurgen Riedel
 
Higher-order organization of complex networks
Higher-order organization of complex networksHigher-order organization of complex networks
Higher-order organization of complex networksDavid Gleich
 
Nearly optimal average case complexity of counting bicliques under seth
Nearly optimal average case complexity of counting bicliques under sethNearly optimal average case complexity of counting bicliques under seth
Nearly optimal average case complexity of counting bicliques under sethNobutaka Shimizu
 
Pattern-based classification of demographic sequences
Pattern-based classification of demographic sequencesPattern-based classification of demographic sequences
Pattern-based classification of demographic sequencesDmitrii Ignatov
 
prior selection for mixture estimation
prior selection for mixture estimationprior selection for mixture estimation
prior selection for mixture estimationChristian Robert
 
Coordinate sampler : A non-reversible Gibbs-like sampler
Coordinate sampler : A non-reversible Gibbs-like samplerCoordinate sampler : A non-reversible Gibbs-like sampler
Coordinate sampler : A non-reversible Gibbs-like samplerChristian Robert
 
Estimating structured vector autoregressive models
Estimating structured vector autoregressive modelsEstimating structured vector autoregressive models
Estimating structured vector autoregressive modelsAkira Tanimoto
 
He laplace method for special nonlinear partial differential equations
He laplace method for special nonlinear partial differential equationsHe laplace method for special nonlinear partial differential equations
He laplace method for special nonlinear partial differential equationsAlexander Decker
 
International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) inventionjournals
 
The Queue Length of a GI M 1 Queue with Set Up Period and Bernoulli Working V...
The Queue Length of a GI M 1 Queue with Set Up Period and Bernoulli Working V...The Queue Length of a GI M 1 Queue with Set Up Period and Bernoulli Working V...
The Queue Length of a GI M 1 Queue with Set Up Period and Bernoulli Working V...YogeshIJTSRD
 
Maximum likelihood estimation of regularisation parameters in inverse problem...
Maximum likelihood estimation of regularisation parameters in inverse problem...Maximum likelihood estimation of regularisation parameters in inverse problem...
Maximum likelihood estimation of regularisation parameters in inverse problem...Valentin De Bortoli
 
IRJET- Global Accurate Domination in Jump Graph
IRJET- Global Accurate Domination in Jump GraphIRJET- Global Accurate Domination in Jump Graph
IRJET- Global Accurate Domination in Jump GraphIRJET Journal
 
New approach to modified schur cohn criterion for stability analysis of a dis...
New approach to modified schur cohn criterion for stability analysis of a dis...New approach to modified schur cohn criterion for stability analysis of a dis...
New approach to modified schur cohn criterion for stability analysis of a dis...Ritesh Keshri
 

Mais procurados (20)

Strong convergence of an algorithm about strongly quasi nonexpansive mappings
Strong convergence of an algorithm about strongly quasi nonexpansive mappingsStrong convergence of an algorithm about strongly quasi nonexpansive mappings
Strong convergence of an algorithm about strongly quasi nonexpansive mappings
 
Stability of Iteration for Some General Operators in b-Metric
Stability of Iteration for Some General Operators in b-MetricStability of Iteration for Some General Operators in b-Metric
Stability of Iteration for Some General Operators in b-Metric
 
A common fixed point theorem for two random operators using random mann itera...
A common fixed point theorem for two random operators using random mann itera...A common fixed point theorem for two random operators using random mann itera...
A common fixed point theorem for two random operators using random mann itera...
 
A lattice-based consensus clustering
A lattice-based consensus clusteringA lattice-based consensus clustering
A lattice-based consensus clustering
 
Internal workshop jub talk jan 2013
Internal workshop jub talk jan 2013Internal workshop jub talk jan 2013
Internal workshop jub talk jan 2013
 
Higher-order organization of complex networks
Higher-order organization of complex networksHigher-order organization of complex networks
Higher-order organization of complex networks
 
Nearly optimal average case complexity of counting bicliques under seth
Nearly optimal average case complexity of counting bicliques under sethNearly optimal average case complexity of counting bicliques under seth
Nearly optimal average case complexity of counting bicliques under seth
 
Pattern-based classification of demographic sequences
Pattern-based classification of demographic sequencesPattern-based classification of demographic sequences
Pattern-based classification of demographic sequences
 
prior selection for mixture estimation
prior selection for mixture estimationprior selection for mixture estimation
prior selection for mixture estimation
 
Beamerpresentation
BeamerpresentationBeamerpresentation
Beamerpresentation
 
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
 
Coordinate sampler : A non-reversible Gibbs-like sampler
Coordinate sampler : A non-reversible Gibbs-like samplerCoordinate sampler : A non-reversible Gibbs-like sampler
Coordinate sampler : A non-reversible Gibbs-like sampler
 
Estimating structured vector autoregressive models
Estimating structured vector autoregressive modelsEstimating structured vector autoregressive models
Estimating structured vector autoregressive models
 
He laplace method for special nonlinear partial differential equations
He laplace method for special nonlinear partial differential equationsHe laplace method for special nonlinear partial differential equations
He laplace method for special nonlinear partial differential equations
 
A05920109
A05920109A05920109
A05920109
 
International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI) International Journal of Mathematics and Statistics Invention (IJMSI)
International Journal of Mathematics and Statistics Invention (IJMSI)
 
The Queue Length of a GI M 1 Queue with Set Up Period and Bernoulli Working V...
The Queue Length of a GI M 1 Queue with Set Up Period and Bernoulli Working V...The Queue Length of a GI M 1 Queue with Set Up Period and Bernoulli Working V...
The Queue Length of a GI M 1 Queue with Set Up Period and Bernoulli Working V...
 
Maximum likelihood estimation of regularisation parameters in inverse problem...
Maximum likelihood estimation of regularisation parameters in inverse problem...Maximum likelihood estimation of regularisation parameters in inverse problem...
Maximum likelihood estimation of regularisation parameters in inverse problem...
 
IRJET- Global Accurate Domination in Jump Graph
IRJET- Global Accurate Domination in Jump GraphIRJET- Global Accurate Domination in Jump Graph
IRJET- Global Accurate Domination in Jump Graph
 
New approach to modified schur cohn criterion for stability analysis of a dis...
New approach to modified schur cohn criterion for stability analysis of a dis...New approach to modified schur cohn criterion for stability analysis of a dis...
New approach to modified schur cohn criterion for stability analysis of a dis...
 

Semelhante a Ch04n

ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02Shanmuganathan C
 
lecture 1(Fundamental of algorithms).pptx
lecture 1(Fundamental of algorithms).pptxlecture 1(Fundamental of algorithms).pptx
lecture 1(Fundamental of algorithms).pptxTejaYadav27
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysisPremjeet Roy
 
Review session2
Review session2Review session2
Review session2NEEDY12345
 
An optimal and progressive algorithm for skyline queries slide
An optimal and progressive algorithm for skyline queries slideAn optimal and progressive algorithm for skyline queries slide
An optimal and progressive algorithm for skyline queries slideWooSung Choi
 
01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptxIshtiaq Rasool Khan
 
Master method
Master method Master method
Master method Rajendran
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Week09
Week09Week09
Week09hccit
 
DeepLearn2022 2. Variance Matters
DeepLearn2022  2. Variance MattersDeepLearn2022  2. Variance Matters
DeepLearn2022 2. Variance MattersSean Meyn
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2Deepak John
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 

Semelhante a Ch04n (20)

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
 
lecture 1(Fundamental of algorithms).pptx
lecture 1(Fundamental of algorithms).pptxlecture 1(Fundamental of algorithms).pptx
lecture 1(Fundamental of algorithms).pptx
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Quicksort analysis
Quicksort analysisQuicksort analysis
Quicksort analysis
 
Review session2
Review session2Review session2
Review session2
 
An optimal and progressive algorithm for skyline queries slide
An optimal and progressive algorithm for skyline queries slideAn optimal and progressive algorithm for skyline queries slide
An optimal and progressive algorithm for skyline queries slide
 
01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx
 
Master method
Master method Master method
Master method
 
sorting
sortingsorting
sorting
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Week09
Week09Week09
Week09
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
DeepLearn2022 2. Variance Matters
DeepLearn2022  2. Variance MattersDeepLearn2022  2. Variance Matters
DeepLearn2022 2. Variance Matters
 
AlgorithmAnalysis2.ppt
AlgorithmAnalysis2.pptAlgorithmAnalysis2.ppt
AlgorithmAnalysis2.ppt
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 

Último

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Último (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Ch04n

  • 1. Chapter 4Chapter 4 Divide-and-ConquerDivide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
  • 2. 4-2Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Divide-and-ConquerDivide-and-Conquer The most-well known algorithm design strategy:The most-well known algorithm design strategy: 1.1. Divide instance of problem into two or more smallerDivide instance of problem into two or more smaller instancesinstances 2.2. Solve smaller instances recursivelySolve smaller instances recursively 3.3. Obtain solution to original (larger) instance by combiningObtain solution to original (larger) instance by combining these solutionsthese solutions
  • 3. 4-3Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Divide-and-Conquer Technique (cont.)Divide-and-Conquer Technique (cont.) subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n (instance) It general leads to a recursive algorithm!
  • 4. 4-4Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Divide-and-Conquer ExamplesDivide-and-Conquer Examples Sorting: mergesort and quicksortSorting: mergesort and quicksort Binary tree traversalsBinary tree traversals Binary search (?)Binary search (?) Multiplication of large integersMultiplication of large integers Matrix multiplication: Strassen’s algorithmMatrix multiplication: Strassen’s algorithm Closest-pair and convex-hull algorithmsClosest-pair and convex-hull algorithms
  • 5. 4-5Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 General Divide-and-Conquer RecurrenceGeneral Divide-and-Conquer Recurrence TT((nn) =) = aTaT((n/bn/b) +) + ff ((nn)) wherewhere ff((nn)) ∈∈ ΘΘ((nndd ),), dd ≥≥ 00 Master TheoremMaster Theorem: If: If a < ba < bdd ,, TT((nn)) ∈∈ ΘΘ((nndd )) IfIf a = ba = bdd ,, TT((nn)) ∈∈ ΘΘ((nndd loglog nn)) IfIf a > ba > bdd ,, TT((nn)) ∈∈ ΘΘ((nnloglog bb aa )) Note: The same results hold with O instead ofNote: The same results hold with O instead of ΘΘ.. Examples:Examples: TT((nn) = 4) = 4TT((nn/2) +/2) + nn ⇒⇒ TT((nn)) ∈∈ ?? TT((nn) = 4) = 4TT((nn/2) +/2) + nn22 ⇒⇒ TT((nn)) ∈∈ ?? TT((nn) = 4) = 4TT((nn/2) +/2) + nn33 ⇒⇒ TT((nn)) ∈∈ ?? ΘΘ((n^2n^2)) ΘΘ((n^2log nn^2log n)) ΘΘ((n^3n^3))
  • 6. 4-6Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 MergesortMergesort Split array A[0..Split array A[0..nn-1] into about equal halves and make-1] into 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 recursivelySort 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 ofRepeat the following until no elements remain in one of the arrays:the arrays: – compare the first elements in the remainingcompare the first elements in the remaining unprocessed portions of the arraysunprocessed portions of the arrays – copy the smaller of the two into A, whilecopy the smaller of the two into A, while incrementing the index indicating the unprocessedincrementing the index indicating the unprocessed portion of that arrayportion of that array • Once all elements in one of the arrays are processed,Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the othercopy the remaining unprocessed elements from the other array into A.array into A.
  • 7. 4-7Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Pseudocode of MergesortPseudocode of Mergesort
  • 8. 4-8Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Pseudocode of MergePseudocode of Merge Time complexity: ΘΘ((p+qp+q)) = ΘΘ((nn)) comparisons
  • 9. 4-9Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Mergesort ExampleMergesort Example 8 3 2 9 7 1 5 4 8 3 2 9 7 1 5 4 8 3 2 9 7 1 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 The non-recursive version of Mergesort starts from merging single elements into sorted pairs.
  • 10. 4-10Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Analysis of MergesortAnalysis of Mergesort All cases have same efficiency:All cases have same efficiency: ΘΘ((nn loglog nn)) Number of comparisons in the worst case is close toNumber of comparisons in the worst case is close to theoretical minimum for comparison-based sorting:theoretical minimum for comparison-based sorting: loglog22 nn!! ≈≈ nn loglog22 nn - 1.44- 1.44nn Space requirement:Space requirement: ΘΘ((nn) () (notnot in-place)in-place) Can be implemented without recursion (bottom-up)Can be implemented without recursion (bottom-up) T(n) = 2T(n/2) +T(n) = 2T(n/2) + ΘΘ(n), T(1) = 0(n), T(1) = 0
  • 11. 4-11Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 QuicksortQuicksort Select aSelect a pivotpivot (partitioning element) – here, the first element(partitioning element) – here, the first element Rearrange the list so that all the elements in the firstRearrange the list so that all the elements in the first ss positions are smaller than or equal to the pivot and all thepositions are smaller than or equal to the pivot and all the elements in the remainingelements in the remaining n-sn-s positions are larger than orpositions are larger than or equal to the pivot (see next slide for an algorithm)equal to the pivot (see next slide for an algorithm) Exchange the pivot with the last element in the first (i.e.,Exchange the pivot with the last element in the first (i.e., ≤≤)) subarray — the pivot is now in its final positionsubarray — the pivot is now in its final position Sort the two subarrays recursivelySort the two subarrays recursively p A[i]≤p A[i]≥p
  • 12. 4-12Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Partitioning AlgorithmPartitioning Algorithm Time complexity: ΘΘ((r-lr-l)) comparisons or i > r or j = l<
  • 13. 4-13Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Quicksort ExampleQuicksort Example 5 3 1 9 8 2 4 75 3 1 9 8 2 4 7 2 3 1 4 5 8 9 7 1 2 3 4 5 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 7 8 9
  • 14. 4-14Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Analysis of QuicksortAnalysis of Quicksort Best case: split in the middleBest case: split in the middle —— ΘΘ((nn loglog nn)) Worst case: sorted array! —Worst case: sorted array! — ΘΘ((nn22 )) Average case: random arraysAverage case: random arrays —— ΘΘ((nn loglog nn)) Improvements:Improvements: • better pivot selection: median of three partitioningbetter pivot selection: median of three partitioning • switch to insertion sort on small subfilesswitch to insertion sort on small subfiles • elimination of recursionelimination of recursion These combine to 20-25% improvementThese combine to 20-25% improvement Considered the method of choice for internal sorting of largeConsidered the method of choice for internal sorting of large files (files (nn ≥≥ 10000)10000) T(n) = T(n-1) +T(n) = T(n-1) + ΘΘ((nn))
  • 15. 4-15Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Binary SearchBinary Search Very efficient algorithm for searching inVery efficient algorithm for searching in sorted arraysorted array:: KK vsvs A[0] . . . A[A[0] . . . A[mm] . . . A[] . . . A[nn-1]-1] IfIf K =K = A[A[mm], stop (successful search); otherwise, continue], stop (successful search); otherwise, continue searching by the same method in A[0..searching by the same method in A[0..mm-1] if-1] if K <K < A[A[mm]] and in A[and in A[mm+1..+1..nn-1] if-1] if K >K > A[A[mm]] ll ←← 0;0; rr ←← nn-1-1 whilewhile ll ≤≤ rr dodo mm ←← ((ll++rr)/2)/2 ifif K =K = A[A[mm] return] return mm else ifelse if K <K < A[A[mm]] rr ←← mm-1-1 elseelse ll ←← mm+1+1 return -1return -1
  • 16. 4-16Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Analysis of Binary SearchAnalysis of Binary Search Time efficiencyTime efficiency • worst-case recurrence:worst-case recurrence: CCww ((nn) = 1 +) = 1 + CCww(( nn/2/2 ),), CCww (1) = 1(1) = 1 solution:solution: CCww((nn) =) = loglog22((nn+1)+1) This is VERY fast:This is VERY fast: e.g., Ce.g., Cww(10(1066 ) = 20) = 20 Optimal for searching a sorted arrayOptimal for searching a sorted array Limitations: must be a sorted array (not linked list)Limitations: must be a sorted array (not linked list) Bad (degenerate) example of divide-and-conquerBad (degenerate) example of divide-and-conquer because only one of the sub-instances is solvedbecause only one of the sub-instances is solved Has a continuous counterpart calledHas a continuous counterpart called bisection methodbisection method for solvingfor solving equations in one unknownequations in one unknown ff((xx)) == 0 (see Sec. 12.4)0 (see Sec. 12.4)
  • 17. 4-17Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Binary Tree AlgorithmsBinary Tree Algorithms Binary tree is a divide-and-conquer ready structure!Binary tree is a divide-and-conquer ready structure! Ex. 1: Classic traversals (preorder, inorder, postorder)Ex. 1: Classic traversals (preorder, inorder, postorder) AlgorithmAlgorithm InorderInorder((TT)) ifif TT ≠≠ ∅∅ aa aa InorderInorder((TTleftleft)) b c b cb c b c print(root ofprint(root of TT)) d ed e   d ed e InorderInorder((TTrightright))     Efficiency:Efficiency: ΘΘ((nn). Why?). Why? Each node is visited/printed once.
  • 18. 4-18Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Binary Tree Algorithms (cont.)Binary Tree Algorithms (cont.) Ex. 2: Computing the height of a binary treeEx. 2: Computing the height of a binary tree T TL R hh((TT) = max{) = max{hh((TTLL),), hh((TTRR)} + 1 if)} + 1 if TT ≠≠ ∅∅ andand hh((∅∅) = -1) = -1 Efficiency:Efficiency: ΘΘ((nn). Why?). Why?
  • 19. 4-19Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Multiplication of Large IntegersMultiplication of Large Integers Consider the problem of multiplying two (large)Consider the problem of multiplying two (large) nn-digit integers-digit integers represented by arrays of their digits such as:represented by arrays of their digits such as: A = 12345678901357986429 B = 87654321284820912836A = 12345678901357986429 B = 87654321284820912836 The grade-school algorithm:The grade-school algorithm: aa11 aa22 …… aann bb11 bb22 …… bbnn ((dd1010))dd1111dd1212 …… dd11nn ((dd2020))dd2121dd2222 …… dd22nn … … … … … … …… … … … … … … ((ddnn00))ddnn11ddnn22 …… ddnnnn Efficiency:Efficiency: ΘΘ((nn22 ) single-digit multiplications) single-digit multiplications
  • 20. 4-20Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 First Divide-and-Conquer AlgorithmFirst Divide-and-Conquer Algorithm A small example: AA small example: A ∗∗ B where A = 2135 and B = 4014B where A = 2135 and B = 4014 A = (21·10A = (21·1022 + 35), B = (40 ·10+ 35), B = (40 ·1022 + 14)+ 14) So, ASo, A ∗∗ B = (21 ·10B = (21 ·1022 + 35)+ 35) ∗∗ (40 ·10(40 ·1022 + 14)+ 14) = 21= 21 ∗∗ 40 ·1040 ·1044 + (21+ (21 ∗∗ 14 + 3514 + 35 ∗∗ 40) ·1040) ·1022 + 35+ 35 ∗∗ 1414 In general, if A = AIn general, if A = A11AA22 and B = Band B = B11BB22 (where A and B are(where A and B are nn-digit,-digit, AA11, A, A22, B, B11,,BB22 areare n/n/2-digit numbers),2-digit numbers), AA ∗∗ B = AB = A11 ∗∗ BB11·10·10nn + (A+ (A11 ∗∗ BB22 + A+ A22 ∗∗ BB11) ·10) ·10n/n/22 + A+ A22 ∗∗ BB22 Recurrence for the number of one-digit multiplications M(Recurrence for the number of one-digit multiplications M(nn):): M(M(nn) = 4M() = 4M(nn/2), M(1) = 1/2), M(1) = 1 Solution: M(Solution: M(nn) =) = nn22
  • 21. 4-21Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Second Divide-and-Conquer AlgorithmSecond Divide-and-Conquer Algorithm AA ∗∗ B = AB = A11 ∗∗ BB11·10·10nn + (A+ (A11 ∗∗ BB22 + A+ A22 ∗∗ BB11) ·10) ·10n/n/22 + A+ A22 ∗∗ BB22 The idea is to decrease the number of multiplications from 4 to 3:The idea is to decrease the number of multiplications from 4 to 3: (A(A11 + A+ A22 )) ∗∗ (B(B11 + B+ B22 ) = A) = A11 ∗∗ BB11 + (A+ (A11 ∗∗ BB22 + A+ A22 ∗∗ BB11) + A) + A22 ∗∗ BB2,2, I.e., (AI.e., (A11 ∗∗ BB22 + A+ A22 ∗∗ BB11) = (A) = (A11 + A+ A22 )) ∗∗ (B(B11 + B+ B22 ) - A) - A11 ∗∗ BB11 - A- A22 ∗∗ BB2,2, which requires only 3 multiplications at the expense of (4-1) extrawhich requires only 3 multiplications at the expense of (4-1) extra add/sub.add/sub. Recurrence for the number of multiplications M(Recurrence for the number of multiplications M(nn):): M(M(nn) = 3) = 3MM((nn/2), M(1) = 1/2), M(1) = 1 Solution: M(Solution: M(nn)) = 3= 3loglog 22nn == nnloglog 2233 ≈≈ nn1.5851.585 What if we count both multiplications and additions?
  • 22. 4-22Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Example of Large-Integer MultiplicationExample of Large-Integer Multiplication 21352135 ∗∗ 40144014 = (21*10^2 + 35) * (40*10^2 + 14) = (21*40)*10^4 + c1*10^2 + 35*14 where c1 = (21+35)*(40+14) - 21*40 - 35*14, and 21*40 = (2*10 + 1) * (4*10 + 0) = (2*4)*10^2 + c2*10 + 1*0 where c2 = (2+1)*(4+0) - 2*4 - 1*0, etc. This process requires 9 digit multiplications as opposed to 16.
  • 23. 4-23Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Conventional Matrix MultiplicationConventional Matrix Multiplication Brute-force algorithmBrute-force algorithm cc0000 cc0101 aa0000 aa0101 bb0000 bb0101 = *= * cc1010 cc1111 aa1010 aa1111 bb1010 bb1111 aa0000 * b* b0000 + a+ a0101 * b* b1010 aa0000 * b* b0101 + a+ a0101 * b* b1111 == aa1010 * b* b0000 + a+ a1111 * b* b1010 aa1010 * b* b0101 + a+ a1111 * b* b1111 8 multiplications 4 additions Efficiency class in general: Θ (n3 )
  • 24. 4-24Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Strassen’s Matrix MultiplicationStrassen’s Matrix Multiplication Strassen’s algorithm for two 2x2 matrices (1969):Strassen’s algorithm for two 2x2 matrices (1969): cc0000 cc0101 aa0000 aa0101 bb0000 bb0101 = *= * cc1010 cc1111 aa1010 aa1111 bb1010 bb1111 mm11 + m+ m44 - m- m55 + m+ m77 mm33 + m+ m55 == mm22 + m+ m44 mm11 + m+ m33 - m- m22 + m+ m66 mm11 = (a= (a0000 + a+ a1111)) ** (b(b0000 + b+ b1111)) mm22 = (a= (a1010 + a+ a1111)) ** bb0000 mm33 = a= a0000 ** (b(b0101 - b- b1111)) mm44 = a= a1111 ** (b(b1010 - b- b0000)) mm55 = (a= (a0000 + a+ a0101)) ** bb1111 mm66 = (a= (a1010 - a- a0000)) ** (b(b0000 + b+ b0101)) mm = (a= (a - a- a )) ** (b(b + b+ b )) 7 multiplications 18 additions
  • 25. 4-25Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Strassen’s Matrix MultiplicationStrassen’s Matrix Multiplication Strassen observed [1969] that the product of two matrices canStrassen observed [1969] that the product of two matrices can be computed in general as follows:be computed in general as follows: CC0000 CC0101 AA0000 AA0101 BB0000 BB0101 = *= * CC1010 CC1111 AA1010 AA1111 BB1010 BB1111 MM11 + M+ M44 - M- M55 + M+ M77 MM33 + M+ M55 == MM22 + M+ M44 MM11 + M+ M33 - M- M22 + M+ M66
  • 26. 4-26Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Formulas for Strassen’s AlgorithmFormulas for Strassen’s Algorithm MM11 = (A= (A0000 + A+ A1111)) ∗∗ (B(B0000 ++ BB1111)) MM22 = (A= (A1010 + A+ A1111)) ∗∗ BB0000 MM33 = A= A0000 ∗∗ (B(B0101 -- BB1111)) MM44 = A= A1111 ∗∗ (B(B1010 -- BB0000)) MM55 = (A= (A0000 + A+ A0101)) ∗∗ BB1111 MM66 = (A= (A1010 - A- A0000)) ∗∗ (B(B0000 ++ BB0101)) MM77 = (A= (A0101 - A- A1111)) ∗∗ (B(B1010 ++ BB1111))
  • 27. 4-27Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Analysis of Strassen’s AlgorithmAnalysis of Strassen’s Algorithm IfIf nn is not a power of 2, matrices can be padded with zeros.is not a power of 2, matrices can be padded with zeros. Number of multiplications:Number of multiplications: M(M(nn) = 7M() = 7M(nn/2), M(1) = 1/2), M(1) = 1 Solution: M(Solution: M(nn)) = 7= 7loglog 22nn == nnloglog 2277 ≈≈ nn2.8072.807 vs.vs. nn33 of brute-force alg.of brute-force alg. Algorithms with better asymptotic efficiency are known but theyAlgorithms with better asymptotic efficiency are known but they are even more complex and not used in practice.are even more complex and not used in practice. What if we count both multiplications and additions?
  • 28. 4-28Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Closest-Pair Problem by Divide-and-ConquerClosest-Pair Problem by Divide-and-Conquer Step 0 Sort the points by x (list one) and then by y (list two).Step 0 Sort the points by x (list one) and then by y (list two). Step 1 Divide the points given into two subsets SStep 1 Divide the points given into two subsets S11 and Sand S22 by aby a vertical linevertical line xx == cc so that half the points lie to the left or onso that half the points lie to the left or on the line and half the points lie to the right or on the line.the line and half the points lie to the right or on the line.
  • 29. 4-29Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Closest Pair by Divide-and-Conquer (cont.)Closest Pair by Divide-and-Conquer (cont.) Step 2 Find recursively the closest pairs for the left and rightStep 2 Find recursively the closest pairs for the left and right subsets.subsets. Step 3 SetStep 3 Set dd = min{= min{dd11,, dd22}} We can limit our attention to the points in the symmetricWe can limit our attention to the points in the symmetric vertical strip of width 2vertical strip of width 2dd as possible closest pair. Let Cas possible closest pair. Let C11 and Cand C22 be the subsets of points in the left subset Sbe the subsets of points in the left subset S11 and ofand of the right subset Sthe right subset S22, respectively, that lie in this vertical, respectively, that lie in this vertical strip. The points in Cstrip. The points in C11 and Cand C22 are stored in increasingare stored in increasing order of theirorder of their yy coordinates, taken from the second list.coordinates, taken from the second list. Step 4 For every pointStep 4 For every point PP((xx,,yy) in C) in C11, we inspect points in, we inspect points in CC22 that may be closer tothat may be closer to PP thanthan dd. There can be no more. There can be no more thanthan 6 such points6 such points (because(because dd ≤≤ dd22)!)!
  • 30. 4-30Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Closest Pair by Divide-and-Conquer: Worst CaseClosest Pair by Divide-and-Conquer: Worst Case The worst case scenario is depicted below:The worst case scenario is depicted below:
  • 31. 4-31Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Efficiency of the Closest-Pair AlgorithmEfficiency of the Closest-Pair Algorithm Running time of the algorithm (without sorting) is:Running time of the algorithm (without sorting) is: T(T(nn) = 2T() = 2T(nn/2) + M(/2) + M(nn), where M(), where M(nn)) ∈∈ ΘΘ((nn)) By the Master Theorem (withBy the Master Theorem (with aa = 2,= 2, bb = 2,= 2, dd = 1)= 1) T(T(nn)) ∈∈ ΘΘ((nn loglog nn)) So the total time isSo the total time is ΘΘ((nn loglog nn).).
  • 32. 4-32Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Quickhull AlgorithmQuickhull Algorithm Convex hullConvex hull: smallest convex set that includes given points. An: smallest convex set that includes given points. An O(n^3) bruteforce time is given in Levitin, Ch 3.O(n^3) bruteforce time is given in Levitin, Ch 3. Assume points are sorted byAssume points are sorted by xx-coordinate values-coordinate values IdentifyIdentify extreme pointsextreme points PP11 andand PP22 (leftmost and rightmost)(leftmost and rightmost) ComputeCompute upper hullupper hull recursively:recursively: • find pointfind point PPmaxmax that is farthest away from linethat is farthest away from line PP11PP22 • compute the upper hull of the points to the left of linecompute the upper hull of the points to the left of line PP11PPmaxmax • compute the upper hull of the points to the left of linecompute the upper hull of the points to the left of line PPmaxmaxPP22 ComputeCompute lower hulllower hull in a similar mannerin a similar manner P1 P2 Pmax
  • 33. 4-33Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 Efficiency of Quickhull AlgorithmEfficiency of Quickhull Algorithm Finding point farthest away from lineFinding point farthest away from line PP11PP22 can be done incan be done in linear timelinear time Time efficiency:Time efficiency: T(n) = T(x) + T(y) + T(z) + T(v) + O(n),T(n) = T(x) + T(y) + T(z) + T(v) + O(n), where x + y + z +v <= n.where x + y + z +v <= n. • worst case:worst case: ΘΘ((nn22 )) T(n) = T(n-1) + O(n)T(n) = T(n-1) + O(n) • average case:average case: ΘΘ((nn) (under reasonable assumptions about) (under reasonable assumptions about distribution of points given)distribution of points given) If points are not initially sorted byIf points are not initially sorted by xx-coordinate value, this-coordinate value, this can be accomplished incan be accomplished in O(O(nn loglog nn) time) time SeveralSeveral O(O(nn loglog nn)) algorithms for convex hull are knownalgorithms for convex hull are known

Notas do Editor

  1. Go over this example in detail, then do another example of merging, something like: (1 2 5 7 9) (3 4 6)
  2. even if not analyzing in detail, show the recurrence for mergesort in worst case: T(n) = 2 T(n/2) + (n-1) worst case comparisons for merge
  3. &amp;lt;number&amp;gt; Unfortunately, d is not necessarily the smallest distance between all pairs of points in S1 and S2 because a closer pair of points can lie on the opposite sides separating the line. When we combine the two sets, we must examine such points. (Illustrate this on the diagram)
  4. &amp;lt;number&amp;gt;
  5. &amp;lt;number&amp;gt;