Design and
Analysis of
Algorithms
DIVIDE AND CONQUER
PART I
GENERAL TEMPLATE
BINARY SEARCH
MERGE SORT & QUICK SORT
SOLVING RECURRENCE RELATIONS
 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 I 2
LOGISTICS
Algorithms
Analysis
Asymptotic
NP-
Completeness
Design
D&C
DP
Greedy
Graph
B&B
Applications
Algorithms Divide and Conquer - Part I 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 accompaniments
 Induction for proving correctness
 Recurrence relation solving for computing time (and/or space)
complexity
Algorithms Divide and Conquer - Part I 4
DIVIDE AND CONQUER
By definition: For D&C, sub
problems must be of same
type.
[The phrase “D&C” is also used
in other contexts. It may refer
to breaking down a task, but in
Computer Science, D&C is a
formal paradigm]
Algorithms Divide and Conquer - Part I 5
D&C – CS, NOT MANAGEMENT/POLITICS
 A recursive algorithm is an algorithm that calls itself on
smaller input.
 Algorithm sort (Array a)
Begin
sort (subarray consisting of first half of a)
sort (subarray consisting of second half of a)
do_something_else();
End
Algorithms Divide and Conquer - Part I 6
RECURSION
 Recurrence Relation is a recursive formula, commonly used to
analyze the time complexity of recursive algorithms
 For example
 T(n) = T(n/2) + T(n/2) + n2
 T(n) = a T(n/b) + f(n)
 Note: Recurrence Relations have uses outside of time
complexity analysis as well (for example in combinatorics),
but for the purpose of this lecture, this is the main use case.
Algorithms Divide and Conquer - Part I 7
RECURRENCE RELATIONS
 Wikipedia says: “…it is often necessary to replace the original
problem by a more general or complicated problem in order to
get the recursion going, and there is no systematic method for
finding the proper generalization.”
 Refer to this as the “generalization” step
 Sometimes counterintuitive that making a “generalization”, that is,
making the problem harder actually helps in solving it!
Algorithms Divide and Conquer - Part I 8
HOW TO D&C
divide_conquer(input J)
{
// Base Case
if (size of input is small enough) {
solve directly and return
}
// Divide Step
divide J into two or more parts J1, J2,...
// Recursive Calls
call divide_conquer(J1) to get a subsolution S1
call divide_conquer(J2) to get a subsolution S2
...
// Merge Step
Merge the subsolutions S1, S2,...into a global solution S
return S
}
Algorithms Divide and Conquer - Part I 9
GENERAL TEMPLATE
 Number of subproblems that you create in the “divide” step
 This plays a role in the recurrence relation that is created for
analysis
 T(n) = a T(n/b) + f(n)
Here “a” branches, each with size “n/b”, and f(n) time spent in
dividing and merging
 Example: T(n) = T(n/2) + 1
1 branch, size half and constant time spent in dividing and merging
Algorithms Divide and Conquer - Part I 10
NUMBER OF BRANCHES
divide_conquer(input J)
{
// Base Case
if (size of input is small enough) {
solve directly and return
}
// Divide Step
divide J into two or more parts J1, J2,...
// Recursive Calls
call divide_conquer(J1) to get a subsolution S1
call divide_conquer(J2) to get a subsolution S2
...
// Merge Step
Merge the subsolutions S1, S2,...into a global solution S
return S
}
Algorithms Divide and Conquer - Part I 11
GENERAL TEMPLATE – TIME COMPLEXITY
VIEW
Combined time
in steps other
than recursive
calls: f(n)
a recursive calls of size
n/b each. Total time:
a T(n/b)
 Binary Search
 Merge Sort
 Quick Sort
Algorithms Divide and Conquer - Part I 12
D&C – EXAMPLE ALGORITHMS
 Search (A, low, high, key)
 Mid = (low + high) / 2
 Compare A[mid] to key, and look either in left half or in right half
 T(n) = T(n/2) + 1
 T(n) = O(log n)
