SlideShare a Scribd company logo
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1
Divide-and-Conquer
Divide-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
Divide instance of problem into two or more
smaller instances
smaller instances
2.
2. Solve smaller instances recursively
Solve smaller instances recursively
3.
3. Obtain solution to original (larger) instance by
Obtain solution to original (larger) instance by
combining these solutions
combining these solutions
Distinction between divide and conquer and decrease and conquer is
Distinction between divide and conquer and decrease and conquer is
somewhat vague. Dec/con typically does not solve both subproblems.
somewhat vague. Dec/con typically does not solve both subproblems.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
2
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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
3
Divide-and-Conquer Examples
Divide-and-Conquer Examples
 Sorting: mergesort and quicksort
Sorting: mergesort and quicksort
 Binary tree traversals
Binary tree traversals
 Multiplication of large integers
Multiplication of large integers
 Matrix multiplication: Strassen’s algorithm
Matrix multiplication: Strassen’s algorithm
 Closest-pair and convex-hull algorithms
Closest-pair and convex-hull algorithms
 Binary search: decrease-by-half (or degenerate divide&conq.)
Binary search: decrease-by-half (or degenerate divide&conq.)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
4
General Divide-and-Conquer Recurrence
General Divide-and-Conquer Recurrence
T
T(
(n
n) =
) = aT
aT(
(n/b
n/b) +
) + f
f (
(n
n)
) where
where f
f(
(n
n)
) 
 
(
(n
nd
d
),
), d
d 
 0
0
Master Theorem
Master Theorem: If
: If a < b
a < bd
d
,
, T
T(
(n
n)
) 
 
(
(n
nd
d
)
)
If
If a = b
a = bd
d
,
, T
T(
(n
n)
) 
 
(
(n
nd
d
log
log n
n)
)
If
If a > b
a > bd
d
,
, T
T(
(n
n)
) 
 
(
(n
nlog
log b
b a
a
)
)
Note: The same results hold with O instead of
Note: The same results hold with O instead of 
.
.
Examples:
Examples: T
T(
(n
n) = 4
) = 4T
T(
(n
n/2) +
/2) + n
n 
 T
T(
(n
n)
) 
 ?
?
T
T(
(n
n) = 4
) = 4T
T(
(n
n/2) +
/2) + n
n2
2

 T
T(
(n
n)
) 
 ?
?
T
T(
(n
n) = 4
) = 4T
T(
(n
n/2) +
/2) + n
n3
3

 T
T(
(n
n)
) 
 ?
?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
5
Mergesort Example
Mergesort 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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
6
Mergesort
Mergesort
 Split array A[0..
Split array A[0..n
n-1] in two about equal halves and make
-1] in two about equal halves and make
copies of each half in arrays B and C
copies of each half in arrays B and C
 Sort arrays B and C recursively
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
Repeat the following until no elements remain in one of
the arrays:
the arrays:
– compare the first elements in the remaining
compare the first elements in the remaining
unprocessed portions of the arrays
unprocessed portions of the arrays
– copy the smaller of the two into A, while
copy the smaller of the two into A, while
incrementing the index indicating the unprocessed
incrementing the index indicating the unprocessed
portion of that array
portion 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
copy the remaining unprocessed elements from the
other array into A.
other array into A.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
7
Pseudocode of Mergesort
Pseudocode of Mergesort
Thinking about recursive programs: Assume recursive calls work
and ask does the whole program then work?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
8
Pseudocode of Merge
Pseudocode of Merge
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
9
Analysis of Mergesort
Analysis of Mergesort
 Number of key comparisons – recurrence:
Number of key comparisons – recurrence:
C
C(
(n
n) = …
) = …
For merge step:
For merge step:
C’
C’(
(n
n) = …
) = …
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10
Analysis of Mergesort
Analysis of Mergesort
 Number of key comparisons – recurrence:
Number of key comparisons – recurrence:
C
C(
(n
n) = 2
) = 2C
C(
(n
n/2) +
/2) + C
C’(
’(n) =
n) = 2
2C
C(
(n
n/2) +
/2) + n
n-1
-1
For merge step – worst case:
For merge step – worst case:
C’
C’(
(n
n) =
) = n
n-1
-1
 How to solve?
How to solve?
 Number of calls? Number of active calls?
Number of calls? Number of active calls?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
11
General Divide-and-Conquer Recurrence
General Divide-and-Conquer Recurrence
T
T(
(n
n) =
) = aT
aT(
(n/b
n/b) +
) + f
f (
(n
n)
) where
where f
f(
(n
n)
) 
 
(
(n
nd
d
),
), d
d 
 0
0
Master Theorem
Master Theorem: If
: If a < b
a < bd
d
,
, T
T(
(n
n)
) 
 
(
(n
nd
d
)
)
If
If a = b
a = bd
d
,
, T
T(
(n
n)
) 
 
(
(n
nd
d
log
log n
n)
)
If
If a > b
a > bd
d
,
, T
T(
(n
n)
) 
 
(
(n
nlog
log b
b a
a
)
)
Using log – Take log
Using log – Take log b
b of both sides:
of both sides:
Case 1: log
Case 1: logb
b a < d
a < d,
, T
T(
(n
n)
) 
 
(
(n
nd
d
)
)
Case 2: log
Case 2: logb
b a = d
a = d,
, T
T(
(n
n)
) 
 
(
(n
nd
d
log
logb
b n
n)
)
Case 3: log
Case 3: logb
b a > d
a > d,
, T
T(
(n
n)
) 
 
(
(n
nlog
log b
b a
a
)
)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
12
Analysis of Mergesort
Analysis of Mergesort
 All cases have same efficiency:
All cases have same efficiency: Θ
Θ(
(n
n log
log n
n)
)
 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:
theoretical minimum for comparison-based sorting:
- Theoretical min:
- Theoretical min: 
log
log2
2 n
n!
!
 ≈
≈ n
n log
log2
2 n
n - 1.44
- 1.44n
n
 Space requirement:
Space requirement:
• Book’s algorithm?
Book’s algorithm?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
13
Analysis of Mergesort
Analysis of Mergesort
 All cases have same efficiency:
All cases have same efficiency: Θ
Θ(
(n
n log
log n
n)
)
 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:
theoretical minimum for comparison-based sorting:
- Theoretical min:
- Theoretical min: 
log
log2
2 n
n!
!
 ≈
≈ n
n log
log2
2 n
n - 1.44
- 1.44n
n
 Space requirement:
Space requirement:
• Book’s algorithm: n + n Sum(1+1/2+1/4+…) = n + n
Book’s algorithm: n + n Sum(1+1/2+1/4+…) = n + n Θ
Θ(2)=
(2)= Θ
Θ(3n)
(3n)
• Can be done
Can be done Θ
Θ(2
(2n
n) : Don’t copy in sort; just specify bounds, copy
) : Don’t copy in sort; just specify bounds, copy
into extra array on merge, then copy back
into extra array on merge, then copy back
– Any other space required?
Any other space required?
• Can be implemented in place. How …
Can be implemented in place. How …
 Can be implemented without recursion (bottom-up)
Can be implemented without recursion (bottom-up)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
14
Mergesort Example
Mergesort 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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
15
Quicksort
Quicksort
 Select a
Select a pivot
pivot (partitioning element) – here, the first element
(partitioning element) – here, the first element
 Rearrange the list so that all the elements in the first
Rearrange the list so that all the elements in the first s
s
positions are smaller than or equal to the pivot and all the
positions are smaller than or equal to the pivot and all the
elements in the remaining
elements in the remaining n-s
n-s positions are larger than or
positions 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 position
subarray — the pivot is now in its final position
 Sort the two subarrays recursively
Sort the two subarrays recursively
p
A[i]p A[i]p
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
16
Quicksort Algorithm
Quicksort Algorithm
QS (A, l, r)
QS (A, l, r)
if l < r then
if l < r then
s = partition (A, l, r)
s = partition (A, l, r)
QS (A, , )
QS (A, , )
QS (A, , )
QS (A, , )
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
17
Quicksort Algorithm
Quicksort Algorithm
QS (A, l, r)
QS (A, l, r)
if l < r then
if l < r then
s = partition (A, l, r)
s = partition (A, l, r)
QS (A, l , s-1)
QS (A, l , s-1)
QS (A, s+1, r )
QS (A, s+1, r )
Ask later: What if we skip final swap and
Ask later: What if we skip final swap and
use simpler bounds for first recursive call?
use simpler bounds for first recursive call?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
18
Hoare’s (Two-way) Partition Algorithm
Hoare’s (Two-way) Partition Algorithm
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
19
Quicksort Example
Quicksort Example
5 3 1 9 8 2 4 7
5 3 1 9 8 2 4 7
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
20
Quicksort Recursion Tree
Quicksort Recursion Tree
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
5 3 1 9 8 2 4 7
5 3 1 9 8 2 4 7
0 .. 7 / s=4
0 .. 7 / s=4
0 .. 3 / s=x
0 .. 3 / s=x
0 .. s-1 / s=?
0 .. s-1 / s=?
s+1 .. 3
s+1 .. 3
5 .. 7 / s=y
5 .. 7 / s=y
5 .. y-1 / s=?
5 .. y-1 / s=?
y+1 .. 7 / s=?
y+1 .. 7 / s=?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
21
Analysis of Quicksort
Analysis of Quicksort
Recurrences:
Recurrences:
 Best case:
