SlideShare a Scribd company logo
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

What's hot

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
Nv Thejaswini
 
Algorithm Using Divide And Conquer
Algorithm Using Divide And ConquerAlgorithm Using Divide And Conquer
Algorithm Using Divide And Conquer
UrviBhalani2
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
Bulbul Agrawal
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
Mohammed Hussein
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
Daffodil International University
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
ShahDhruv21
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Binary Search
Binary SearchBinary Search
Binary Search
kunj desai
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
Rajandeep Gill
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
Rajendran
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 

What's hot (20)

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Algorithm Using Divide And Conquer
Algorithm Using Divide And ConquerAlgorithm Using Divide And Conquer
Algorithm Using Divide And Conquer
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Merge sort
Merge sortMerge sort
Merge sort
 

Viewers also liked

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
Amrinder Arora
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
Amrinder Arora
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
Amrinder Arora
 
NP completeness
NP completenessNP completeness
NP completeness
Amrinder Arora
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
Amrinder Arora
 
NP-Completeness - II
NP-Completeness - IINP-Completeness - II
NP-Completeness - II
Amrinder Arora
 
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 conquer
Krish_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 Conquer
Andres 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 recurrences
jayavignesh86
 
Big o notation
Big o notationBig o notation
Big o notation
hamza mushtaq
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Vikas 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
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Muhammad Sarfraz
 
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
Amrinder Arora
 

Viewers also liked (20)

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
 
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
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
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
 

Similar to Divide and Conquer - Part 1

T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
Merge Sort
Merge SortMerge Sort
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
KokilaK25
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
B.Kirron Reddi
 
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
 
2.pptx
2.pptx2.pptx
2.pptx
MohAlyasin1
 
03 dc
03 dc03 dc
03 dc
Hira Gul
 
lecture 1
lecture 1lecture 1
lecture 1sajinsc
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
subhashchandra197
 
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
NANDINI 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 key
appasami
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013Gary Short
 
analysis.ppt
analysis.pptanalysis.ppt
analysis.ppt
AarushSharma69
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
MuhammadSheraz836877
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
Nv Thejaswini
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 

Similar to 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
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 

More from 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 Mahana
Amrinder 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 Learning
Amrinder Arora
 
Algorithmic Puzzles
Algorithmic PuzzlesAlgorithmic Puzzles
Algorithmic Puzzles
Amrinder 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 Filters
Amrinder Arora
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci Heaps
Amrinder Arora
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data Structures
Amrinder Arora
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for Strings
Amrinder 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 Structures
Amrinder 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 BSTs
Amrinder 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 Black
Amrinder Arora
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their Representations
Amrinder 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 Structures
Amrinder Arora
 
Online Algorithms - An Introduction
Online Algorithms - An IntroductionOnline Algorithms - An Introduction
Online Algorithms - An Introduction
Amrinder Arora
 
Learning to learn
Learning to learnLearning to learn
Learning to learn
Amrinder Arora
 

More from Amrinder Arora (19)

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

Recently uploaded

Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 

Recently uploaded (20)

Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 

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.