Design and
Analysis of
Algorithms
DIVIDE AND CONQUER
PART II
CLOSEST PAIR OF POINTS
MEDIAN FINDING
SELECTION ALGORITHMS
 Instructor
Prof. Amrinder Arora
amrinder@gwu.edu
Please copy TA on emails
Please feel free to call as well

 Available for study sessions
Science and Engineering Hall
GWU
Algorithms Divide and Conquer - Part II 2
LOGISTICS
Algorithms
Analysis
Asymptotic
NP-
Completeness
Design
D&C
DP
Greedy
Graph
B&B
Applications
Algorithms Divide and Conquer - Part II 3
WHERE WE ARE
 A technique to solve complex problems by breaking into
smaller instances of the problem and combining the results
 Recursive methodology – Smaller instances of the same type of
problem
 Typically used with:
 Principle of Mathematical Induction – For proving correctness
 Recurrence relation solving – For computing time (and/or space)
complexity
Algorithms Divide and Conquer - Part II 4
DIVIDE AND CONQUER
 Generalization step: Given an array and indexes i and j (start
and end) to sort that portion of it
 Algorithm MergeSort (input: A,i,j) {
if input size is small, solve differently and return
int k=(i+j)/2
MergeSort(A,i,k)
MergeSort(A,k+1,j)
Merge(A,i,k,k+1,j)
}
• T(n) = 2T(n/2) + n
• T(n) = O(n log n)
[Discussed in previous lectures.]
Algorithms Divide and Conquer - Part II 5
MERGESORT
 quicksort(A,p,r)
if (p < r) {
q = partition (A,p,r)
quicksort(A,p,q-1)
quicksort(A,q+1,r)
}
• Time complexity = ?
[Discussed in previous lectures.]
Algorithms Divide and Conquer - Part II 6
QUICKSORT
 Given an array of unsorted numbers, how to find the median?
 Option 1: Sort and return a[n/2]
 That takes O(n log n) time. Is there a more efficient
algorithm?
Algorithms Divide and Conquer - Part II 7
MEDIAN FINDING
 Step 1: Generalize the problem
 Selection (A, k): Returns k-th smallest element in given array.
For example:
 Selection (A,1) = Smallest
 Selection(A,n) = Largest // n is the array size.
 Selection(A,n/2) = Median
 Selection algorithms:
http://en.wikipedia.org/wiki/Selection_algorithm
 Also known as order statistic.
 Generalization of largest, smallest, median, etc. problems.
 This generalization step is very important for recursive calls!
Algorithms Divide and Conquer - Part II 8
MEDIAN FINDING
Selection (A,k)
Partition the array A
Suppose, the partition element lands at location k’
If (k == k’) {
return x // Great, we really got lucky.
}
If (k < k’) {
return Selection (A’,k) // Recursive call. Discard A”
}
If (k > k’) {
return Selection (A”,k-k’) // Recursive call. Discard A’
}
Algorithms Divide and Conquer - Part II 9
SELECTION – GENERAL TEMPLATE
 The main complication (as in case of Quicksort) is to find a
good partition. If the partition is very uneven, the algorithm
may not be efficient.
 We have two good choices:
 Use probability to find a good partition by trying repeatedly
 Find a good partition deterministically
 In the coming few slides, we explore both of these methods.
Algorithms Divide and Conquer - Part II 10
SELECTION – TWO APPROACHES
 Partition randomly.
 Define: A “good” partition is something that lands between the
middle half, that is between ¼ and ¾ of the array.
 You can find a “good” partition with probability ½
 Expected number of times that you have to do a partition until
you get a “good” partition = 2
 Suppose the good partition “lands” at location k’, where n/4 <=
k’ <= 3n/4
 Depending upon value of k and k’, we make the appropriate
recursive call, as specified in general template.
 After partition, we remove at least 25% of the array.
 T(n) <= 2n + T(0.75 n) // 2 reflects the expected # of times