Best case:
 Worst case:
Worst case:
 Average case:
Average case:
Performance:
Performance:
 Best case:
Best case:
 Worst case:
Worst case:
 Average case:
Average case:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
22
Analysis of Quicksort
Analysis of Quicksort
Recurrences:
Recurrences:
 Best case: T(n) = T(n/2) +
Best case: T(n) = T(n/2) + Θ
Θ (
(n)
n)
 Worst case:
Worst case: T(n) = T(n-1) +
T(n) = T(n-1) + Θ
Θ (
(n)
n)
 Average case:
Average case:
Performance:
Performance:
 Best case:
Best case:
 Worst case:
Worst case:
 Average case:
Average case:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
23
Analysis of Quicksort
Analysis of Quicksort
Recurrences:
Recurrences:
 Best case: T(n) = T(n/2) +
Best case: T(n) = T(n/2) + Θ
Θ (
(n)
n)
 Worst case:
Worst case: T(n) = T(n-1) +
T(n) = T(n-1) + Θ
Θ (
(n)
n)
 Average case:
Average case:
Performance:
Performance:
 Best case: split in the middle
Best case: split in the middle —
— Θ
Θ(
(n
n log
log n
n)
)
 Worst case: sorted array! —
Worst case: sorted array! — Θ
Θ(
(n
n2
2
)
)
 Average case: random arrays
Average case: random arrays —
— Θ
Θ(
(n
n log
log n
n)
)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
24
Analysis of Quicksort
Analysis of Quicksort
 Problems:
Problems:
• Duplicate elements
Duplicate elements
 Improvements:
Improvements:
• better pivot selection: median of three partitioning
better pivot selection: median of three partitioning
• switch to insertion sort on small sub-arrays
switch to insertion sort on small sub-arrays
• elimination of recursion – How?
elimination of recursion – How?
These combine to 20-25% improvement
These combine to 20-25% improvement
 Improvements: Dual Pivot
Improvements: Dual Pivot
 Method of choice for internal sorting of large files (
Method of choice for internal sorting of large files (n
n ≥
≥
10000)
10000)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
25
Binary Tree Algorithms
Binary 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)
Algorithm
Algorithm Inorder
Inorder(
(T
T)
)
a
a a
a
b c b c
b c b c
d e d e
 
d e d e
 
   
   
Efficiency:
Efficiency: Θ
Θ(
(n
n)
)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
26
Binary Tree Algorithms
Binary 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)
Algorithm
Algorithm Inorder
Inorder(
(T
T)
)
if
if T
T 
 
 a
a a
a
Inorder
Inorder(
(T
Tleft
left)
) b c b c
b c b c
print(root of
print(root of T
T)
) d e d e
 
d e d e
 
Inorder
Inorder(
(T
Tright
right)
)    
   
Efficiency:
Efficiency: Θ
Θ(
(n
n)
)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
27
Binary Tree Algorithms (cont.)
Binary Tree Algorithms (cont.)
Ex. 2: Computing the height of a binary tree
Ex. 2: Computing the height of a binary tree
T T
L R
h
h(
(T
T) = ??
) = ??
Efficiency:
Efficiency: Θ
Θ(
(n
n)
)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
28
Binary Tree Algorithms (cont.)
Binary Tree Algorithms (cont.)
Ex. 2: Computing the height of a binary tree
Ex. 2: Computing the height of a binary tree
T T
L R
h
h(
(T
T) = max{
) = max{h
h(
(T
TL
L),
), h
h(
(T
TR
R)} + 1 if
)} + 1 if T
T 
 
 and
and h
h(
(
) = -1
) = -1
Efficiency:
Efficiency: Θ
Θ(
(n
n)
)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
29
Multiplication of Large Integers
Multiplication of Large Integers
Consider the problem of multiplying two (large)
Consider the problem of multiplying two (large) n
n-digit integers
-digit integers
represented by arrays of their digits such as:
represented by arrays of their digits such as:
A = 12345678901357986429 B = 87654321284820912836
A = 12345678901357986429 B = 87654321284820912836
The grade-school algorithm:
The grade-school algorithm:
a
a1
1 a
a2
2 …
… a
an
n
b
b1
1 b
b2
2 …
… b
bn
n
(
(d
d10
10)
) d
d11
11d
d12
12 …
… d
d1
1n
n
(
(d
d20
20)
) d
d21
21d
d22
22 …
… d
d2
2n
n
… … … … … … …
… … … … … … …
(
(d
dn
n0
0)
) d
dn
n1
1d
dn
n2
2 …
… d
dnn
nn
Efficiency: ?
Efficiency: ??
? one-digit multiplications
one-digit multiplications
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
30
Multiplication of Large Integers
Multiplication of Large Integers
Consider the problem of multiplying two (large)
Consider the problem of multiplying two (large) n
n-digit integers
-digit integers
represented by arrays of their digits such as:
represented by arrays of their digits such as:
A = 12345678901357986429 B = 87654321284820912836
A = 12345678901357986429 B = 87654321284820912836
The grade-school algorithm:
The grade-school algorithm:
a
a1
1 a
a2
2 …
… a
an
n
b
b1
1 b
b2
2 …
… b
bn
n
(
(d
d10
10)
) d
d11
11d
d12
12 …
… d
d1
1n
n
(
(d
d20
20)
) d
d21
21d
d22
22 …
… d
d2
2n
n
… … … … … … …
… … … … … … …
(
(d
dn
n0
0)
) d
dn
n1
1d
dn
n2
2 …
… d
dnn
nn
Efficiency:
Efficiency: n
n2
2
one-digit multiplications
one-digit multiplications
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
31
First Divide-and-Conquer Algorithm
First Divide-and-Conquer Algorithm
A small example: A
A small example: A 
 B where A = 2135 and B = 4014
B where A = 2135 and B = 4014
A = (21·10
A = (21·102
2
+ 35), B = (40 ·10
+ 35), B = (40 ·102
2
+ 14)
+ 14)
So, A
So, A 
 B = (21 ·10
B = (21 ·102
2
+ 35)
+ 35) 
 (40 ·10
(40 ·102
2
+ 14)
+ 14)
= 21
= 21 
 40 ·10
40 ·104
4
+ (21
+ (21 
 14 + 35
14 + 35 
 40) ·10
40) ·102
2
+ 35
+ 35 
 14
14
In general, if A = A
In general, if A = A1
1A
A2
2 and B = B
and B = B1
1B
B2
2 (where A and B are
(where A and B are n
n-digit,
-digit,
and A
and A1
1, A
, A2
2, B
, B1
1,
, B
B2
2 are
are n /
n / 2-digit numbers),
2-digit numbers),
A
A 
 B = A
B = A1
1 
 B
B1
1·10
·10n
n
+ (A
+ (A1
1 
 B
B2
2 + A
+ A2
2 
 B
B1
1) ·10
) ·10n/
n/2
2
+ A
+ A2
2 
 B
B2
2
Recurrence for the number of one-digit multiplications M(
Recurrence for the number of one-digit multiplications M(n
n):
):
M(
M(n
n) = 4M(
) = 4M(n
n/2), M(1) = 1
/2), M(1) = 1
Solution: M(
Solution: M(n
n) =
) = n
n2
2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
32
Second Divide-and-Conquer Algorithm
Second Divide-and-Conquer Algorithm
A
A 
 B =
B = A
A1
1 
 B
B1
1·10
·10n
n
+
+ (A
(A1
1 
 B
B2
2 + A
+ A2
2 
 B
B1
1)
) ·10
·10n/
n/2
2
+
+ A
A2
2 
 B
B2
2
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
(A1
1 + A
+ A2
2 )
) 
 (B
(B1
1 + B
+ B2
2 )
) =
= A
A1
1 
 B
B1
1 +
+ (A
(A1
1 
 B
B2
2 + A
+ A2
2 
 B
B1
1)
) +
+ A
A2
2 
 B
B2
2,
,
I.e.,
I.e., (A
(A1
1 
 B
B2
2 + A
+ A2
2 
 B
B1
1)
) =
= (A
(A1
1 + A
+ A2
2 )
) 
 (B
(B1
1 + B
+ B2
2 )
) -
- A
A1
1 
 B
B1
1 -
- A
A2
2 
 B