Algorithms Divide and Conquer - Part I 13
BINARY SEARCH
 Classic problem: Given an array, to sort it
 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) {
// Divide portion
if (j – i < THRESHOLD) {
InsertionSort(A,i,j)
Return
}
int k=(i+j)/2
// Recursive Calls
MergeSort(A,i,k)
MergeSort(A,k+1,j)
// Merge Calls
Merge(A,i,k,k+1,j)
}
Algorithms Divide and Conquer - Part I 14
MERGE SORT
 How to merge two lists effectively?
Algorithms Divide and Conquer - Part I 15
MERGING
 T(n) = 2T(n/2) + (n)
 Need some methods for solving such recurrence equations
 Substitution method
 Recursion tree method (unfolding)
 Master theorem
 T(n) = (n log n)
Algorithms Divide and Conquer - Part I 16
TIME COMPLEXITY OF MERGE SORT
Algorithms Divide and Conquer - Part I 17
EXAMPLES OF RECURRENCE RELATIONS
 Examples:
 T(n) = 2 T(n/2) + cn
T(n) = O (n log n)
 T(n) = T(n/2) + n
T(n) = O (n)
 3 General Approaches:
 Substitution method (Guess and Prove)
 Recursion tree method (unfold and reach a pattern)
 Master theorem
Algorithms Divide and Conquer - Part I 18
SOLVING RECURRENCE RELATIONS
 Given T(n) = 2 T(n/2) + cn
 We first “guess” that the solution is O(n log n)
 To prove this using induction, we first assume T(m) <= km log
m for all m < n
 Then T(n) = 2 T(n/2) + cn
<= 2 kn/2 log (n/2) + cn
= kn log n – (k – c)n // log (n/2) = log n – 1
<= k n log n, as long as k >= c
Algorithms Divide and Conquer - Part I 19
SUBSTITUTION METHOD FOR MERGE SORT
Algorithms Divide and Conquer - Part I 20
MASTER THEOREM FOR SOLVING
RECURRENCE RELATIONS
Only applies to Recurrence Relations of following type
T(n) = aT(n/b) + f(n)
 Case 1. If f(n) = O(nc) where c < logb a, then T(n) = θ(n^logb a)
 Case 2. If it is true, for some constant k ≥ 0, that f(n) = θ(nc
logk n) where c = logb a, then T(n) = θ(nc logk+1 n)
 Case 3. If it is true that f(n) = Ω(nc) where c > logb a, then T(n)
= θ(f(n))
T(n) = a T(n/b) + f(n)
If we unfold this, we get an expression like:
T(n) = ak T(n/bk) + f(n) + a f(n/b) + … + ak f(n/bk)
Then, for k ≈ logbn, T(n/bk) will be a small constant, and we can
assume T(n/bk) = 1.
Then, T(n) = a^(logbn) + f(n) + af(n/b) + … + ak f(n/bk)
= n^(logba) + f(n) + af(n/b) + … + ak f(n/bk)
We note that there are about logbn terms.
Algorithms Divide and Conquer - Part I 21
MASTER THEOREM – INTUITION
Intuition != Formal Proof
T(n) = n^(logba) + f(n) + af(n/b) + … + ak f(n/bk)
We observe that:
• If f(n) is very small, say a constant, then the first term dominates
• If f(n) =  (n^(logba)), then the T(n) = f(n) log n.
// The log n factor arises because there are ~ log n terms
• If f(n) is too large, then f(n) terms dominate
Algorithms Divide and Conquer - Part I 22
MASTER THEOREM – INTUITION (CONT.)
T(n) = 2 T(n/2) + c n
In this case:
• a = b = 2
• f(n) = c n
• logba = 1
• n^(logba) = n
So, f(n) =  (n^(logba))
Therefore, by Master Theorem,
T(n) = (f(n) log n)
That is, T(n) = (n log n)
Algorithms Divide and Conquer - Part I 23
APPLYING MASTER THEOREM TO MERGE
SORT RECURRENCE
 Select a “partition” element
 Partition the array into “left” and “right” portions (not
necessarily equal) based on the partition element
 Sort the left and right sides
 An inverted view of mergesort – spend time upfront
(partition), no need to merge later.
Algorithms Divide and Conquer - Part I 24
QUICKSORT
 quicksort(A,p,r)
