Submit Search
Upload
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
•
Download as PPT, PDF
•
0 likes
•
2 views
S
Shanmuganathan C
Follow
Divide and conquer
Read less
Read more
Engineering
Report
Share
Report
Share
1 of 50
Download now
Recommended
5261506.ppt
5261506.ppt
RamaKrishnaErroju
ch11-04-27-15.ppt
ch11-04-27-15.ppt
ssuser15a62a
ch03-2018.02.02.pptx
ch03-2018.02.02.pptx
SYAMDAVULURI
Ch6 transform and conquer
Ch6 transform and conquer
Younes Khafaja
ch08-2019-03-27 (1).ppt
ch08-2019-03-27 (1).ppt
SaimaShaheen14
Incremental View Maintenance for openCypher Queries
Incremental View Maintenance for openCypher Queries
Gábor Szárnyas
Incremental View Maintenance for openCypher Queries
Incremental View Maintenance for openCypher Queries
openCypher
Ch04n
Ch04n
kpcs2010
Recommended
5261506.ppt
5261506.ppt
RamaKrishnaErroju
ch11-04-27-15.ppt
ch11-04-27-15.ppt
ssuser15a62a
ch03-2018.02.02.pptx
ch03-2018.02.02.pptx
SYAMDAVULURI
Ch6 transform and conquer
Ch6 transform and conquer
Younes Khafaja
ch08-2019-03-27 (1).ppt
ch08-2019-03-27 (1).ppt
SaimaShaheen14
Incremental View Maintenance for openCypher Queries
Incremental View Maintenance for openCypher Queries
Gábor Szárnyas
Incremental View Maintenance for openCypher Queries
Incremental View Maintenance for openCypher Queries
openCypher
Ch04n
Ch04n
kpcs2010
Introduction to Reinforcement Learning for Molecular Design
Introduction to Reinforcement Learning for Molecular Design
Dan Elton
data structure and algorithm (Advanced algorithm Stretegies)
data structure and algorithm (Advanced algorithm Stretegies)
shahghanikhan
01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx
Ishtiaq Rasool Khan
Q
Q
guest9b2176
lecture 1
lecture 1
sajinsc
01 - Fundamentals of the Analysis of Algorithm Efficiency.pptx
01 - Fundamentals of the Analysis of Algorithm Efficiency.pptx
Ishtiaq Rasool Khan
01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx
Ishtiaq Rasool Khan
Linear models for data science
Linear models for data science
Brad Klingenberg
Intro to Feature Selection
Intro to Feature Selection
chenhm
ilp-nlp-slides.pdf
ilp-nlp-slides.pdf
FlorentBersani
Correlation and regression
Correlation and regression
Khalid Aziz
CLIM Fall 2017 Course: Statistics for Climate Research, Climate Informatics -...
CLIM Fall 2017 Course: Statistics for Climate Research, Climate Informatics -...
The Statistical and Applied Mathematical Sciences Institute
Minimizing cost in distributed multiquery processing applications
Minimizing cost in distributed multiquery processing applications
Luis Galárraga
Algorithm review
Algorithm review
chidabdu
Project management
Project management
Avay Minni
Ada notes
Ada notes
VIKAS SINGH BHADOURIA
MODULE_05-Matrix Decomposition.pptx
MODULE_05-Matrix Decomposition.pptx
AlokSingh205089
MS Presentation
MS Presentation
rajeeja
Algorithum Analysis
Algorithum Analysis
Ain-ul-Moiz Khawaja
Program on Mathematical and Statistical Methods for Climate and the Earth Sys...
Program on Mathematical and Statistical Methods for Climate and the Earth Sys...
The Statistical and Applied Mathematical Sciences Institute
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
Shanmuganathan C
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
Shanmuganathan C
More Related Content
Similar to ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
Introduction to Reinforcement Learning for Molecular Design
Introduction to Reinforcement Learning for Molecular Design
Dan Elton
data structure and algorithm (Advanced algorithm Stretegies)
data structure and algorithm (Advanced algorithm Stretegies)
shahghanikhan
01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx
Ishtiaq Rasool Khan
Q
Q
guest9b2176
lecture 1
lecture 1
sajinsc
01 - Fundamentals of the Analysis of Algorithm Efficiency.pptx
01 - Fundamentals of the Analysis of Algorithm Efficiency.pptx
Ishtiaq Rasool Khan
01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx
Ishtiaq Rasool Khan
Linear models for data science
Linear models for data science
Brad Klingenberg
Intro to Feature Selection
Intro to Feature Selection
chenhm
ilp-nlp-slides.pdf
ilp-nlp-slides.pdf
FlorentBersani
Correlation and regression
Correlation and regression
Khalid Aziz
CLIM Fall 2017 Course: Statistics for Climate Research, Climate Informatics -...
CLIM Fall 2017 Course: Statistics for Climate Research, Climate Informatics -...
The Statistical and Applied Mathematical Sciences Institute
Minimizing cost in distributed multiquery processing applications
Minimizing cost in distributed multiquery processing applications
Luis Galárraga
Algorithm review
Algorithm review
chidabdu
Project management
Project management
Avay Minni
Ada notes
Ada notes
VIKAS SINGH BHADOURIA
MODULE_05-Matrix Decomposition.pptx
MODULE_05-Matrix Decomposition.pptx
AlokSingh205089
MS Presentation
MS Presentation
rajeeja
Algorithum Analysis
Algorithum Analysis
Ain-ul-Moiz Khawaja
Program on Mathematical and Statistical Methods for Climate and the Earth Sys...
Program on Mathematical and Statistical Methods for Climate and the Earth Sys...
The Statistical and Applied Mathematical Sciences Institute
Similar to ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
(20)
Introduction to Reinforcement Learning for Molecular Design
Introduction to Reinforcement Learning for Molecular Design
data structure and algorithm (Advanced algorithm Stretegies)
data structure and algorithm (Advanced algorithm Stretegies)
01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx
Q
Q
lecture 1
lecture 1
01 - Fundamentals of the Analysis of Algorithm Efficiency.pptx
01 - Fundamentals of the Analysis of Algorithm Efficiency.pptx
01 - Analysis of Efficiency.pptx
01 - Analysis of Efficiency.pptx
Linear models for data science
Linear models for data science
Intro to Feature Selection
Intro to Feature Selection
ilp-nlp-slides.pdf
ilp-nlp-slides.pdf
Correlation and regression
Correlation and regression
CLIM Fall 2017 Course: Statistics for Climate Research, Climate Informatics -...
CLIM Fall 2017 Course: Statistics for Climate Research, Climate Informatics -...
Minimizing cost in distributed multiquery processing applications
Minimizing cost in distributed multiquery processing applications
Algorithm review
Algorithm review
Project management
Project management
Ada notes
Ada notes
MODULE_05-Matrix Decomposition.pptx
MODULE_05-Matrix Decomposition.pptx
MS Presentation
MS Presentation
Algorithum Analysis
Algorithum Analysis
Program on Mathematical and Statistical Methods for Climate and the Earth Sys...
Program on Mathematical and Statistical Methods for Climate and the Earth Sys...
More from Shanmuganathan C
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
Shanmuganathan C
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
Shanmuganathan C
ClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPair
Shanmuganathan C
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
Shanmuganathan C
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
Shanmuganathan C
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
Shanmuganathan C
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
Shanmuganathan C
OODP UNIT 2 operator overloading
OODP UNIT 2 operator overloading
Shanmuganathan C
OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloading
Shanmuganathan C
OODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objects
Shanmuganathan C
More from Shanmuganathan C
(10)
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
17-dynprog2 17-dynprog2 17-dynprog2 17-dynprog2
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
ClosestPairClosestPairClosestPairClosestPair
ClosestPairClosestPairClosestPairClosestPair
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
convexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHullsconvexHulls
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overview_nemo (1)ch01_overvi...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
AlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorithmsAlgorit...
OODP UNIT 2 operator overloading
OODP UNIT 2 operator overloading
OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloading
OODP Unit 1 OOPs classes and objects
OODP Unit 1 OOPs classes and objects
Recently uploaded
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
RagavanV2
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Call Girls in Nagpur High Profile
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
Call Girls in Nagpur High Profile Call Girls
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
rknatarajan
Thermal Engineering Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
DineshKumar4165
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
rknatarajan
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
bhaskargani46
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
ranjana rawat
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
fenichawla
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
JiananWang21
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
ManishPatel169454
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
DineshKumar4165
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
Prabhanshu Chaturvedi
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Call Girls in Nagpur High Profile
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
timesproduction05
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
ranjana rawat
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Christo Ananth
Recently uploaded
(20)
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
Thermal Engineering Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
ch05-2018.03.02 ch05-2018.03.02ch05-2018.03.02
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 The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original (larger) instance by combining these solutions Distinction between divide and conquer and decrease and conquer is 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.) 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 Sorting: mergesort and quicksort Binary tree traversals Multiplication of large integers Matrix multiplication: Strassen’s algorithm Closest-pair and convex-hull algorithms 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 T(n) = aT(n/b) + f (n) where f(n) (nd), d 0 Master Theorem: If a < bd, T(n) (nd) If a = bd, T(n) (nd log n) If a > bd, T(n) (nlog b a ) Note: The same results hold with O instead of . Examples: T(n) = 4T(n/2) + n T(n) ? T(n) = 4T(n/2) + n2 T(n) ? T(n) = 4T(n/2) + n3 T(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 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 Split array A[0..n-1] in two about equal halves and make copies of each half in arrays B and C Sort arrays B and C recursively Merge sorted arrays B and C into array A as follows: • Repeat the following until no elements remain in one of the arrays: – compare the first elements in the remaining unprocessed portions of the arrays – copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array • Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the 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 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
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 Number of key comparisons – recurrence: C(n) = … For merge step: C’(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 Number of key comparisons – recurrence: C(n) = 2C(n/2) + C’(n) = 2C(n/2) + n-1 For merge step – worst case: C’(n) = n-1 How to solve? 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 T(n) = aT(n/b) + f (n) where f(n) (nd), d 0 Master Theorem: If a < bd, T(n) (nd) If a = bd, T(n) (nd log n) If a > bd, T(n) (nlog b a ) Using log – Take log b of both sides: Case 1: logb a < d, T(n) (nd) Case 2: logb a = d, T(n) (nd logb n) Case 3: logb a > d, T(n) (nlog b 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 All cases have same efficiency: Θ(n log n) Number of comparisons in the worst case is close to theoretical minimum for comparison-based sorting: - Theoretical min: log2 n! ≈ n log2 n - 1.44n Space requirement: • 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 All cases have same efficiency: Θ(n log n) Number of comparisons in the worst case is close to theoretical minimum for comparison-based sorting: - Theoretical min: log2 n! ≈ n log2 n - 1.44n Space requirement: • Book’s algorithm: n + n Sum(1+1/2+1/4+…) = n + n Θ(2)= Θ(3n) • Can be done Θ(2n) : Don’t copy in sort; just specify bounds, copy into extra array on merge, then copy back – Any other space required? • Can be implemented in place. How … 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 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 Select a pivot (partitioning element) – here, the first element Rearrange the list so that all the elements in the first s positions are smaller than or equal to the pivot and all the elements in the remaining n-s positions are larger than or equal to the pivot (see next slide for an algorithm) Exchange the pivot with the last element in the first (i.e., ) subarray — the pivot is now in its final position Sort the two subarrays recursively 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 QS (A, l, r) if l < r then s = partition (A, l, r) 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 QS (A, l, r) if l < r then s = partition (A, l, r) QS (A, l , s-1) QS (A, s+1, r ) Ask later: What if we skip final swap and 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
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 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 0 1 2 3 4 5 6 7 5 3 1 9 8 2 4 7 0 .. 7 / s=4 0 .. 3 / s=x 0 .. s-1 / s=? s+1 .. 3 5 .. 7 / s=y 5 .. y-1 / 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 Recurrences: Best case: Worst case: Average case: Performance: Best case: Worst 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 Recurrences: Best case: T(n) = T(n/2) + Θ (n) Worst case: T(n) = T(n-1) + Θ (n) Average case: Performance: Best case: Worst 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 Recurrences: Best case: T(n) = T(n/2) + Θ (n) Worst case: T(n) = T(n-1) + Θ (n) Average case: Performance: Best case: split in the middle — Θ(n log n) Worst case: sorted array! — Θ(n2) Average case: random arrays — Θ(n log 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 Problems: • Duplicate elements Improvements: • better pivot selection: median of three partitioning • switch to insertion sort on small sub-arrays • elimination of recursion – How? These combine to 20-25% improvement Improvements: Dual Pivot Method of choice for internal sorting of large files (n ≥ 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 is a divide-and-conquer ready structure! Ex. 1: Classic traversals (preorder, inorder, postorder) Algorithm Inorder(T) a a b c b c d e • • d e • • • • Efficiency: Θ(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 is a divide-and-conquer ready structure! Ex. 1: Classic traversals (preorder, inorder, postorder) Algorithm Inorder(T) if T a a Inorder(Tleft) b c b c print(root of T) d e • • d e Inorder(Tright) • • • • Efficiency: Θ(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.) Ex. 2: Computing the height of a binary tree T T L R h(T) = ?? Efficiency: Θ(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.) Ex. 2: Computing the height of a binary tree T T L R h(T) = max{h(TL), h(TR)} + 1 if T and h() = -1 Efficiency: Θ(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 Consider the problem of multiplying two (large) n-digit integers represented by arrays of their digits such as: A = 12345678901357986429 B = 87654321284820912836 The grade-school algorithm: a1 a2 … an b1 b2 … bn (d10) d11d12 … d1n (d20) d21d22 … d2n … … … … … … … (dn0) dn1dn2 … dnn Efficiency: ?? 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 Consider the problem of multiplying two (large) n-digit integers represented by arrays of their digits such as: A = 12345678901357986429 B = 87654321284820912836 The grade-school algorithm: a1 a2 … an b1 b2 … bn (d10) d11d12 … d1n (d20) d21d22 … d2n … … … … … … … (dn0) dn1dn2 … dnn Efficiency: n2 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 A small example: A B where A = 2135 and B = 4014 A = (21·102 + 35), B = (40 ·102 + 14) So, A B = (21 ·102 + 35) (40 ·102 + 14) = 21 40 ·104 + (21 14 + 35 40) ·102 + 35 14 In general, if A = A1A2 and B = B1B2 (where A and B are n-digit, and A1, A2, B1, B2 are n / 2-digit numbers), A B = A1 B1·10n + (A1 B2 + A2 B1) ·10n/2 + A2 B2 Recurrence for the number of one-digit multiplications M(n): M(n) = 4M(n/2), M(1) = 1 Solution: M(n) = n2
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 A B = A1 B1·10n + (A1 B2 + A2 B1) ·10n/2 + A2 B2 The idea is to decrease the number of multiplications from 4 to 3: (A1 + A2 ) (B1 + B2 ) = A1 B1 + (A1 B2 + A2 B1) + A2 B2, I.e., (A1 B2 + A2 B1) = (A1 + A2 ) (B1 + B2 ) - A1 B1 - A2 B2, which requires only 3 multiplications at the expense of (4-1=3) extra additions and subtractions (2 adds and 2 subs – 1 add) Recurrence for the number of multiplications M(n): M(n) = 3M(n/2), M(1) = 1 Solution: M(n) = 3log 2n = nlog 23 ≈ n1.585
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 2135 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 observed [1969] that the product of two matrices can be computed as follows: C00 C01 A00 A01 B00 B01 = * C10 C11 A10 A11 B10 B11 M1 + M4 - M5 + M7 M3 + M5 = M2 + M4 M1 + M3 - M2 + M6 Example: M2 + M4 = (A10 + A11) B00 + A11 (B10 - B00) = A10B00 + A11B10
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 M1 = (A00 + A11) (B00 + B11) M2 = (A10 + A11) B00 M3 = A00 (B01 - B11) M4 = A11 (B10 - B00) M5 = (A00 + A01) B11 M6 = (A10 - A00) (B00 + B01) M7 = (A01 - A11) (B10 + B11)
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 If n is not a power of 2, matrices can be padded with zeros. Number of multiplications: M(n) = 7M(n/2), M(1) = 1 Solution: M(n) = 7log 2n = nlog 27 ≈ n2.807 vs. n3 of brute-force alg. Algorithms with better asymptotic efficiency are known but they 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 Step 1 Divide the points given into two subsets Pl and Pr by a vertical line x = m 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. 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 CP_Driver(A) Pre => A(1..n), n > 1, is an array of (X, Y), pairs Post => Returns distance between closest pair in A P(1 .. n) := Copy of A, sorted by X Q(1 .. n) := Copy of A, sorted by Y 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 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) • Copy P(1 .. n/2) into PL(1 .. n/2) • Distribute-copy the same n/2 points from Q into QL (anti-merge) • Copy P(n/2+1 .. n) into PR(1 .. n/2) • Distribute-copy the same n/2 points from Q into QR !! Careful • dL := CP(PL, QL); dR := CP(PR, QR) • d := min(dL, dR); dsq := d ** 2 • midx = P(n/2).x • strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d • For i in 1 .. Num – 1 loop • k := i + 1 • 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) • k := k + 1 • 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(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(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 • -- N.B. P and Q are sorted. PL, PR, QL, QR are distributed, not resorted. • dL := CP(PL, QL); dR := CP(PR, QR) • d := min(dL, dR) ; dsq := d** 2 -- For efficiency • midx = P(n/2).x -- x value of middle point • 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. • For i in 1 .. Num – 1 loop k := i + 1 • 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) • k := k + 1 • return sqrt(dsq) • -- 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(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 • 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 • dL := CP(PL, QL); dR := CP(PR, QR) • d := min(dL, dR) ; dsq := d** 2 • midx = P(n/2).x • strip(1 .. num) => strip(i) := Q(k) iff | Q(k).x – midx | <= d • For i in 1 .. Num – 1 loop k := i + 1 • while k <= num and (strip(i).y – strip(k).y)**2 < dsq loop • -- 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) • k := k + 1 • 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.) Step 2 Find recursively the closest pairs for the left and right subsets. Step 3 Set d = min{dl, dr} We can limit our attention to the points in the symmetric vertical strip S of width 2d as possible closest pair. (The points are stored and processed in increasing order of their y coordinates.) Step 4 Scan the points in the vertical strip S from the lowest up. For every point p(x,y) in the strip, inspect points in in the strip that may be closer to p than d. There can be no more than 5 such points following p 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.) How many points could be in each strip? n/2 Brute force would look at all n/2 x n/2 pairs Why can we restrict ourselves to calculating the distance from each point to the next 5 points Because only the next 5 points could be within distance d of the current point. Why? 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 won’t fit). 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 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 Running time of the algorithm is described by T(n) = 2T(n/2) + M(n), where M(n) O(n) By the Master Theorem (with a = 2, b = 2, d = 1) T(n) O(n log n)
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 Convex hull: smallest convex set that includes given points Assume points are sorted by x-coordinate values Identify extreme points P1 and P2 (leftmost and rightmost) Compute upper hull recursively: • find point Pmax that is farthest away from line P1P2 • compute the upper hull of the points to the left of line P1Pmax • compute the upper hull of the points to the left of line PmaxP2 Compute lower hull 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 Finding point farthest away from line P1P2 can be done in linear time Time efficiency: • Assume nu and nl points in upper and lower hulls • Performance: T(n) = T(?) + T(?) + Θ(?) • Worst case: T(n) = T(?) + T(?) + Θ(?) • Expected case: 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 Finding point farthest away from line P1P2 can be done in linear time Time efficiency: • Assume nu and nl points in upper and lower hulls • Performance: T(n) = T(nu) + T(nl) + Θ(n) • Worst case: T(n) = T(1) + T(n-1) + Θ n) = ? • Expected case: T(n) = T(n/2) + T(n/2) + Θ(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 Finding point farthest away from line P1P2 can be done in linear time Time efficiency: • Assume nu and nl points in upper and lower hulls • Performance: T(n) = T(nu) + T(nl) + Θ(n) • Worst case: T(n) = T(1) + T(n-1) + Θ(n) = Θ(n^2) • Expected case: T(n) = T(n/2) + T(n/2) + Θ(n) = Θ(n lg n) 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 Finding point farthest from line P1P2 can be done in Θ(n) Time efficiency: • Assume nu and nl points in upper and lower hulls • Performance: T(n) = T(nu) + T(nl) + Θ(n) • Worst case: T(n) = T(1) + T(n-1) + Θ(n) = Θ(n^2) • Expected case: T(n) = T(n/2) + T(n/2) + Θ(n) = Θ(n lg n) Can we do better? T(n) = T(n/4) + T(n/4) + Θ(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 Finding point farthest from line P1P2 can be done in Θ(n) Time efficiency: • worst case: Θ(n2) (as quicksort) • average case: Θ(n lg n) or Θ(n) (assuming reasonable distribution of points) If points are not initially sorted by x-coordinate value, this can be accomplished in O(n log n) time Several O(n log n) algorithms for convex hull are known Field is called Computational Geometry
Download now