B2,
2,
which requires only 3 multiplications at the expense of (4-1=3)
which requires only 3 multiplications at the expense of (4-1=3)
extra additions and subtractions (2 adds and 2 subs – 1 add)
extra additions and subtractions (2 adds and 2 subs – 1 add)
Recurrence for the number of multiplications M(
Recurrence for the number of multiplications M(n
n):
):
M(
M(n
n) = 3
) = 3M
M(
(n
n/2), M(1) = 1
/2), M(1) = 1
Solution: M(
Solution: M(n
n) = 3
) = 3log
log 2
2n
n
=
= n
nlog
log 2
23
3
≈
≈ n
n1.585
1.585
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
33
Example of Large-Integer Multiplication
Example of Large-Integer Multiplication
2135
2135 
 4014
4014
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
34
Strassen’s Matrix Multiplication
Strassen’s Matrix Multiplication
Strassen observed [1969] that the product of two matrices can be
Strassen observed [1969] that the product of two matrices can be
computed as follows:
computed as follows:
C
C00
00 C
C01
01 A
A00
00 A
A01
01 B
B00
00 B
B01
01
= *
= *
C
C10
10 C
C11
11 A
A10
10 A
A11
11 B
B10
10 B
B11
11
M
M1
1 + M
+ M4
4 - M
- M5
5 + M
+ M7
7 M
M3
3 + M
+ M5
5
=
=
M
M2
2 + M
+ M4
4 M
M1
1 + M
+ M3
3 - M
- M2
2 + M
+ M6
6
 Example: M
Example: M2
2 + M
+ M4
4 = (A
= (A10
10 + A
+ A11
11)
) 
 B
B00
00 + A
+ A11
11 
 (B
(B10
10 -
- B
B00
00)
)
 = A
= A10
10B
B00
00 + A
+ A11
11B
B10
10
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
35
Formulas for Strassen’s Algorithm
Formulas for Strassen’s Algorithm
M
M1
1 = (A
= (A00
00 + A
+ A11
11)
) 
 (B
(B00
00 +
+ B
B11
11)
)
M
M2
2 = (A
= (A10
10 + A
+ A11
11)
) 
 B
B00
00
M
M3
3 = A
= A00
00 
 (B
(B01
01 -
- B
B11
11)
)
M
M4
4 = A
= A11
11 
 (B
(B10
10 -
- B
B00
00)
)
M
M5
5 = (A
= (A00
00 + A
+ A01
01)
) 
 B
B11
11
M
M6
6 = (A
= (A10
10 - A
- A00
00)
) 
 (B
(B00
00 +
+ B
B01
01)
)
M
M7
7 = (A
= (A01
01 - A
- A11
11)
) 
 (B
(B10
10 +
+ B
B11
11)
)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
36
Analysis of Strassen’s Algorithm
Analysis of Strassen’s Algorithm
If
If n
n 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(n
n) = 7M(
) = 7M(n
n/2), M(1) = 1
/2), M(1) = 1
Solution: M(
Solution: M(n
n) = 7
) = 7log
log 2
2n
n
=
= n
nlog
log 2
27
7
≈
≈ n
n2.807
2.807
vs.
vs. n
n3
3
of brute-force alg.
of brute-force alg.
Algorithms with better asymptotic efficiency are known but they
Algorithms with better asymptotic efficiency are known but they
are even more complex.
are even more complex.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
37
Closest-Pair Problem by Divide-and-Conquer
Closest-Pair Problem by Divide-and-Conquer
Step 1 Divide the points given into two subsets
Step 1 Divide the points given into two subsets P
Pl
l and
and P
Pr
r by a
by a
vertical line
vertical line x
x =
= m
m so that half the points lie to the left or on
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.
the line and half the points lie to the right or on the line.
x = m
d l
d
r
d d
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
38
Closest-Pair Driver
Closest-Pair Driver
 CP_Driver(A)
CP_Driver(A)
 Pre => A(1..n), n > 1, is an array of (X, Y), pairs
Pre => A(1..n), n > 1, is an array of (X, Y), pairs
 Post => Returns distance between closest pair in A
Post => Returns distance between closest pair in A
 P(1 .. n) := Copy of A, sorted by X
P(1 .. n) := Copy of A, sorted by X
 Q(1 .. n) := Copy of A, sorted by Y
Q(1 .. n) := Copy of A, sorted by Y
 return CP(P, Q)
return CP(P, Q)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
39
Closest-Pair Problem by Divide-and-Conquer
Closest-Pair Problem by Divide-and-Conquer
 CP(P, Q) – Pre=> P (1..n) and Q (1..n) have same points, P is x-sorted, Q y-sorted
CP(P, Q) – Pre=> P (1..n) and Q (1..n) have same points, P is x-sorted, Q y-sorted
• If n <= 3 then return CPBF(P)
If n <= 3 then return CPBF(P)
• Copy P(1 .. n/2) into PL(1 .. n/2)
Copy P(1 .. n/2) into PL(1 .. n/2)
• Distribute-copy the same n/2 points from Q into QL (anti-merge)
Distribute-copy the same n/2 points from Q into QL (anti-merge)
• Copy P(n/2+1 .. n) into PR(1 .. n/2)
Copy P(n/2+1 .. n) into PR(1 .. n/2)
• Distribute-copy the same n/2 points from Q into QR !! Careful
Distribute-copy the same n/2 points from Q into QR !! Careful
• dL := CP(PL, QL); dR := CP(PR, QR)
dL := CP(PL, QL); dR := CP(PR, QR)
• d := min(dL, dR); dsq := d ** 2
d := min(dL, dR); dsq := d ** 2
• midx = P(n/2).x
midx = P(n/2).x
• strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d
strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d
• For i in 1 .. Num – 1 loop
For i in 1 .. Num – 1 loop
• k := i + 1
k := i + 1
• while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop
while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop
• dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2)
dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2)
• k := k + 1
k := k + 1
• return sqrt(dsq)
return sqrt(dsq)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
40
CP Notes
CP Notes
 CP(P, Q) – Pre=>P and Q have same points, P x sorted, Q y sorted y, P(1..n), n> 1
CP(P, Q) – Pre=>P and Q have same points, P x sorted, Q y sorted y, P(1..n), n> 1
• Copy P(1 .. n/2) into PL(1 .. n/2) and same n/2 points from Q into QL
Copy P(1 .. n/2) into PL(1 .. n/2) and same n/2 points from Q into QL
• Copy P(n/2+1 .. n) into PR(1 .. n/2)and same n/2 points from Q into QR
Copy P(n/2+1 .. n) into PR(1 .. n/2)and same n/2 points from Q into QR
• -- Divide Q based on Q(i).x < P(n/2).x. if Q(k).x = P(n/2).x must send to correct side
-- Divide Q based on Q(i).x < P(n/2).x. if Q(k).x = P(n/2).x must send to correct side
• -- N.B. P and Q are sorted. PL, PR, QL, QR are distributed, not resorted.
-- N.B. P and Q are sorted. PL, PR, QL, QR are distributed, not resorted.
• dL := CP(PL, QL); dR := CP(PR, QR)
dL := CP(PL, QL); dR := CP(PR, QR)
• d := min(dL, dR) ; dsq := d** 2
d := min(dL, dR) ; dsq := d** 2 -- For efficiency
-- For efficiency
• midx = P(n/2).x
midx = P(n/2).x -- x value of middle point
-- x value of middle point
• strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d
strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d
• -- Copy points of Q within distance d of middle into strip. Num such points.
-- Copy points of Q within distance d of middle into strip. Num such points.
• For i in 1 .. Num – 1 loop k := i + 1
For i in 1 .. Num – 1 loop k := i + 1
• while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop
while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop
• dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2)
dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2)
• k := k + 1
k := k + 1
• return sqrt(dsq)
return sqrt(dsq)
• -- CP can add sorted P index to points of Q, simplifying distribution!
-- CP can add sorted P index to points of Q, simplifying distribution!
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
41
CP Assignment: Performance Measurement
CP Assignment: Performance Measurement
 CP(P, Q) – Pre=>P and Q have same points, P x sorted, Q y sorted y, P(1..n), n> 1