if (p < r) {
q = partition (A,p,r)
quicksort(A,p,q-1)
quicksort(A,q+1,r)
}
Algorithms Divide and Conquer - Part I 25
QUICKSORT – THE PSEUDO CODE
 Invented in 1960 by C. A. R. Hoare
 More widely used than any other sort
 A well-studied, not difficult to implement algorithm
 R. Sedgewick – 1975 Ph.D. thesis at Stanford Univ. –
Analysis and Variations of QuickSort
Algorithms Divide and Conquer - Part I 26
QUICKSORT (THE TRIVIA CLUB VIEW)
Who said: “Elementary, My Dear Watson”?
 “There are two ways of constructing a software design: One
way is to make it so simple that there are obviously no
deficiencies, and the other way is to make it so complicated
that there are no obvious deficiencies. The first method is far
more difficult.”
 “We should forget about small efficiencies, say about 97% of
the time: premature optimization is the root of all evil.”
Algorithms Divide and Conquer - Part I 27
QUOTES, QUOTES
 How to find a good partition element
 How to partition (efficiently)
 Partition array so that:
 Some partitioning element (q) is its final position
 Every element smaller than q is to the left of q
 Every element larger than q is to the right of q
 Sedgwick states that “improving QuickSort is the better
mousetrap of computer science”
Algorithms Divide and Conquer - Part I 28
CENTRAL PROBLEM IN QUICKSORT
 T(n) = T(n1) + T(n2) + O(n)
Where n1 + n2 = n – 1
 So it all depends upon the kind of the split, and split will
likely not be the same each time.
 Worst case – very bad split: O(n2)
 Best case – good split: O(n log n)
 Average case – where does that fit?
http://mathworld.wolfram.com/Quicksort.html
Algorithms Divide and Conquer - Part I 29
QUICKSORT – TIME COMPLEXITY ANALYSIS
How long will the algorithm take?
Function sum (integer a) {
if (a == 1) exit;
if (a is odd) {
a = 3a + 1
} else {
a = a/2
}
}
Trichotomy – Extended
Given two functions f(n) and g(n), both strictly increasing with n,
is it possible that f(n) and g(n) cannot be compared
asymptotically?
Algorithms Divide and Conquer - Part I 30
OPEN QUESTIONS
1. Median Finding
(Textbook § 4.6)
2. Closest pair of
points algorithm
(Textbook § 4.7)
3. Strassen’s
algorithm for
matrix
multiplication
(Textbook § 4.8)
https://youtu.be/1AIvlizGo7Y
Algorithms Divide and Conquer - Part I 31
READING ASSIGNMENTS
We already have quite a few people who know how
to divide. So essentially we are now looking for
people who know how to conquer.
Algorithms
Analysis
Asymptotic
NP-
Completeness
Design
D&C
DP
Greedy
Graph
B&B
Applications
Algorithms Divide and Conquer - Part I 32
WHERE WE ARE
More D&C
in Next
Lecture

More Related Content

PPTX
Greedy Algorithms
PPTX
Divide and conquer - Quick sort
PPTX
unit-4-dynamic programming
PPT
Lower bound
PPTX
NP completeness
PPTX
Randomized Algorithm- Advanced Algorithm
PPT
Introduction to Design Algorithm And Analysis.ppt
PPT
Divide and Conquer
Greedy Algorithms
Divide and conquer - Quick sort
unit-4-dynamic programming
Lower bound
NP completeness
Randomized Algorithm- Advanced Algorithm
Introduction to Design Algorithm And Analysis.ppt
Divide and Conquer

What's hot (20)