that we have to do the partition
 T(n) <= cn // Using substitution method
 Therefore, T(n) = O(n)
Algorithms Divide and Conquer - Part II 11
SELECTION – PROBABILISTIC METHOD
 Divide the array into groups of 5 (or 7)
 Sort the small groups
 Find the median of the medians
 Partition the array on that median
x x x x x ......... x x x x x
x x x x x ......... x x x x x
x x x x x ......... x x x x x
x x x x x ......... x x x x x
x x x x x ......... x x x x x
Algorithms Divide and Conquer - Part II 12
SELECTION – QUICKSELECT ALGORITHM
 Say x = the median of median
 Then at least 30% of elements are <= x
 At least 30% of elements are >= x
 We use this x to be the partition element
 Suppose x “lands” at location k’, where 3n/10 <= k’ <= 7n/10
 Depending upon value of k and k’, we make the appropriate
recursive call, as specified in general template.
 Depending upon the value of k, either the left side of the
partition or the right side will be discarded. In either case, we
eliminate at least 30% of data.
 We note that there are 2 recursive calls – 1st call to find the
median of medians, and 2nd call after removing 25% of
elements
Algorithms Divide and Conquer - Part II 13
QUICKSELECT (CONT.)
 T(n) = cn + T(n/5) + T(7n/10)
 Proof that T(n) is linear, that is O(n), by substitution method
(Using Principle of Mathematical Induction)
We claim that T(n) <= 10 cn for all values of n < N
T(N/5) <= 2cN
T(7N/10) <= 7cN
 T(N) <= cN + 2cN + 7cN = 10cN
 The inequality holds for N
 By principle of mathematical induction, inequality holds for
all values of n.
 Therefore, T(n) = O(n)
Algorithms Divide and Conquer - Part II 14
QUICKSELECT – ANALYSIS
 Given n points on the plane, find the closest pair of points.
 http://en.wikipedia.org/wiki/Closest_pair_of_points_problem
 Textbook section 4.7
Algorithms Divide and Conquer - Part II 15
DIVIDE AND CONQUER – ANOTHER
APPLICATION
 Naïve algorithm requires O(n2) time – simply compute all
distances and find the minimum.
 We are interested in a divide and conquer approach.
 We remember that there is a signification difference between
O(n2) time algorithm and O(n log n) time algorithm. For
example, latter can easily be run with a trillion points, the
O(n2) algorithm, not so much.
Algorithms Divide and Conquer - Part II 16
CLOSEST PAIR OF POINTS
 Divide and Conquer approach:
1. Split the set of points into two equal-sized subsets by finding the
median of the x-dimensions of the points.
2. Solve the problem recursively for subsets to the left and right
subsets. Thus, there are two recursive calls.
3. Find the minimal distance among the pair of points in which one
point lies on the left of the dividing vertical and the second point
lies to the right.
 T(n) = cn + 2T(n/2) + f(n), where:
 cn represents the time spent in Step 1 (linear time median finding)
 2T(n/2) is time spent in two recursive calls in Step 2.
 f(n) is the time spent in step 3, that is, time to find the minimum
distance among the pairs, where one point lies to the left of the
dividing vertical, and the second point lies to the right.
Algorithms Divide and Conquer - Part II 17
CLOSEST PAIR OF POINTS (CONT.)
 T(n) = 2T(n/2) + cn + f(n)
 If f(n) = O(n2), then, T(n) = O(n2)
 If f(n) = O(n), then T(n) = O(n log n)
 Main complication then is to bound f(n)
 If we can do f(n) in linear time, we will have an O(n log n) time
algorithm.
 In the next couple of slides, we discuss indeed how this can
be done in linear time.
Algorithms Divide and Conquer - Part II 18
CLOSEST PAIR OF POINTS (CONT.)
 We define:
 1 = Minimum distance found in the left side
 2 = Minimum distance found in the right side
  = Minimum of 1 and 2
 Firstly, we are only interested in the points that are within a
