SlideShare una empresa de Scribd logo
1 de 33
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

Más contenido relacionado

La actualidad más candente

DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsMohamed Loey
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using BacktrackingAbhishek Singh
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of AlgorithmsSwapnil Agrawal
 
Master method
Master method Master method
Master method Rajendran
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's AlgorithmTanmay Baranwal
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 

La actualidad más candente (20)

Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
 
Master method
Master method Master method
Master method
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 

Destacado

Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsAmrinder Arora
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part IIAmrinder Arora
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchAmrinder Arora
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1Amrinder Arora
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sortMadhu Bala
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
02 Analysis of Algorithms: Divide and Conquer
02 Analysis of Algorithms: Divide and Conquer02 Analysis of Algorithms: Divide and Conquer
02 Analysis of Algorithms: Divide and ConquerAndres Mendez-Vazquez
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesjayavignesh86
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notationirdginfo
 
02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrences02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrencesNoushadur Shoukhin
 

Destacado (20)

Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
 
NP completeness
NP completenessNP completeness
NP completeness
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
NP-Completeness - II
NP-Completeness - IINP-Completeness - II
NP-Completeness - II
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
02 Analysis of Algorithms: Divide and Conquer
02 Analysis of Algorithms: Divide and Conquer02 Analysis of Algorithms: Divide and Conquer
02 Analysis of Algorithms: Divide and Conquer
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Big o notation
Big o notationBig o notation
Big o notation
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
 
02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrences02 asymptotic-notation-and-recurrences
02 asymptotic-notation-and-recurrences
 

Similar a Divide and Conquer - Part 1

T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)Mumtaz Ali
 
lecture 1
lecture 1lecture 1
lecture 1sajinsc
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutionssubhashchandra197
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusNANDINI SHARMA
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013Gary Short
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 

Similar a Divide and Conquer - Part 1 (20)

T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 
2.pptx
2.pptx2.pptx
2.pptx
 
03 dc
03 dc03 dc
03 dc
 
Slide2
Slide2Slide2
Slide2
 
lecture 1
lecture 1lecture 1
lecture 1
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013
 
analysis.ppt
analysis.pptanalysis.ppt
analysis.ppt
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 

Más de Amrinder Arora

Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Amrinder Arora
 
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Amrinder Arora
 
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaArima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaAmrinder Arora
 
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Amrinder Arora
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Amrinder Arora
 
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Amrinder Arora
 
Online algorithms in Machine Learning
Online algorithms in Machine LearningOnline algorithms in Machine Learning
Online algorithms in Machine LearningAmrinder Arora
 
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisEuclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisAmrinder Arora
 
Set Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersSet Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersAmrinder Arora
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsAmrinder Arora
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresAmrinder Arora
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsAmrinder Arora
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresAmrinder Arora
 
BTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTsBTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTsAmrinder Arora
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackAmrinder Arora
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsAmrinder Arora
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data StructuresAmrinder Arora
 
Online Algorithms - An Introduction
Online Algorithms - An IntroductionOnline Algorithms - An Introduction
Online Algorithms - An IntroductionAmrinder Arora
 

Más de Amrinder Arora (20)

Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
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...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
 
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaArima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
 
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
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 O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
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)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
 
Online algorithms in Machine Learning
Online algorithms in Machine LearningOnline algorithms in Machine Learning
Online algorithms in Machine Learning
 
Algorithmic Puzzles
Algorithmic PuzzlesAlgorithmic Puzzles
Algorithmic Puzzles
 
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisEuclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
 
Set Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersSet Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom Filters
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci Heaps
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data Structures
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for Strings
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data Structures
 
BTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTsBTrees - Great alternative to Red Black, AVL and other BSTs
BTrees - Great alternative to Red Black, AVL and other BSTs
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their Representations
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
 
Online Algorithms - An Introduction
Online Algorithms - An IntroductionOnline Algorithms - An Introduction
Online Algorithms - An Introduction
 
Learning to learn
Learning to learnLearning to learn
Learning to learn
 

Último

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 

Último (20)

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 

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 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
  • 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.