PPTX
Greedy Algorithm - Knapsack Problem
PPTX
Stressen's matrix multiplication
PPT
Divide and conquer
PDF
Recurrence relation solutions
PPT
Greedy algorithms
PPTX
Algorithm Using Divide And Conquer
PPTX
Introduction to Algorithms and Asymptotic Notation
PPTX
Greedy algorithms
PPTX
Analysis of algorithm
PDF
Divide and Conquer
PPTX
Strassen's matrix multiplication
PPTX
Kruskal Algorithm
PPTX
Webinar : P, NP, NP-Hard , NP - Complete problems
PDF
Design and Analysis Algorithms.pdf
PPT
Chapter 5 Syntax Directed Translation
PPT
SINGLE-SOURCE SHORTEST PATHS
PPTX
String matching algorithms
PPTX
All pair shortest path
PDF
Algorithms Lecture 7: Graph Algorithms
Greedy Algorithm - Knapsack Problem
Stressen's matrix multiplication
Divide and conquer
Recurrence relation solutions
Greedy algorithms
Algorithm Using Divide And Conquer
Introduction to Algorithms and Asymptotic Notation
Greedy algorithms
Analysis of algorithm
Divide and Conquer
Strassen's matrix multiplication
Kruskal Algorithm
Webinar : P, NP, NP-Hard , NP - Complete problems
Design and Analysis Algorithms.pdf
Chapter 5 Syntax Directed Translation
SINGLE-SOURCE SHORTEST PATHS
String matching algorithms
All pair shortest path
Algorithms Lecture 7: Graph Algorithms
Ad

Viewers also liked (20)

PPTX
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
PPTX
Graph Traversal Algorithms - Depth First Search Traversal
PPT
Dinive conquer algorithm
PPTX
Asymptotic Notation and Data Structures
PPTX
Dynamic Programming - Part II
PPTX
Graph Traversal Algorithms - Breadth First Search
PPTX
Dynamic Programming - Part 1
PPTX
NP-Completeness - II
PPTX
Divide and conquer 1
PPT
5.2 divide and conquer
PDF
02 Analysis of Algorithms: Divide and Conquer
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
PPTX
Big o notation
PPT
Algorithm: Quick-Sort
PDF
Quick Sort , Merge Sort , Heap Sort
PDF
Sorting Algorithms
PPT
Quick Sort
PPT
Divide and conquer
PPTX
9 big o-notation
PDF
02 asymptotic-notation-and-recurrences
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Graph Traversal Algorithms - Depth First Search Traversal
Dinive conquer algorithm
Asymptotic Notation and Data Structures
Dynamic Programming - Part II
Graph Traversal Algorithms - Breadth First Search
Dynamic Programming - Part 1
NP-Completeness - II
Divide and conquer 1
5.2 divide and conquer
02 Analysis of Algorithms: Divide and Conquer
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Big o notation
Algorithm: Quick-Sort
Quick Sort , Merge Sort , Heap Sort
Sorting Algorithms
Quick Sort
Divide and conquer
9 big o-notation
02 asymptotic-notation-and-recurrences
Ad

Similar to Divide and Conquer - Part 1 (20)

PDF
Divide and conquer
PPTX
Divide and Conquer in DAA concept. For B Tech CSE
PPTX
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
PDF
Chapter 9 divide and conquer handouts with notes
PPT
Divide and conquer problem using merge sort
PPT
DivideAndConquer.pptDivideAndConquer.ppt
PPT
Introduction to basic algorithm knowledge.ppt
PPT
lecture4.ppt
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
PPT
MergesortQuickSort.ppt
PPTX
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
DOC
algorithm Unit 2
DOC
Unit 2 in daa
PPTX
T2311 - Ch 4_Part1.pptx
PDF
Sienna 4 divideandconquer
PPT
3-Chapter Three - Divide and Conquer.ppt
PPTX
Module 2_ Divide and Conquer Approach.pptx
PPTX
Divided and conqurddddddddddddddfffffe.pptx
PPTX
Merge sort and quick sort
PDF
Jurnal informatika
Divide and conquer
Divide and Conquer in DAA concept. For B Tech CSE
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
Chapter 9 divide and conquer handouts with notes
Divide and conquer problem using merge sort
DivideAndConquer.pptDivideAndConquer.ppt
Introduction to basic algorithm knowledge.ppt
lecture4.ppt
presentation_mergesortquicksort_1458716068_193111.ppt
MergesortQuickSort.ppt
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
algorithm Unit 2
Unit 2 in daa
T2311 - Ch 4_Part1.pptx
Sienna 4 divideandconquer
3-Chapter Three - Divide and Conquer.ppt
Module 2_ Divide and Conquer Approach.pptx
Divided and conqurddddddddddddddfffffe.pptx
Merge sort and quick sort
Jurnal informatika