CP(P, Q) – Pre=>P and Q have same points, P x sorted, Q y sorted y, P(1..n), n> 1
• -- Count number of calls here, using a GLOBAL VARIABLE
-- Count number of calls here, using a GLOBAL VARIABLE
• Copy P(1 .. n/2) into PL(1 .. n/2) and same n/2 points from Q into QL
Copy P(1 .. n/2) into PL(1 .. n/2) and same n/2 points from Q into QL
• Copy P(n/2+1 .. n) into PR(1 .. n/2)and same n/2 points from Q into QR
Copy P(n/2+1 .. n) into PR(1 .. n/2)and same n/2 points from Q into QR
• dL := CP(PL, QL); dR := CP(PR, QR)
dL := CP(PL, QL); dR := CP(PR, QR)
• d := min(dL, dR) ; dsq := d** 2
d := min(dL, dR) ; dsq := d** 2
• midx = P(n/2).x
midx = P(n/2).x
• strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d
strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d
• For i in 1 .. Num – 1 loop k := i + 1
For i in 1 .. Num – 1 loop k := i + 1
• while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop
while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop
• -- Count number of distance calculations here, using a global
-- Count number of distance calculations here, using a global
• dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2)
dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2)
• k := k + 1
k := k + 1
• return sqrt(dsq)
return sqrt(dsq)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
42
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 right
Step 2 Find recursively the closest pairs for the left and right
subsets.
subsets.
Step 3 Set
Step 3 Set d
d = min{
= min{d
dl
l,
, d
dr
r}
}
We can limit our attention to the points in the symmetric
We can limit our attention to the points in the symmetric
vertical strip
vertical strip S
S of width 2
of width 2d
d as possible closest pair. (The
as possible closest pair. (The
points are stored and processed in increasing order of
points are stored and processed in increasing order of
their
their y
y coordinates.)
coordinates.)
Step 4 Scan the points in the vertical strip
Step 4 Scan the points in the vertical strip S
S from the lowest up.
from the lowest up.
For every point
For every point p
p(
(x
x,
,y
y) in the strip, inspect points in
) in the strip, inspect points in
in the strip that may be closer to
in the strip that may be closer to p
p than
than d
d. There can be
. There can be
no more than 5 such points following
no more than 5 such points following p
p on the strip list!
on the strip list!
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
43
Closest Pair by Divide-and-Conquer (cont.)
Closest Pair by Divide-and-Conquer (cont.)
How many points could be in each strip? n/2
How many points could be in each strip? n/2
Brute force would look at all n/2 x n/2 pairs
Brute force would look at all n/2 x n/2 pairs
Why can we restrict ourselves to calculating the distance
Why can we restrict ourselves to calculating the distance
from each point to the next 5 points
from each point to the next 5 points
Because only the next 5 points could be within distance d of the
Because only the next 5 points could be within distance d of the
current point. Why?
current point. Why?
Consider a square of size d: No two points in it can be closer than
Consider a square of size d: No two points in it can be closer than
d. How many points can it contain. No more than 4 (since 5
d. How many points can it contain. No more than 4 (since 5
won’t fit).
won’t fit).
Thus a rectangle of size dx2d can only contain 8 points (or 6 if
Thus a rectangle of size dx2d can only contain 8 points (or 6 if
duplicates are not allowed). So from a point, only check the
duplicates are not allowed). So from a point, only check the
next 7 (or 5) points.
next 7 (or 5) points.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
44
Efficiency of the Closest-Pair Algorithm
Efficiency of the Closest-Pair Algorithm
Running time of the algorithm is described by
Running time of the algorithm is described by
T(
T(n
n) = 2T(
) = 2T(n
n/2) + M(
/2) + M(n
n), where M(
), where M(n
n)
) 
 O(
O(n
n)
)
By the Master Theorem (with
By the Master Theorem (with a
a = 2,
= 2, b
b = 2,
= 2, d
d = 1)
= 1)
T(
T(n
n)
) 
 O(
O(n
n log
log n
n)
)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
45
Quickhull Algorithm
Quickhull Algorithm
Convex hull
Convex hull: smallest convex set that includes given points
: smallest convex set that includes given points
 Assume points are sorted by
Assume points are sorted by x
x-coordinate values
-coordinate values
 Identify
Identify extreme points
extreme points P
P1
1 and
and P
P2
2 (leftmost and rightmost)
(leftmost and rightmost)
 Compute
Compute upper hull
upper hull recursively:
recursively:
• find point
find point P
Pmax
max that is farthest away from line
that is farthest away from line P
P1
1P
P2
2
• compute the upper hull of the points to the left of line
compute the upper hull of the points to the left of line P
P1
1P
Pmax
max
• compute the upper hull of the points to the left of line
compute the upper hull of the points to the left of line P
Pmax
maxP
P2
2
 Compute
Compute lower hull
lower hull in a similar manner
in a similar manner
P1
P2
Pmax
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
46
Efficiency of Quickhull Algorithm
Efficiency of Quickhull Algorithm
 Finding point farthest away from line
Finding point farthest away from line P
P1
1P
P2
2 can be done in
can be done in
linear time
linear time
 Time efficiency:
Time efficiency:
• Assume nu and nl points in upper and lower hulls
Assume nu and nl points in upper and lower hulls
• Performance: T(n) = T(?) + T(?) +
Performance: T(n) = T(?) + T(?) + Θ
Θ(?)
(?)
• Worst case: T(n) = T(?) + T(?) +
Worst case: T(n) = T(?) + T(?) + Θ
Θ(?)
(?)
• Expected case:
Expected case: T(n) = T(?) + T(?) +
T(n) = T(?) + T(?) + Θ
Θ(?)
(?)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
47
Efficiency of Quickhull Algorithm
Efficiency of Quickhull Algorithm
 Finding point farthest away from line
Finding point farthest away from line P
P1
1P
P2
2 can be done in
can be done in
linear time
linear time
 Time efficiency:
Time efficiency:
• Assume nu and nl points in upper and lower hulls
Assume nu and nl points in upper and lower hulls
• Performance: T(n) = T(nu) + T(nl) +
Performance: T(n) = T(nu) + T(nl) + Θ
Θ(n)
(n)
• Worst case: T(n) = T(1) + T(n-1) +
Worst case: T(n) = T(1) + T(n-1) + Θ
Θ n) = ?
n) = ?
• Expected case:
Expected case: T(n) = T(n/2) + T(n/2) +
T(n) = T(n/2) + T(n/2) + Θ
Θ(n) = ?
(n) = ?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
48
Efficiency of Quickhull Algorithm
Efficiency of Quickhull Algorithm
 Finding point farthest away from line
Finding point farthest away from line P
P1
1P
P2
2 can be done in
can be done in
linear time
linear time
 Time efficiency:
Time efficiency:
• Assume nu and nl points in upper and lower hulls
Assume nu and nl points in upper and lower hulls
• Performance: T(n) = T(nu) + T(nl) +
Performance: T(n) = T(nu) + T(nl) + Θ
Θ(n)
(n)
• Worst case: T(n) = T(1) + T(n-1) +
Worst case: T(n) = T(1) + T(n-1) + Θ
Θ(n) =
(n) = Θ
Θ(n^2)
(n^2)
• Expected case:
Expected case: T(n) = T(n/2) + T(n/2) +
T(n) = T(n/2) + T(n/2) + Θ
Θ(n) =
(n) = Θ
Θ(n lg n)
(n lg n)
Can we do better?
Can we do better?
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
49
Efficiency of Quickhull Algorithm
Efficiency of Quickhull Algorithm
 Finding point farthest from line
Finding point farthest from line P
P1
1P
P2
2 can be done in
can be done in Θ
Θ(
(n
n)
)
 Time efficiency:
Time efficiency:
• Assume nu and nl points in upper and lower hulls
Assume nu and nl points in upper and lower hulls
• Performance: T(n) = T(nu) + T(nl) +
Performance: T(n) = T(nu) + T(nl) + Θ
Θ(n)
(n)
• Worst case: T(n) = T(1) + T(n-1) +
Worst case: T(n) = T(1) + T(n-1) + Θ
Θ(n) =
(n) = Θ
Θ(n^2)
(n^2)
• Expected case:
Expected case: T(n) = T(n/2) + T(n/2) +
T(n) = T(n/2) + T(n/2) + Θ
Θ(n) =
(n) = Θ
Θ(n lg n)
(n lg n)
Can we do better? T(n) = T(n/4) + T(n/4) +
Can we do better? T(n) = T(n/4) + T(n/4) + Θ
Θ(n) =
(n) = Θ
Θ(n)
(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
50
Efficiency of Quickhull Algorithm
Efficiency of Quickhull Algorithm
 Finding point farthest from line
Finding point farthest from line P
P1
1P
P2
2 can be done in
can be done in Θ
Θ(
(n
n)
)
 Time efficiency:
Time efficiency:
• worst case:
worst case: Θ
Θ(
(n
n2
2
) (as
) (as quicksort)
quicksort)
• average case:
average case: Θ
Θ(
(n lg n
n lg n) or
) or Θ
Θ(
(n
n)
)
(assuming reasonable distribution of points)
(assuming reasonable distribution of points)
 If points are not initially sorted by
If points are not initially sorted by x
x-coordinate value, this
-coordinate value, this
can be accomplished in
can be accomplished in O(
O(n
n log
log n
n) time
) time
 Several
Several O(
O(n
n log
log n
n)
) algorithms for convex hull are known
algorithms for convex hull are known
 Field is called Computational Geometry
Field is called Computational Geometry

More Related Content

PPT
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
Shanmuganathan C
 
PPT
Ch04n
kpcs2010
 
PPT
divide and conquer ppt in Design and Analysis of Algorithms
dimpuk1
 
PDF
Sienna 4 divideandconquer
chidabdu
 
PPTX
Chapter 4.2 - ADTree_Divide_n_Conquer 2021
g46179042
 
PPTX
Lecture -16-merge sort (slides).pptx
Fazlullah28
 
PDF
Divide and conquer
Emmanuel college
 
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
Shanmuganathan C
 
Ch04n
kpcs2010
 
divide and conquer ppt in Design and Analysis of Algorithms
dimpuk1
 
Sienna 4 divideandconquer
chidabdu
 
Chapter 4.2 - ADTree_Divide_n_Conquer 2021
g46179042
 
Lecture -16-merge sort (slides).pptx
Fazlullah28
 
Divide and conquer
Emmanuel college
 
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 

Similar to daa notes for students in jmtu018.03.02.ppt (20)

PPT
MergesortQuickSort.ppt
AliAhmad38278
 
PPTX
lecture 1(Fundamental of algorithms).pptx
TejaYadav27
 
PPTX
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
madhu614742
 
PDF
Ada notes
VIKAS SINGH BHADOURIA
 
PPTX
Algorithim lec1.pptx
rediet43
 
PPT
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
DarioVelo1
 
PPT
Algorithms and Data structures: Merge Sort
pharmaci
 
PPT
Introduction
pilavare
 
PPTX
Merge sort and quick sort
Shakila Mahjabin
 
PDF
Analysis and design of algorithms part2
Deepak John
 
PDF
Daa chapter5
B.Kirron Reddi
 
PPTX
Divide and Conquer - Part 1
Amrinder Arora
 
PPT
Algorithm Design and Analysis
Reetesh Gupta
 
PPT
Introduction to basic algorithm knowledge.ppt
Adrianaany
 
PPT
Types of Algorithms.ppt
ALIZAIB KHAN
 
PPT
Divide and conquer
Muhammad Sarfraz
 
PPT
Cis435 week01
ashish bansal
 
PPT
MITP Ch 2jbhjbhjbhjbhjbhjb Sbhjblides.ppt
123456789hangnhigau
 
PPT
05 dc1
Hira Gul
 
MergesortQuickSort.ppt
AliAhmad38278
 
lecture 1(Fundamental of algorithms).pptx
TejaYadav27
 
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
madhu614742
 
Algorithim lec1.pptx
rediet43
 
02_Gffdvxvvxzxzczcczzczcczczczxvxvxvds2.ppt
DarioVelo1
 
Algorithms and Data structures: Merge Sort
pharmaci
 
Introduction
pilavare
 
Merge sort and quick sort
Shakila Mahjabin
 
Analysis and design of algorithms part2
Deepak John
 
Daa chapter5
B.Kirron Reddi
 
Divide and Conquer - Part 1
Amrinder Arora
 
Algorithm Design and Analysis
Reetesh Gupta
 
Introduction to basic algorithm knowledge.ppt
Adrianaany
 
Types of Algorithms.ppt
ALIZAIB KHAN
 
Divide and conquer
Muhammad Sarfraz
 
Cis435 week01
ashish bansal
 
MITP Ch 2jbhjbhjbhjbhjbhjb Sbhjblides.ppt
123456789hangnhigau
 
05 dc1
Hira Gul
 
Ad

Recently uploaded (20)

PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
CDH. pptx
AneetaSharma15
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Virus sequence retrieval from NCBI database
yamunaK13
 
CDH. pptx
AneetaSharma15
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
Ad

daa notes for students in jmtu018.03.02.ppt

  • 1. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1 Divide-and-Conquer Divide-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 Divide instance of problem into two or more smaller instances smaller instances 2. 2. Solve smaller instances recursively Solve smaller instances recursively 3. 3. Obtain solution to original (larger) instance by Obtain solution to original (larger) instance by combining these solutions combining these solutions Distinction between divide and conquer and decrease and conquer is Distinction between divide and conquer and decrease and conquer is somewhat vague. Dec/con typically does not solve both subproblems. somewhat vague. Dec/con typically does not solve both subproblems.
  • 2. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2 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
  • 3. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3 Divide-and-Conquer Examples Divide-and-Conquer Examples  Sorting: mergesort and quicksort Sorting: mergesort and quicksort  Binary tree traversals Binary tree traversals  Multiplication of large integers Multiplication of large integers  Matrix multiplication: Strassen’s algorithm Matrix multiplication: Strassen’s algorithm  Closest-pair and convex-hull algorithms Closest-pair and convex-hull algorithms  Binary search: decrease-by-half (or degenerate divide&conq.) Binary search: decrease-by-half (or degenerate divide&conq.)
  • 4. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4 General Divide-and-Conquer Recurrence General Divide-and-Conquer Recurrence T T( (n n) = ) = aT aT( (n/b n/b) + ) + f f ( (n n) ) where where f f( (n n) )    ( (n nd d ), ), d d   0 0 Master Theorem Master Theorem: If : If a < b a < bd d , , T T( (n n) )    ( (n nd d ) ) If If a = b a = bd d , , T T( (n n) )    ( (n nd d log log n n) ) If If a > b a > bd d , , T T( (n n) )    ( (n nlog log b b a a ) ) Note: The same results hold with O instead of Note: The same results hold with O instead of  . . Examples: Examples: T T( (n n) = 4 ) = 4T T( (n n/2) + /2) + n n   T T( (n n) )   ? ? T T( (n n) = 4 ) = 4T T( (n n/2) + /2) + n n2 2   T T( (n n) )   ? ? T T( (n n) = 4 ) = 4T T( (n n/2) + /2) + n n3 3   T T( (n n) )   ? ?
  • 5. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 5 Mergesort Example Mergesort 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
  • 6. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6 Mergesort Mergesort  Split array A[0.. Split array A[0..n n-1] in two about equal halves and make -1] in two about equal halves and make copies of each half in arrays B and C copies of each half in arrays B and C  Sort arrays B and C recursively 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 Repeat the following until no elements remain in one of the arrays: the arrays: – compare the first elements in the remaining compare the first elements in the remaining unprocessed portions of the arrays unprocessed portions of the arrays – copy the smaller of the two into A, while copy the smaller of the two into A, while incrementing the index indicating the unprocessed incrementing the index indicating the unprocessed portion of that array portion 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 copy the remaining unprocessed elements from the other array into A. other array into A.
  • 7. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7 Pseudocode of Mergesort Pseudocode of Mergesort Thinking about recursive programs: Assume recursive calls work and ask does the whole program then work?
  • 8. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8 Pseudocode of Merge Pseudocode of Merge
  • 9. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 9 Analysis of Mergesort Analysis of Mergesort  Number of key comparisons – recurrence: Number of key comparisons – recurrence: C C( (n n) = … ) = … For merge step: For merge step: C’ C’( (n n) = … ) = …
  • 10. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10 Analysis of Mergesort Analysis of Mergesort  Number of key comparisons – recurrence: Number of key comparisons – recurrence: C C( (n n) = 2 ) = 2C C( (n n/2) + /2) + C C’( ’(n) = n) = 2 2C C( (n n/2) + /2) + n n-1 -1 For merge step – worst case: For merge step – worst case: C’ C’( (n n) = ) = n n-1 -1  How to solve? How to solve?  Number of calls? Number of active calls? Number of calls? Number of active calls?
  • 11. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11 General Divide-and-Conquer Recurrence General Divide-and-Conquer Recurrence T T( (n n) = ) = aT aT( (n/b n/b) + ) + f f ( (n n) ) where where f f( (n n) )    ( (n nd d ), ), d d   0 0 Master Theorem Master Theorem: If : If a < b a < bd d , , T T( (n n) )    ( (n nd d ) ) If If a = b a = bd d , , T T( (n n) )    ( (n nd d log log n n) ) If If a > b a > bd d , , T T( (n n) )    ( (n nlog log b b a a ) ) Using log – Take log Using log – Take log b b of both sides: of both sides: Case 1: log Case 1: logb b a < d a < d, , T T( (n n) )    ( (n nd d ) ) Case 2: log Case 2: logb b a = d a = d, , T T( (n n) )    ( (n nd d log logb b n n) ) Case 3: log Case 3: logb b a > d a > d, , T T( (n n) )    ( (n nlog log b b a a ) )
  • 12. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 12 Analysis of Mergesort Analysis of Mergesort  All cases have same efficiency: All cases have same efficiency: Θ Θ( (n n log log n n) )  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: theoretical minimum for comparison-based sorting: - Theoretical min: - Theoretical min:  log log2 2 n n! !  ≈ ≈ n n log log2 2 n n - 1.44 - 1.44n n  Space requirement: Space requirement: • Book’s algorithm? Book’s algorithm?
  • 13. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 13 Analysis of Mergesort Analysis of Mergesort  All cases have same efficiency: All cases have same efficiency: Θ Θ( (n n log log n n) )  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: theoretical minimum for comparison-based sorting: - Theoretical min: - Theoretical min:  log log2 2 n n! !  ≈ ≈ n n log log2 2 n n - 1.44 - 1.44n n  Space requirement: Space requirement: • Book’s algorithm: n + n Sum(1+1/2+1/4+…) = n + n Book’s algorithm: n + n Sum(1+1/2+1/4+…) = n + n Θ Θ(2)= (2)= Θ Θ(3n) (3n) • Can be done Can be done Θ Θ(2 (2n n) : Don’t copy in sort; just specify bounds, copy ) : Don’t copy in sort; just specify bounds, copy into extra array on merge, then copy back into extra array on merge, then copy back – Any other space required? Any other space required? • Can be implemented in place. How … Can be implemented in place. How …  Can be implemented without recursion (bottom-up) Can be implemented without recursion (bottom-up)
  • 14. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 14 Mergesort Example Mergesort 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
  • 15. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15 Quicksort Quicksort  Select a Select a pivot pivot (partitioning element) – here, the first element (partitioning element) – here, the first element  Rearrange the list so that all the elements in the first Rearrange the list so that all the elements in the first s s positions are smaller than or equal to the pivot and all the positions are smaller than or equal to the pivot and all the elements in the remaining elements in the remaining n-s n-s positions are larger than or positions 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 position subarray — the pivot is now in its final position  Sort the two subarrays recursively Sort the two subarrays recursively p A[i]p A[i]p
  • 16. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16 Quicksort Algorithm Quicksort Algorithm QS (A, l, r) QS (A, l, r) if l < r then if l < r then s = partition (A, l, r) s = partition (A, l, r) QS (A, , ) QS (A, , ) QS (A, , ) QS (A, , )
  • 17. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17 Quicksort Algorithm Quicksort Algorithm QS (A, l, r) QS (A, l, r) if l < r then if l < r then s = partition (A, l, r) s = partition (A, l, r) QS (A, l , s-1) QS (A, l , s-1) QS (A, s+1, r ) QS (A, s+1, r ) Ask later: What if we skip final swap and Ask later: What if we skip final swap and use simpler bounds for first recursive call? use simpler bounds for first recursive call?
  • 18. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18 Hoare’s (Two-way) Partition Algorithm Hoare’s (Two-way) Partition Algorithm
  • 19. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19 Quicksort Example Quicksort Example 5 3 1 9 8 2 4 7 5 3 1 9 8 2 4 7
  • 20. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 20 Quicksort Recursion Tree Quicksort Recursion Tree 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 5 3 1 9 8 2 4 7 5 3 1 9 8 2 4 7 0 .. 7 / s=4 0 .. 7 / s=4 0 .. 3 / s=x 0 .. 3 / s=x 0 .. s-1 / s=? 0 .. s-1 / s=? s+1 .. 3 s+1 .. 3 5 .. 7 / s=y 5 .. 7 / s=y 5 .. y-1 / s=? 5 .. y-1 / s=? y+1 .. 7 / s=? y+1 .. 7 / s=?
  • 21. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 21 Analysis of Quicksort Analysis of Quicksort Recurrences: Recurrences:  Best case: Best case:  Worst case: Worst case:  Average case: Average case: Performance: Performance:  Best case: Best case:  Worst case: Worst case:  Average case: Average case:
  • 22. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 22 Analysis of Quicksort Analysis of Quicksort Recurrences: Recurrences:  Best case: T(n) = T(n/2) + Best case: T(n) = T(n/2) + Θ Θ ( (n) n)  Worst case: Worst case: T(n) = T(n-1) + T(n) = T(n-1) + Θ Θ ( (n) n)  Average case: Average case: Performance: Performance:  Best case: Best case:  Worst case: Worst case:  Average case: Average case:
  • 23. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 23 Analysis of Quicksort Analysis of Quicksort Recurrences: Recurrences:  Best case: T(n) = T(n/2) + Best case: T(n) = T(n/2) + Θ Θ ( (n) n)  Worst case: Worst case: T(n) = T(n-1) + T(n) = T(n-1) + Θ Θ ( (n) n)  Average case: Average case: Performance: Performance:  Best case: split in the middle Best case: split in the middle — — Θ Θ( (n n log log n n) )  Worst case: sorted array! — Worst case: sorted array! — Θ Θ( (n n2 2 ) )  Average case: random arrays Average case: random arrays — — Θ Θ( (n n log log n n) )
  • 24. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 24 Analysis of Quicksort Analysis of Quicksort  Problems: Problems: • Duplicate elements Duplicate elements  Improvements: Improvements: • better pivot selection: median of three partitioning better pivot selection: median of three partitioning • switch to insertion sort on small sub-arrays switch to insertion sort on small sub-arrays • elimination of recursion – How? elimination of recursion – How? These combine to 20-25% improvement These combine to 20-25% improvement  Improvements: Dual Pivot Improvements: Dual Pivot  Method of choice for internal sorting of large files ( Method of choice for internal sorting of large files (n n ≥ ≥ 10000) 10000)
  • 25. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 25 Binary Tree Algorithms Binary 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) Algorithm Algorithm Inorder Inorder( (T T) ) a a a a b c b c b c b c d e d e   d e d e           Efficiency: Efficiency: Θ Θ( (n n) )
  • 26. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26 Binary Tree Algorithms Binary 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) Algorithm Algorithm Inorder Inorder( (T T) ) if if T T     a a a a Inorder Inorder( (T Tleft left) ) b c b c b c b c print(root of print(root of T T) ) d e d e   d e d e   Inorder Inorder( (T Tright right) )         Efficiency: Efficiency: Θ Θ( (n n) )
  • 27. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 27 Binary Tree Algorithms (cont.) Binary Tree Algorithms (cont.) Ex. 2: Computing the height of a binary tree Ex. 2: Computing the height of a binary tree T T L R h h( (T T) = ?? ) = ?? Efficiency: Efficiency: Θ Θ( (n n) )
  • 28. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 28 Binary Tree Algorithms (cont.) Binary Tree Algorithms (cont.) Ex. 2: Computing the height of a binary tree Ex. 2: Computing the height of a binary tree T T L R h h( (T T) = max{ ) = max{h h( (T TL L), ), h h( (T TR R)} + 1 if )} + 1 if T T     and and h h( ( ) = -1 ) = -1 Efficiency: Efficiency: Θ Θ( (n n) )
  • 29. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 29 Multiplication of Large Integers Multiplication of Large Integers Consider the problem of multiplying two (large) Consider the problem of multiplying two (large) n n-digit integers -digit integers represented by arrays of their digits such as: represented by arrays of their digits such as: A = 12345678901357986429 B = 87654321284820912836 A = 12345678901357986429 B = 87654321284820912836 The grade-school algorithm: The grade-school algorithm: a a1 1 a a2 2 … … a an n b b1 1 b b2 2 … … b bn n ( (d d10 10) ) d d11 11d d12 12 … … d d1 1n n ( (d d20 20) ) d d21 21d d22 22 … … d d2 2n n … … … … … … … … … … … … … … ( (d dn n0 0) ) d dn n1 1d dn n2 2 … … d dnn nn Efficiency: ? Efficiency: ?? ? one-digit multiplications one-digit multiplications
  • 30. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 30 Multiplication of Large Integers Multiplication of Large Integers Consider the problem of multiplying two (large) Consider the problem of multiplying two (large) n n-digit integers -digit integers represented by arrays of their digits such as: represented by arrays of their digits such as: A = 12345678901357986429 B = 87654321284820912836 A = 12345678901357986429 B = 87654321284820912836 The grade-school algorithm: The grade-school algorithm: a a1 1 a a2 2 … … a an n b b1 1 b b2 2 … … b bn n ( (d d10 10) ) d d11 11d d12 12 … … d d1 1n n ( (d d20 20) ) d d21 21d d22 22 … … d d2 2n n … … … … … … … … … … … … … … ( (d dn n0 0) ) d dn n1 1d dn n2 2 … … d dnn nn Efficiency: Efficiency: n n2 2 one-digit multiplications one-digit multiplications
  • 31. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 31 First Divide-and-Conquer Algorithm First Divide-and-Conquer Algorithm A small example: A A small example: A   B where A = 2135 and B = 4014 B where A = 2135 and B = 4014 A = (21·10 A = (21·102 2 + 35), B = (40 ·10 + 35), B = (40 ·102 2 + 14) + 14) So, A So, A   B = (21 ·10 B = (21 ·102 2 + 35) + 35)   (40 ·10 (40 ·102 2 + 14) + 14) = 21 = 21   40 ·10 40 ·104 4 + (21 + (21   14 + 35 14 + 35   40) ·10 40) ·102 2 + 35 + 35   14 14 In general, if A = A In general, if A = A1 1A A2 2 and B = B and B = B1 1B B2 2 (where A and B are (where A and B are n n-digit, -digit, and A and A1 1, A , A2 2, B , B1 1, , B B2 2 are are n / n / 2-digit numbers), 2-digit numbers), A A   B = A B = A1 1   B B1 1·10 ·10n n + (A + (A1 1   B B2 2 + A + A2 2   B B1 1) ·10 ) ·10n/ n/2 2 + A + A2 2   B B2 2 Recurrence for the number of one-digit multiplications M( Recurrence for the number of one-digit multiplications M(n n): ): M( M(n n) = 4M( ) = 4M(n n/2), M(1) = 1 /2), M(1) = 1 Solution: M( Solution: M(n n) = ) = n n2 2
  • 32. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 32 Second Divide-and-Conquer Algorithm Second Divide-and-Conquer Algorithm A A   B = B = A A1 1   B B1 1·10 ·10n n + + (A (A1 1   B B2 2 + A + A2 2   B B1 1) ) ·10 ·10n/ n/2 2 + + A A2 2   B B2 2 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 (A1 1 + A + A2 2 ) )   (B (B1 1 + B + B2 2 ) ) = = A A1 1   B B1 1 + + (A (A1 1   B B2 2 + A + A2 2   B B1 1) ) + + A A2 2   B B2 2, , I.e., I.e., (A (A1 1   B B2 2 + A + A2 2   B B1 1) ) = = (A (A1 1 + A + A2 2 ) )   (B (B1 1 + B + B2 2 ) ) - - A A1 1   B B1 1 - - A A2 2   B B2, 2, which requires only 3 multiplications at the expense of (4-1=3) which requires only 3 multiplications at the expense of (4-1=3) extra additions and subtractions (2 adds and 2 subs – 1 add) extra additions and subtractions (2 adds and 2 subs – 1 add) Recurrence for the number of multiplications M( Recurrence for the number of multiplications M(n n): ): M( M(n n) = 3 ) = 3M M( (n n/2), M(1) = 1 /2), M(1) = 1 Solution: M( Solution: M(n n) = 3 ) = 3log log 2 2n n = = n nlog log 2 23 3 ≈ ≈ n n1.585 1.585
  • 33. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 33 Example of Large-Integer Multiplication Example of Large-Integer Multiplication 2135 2135   4014 4014
  • 34. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 34 Strassen’s Matrix Multiplication Strassen’s Matrix Multiplication Strassen observed [1969] that the product of two matrices can be Strassen observed [1969] that the product of two matrices can be computed as follows: computed as follows: C C00 00 C C01 01 A A00 00 A A01 01 B B00 00 B B01 01 = * = * C C10 10 C C11 11 A A10 10 A A11 11 B B10 10 B B11 11 M M1 1 + M + M4 4 - M - M5 5 + M + M7 7 M M3 3 + M + M5 5 = = M M2 2 + M + M4 4 M M1 1 + M + M3 3 - M - M2 2 + M + M6 6  Example: M Example: M2 2 + M + M4 4 = (A = (A10 10 + A + A11 11) )   B B00 00 + A + A11 11   (B (B10 10 - - B B00 00) )  = A = A10 10B B00 00 + A + A11 11B B10 10
  • 35. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 35 Formulas for Strassen’s Algorithm Formulas for Strassen’s Algorithm M M1 1 = (A = (A00 00 + A + A11 11) )   (B (B00 00 + + B B11 11) ) M M2 2 = (A = (A10 10 + A + A11 11) )   B B00 00 M M3 3 = A = A00 00   (B (B01 01 - - B B11 11) ) M M4 4 = A = A11 11   (B (B10 10 - - B B00 00) ) M M5 5 = (A = (A00 00 + A + A01 01) )   B B11 11 M M6 6 = (A = (A10 10 - A - A00 00) )   (B (B00 00 + + B B01 01) ) M M7 7 = (A = (A01 01 - A - A11 11) )   (B (B10 10 + + B B11 11) )
  • 36. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 36 Analysis of Strassen’s Algorithm Analysis of Strassen’s Algorithm If If n n 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(n n) = 7M( ) = 7M(n n/2), M(1) = 1 /2), M(1) = 1 Solution: M( Solution: M(n n) = 7 ) = 7log log 2 2n n = = n nlog log 2 27 7 ≈ ≈ n n2.807 2.807 vs. vs. n n3 3 of brute-force alg. of brute-force alg. Algorithms with better asymptotic efficiency are known but they Algorithms with better asymptotic efficiency are known but they are even more complex. are even more complex.
  • 37. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 37 Closest-Pair Problem by Divide-and-Conquer Closest-Pair Problem by Divide-and-Conquer Step 1 Divide the points given into two subsets Step 1 Divide the points given into two subsets P Pl l and and P Pr r by a by a vertical line vertical line x x = = m m so that half the points lie to the left or on 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. the line and half the points lie to the right or on the line. x = m d l d r d d
  • 38. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 38 Closest-Pair Driver Closest-Pair Driver  CP_Driver(A) CP_Driver(A)  Pre => A(1..n), n > 1, is an array of (X, Y), pairs Pre => A(1..n), n > 1, is an array of (X, Y), pairs  Post => Returns distance between closest pair in A Post => Returns distance between closest pair in A  P(1 .. n) := Copy of A, sorted by X P(1 .. n) := Copy of A, sorted by X  Q(1 .. n) := Copy of A, sorted by Y Q(1 .. n) := Copy of A, sorted by Y  return CP(P, Q) return CP(P, Q)
  • 39. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 39 Closest-Pair Problem by Divide-and-Conquer Closest-Pair Problem by Divide-and-Conquer  CP(P, Q) – Pre=> P (1..n) and Q (1..n) have same points, P is x-sorted, Q y-sorted CP(P, Q) – Pre=> P (1..n) and Q (1..n) have same points, P is x-sorted, Q y-sorted • If n <= 3 then return CPBF(P) If n <= 3 then return CPBF(P) • Copy P(1 .. n/2) into PL(1 .. n/2) Copy P(1 .. n/2) into PL(1 .. n/2) • Distribute-copy the same n/2 points from Q into QL (anti-merge) Distribute-copy the same n/2 points from Q into QL (anti-merge) • Copy P(n/2+1 .. n) into PR(1 .. n/2) Copy P(n/2+1 .. n) into PR(1 .. n/2) • Distribute-copy the same n/2 points from Q into QR !! Careful Distribute-copy the same n/2 points from Q into QR !! Careful • dL := CP(PL, QL); dR := CP(PR, QR) dL := CP(PL, QL); dR := CP(PR, QR) • d := min(dL, dR); dsq := d ** 2 d := min(dL, dR); dsq := d ** 2 • midx = P(n/2).x midx = P(n/2).x • strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d • For i in 1 .. Num – 1 loop For i in 1 .. Num – 1 loop • k := i + 1 k := i + 1 • while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop • dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2) dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2) • k := k + 1 k := k + 1 • return sqrt(dsq) return sqrt(dsq)
  • 40. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 40 CP Notes CP Notes  CP(P, Q) – Pre=>P and Q have same points, P x sorted, Q y sorted y, P(1..n), n> 1 CP(P, Q) – Pre=>P and Q have same points, P x sorted, Q y sorted y, P(1..n), n> 1 • Copy P(1 .. n/2) into PL(1 .. n/2) and same n/2 points from Q into QL Copy P(1 .. n/2) into PL(1 .. n/2) and same n/2 points from Q into QL • Copy P(n/2+1 .. n) into PR(1 .. n/2)and same n/2 points from Q into QR Copy P(n/2+1 .. n) into PR(1 .. n/2)and same n/2 points from Q into QR • -- Divide Q based on Q(i).x < P(n/2).x. if Q(k).x = P(n/2).x must send to correct side -- Divide Q based on Q(i).x < P(n/2).x. if Q(k).x = P(n/2).x must send to correct side • -- N.B. P and Q are sorted. PL, PR, QL, QR are distributed, not resorted. -- N.B. P and Q are sorted. PL, PR, QL, QR are distributed, not resorted. • dL := CP(PL, QL); dR := CP(PR, QR) dL := CP(PL, QL); dR := CP(PR, QR) • d := min(dL, dR) ; dsq := d** 2 d := min(dL, dR) ; dsq := d** 2 -- For efficiency -- For efficiency • midx = P(n/2).x midx = P(n/2).x -- x value of middle point -- x value of middle point • strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d • -- Copy points of Q within distance d of middle into strip. Num such points. -- Copy points of Q within distance d of middle into strip. Num such points. • For i in 1 .. Num – 1 loop k := i + 1 For i in 1 .. Num – 1 loop k := i + 1 • while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop • dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2) dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2) • k := k + 1 k := k + 1 • return sqrt(dsq) return sqrt(dsq) • -- CP can add sorted P index to points of Q, simplifying distribution! -- CP can add sorted P index to points of Q, simplifying distribution!
  • 41. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 41 CP Assignment: Performance Measurement CP Assignment: Performance Measurement  CP(P, Q) – Pre=>P and Q have same points, P x sorted, Q y sorted y, P(1..n), n> 1 CP(P, Q) – Pre=>P and Q have same points, P x sorted, Q y sorted y, P(1..n), n> 1 • -- Count number of calls here, using a GLOBAL VARIABLE -- Count number of calls here, using a GLOBAL VARIABLE • Copy P(1 .. n/2) into PL(1 .. n/2) and same n/2 points from Q into QL Copy P(1 .. n/2) into PL(1 .. n/2) and same n/2 points from Q into QL • Copy P(n/2+1 .. n) into PR(1 .. n/2)and same n/2 points from Q into QR Copy P(n/2+1 .. n) into PR(1 .. n/2)and same n/2 points from Q into QR • dL := CP(PL, QL); dR := CP(PR, QR) dL := CP(PL, QL); dR := CP(PR, QR) • d := min(dL, dR) ; dsq := d** 2 d := min(dL, dR) ; dsq := d** 2 • midx = P(n/2).x midx = P(n/2).x • strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d • For i in 1 .. Num – 1 loop k := i + 1 For i in 1 .. Num – 1 loop k := i + 1 • while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop • -- Count number of distance calculations here, using a global -- Count number of distance calculations here, using a global • dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2) dsq := min (dsq, (strip(i).x – strip(k).x)**2 + (strip(i).y – strip(k).y)**2) • k := k + 1 k := k + 1 • return sqrt(dsq) return sqrt(dsq)
  • 42. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 42 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 right Step 2 Find recursively the closest pairs for the left and right subsets. subsets. Step 3 Set Step 3 Set d d = min{ = min{d dl l, , d dr r} } We can limit our attention to the points in the symmetric We can limit our attention to the points in the symmetric vertical strip vertical strip S S of width 2 of width 2d d as possible closest pair. (The as possible closest pair. (The points are stored and processed in increasing order of points are stored and processed in increasing order of their their y y coordinates.) coordinates.) Step 4 Scan the points in the vertical strip Step 4 Scan the points in the vertical strip S S from the lowest up. from the lowest up. For every point For every point p p( (x x, ,y y) in the strip, inspect points in ) in the strip, inspect points in in the strip that may be closer to in the strip that may be closer to p p than than d d. There can be . There can be no more than 5 such points following no more than 5 such points following p p on the strip list! on the strip list!
  • 43. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 43 Closest Pair by Divide-and-Conquer (cont.) Closest Pair by Divide-and-Conquer (cont.) How many points could be in each strip? n/2 How many points could be in each strip? n/2 Brute force would look at all n/2 x n/2 pairs Brute force would look at all n/2 x n/2 pairs Why can we restrict ourselves to calculating the distance Why can we restrict ourselves to calculating the distance from each point to the next 5 points from each point to the next 5 points Because only the next 5 points could be within distance d of the Because only the next 5 points could be within distance d of the current point. Why? current point. Why? Consider a square of size d: No two points in it can be closer than Consider a square of size d: No two points in it can be closer than d. How many points can it contain. No more than 4 (since 5 d. How many points can it contain. No more than 4 (since 5 won’t fit). won’t fit). Thus a rectangle of size dx2d can only contain 8 points (or 6 if Thus a rectangle of size dx2d can only contain 8 points (or 6 if duplicates are not allowed). So from a point, only check the duplicates are not allowed). So from a point, only check the next 7 (or 5) points. next 7 (or 5) points.
  • 44. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 44 Efficiency of the Closest-Pair Algorithm Efficiency of the Closest-Pair Algorithm Running time of the algorithm is described by Running time of the algorithm is described by T( T(n n) = 2T( ) = 2T(n n/2) + M( /2) + M(n n), where M( ), where M(n n) )   O( O(n n) ) By the Master Theorem (with By the Master Theorem (with a a = 2, = 2, b b = 2, = 2, d d = 1) = 1) T( T(n n) )   O( O(n n log log n n) )
  • 45. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 45 Quickhull Algorithm Quickhull Algorithm Convex hull Convex hull: smallest convex set that includes given points : smallest convex set that includes given points  Assume points are sorted by Assume points are sorted by x x-coordinate values -coordinate values  Identify Identify extreme points extreme points P P1 1 and and P P2 2 (leftmost and rightmost) (leftmost and rightmost)  Compute Compute upper hull upper hull recursively: recursively: • find point find point P Pmax max that is farthest away from line that is farthest away from line P P1 1P P2 2 • compute the upper hull of the points to the left of line compute the upper hull of the points to the left of line P P1 1P Pmax max • compute the upper hull of the points to the left of line compute the upper hull of the points to the left of line P Pmax maxP P2 2  Compute Compute lower hull lower hull in a similar manner in a similar manner P1 P2 Pmax
  • 46. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 46 Efficiency of Quickhull Algorithm Efficiency of Quickhull Algorithm  Finding point farthest away from line Finding point farthest away from line P P1 1P P2 2 can be done in can be done in linear time linear time  Time efficiency: Time efficiency: • Assume nu and nl points in upper and lower hulls Assume nu and nl points in upper and lower hulls • Performance: T(n) = T(?) + T(?) + Performance: T(n) = T(?) + T(?) + Θ Θ(?) (?) • Worst case: T(n) = T(?) + T(?) + Worst case: T(n) = T(?) + T(?) + Θ Θ(?) (?) • Expected case: Expected case: T(n) = T(?) + T(?) + T(n) = T(?) + T(?) + Θ Θ(?) (?)
  • 47. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 47 Efficiency of Quickhull Algorithm Efficiency of Quickhull Algorithm  Finding point farthest away from line Finding point farthest away from line P P1 1P P2 2 can be done in can be done in linear time linear time  Time efficiency: Time efficiency: • Assume nu and nl points in upper and lower hulls Assume nu and nl points in upper and lower hulls • Performance: T(n) = T(nu) + T(nl) + Performance: T(n) = T(nu) + T(nl) + Θ Θ(n) (n) • Worst case: T(n) = T(1) + T(n-1) + Worst case: T(n) = T(1) + T(n-1) + Θ Θ n) = ? n) = ? • Expected case: Expected case: T(n) = T(n/2) + T(n/2) + T(n) = T(n/2) + T(n/2) + Θ Θ(n) = ? (n) = ?
  • 48. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 48 Efficiency of Quickhull Algorithm Efficiency of Quickhull Algorithm  Finding point farthest away from line Finding point farthest away from line P P1 1P P2 2 can be done in can be done in linear time linear time  Time efficiency: Time efficiency: • Assume nu and nl points in upper and lower hulls Assume nu and nl points in upper and lower hulls • Performance: T(n) = T(nu) + T(nl) + Performance: T(n) = T(nu) + T(nl) + Θ Θ(n) (n) • Worst case: T(n) = T(1) + T(n-1) + Worst case: T(n) = T(1) + T(n-1) + Θ Θ(n) = (n) = Θ Θ(n^2) (n^2) • Expected case: Expected case: T(n) = T(n/2) + T(n/2) + T(n) = T(n/2) + T(n/2) + Θ Θ(n) = (n) = Θ Θ(n lg n) (n lg n) Can we do better? Can we do better?
  • 49. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 49 Efficiency of Quickhull Algorithm Efficiency of Quickhull Algorithm  Finding point farthest from line Finding point farthest from line P P1 1P P2 2 can be done in can be done in Θ Θ( (n n) )  Time efficiency: Time efficiency: • Assume nu and nl points in upper and lower hulls Assume nu and nl points in upper and lower hulls • Performance: T(n) = T(nu) + T(nl) + Performance: T(n) = T(nu) + T(nl) + Θ Θ(n) (n) • Worst case: T(n) = T(1) + T(n-1) + Worst case: T(n) = T(1) + T(n-1) + Θ Θ(n) = (n) = Θ Θ(n^2) (n^2) • Expected case: Expected case: T(n) = T(n/2) + T(n/2) + T(n) = T(n/2) + T(n/2) + Θ Θ(n) = (n) = Θ Θ(n lg n) (n lg n) Can we do better? T(n) = T(n/4) + T(n/4) + Can we do better? T(n) = T(n/4) + T(n/4) + Θ Θ(n) = (n) = Θ Θ(n) (n)
  • 50. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 50 Efficiency of Quickhull Algorithm Efficiency of Quickhull Algorithm  Finding point farthest from line Finding point farthest from line P P1 1P P2 2 can be done in can be done in Θ Θ( (n n) )  Time efficiency: Time efficiency: • worst case: worst case: Θ Θ( (n n2 2 ) (as ) (as quicksort) quicksort) • average case: average case: Θ Θ( (n lg n n lg n) or ) or Θ Θ( (n n) ) (assuming reasonable distribution of points) (assuming reasonable distribution of points)  If points are not initially sorted by If points are not initially sorted by x x-coordinate value, this -coordinate value, this can be accomplished in can be accomplished in O( O(n n log log n n) time ) time  Several Several O( O(n n log log n n) ) algorithms for convex hull are known algorithms for convex hull are known  Field is called Computational Geometry Field is called Computational Geometry

Editor's Notes

  • #5: Go over this example in detail, then do another example of merging, something like: (1 2 5 7 9) (3 4 6)
  • #9: 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
  • #10: 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
  • #12: 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
  • #13: 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
  • #14: Go over this example in detail, then do another example of merging, something like: (1 2 5 7 9) (3 4 6)
  • #42: 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)
  • #43: 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)