distance  of the dividing vertical.
 For a point p1 on the left side of the dividing vertical, we are
only interested in:
 Points that are on the right side of the vertical, and within a distance
 from the dividing vertical
 Points that are within a vertical distance  from the point p1.
Algorithms Divide and Conquer - Part II 19
CLOSEST PAIR OF POINTS (CONT.)
Algorithms Divide and Conquer - Part II 20
CLOSEST PAIR OF POINTS (CONT.)
 Thus for a point p1, there can only be 6 points of interest for
that point.
 Even if there are n/2 points to the left of the dividing vertical
and within a distance , still that only means 3n pairs to
consider
 Therefore, f(n) = O(n)
 Therefore, T(n) = 2T(n/2) + O(n)
 Therefore, T(n) = O(n log n)
Algorithms Divide and Conquer - Part II 21
CLOSEST PAIR OF POINTS (CONT.)
 D&C – An interesting technique for solving problems
 Three different ways to solve recurrence relations
 Unfolding method
 Substitution (guess and prove method)
 Master Theorem – Another Method for solving Recurrence Relations
 QuickSelect – O(n) time algorithm for selecting k-th largest
element in an unsorted array.
 The constant is rather large, use this algorithm with caution
 Closest pair of points – An interesting O(n log n) time
recursive algorithm for finding the closest pair of points.
Algorithms Divide and Conquer - Part II 22
SUMMARY AND WRAP UP
Algorithms
Analysis
Asymptotic
NP-
Completeness
Design
D&C
DP
Greedy
Graph
B&B
Applications
Algorithms Divide and Conquer - Part II 23
WHERE WE ARE
 Strassen’s algorithm for matrix multiplication (Textbook § 4.8)
 Greedy Algorithms ((Textbook § 5.1 – 5.4)
 Section 4.2 in
http://compgeom.cs.uiuc.edu/~jeffe/teaching/algorithms/no
tes/04-greedy.pdf
Algorithms Divide and Conquer - Part II 24
READING ASSIGNMENTS

More Related Content

PPTX
Divide and Conquer - Part 1
PPTX
Bruteforce algorithm
PPT
5.2 divide and conquer
PDF
Divide and Conquer
PPT
Unit 1 chapter 1 Design and Analysis of Algorithms
PPT
Recursion tree method
PPT
Backtracking
PPT
Quick sort Algorithm Discussion And Analysis
Divide and Conquer - Part 1
Bruteforce algorithm
5.2 divide and conquer
Divide and Conquer
Unit 1 chapter 1 Design and Analysis of Algorithms
Recursion tree method
Backtracking
Quick sort Algorithm Discussion And Analysis

What's hot (20)

PPTX
Quick sort
PPTX
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
PPTX
Algorithm analysis (All in one)
PPT
Algorithm analysis
PPT
Peterson Critical Section Problem Solution
PPT
02 order of growth
PPTX
Brute force method
PPT
Hash tables
PDF
Dining Philosopher's Problem
PPTX
All pair shortest path
PPTX
Searching techniques in Data Structure And Algorithm
PPTX
Merge sort algorithm power point presentation
PPT
44 randomized-algorithms
PPTX
Daa:Dynamic Programing
PPTX
Parallel algorithms
PPT
minimum spanning tree
PPTX
String Matching Algorithms-The Naive Algorithm
PPTX
PRIM'S ALGORITHM
PDF
Red black tree
Quick sort
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Algorithm analysis (All in one)
Algorithm analysis
Peterson Critical Section Problem Solution
02 order of growth
Brute force method
Hash tables
Dining Philosopher's Problem
All pair shortest path
Searching techniques in Data Structure And Algorithm
Merge sort algorithm power point presentation
44 randomized-algorithms
Daa:Dynamic Programing
Parallel algorithms
minimum spanning tree
String Matching Algorithms-The Naive Algorithm
PRIM'S ALGORITHM
Red black tree
Ad

Viewers also liked (20)

PPTX
Introduction to Algorithms and Asymptotic Notation
PPTX
Graph Traversal Algorithms - Depth First Search Traversal
PPTX
Dynamic Programming - Part II
PPTX
Graph Traversal Algorithms - Breadth First Search
PPTX
NP completeness
PPTX
Dynamic Programming - Part 1
PPTX
Asymptotic Notation and Data Structures
PPTX
Greedy Algorithms
PPTX
NP-Completeness - II
PPTX
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
PPTX
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
PPT
Dinive conquer algorithm
PPTX
Divide and conquer - Quick sort
PDF
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
PPTX
Big o notation
PPTX
Algorithmic Puzzles
PPTX
Asymptotic Notations
PPT
Asymptotic notations
PPTX
CLOSEST PAIR (Final)
PPTX
BTrees - Great alternative to Red Black, AVL and other BSTs
Introduction to Algorithms and Asymptotic Notation
Graph Traversal Algorithms - Depth First Search Traversal
Dynamic Programming - Part II
Graph Traversal Algorithms - Breadth First Search
NP completeness
Dynamic Programming - Part 1
Asymptotic Notation and Data Structures
Greedy Algorithms
NP-Completeness - II
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Dinive conquer algorithm
Divide and conquer - Quick sort
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Big o notation
Algorithmic Puzzles
Asymptotic Notations
Asymptotic notations
CLOSEST PAIR (Final)
BTrees - Great alternative to Red Black, AVL and other BSTs
Ad

Similar to Divide and Conquer - Part II - Quickselect and Closest Pair of Points (20)

PDF
Daa chapter 2
PPTX
T2311 - Ch 4_Part1.pptx
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
PDF
02 Notes Divide and Conquer
PPTX
01 - DAA - PPT.pptx
PDF
Cs6402 design and analysis of algorithms may june 2016 answer key
PPTX
Divided and conqurddddddddddddddfffffe.pptx
PPT
5.2 divede and conquer 03
PPT
5.2 divede and conquer 03
PPTX
Divide and Conquer in DAA concept. For B Tech CSE
PDF
Sienna 4 divideandconquer
DOC
Unit 2 in daa
DOC
algorithm Unit 2
PDF
module2_dIVIDEncONQUER_2022.pdf
PPT
PPT
Divide and Conquer
PPT
Matrix Multiplication(An example of concurrent programming)
PDF
Seminar Report (Final)
PPTX
HOME ASSIGNMENT (0).pptx
PPTX
Design and Analysis of Algorithm in Compter Science.pptx
Daa chapter 2
T2311 - Ch 4_Part1.pptx
Book.pdf01_Intro.ppt algorithm for preperation stu used
02 Notes Divide and Conquer
01 - DAA - PPT.pptx
Cs6402 design and analysis of algorithms may june 2016 answer key
Divided and conqurddddddddddddddfffffe.pptx
5.2 divede and conquer 03
5.2 divede and conquer 03
Divide and Conquer in DAA concept. For B Tech CSE
Sienna 4 divideandconquer
Unit 2 in daa
algorithm Unit 2
module2_dIVIDEncONQUER_2022.pdf
Divide and Conquer
Matrix Multiplication(An example of concurrent programming)
Seminar Report (Final)
HOME ASSIGNMENT (0).pptx
Design and Analysis of Algorithm in Compter Science.pptx

More from Amrinder Arora (15)

PDF
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
PDF
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
PDF
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
PDF
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
PPTX
Online algorithms in Machine Learning
PPTX
Set Operations - Union Find and Bloom Filters
PPTX
Binomial Heaps and Fibonacci Heaps
PPTX
R-Trees and Geospatial Data Structures
PPTX
Tries - Tree Based Structures for Strings
PPTX
Splay Trees and Self Organizing Data Structures
PPTX
Binary Search Trees - AVL and Red Black
PPTX
Graphs, Trees, Paths and Their Representations
PPTX
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
PPTX
Online Algorithms - An Introduction
PPTX
Learning to learn
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Online algorithms in Machine Learning
Set Operations - Union Find and Bloom Filters
Binomial Heaps and Fibonacci Heaps
R-Trees and Geospatial Data Structures
Tries - Tree Based Structures for Strings
Splay Trees and Self Organizing Data Structures
Binary Search Trees - AVL and Red Black
Graphs, Trees, Paths and Their Representations
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Online Algorithms - An Introduction
Learning to learn

Recently uploaded (20)

PPTX
Presentation - Principles of Instructional Design.pptx
PDF
4 layer Arch & Reference Arch of IoT.pdf
PDF
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
PDF
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
PDF
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
PDF
Aug23rd - Mulesoft Community Workshop - Hyd, India.pdf
PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PDF
Electrocardiogram sequences data analytics and classification using unsupervi...
PDF
Advancing precision in air quality forecasting through machine learning integ...
PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
PDF
Build Real-Time ML Apps with Python, Feast & NoSQL
PDF
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
PDF
Ensemble model-based arrhythmia classification with local interpretable model...
PDF
A symptom-driven medical diagnosis support model based on machine learning te...
PPTX
SGT Report The Beast Plan and Cyberphysical Systems of Control
PPTX
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
PDF
EIS-Webinar-Regulated-Industries-2025-08.pdf
PDF
Rapid Prototyping: A lecture on prototyping techniques for interface design
PDF
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
PDF
CEH Module 2 Footprinting CEH V13, concepts
Presentation - Principles of Instructional Design.pptx
4 layer Arch & Reference Arch of IoT.pdf
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
Aug23rd - Mulesoft Community Workshop - Hyd, India.pdf
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
Electrocardiogram sequences data analytics and classification using unsupervi...
Advancing precision in air quality forecasting through machine learning integ...
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
Build Real-Time ML Apps with Python, Feast & NoSQL
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
Ensemble model-based arrhythmia classification with local interpretable model...
A symptom-driven medical diagnosis support model based on machine learning te...
SGT Report The Beast Plan and Cyberphysical Systems of Control
AI-driven Assurance Across Your End-to-end Network With ThousandEyes
EIS-Webinar-Regulated-Industries-2025-08.pdf
Rapid Prototyping: A lecture on prototyping techniques for interface design
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
CEH Module 2 Footprinting CEH V13, concepts

Divide and Conquer - Part II - Quickselect and Closest Pair of Points

  • 1. Design and Analysis of Algorithms DIVIDE AND CONQUER PART II CLOSEST PAIR OF POINTS MEDIAN FINDING SELECTION ALGORITHMS
  • 2.  Instructor Prof. Amrinder Arora [email protected] Please copy TA on emails Please feel free to call as well   Available for study sessions Science and Engineering Hall GWU Algorithms Divide and Conquer - Part II 2 LOGISTICS
  • 4.  A technique to solve complex problems by breaking into smaller instances of the problem and combining the results  Recursive methodology – Smaller instances of the same type of problem  Typically used with:  Principle of Mathematical Induction – For proving correctness  Recurrence relation solving – For computing time (and/or space) complexity Algorithms Divide and Conquer - Part II 4 DIVIDE AND CONQUER
  • 5.  Generalization step: Given an array and indexes i and j (start and end) to sort that portion of it  Algorithm MergeSort (input: A,i,j) { if input size is small, solve differently and return int k=(i+j)/2 MergeSort(A,i,k) MergeSort(A,k+1,j) Merge(A,i,k,k+1,j) } • T(n) = 2T(n/2) + n • T(n) = O(n log n) [Discussed in previous lectures.] Algorithms Divide and Conquer - Part II 5 MERGESORT
  • 6.  quicksort(A,p,r) if (p < r) { q = partition (A,p,r) quicksort(A,p,q-1) quicksort(A,q+1,r) } • Time complexity = ? [Discussed in previous lectures.] Algorithms Divide and Conquer - Part II 6 QUICKSORT
  • 7.  Given an array of unsorted numbers, how to find the median?  Option 1: Sort and return a[n/2]  That takes O(n log n) time. Is there a more efficient algorithm? Algorithms Divide and Conquer - Part II 7 MEDIAN FINDING
  • 8.  Step 1: Generalize the problem  Selection (A, k): Returns k-th smallest element in given array. For example:  Selection (A,1) = Smallest  Selection(A,n) = Largest // n is the array size.  Selection(A,n/2) = Median  Selection algorithms: http://en.wikipedia.org/wiki/Selection_algorithm  Also known as order statistic.  Generalization of largest, smallest, median, etc. problems.  This generalization step is very important for recursive calls! Algorithms Divide and Conquer - Part II 8 MEDIAN FINDING
  • 9. Selection (A,k) Partition the array A Suppose, the partition element lands at location k’ If (k == k’) { return x // Great, we really got lucky. } If (k < k’) { return Selection (A’,k) // Recursive call. Discard A” } If (k > k’) { return Selection (A”,k-k’) // Recursive call. Discard A’ } Algorithms Divide and Conquer - Part II 9 SELECTION – GENERAL TEMPLATE
  • 10.  The main complication (as in case of Quicksort) is to find a good partition. If the partition is very uneven, the algorithm may not be efficient.  We have two good choices:  Use probability to find a good partition by trying repeatedly  Find a good partition deterministically  In the coming few slides, we explore both of these methods. Algorithms Divide and Conquer - Part II 10 SELECTION – TWO APPROACHES
  • 11.  Partition randomly.  Define: A “good” partition is something that lands between the middle half, that is between ¼ and ¾ of the array.  You can find a “good” partition with probability ½  Expected number of times that you have to do a partition until you get a “good” partition = 2  Suppose the good partition “lands” at location k’, where n/4 <= k’ <= 3n/4  Depending upon value of k and k’, we make the appropriate recursive call, as specified in general template.  After partition, we remove at least 25% of the array.  T(n) <= 2n + T(0.75 n) // 2 reflects the expected # of times that we have to do the partition  T(n) <= cn // Using substitution method  Therefore, T(n) = O(n) Algorithms Divide and Conquer - Part II 11 SELECTION – PROBABILISTIC METHOD
  • 12.  Divide the array into groups of 5 (or 7)  Sort the small groups  Find the median of the medians  Partition the array on that median x x x x x ......... x x x x x x x x x x ......... x x x x x x x x x x ......... x x x x x x x x x x ......... x x x x x x x x x x ......... x x x x x Algorithms Divide and Conquer - Part II 12 SELECTION – QUICKSELECT ALGORITHM
  • 13.  Say x = the median of median  Then at least 30% of elements are <= x  At least 30% of elements are >= x  We use this x to be the partition element  Suppose x “lands” at location k’, where 3n/10 <= k’ <= 7n/10  Depending upon value of k and k’, we make the appropriate recursive call, as specified in general template.  Depending upon the value of k, either the left side of the partition or the right side will be discarded. In either case, we eliminate at least 30% of data.  We note that there are 2 recursive calls – 1st call to find the median of medians, and 2nd call after removing 25% of elements Algorithms Divide and Conquer - Part II 13 QUICKSELECT (CONT.)
  • 14.  T(n) = cn + T(n/5) + T(7n/10)  Proof that T(n) is linear, that is O(n), by substitution method (Using Principle of Mathematical Induction) We claim that T(n) <= 10 cn for all values of n < N T(N/5) <= 2cN T(7N/10) <= 7cN  T(N) <= cN + 2cN + 7cN = 10cN  The inequality holds for N  By principle of mathematical induction, inequality holds for all values of n.  Therefore, T(n) = O(n) Algorithms Divide and Conquer - Part II 14 QUICKSELECT – ANALYSIS
  • 15.  Given n points on the plane, find the closest pair of points.  http://en.wikipedia.org/wiki/Closest_pair_of_points_problem  Textbook section 4.7 Algorithms Divide and Conquer - Part II 15 DIVIDE AND CONQUER – ANOTHER APPLICATION
  • 16.  Naïve algorithm requires O(n2) time – simply compute all distances and find the minimum.  We are interested in a divide and conquer approach.  We remember that there is a signification difference between O(n2) time algorithm and O(n log n) time algorithm. For example, latter can easily be run with a trillion points, the O(n2) algorithm, not so much. Algorithms Divide and Conquer - Part II 16 CLOSEST PAIR OF POINTS
  • 17.  Divide and Conquer approach: 1. Split the set of points into two equal-sized subsets by finding the median of the x-dimensions of the points. 2. Solve the problem recursively for subsets to the left and right subsets. Thus, there are two recursive calls. 3. Find the minimal distance among the pair of points in which one point lies on the left of the dividing vertical and the second point lies to the right.  T(n) = cn + 2T(n/2) + f(n), where:  cn represents the time spent in Step 1 (linear time median finding)  2T(n/2) is time spent in two recursive calls in Step 2.  f(n) is the time spent in step 3, that is, time to find the minimum distance among the pairs, where one point lies to the left of the dividing vertical, and the second point lies to the right. Algorithms Divide and Conquer - Part II 17 CLOSEST PAIR OF POINTS (CONT.)
  • 18.  T(n) = 2T(n/2) + cn + f(n)  If f(n) = O(n2), then, T(n) = O(n2)  If f(n) = O(n), then T(n) = O(n log n)  Main complication then is to bound f(n)  If we can do f(n) in linear time, we will have an O(n log n) time algorithm.  In the next couple of slides, we discuss indeed how this can be done in linear time. Algorithms Divide and Conquer - Part II 18 CLOSEST PAIR OF POINTS (CONT.)
  • 19.  We define:  1 = Minimum distance found in the left side  2 = Minimum distance found in the right side   = Minimum of 1 and 2  Firstly, we are only interested in the points that are within a distance  of the dividing vertical.  For a point p1 on the left side of the dividing vertical, we are only interested in:  Points that are on the right side of the vertical, and within a distance  from the dividing vertical  Points that are within a vertical distance  from the point p1. Algorithms Divide and Conquer - Part II 19 CLOSEST PAIR OF POINTS (CONT.)
  • 20. Algorithms Divide and Conquer - Part II 20 CLOSEST PAIR OF POINTS (CONT.)
  • 21.  Thus for a point p1, there can only be 6 points of interest for that point.  Even if there are n/2 points to the left of the dividing vertical and within a distance , still that only means 3n pairs to consider  Therefore, f(n) = O(n)  Therefore, T(n) = 2T(n/2) + O(n)  Therefore, T(n) = O(n log n) Algorithms Divide and Conquer - Part II 21 CLOSEST PAIR OF POINTS (CONT.)
  • 22.  D&C – An interesting technique for solving problems  Three different ways to solve recurrence relations  Unfolding method  Substitution (guess and prove method)  Master Theorem – Another Method for solving Recurrence Relations  QuickSelect – O(n) time algorithm for selecting k-th largest element in an unsorted array.  The constant is rather large, use this algorithm with caution  Closest pair of points – An interesting O(n log n) time recursive algorithm for finding the closest pair of points. Algorithms Divide and Conquer - Part II 22 SUMMARY AND WRAP UP
  • 24.  Strassen’s algorithm for matrix multiplication (Textbook § 4.8)  Greedy Algorithms ((Textbook § 5.1 – 5.4)  Section 4.2 in http://compgeom.cs.uiuc.edu/~jeffe/teaching/algorithms/no tes/04-greedy.pdf Algorithms Divide and Conquer - Part II 24 READING ASSIGNMENTS