More from Amrinder Arora (20)

PPTX
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
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...
PDF
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
PPTX
Online algorithms in Machine Learning
PPTX
Algorithmic Puzzles
PPTX
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
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
BTrees - Great alternative to Red Black, AVL and other BSTs
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
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
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...
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Online algorithms in Machine Learning
Algorithmic Puzzles
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
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
BTrees - Great alternative to Red Black, AVL and other BSTs
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)

PDF
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
PDF
Aug23rd - Mulesoft Community Workshop - Hyd, India.pdf
PDF
4 layer Arch & Reference Arch of IoT.pdf
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
PDF
NewMind AI Weekly Chronicles – August ’25 Week IV
PDF
Early detection and classification of bone marrow changes in lumbar vertebrae...
PDF
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
PDF
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
PDF
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
PDF
A symptom-driven medical diagnosis support model based on machine learning te...
PDF
Lung cancer patients survival prediction using outlier detection and optimize...
PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PDF
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
PDF
SaaS reusability assessment using machine learning techniques
PDF
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
PDF
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
PDF
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
PDF
Examining Bias in AI Generated News Content.pdf
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
Aug23rd - Mulesoft Community Workshop - Hyd, India.pdf
4 layer Arch & Reference Arch of IoT.pdf
Connector Corner: Transform Unstructured Documents with Agentic Automation
NewMind AI Weekly Chronicles – August ’25 Week IV
Early detection and classification of bone marrow changes in lumbar vertebrae...
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
A symptom-driven medical diagnosis support model based on machine learning te...
Lung cancer patients survival prediction using outlier detection and optimize...
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
SaaS reusability assessment using machine learning techniques
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
Examining Bias in AI Generated News Content.pdf

Divide and Conquer - Part 1

  • 1. Design and Analysis of Algorithms DIVIDE AND CONQUER PART I GENERAL TEMPLATE BINARY SEARCH MERGE SORT & QUICK SORT SOLVING RECURRENCE RELATIONS
  • 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 I 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 accompaniments  Induction for proving correctness  Recurrence relation solving for computing time (and/or space) complexity Algorithms Divide and Conquer - Part I 4 DIVIDE AND CONQUER
  • 5. By definition: For D&C, sub problems must be of same type. [The phrase “D&C” is also used in other contexts. It may refer to breaking down a task, but in Computer Science, D&C is a formal paradigm] Algorithms Divide and Conquer - Part I 5 D&C – CS, NOT MANAGEMENT/POLITICS
  • 6.  A recursive algorithm is an algorithm that calls itself on smaller input.  Algorithm sort (Array a) Begin sort (subarray consisting of first half of a) sort (subarray consisting of second half of a) do_something_else(); End Algorithms Divide and Conquer - Part I 6 RECURSION
  • 7.  Recurrence Relation is a recursive formula, commonly used to analyze the time complexity of recursive algorithms  For example  T(n) = T(n/2) + T(n/2) + n2  T(n) = a T(n/b) + f(n)  Note: Recurrence Relations have uses outside of time complexity analysis as well (for example in combinatorics), but for the purpose of this lecture, this is the main use case. Algorithms Divide and Conquer - Part I 7 RECURRENCE RELATIONS
  • 8.  Wikipedia says: “…it is often necessary to replace the original problem by a more general or complicated problem in order to get the recursion going, and there is no systematic method for finding the proper generalization.”  Refer to this as the “generalization” step  Sometimes counterintuitive that making a “generalization”, that is, making the problem harder actually helps in solving it! Algorithms Divide and Conquer - Part I 8 HOW TO D&C
  • 9. divide_conquer(input J) { // Base Case if (size of input is small enough) { solve directly and return } // Divide Step divide J into two or more parts J1, J2,... // Recursive Calls call divide_conquer(J1) to get a subsolution S1 call divide_conquer(J2) to get a subsolution S2 ... // Merge Step Merge the subsolutions S1, S2,...into a global solution S return S } Algorithms Divide and Conquer - Part I 9 GENERAL TEMPLATE
  • 10.  Number of subproblems that you create in the “divide” step  This plays a role in the recurrence relation that is created for analysis  T(n) = a T(n/b) + f(n) Here “a” branches, each with size “n/b”, and f(n) time spent in dividing and merging  Example: T(n) = T(n/2) + 1 1 branch, size half and constant time spent in dividing and merging Algorithms Divide and Conquer - Part I 10 NUMBER OF BRANCHES
  • 11. divide_conquer(input J) { // Base Case if (size of input is small enough) { solve directly and return } // Divide Step divide J into two or more parts J1, J2,... // Recursive Calls call divide_conquer(J1) to get a subsolution S1 call divide_conquer(J2) to get a subsolution S2 ... // Merge Step Merge the subsolutions S1, S2,...into a global solution S return S } Algorithms Divide and Conquer - Part I 11 GENERAL TEMPLATE – TIME COMPLEXITY VIEW Combined time in steps other than recursive calls: f(n) a recursive calls of size n/b each. Total time: a T(n/b)
  • 12.  Binary Search  Merge Sort  Quick Sort Algorithms Divide and Conquer - Part I 12 D&C – EXAMPLE ALGORITHMS
  • 13.  Search (A, low, high, key)  Mid = (low + high) / 2  Compare A[mid] to key, and look either in left half or in right half  T(n) = T(n/2) + 1  T(n) = O(log n) Algorithms Divide and Conquer - Part I 13 BINARY SEARCH
  • 14.  Classic problem: Given an array, to sort it  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) { // Divide portion if (j – i < THRESHOLD) { InsertionSort(A,i,j) Return } int k=(i+j)/2 // Recursive Calls MergeSort(A,i,k) MergeSort(A,k+1,j) // Merge Calls Merge(A,i,k,k+1,j) } Algorithms Divide and Conquer - Part I 14 MERGE SORT
  • 15.  How to merge two lists effectively? Algorithms Divide and Conquer - Part I 15 MERGING
  • 16.  T(n) = 2T(n/2) + (n)  Need some methods for solving such recurrence equations  Substitution method  Recursion tree method (unfolding)  Master theorem  T(n) = (n log n) Algorithms Divide and Conquer - Part I 16 TIME COMPLEXITY OF MERGE SORT
  • 17. Algorithms Divide and Conquer - Part I 17 EXAMPLES OF RECURRENCE RELATIONS
  • 18.  Examples:  T(n) = 2 T(n/2) + cn T(n) = O (n log n)  T(n) = T(n/2) + n T(n) = O (n)  3 General Approaches:  Substitution method (Guess and Prove)  Recursion tree method (unfold and reach a pattern)  Master theorem Algorithms Divide and Conquer - Part I 18 SOLVING RECURRENCE RELATIONS
  • 19.  Given T(n) = 2 T(n/2) + cn  We first “guess” that the solution is O(n log n)  To prove this using induction, we first assume T(m) <= km log m for all m < n  Then T(n) = 2 T(n/2) + cn <= 2 kn/2 log (n/2) + cn = kn log n – (k – c)n // log (n/2) = log n – 1 <= k n log n, as long as k >= c Algorithms Divide and Conquer - Part I 19 SUBSTITUTION METHOD FOR MERGE SORT
  • 20. Algorithms Divide and Conquer - Part I 20 MASTER THEOREM FOR SOLVING RECURRENCE RELATIONS Only applies to Recurrence Relations of following type T(n) = aT(n/b) + f(n)  Case 1. If f(n) = O(nc) where c < logb a, then T(n) = θ(n^logb a)  Case 2. If it is true, for some constant k ≥ 0, that f(n) = θ(nc logk n) where c = logb a, then T(n) = θ(nc logk+1 n)  Case 3. If it is true that f(n) = Ω(nc) where c > logb a, then T(n) = θ(f(n))
  • 21. T(n) = a T(n/b) + f(n) If we unfold this, we get an expression like: T(n) = ak T(n/bk) + f(n) + a f(n/b) + … + ak f(n/bk) Then, for k ≈ logbn, T(n/bk) will be a small constant, and we can assume T(n/bk) = 1. Then, T(n) = a^(logbn) + f(n) + af(n/b) + … + ak f(n/bk) = n^(logba) + f(n) + af(n/b) + … + ak f(n/bk) We note that there are about logbn terms. Algorithms Divide and Conquer - Part I 21 MASTER THEOREM – INTUITION Intuition != Formal Proof
  • 22. T(n) = n^(logba) + f(n) + af(n/b) + … + ak f(n/bk) We observe that: • If f(n) is very small, say a constant, then the first term dominates • If f(n) =  (n^(logba)), then the T(n) = f(n) log n. // The log n factor arises because there are ~ log n terms • If f(n) is too large, then f(n) terms dominate Algorithms Divide and Conquer - Part I 22 MASTER THEOREM – INTUITION (CONT.)
  • 23. T(n) = 2 T(n/2) + c n In this case: • a = b = 2 • f(n) = c n • logba = 1 • n^(logba) = n So, f(n) =  (n^(logba)) Therefore, by Master Theorem, T(n) = (f(n) log n) That is, T(n) = (n log n) Algorithms Divide and Conquer - Part I 23 APPLYING MASTER THEOREM TO MERGE SORT RECURRENCE
  • 24.  Select a “partition” element  Partition the array into “left” and “right” portions (not necessarily equal) based on the partition element  Sort the left and right sides  An inverted view of mergesort – spend time upfront (partition), no need to merge later. Algorithms Divide and Conquer - Part I 24 QUICKSORT
  • 25.  quicksort(A,p,r) if (p < r) { q = partition (A,p,r) quicksort(A,p,q-1) quicksort(A,q+1,r) } Algorithms Divide and Conquer - Part I 25 QUICKSORT – THE PSEUDO CODE
  • 26.  Invented in 1960 by C. A. R. Hoare  More widely used than any other sort  A well-studied, not difficult to implement algorithm  R. Sedgewick – 1975 Ph.D. thesis at Stanford Univ. – Analysis and Variations of QuickSort Algorithms Divide and Conquer - Part I 26 QUICKSORT (THE TRIVIA CLUB VIEW) Who said: “Elementary, My Dear Watson”?
  • 27.  “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”  “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” Algorithms Divide and Conquer - Part I 27 QUOTES, QUOTES
  • 28.  How to find a good partition element  How to partition (efficiently)  Partition array so that:  Some partitioning element (q) is its final position  Every element smaller than q is to the left of q  Every element larger than q is to the right of q  Sedgwick states that “improving QuickSort is the better mousetrap of computer science” Algorithms Divide and Conquer - Part I 28 CENTRAL PROBLEM IN QUICKSORT
  • 29.  T(n) = T(n1) + T(n2) + O(n) Where n1 + n2 = n – 1  So it all depends upon the kind of the split, and split will likely not be the same each time.  Worst case – very bad split: O(n2)  Best case – good split: O(n log n)  Average case – where does that fit? http://mathworld.wolfram.com/Quicksort.html Algorithms Divide and Conquer - Part I 29 QUICKSORT – TIME COMPLEXITY ANALYSIS
  • 30. How long will the algorithm take? Function sum (integer a) { if (a == 1) exit; if (a is odd) { a = 3a + 1 } else { a = a/2 } } Trichotomy – Extended Given two functions f(n) and g(n), both strictly increasing with n, is it possible that f(n) and g(n) cannot be compared asymptotically? Algorithms Divide and Conquer - Part I 30 OPEN QUESTIONS
  • 31. 1. Median Finding (Textbook § 4.6) 2. Closest pair of points algorithm (Textbook § 4.7) 3. Strassen’s algorithm for matrix multiplication (Textbook § 4.8) https://youtu.be/1AIvlizGo7Y Algorithms Divide and Conquer - Part I 31 READING ASSIGNMENTS We already have quite a few people who know how to divide. So essentially we are now looking for people who know how to conquer.