SlideShare a Scribd company logo
1 of 23
Comp 122, Spring 2004
Divide and Conquer
(Merge Sort)
Divide and Conquer
(Merge Sort)
Comp 122- 2
Divide and Conquer
 Recursive in structure
 Divide the problem into sub-problems that are
similar to the original but smaller in size
 Conquer the sub-problems by solving them
recursively. If they are small enough, just solve
them in a straightforward manner.
 Combine the solutions to create a solution to
the original problem
Comp 122- 3
An Example: Merge Sort
Sorting Problem: Sort a sequence of n elements into
non-decreasing order.
 Divide: Divide the n-element sequence to be
sorted into two subsequences of n/2 elements each
 Conquer: Sort the two subsequences recursively
using merge sort.
 Combine: Merge the two sorted subsequences to
produce the sorted answer.
Comp 122- 5
Merge Sort – Example
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
2618 632 1543 19
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
18 26 326 15 43 1 9
6 18 26 32 1 9 15 43
1 6 9 15 18 26 32 43
18 26
18 26
18 26
32
32
6
6
32 6
18 26 32 6
43
43
15
15
43 15
9
9
1
1
9 1
43 15 9 1
18 26 32 6 43 15 9 1
18 26 632
626 3218
1543 19
1 915 43
16 9 1518 26 32 43
Original Sequence Sorted Sequence
Comp 122- 6
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers
MergeSort (A, p, r) // sort A[p..r] by divide & conquer
1 if p < r
2 then q ← (p+r)/2
3 MergeSort (A, p, q)
4 MergeSort (A, q+1, r)
5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]
Initial Call: MergeSort(A, 1, n)
Comp 122- 7
Procedure Merge
Merge(A, p, q, r)
1 n1 ← q – p + 1
2 n2 ← r – q
3 for i ← 1 to n1
4 do L[i] ← A[p + i – 1]
5 for j ← 1 to n2
6 do R[j] ← A[q + j]
7 L[n1+1] ← ∞
8 R[n2+1] ← ∞
9 i ← 1
10 j ← 1
11 for k ←p to r
12 do if L[i] ≤ R[j]
13 then A[k] ← L[i]
14 i ← i + 1
15 else A[k] ← R[j]
16 j ← j + 1
Sentinels, to avoid having to
check if either subarray is
fully copied at each step.
Input: Array containing
sorted subarrays A[p..q]
and A[q+1..r].
Output: Merged sorted
subarray in A[p..r].
Comp 122- 8
j
Merge – Example
6 8 26 32 1 9 42 43… …A
k
6 8 26 32 1 9 42 43
k k k k k k k
i i i i
∞ ∞
i j j j j
6 8 26 32 1 9 42 43
1 6 8 9 26 32 42 43
k
L R
Comp 122- 9
Correctness of Merge
Merge(A, p, q, r)
1 n1 ← q – p + 1
2 n2 ← r – q
3 for i ← 1 to n1
4 do L[i] ← A[p + i – 1]
5 for j ← 1 to n2
6 do R[j] ← A[q + j]
7 L[n1+1] ← ∞
8 R[n2+1] ← ∞
9 i ← 1
10 j ← 1
11 for k ←p to r
12 do if L[i] ≤ R[j]
13 then A[k] ← L[i]
14 i ← i + 1
15 else A[k] ← R[j]
16 j ← j + 1
Loop Invariant for the for loop
At the start of each iteration of the
for loop:
Subarray A[p..k – 1]
contains the k – p smallest elements
of L and R in sorted order.
L[i] and R[j] are the smallest elements of
L and R that have not been copied back into
A.
Initialization:
Before the first iteration:
•A[p..k – 1] is empty.
•i = j = 1.
•L[1] and R[1] are the smallest
elements of L and R not copied to A.
Comp 122- 10
Correctness of Merge
Merge(A, p, q, r)
1 n1 ← q – p + 1
2 n2 ← r – q
3 for i ← 1 to n1
4 do L[i] ← A[p + i – 1]
5 for j ← 1 to n2
6 do R[j] ← A[q + j]
7 L[n1+1] ← ∞
8 R[n2+1] ← ∞
9 i ← 1
10 j ← 1
11 for k ←p to r
12 do if L[i] ≤ R[j]
13 then A[k] ← L[i]
14 i ← i + 1
15 else A[k] ← R[j]
16 j ← j + 1
Maintenance:
Case 1: L[i] ≤ R[j]
•By LI, A contains p – k smallest elements
of L and R in sorted order.
•By LI, L[i] and R[j] are the smallest
elements of L and R not yet copied into A.
•Line 13 results in A containing p – k + 1
smallest elements (again in sorted order).
Incrementing i and k reestablishes the LI
for the next iteration.
Similarly for L[i] > R[j].
Termination:
•On termination, k = r + 1.
•By LI, A contains r – p + 1 smallest
elements of L and R in sorted order.
•L and R together contain r – p + 3 elements.
All but the two sentinels have been copied
back into A.
Comp 122- 11
Analysis of Merge Sort
 Running time T(n) of Merge Sort:
 Divide: computing the middle takes Θ(1)
 Conquer: solving 2 subproblems takes 2T(n/2)
 Combine: merging n elements takes Θ(n)
 Total:
T(n) = Θ(1) if n = 1
T(n) = 2T(n/2) + Θ(n) if n > 1
⇒ T(n) = Θ(n lg n) (CLRS, Chapter 4)
Comp 122, Spring 2004
Recurrences – IRecurrences – I
Comp 122- 13
Recurrence Relations
 Equation or an inequality that characterizes a
function by its values on smaller inputs.
 Solution Methods (Chapter 4)
 Substitution Method.
 Recursion-tree Method.
 Master Method.
 Recurrence relations arise when we analyze the
running time of iterative or recursive algorithms.
 Ex: Divide and Conquer.
T(n) = Θ(1) if n ≤ c
T(n) = a T(n/b) + D(n) + C(n) otherwise
Comp 122- 14
Substitution Method
 Guess the form of the solution, then
use mathematical induction to show it correct.
 Substitute guessed answer for the function when the
inductive hypothesis is applied to smaller values –
hence, the name.
 Works well when the solution is easy to guess.
 No general way to guess the correct solution.
Comp 122- 15
Example – Exact Function
Recurrence: T(n) = 1 if n = 1
T(n) = 2T(n/2) + n if n > 1
Guess: T(n) = n lg n + n.
Induction:
•Basis: n = 1 ⇒ n lgn + n = 1 = T(n).
•Hypothesis: T(k) = k lg k + k for all k < n.
•Inductive Step: T(n) = 2 T(n/2) + n
= 2 ((n/2)lg(n/2) + (n/2)) + n
= n (lg(n/2)) + 2n
= n lg n – n + 2n
= n lg n + n
Comp 122- 16
Recursion-tree Method
 Making a good guess is sometimes difficult with
the substitution method.
 Use recursion trees to devise good guesses.
 Recursion Trees
 Show successive expansions of recurrences using
trees.
 Keep track of the time spent on the subproblems of a
divide and conquer algorithm.
 Help organize the algebraic bookkeeping necessary
to solve a recurrence.
Comp 122- 17
Recursion Tree – Example
 Running time of Merge Sort:
T(n) = Θ(1) if n = 1
T(n) = 2T(n/2) + Θ(n) if n > 1
 Rewrite the recurrence as
T(n) = c if n = 1
T(n) = 2T(n/2) + cn if n > 1
c > 0: Running time for the base case and
time per array element for the divide and
combine steps.
Comp 122- 18
Recursion Tree for Merge Sort
For the original problem,
we have a cost of cn,
plus two subproblems
each of size (n/2) and
running time T(n/2).
cn
T(n/2) T(n/2)
Each of the size n/2 problems
has a cost of cn/2 plus two
subproblems, each costing
T(n/4).
cn
cn/2 cn/2
T(n/4) T(n/4) T(n/4) T(n/4)
Cost of divide and
merge.
Cost of sorting
subproblems.
Comp 122- 19
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn
cn/2 cn/2
cn/4 cn/4 cn/4 cn/4
c c c cc c
lg n
cn
cn
cn
cn
Total : cnlgn+cn
Comp 122- 20
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn
cn/2 cn/2
cn/4 cn/4 cn/4 cn/4
c c c cc c
•Each level has total cost cn.
•Each time we go down one level,
the number of subproblems doubles,
but the cost per subproblem halves
⇒ cost per level remains the same.
•There are lg n + 1 levels, height is
lg n. (Assuming n is a power of 2.)
•Can be proved by induction.
•Total cost = sum of costs at each
level = (lg n + 1)cn = cnlgn + cn =
Θ(n lgn).
Comp 122- 21
Other Examples
 Use the recursion-tree method to determine a
guess for the recurrences
 T(n) = 3T(n/4) + Θ(n2
).
 T(n) = T(n/3) + T(2n/3) + O(n).
Comp 122- 22
Recursion Trees – Caution Note
 Recursion trees only generate guesses.
 Verify guesses using substitution method.
 A small amount of “sloppiness” can be
tolerated. Why?
 If careful when drawing out a recursion tree and
summing the costs, can be used as direct proof.
Comp 122- 23
The Master Method
 Based on the Master theorem.
 “Cookbook” approach for solving recurrences
of the form
T(n) = aT(n/b) + f(n)
• a ≥ 1, b > 1 are constants.
• f(n) is asymptotically positive.
• n/b may not be an integer, but we ignore floors and
ceilings. Why?
 Requires memorization of three cases.
Comp 122- 24
The Master Theorem
Theorem 4.1
Let a ≥ 1 and b > 1 be constants, let f(n) be a function, and
Let T(n) be defined on nonnegative integers by the recurrence
T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b.
T(n) can be bounded asymptotically in three cases:
1. If f(n) = O(nlogba–ε
) for some constant ε > 0, then T(n) = Θ(nlogba
).
2. If f(n) = Θ(nlogba
), then T(n) = Θ(nlogba
lg n).
3. If f(n) = Ω(nlogba+ε
) for some constant ε > 0,
and if, for some constant c < 1 and all sufficiently large n,
we have a·f(n/b) ≤ c f(n), then T(n) = Θ(f(n)).
Theorem 4.1
Let a ≥ 1 and b > 1 be constants, let f(n) be a function, and
Let T(n) be defined on nonnegative integers by the recurrence
T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b.
T(n) can be bounded asymptotically in three cases:
1. If f(n) = O(nlogba–ε
) for some constant ε > 0, then T(n) = Θ(nlogba
).
2. If f(n) = Θ(nlogba
), then T(n) = Θ(nlogba
lg n).
3. If f(n) = Ω(nlogba+ε
) for some constant ε > 0,
and if, for some constant c < 1 and all sufficiently large n,
we have a·f(n/b) ≤ c f(n), then T(n) = Θ(f(n)).
We’ll return to recurrences as we need them…

More Related Content

What's hot (20)

Data structures using c
Data structures using cData structures using c
Data structures using c
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Data Structure and Algorithm - Divide and Conquer
Data Structure and Algorithm - Divide and ConquerData Structure and Algorithm - Divide and Conquer
Data Structure and Algorithm - Divide and Conquer
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Kmp
KmpKmp
Kmp
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
 
Time complexity
Time complexityTime complexity
Time complexity
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 

Similar to 5.2 divide and conquer

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015Edhole.com
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
Introduction
IntroductionIntroduction
Introductionpilavare
 
Top school in noida
Top school in noidaTop school in noida
Top school in noidaEdhole.com
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
lecture 15
lecture 15lecture 15
lecture 15sajinsc
 
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docxeugeniadean34240
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutionssubhashchandra197
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika MamaMa28
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesMegha V
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and AnalysisReetesh Gupta
 

Similar to 5.2 divide and conquer (20)

Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
Mergesort
MergesortMergesort
Mergesort
 
Introduction
IntroductionIntroduction
Introduction
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Top school in noida
Top school in noidaTop school in noida
Top school in noida
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
lecture 15
lecture 15lecture 15
lecture 15
 
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
2.pptx
2.pptx2.pptx
2.pptx
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 

More from Krish_ver2

5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back trackingKrish_ver2
 
5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithmKrish_ver2
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programmingKrish_ver2
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-iKrish_ver2
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02Krish_ver2
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing extKrish_ver2
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashingKrish_ver2
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal searchKrish_ver2
 

More from Krish_ver2 (20)

5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithm
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing ext
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashing
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
 
4.2 bst 02
4.2 bst 024.2 bst 02
4.2 bst 02
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 

Recently uploaded

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 

Recently uploaded (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 

5.2 divide and conquer

  • 1. Comp 122, Spring 2004 Divide and Conquer (Merge Sort) Divide and Conquer (Merge Sort)
  • 2. Comp 122- 2 Divide and Conquer  Recursive in structure  Divide the problem into sub-problems that are similar to the original but smaller in size  Conquer the sub-problems by solving them recursively. If they are small enough, just solve them in a straightforward manner.  Combine the solutions to create a solution to the original problem
  • 3. Comp 122- 3 An Example: Merge Sort Sorting Problem: Sort a sequence of n elements into non-decreasing order.  Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each  Conquer: Sort the two subsequences recursively using merge sort.  Combine: Merge the two sorted subsequences to produce the sorted answer.
  • 4. Comp 122- 5 Merge Sort – Example 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 2618 632 1543 19 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 26 326 15 43 1 9 6 18 26 32 1 9 15 43 1 6 9 15 18 26 32 43 18 26 18 26 18 26 32 32 6 6 32 6 18 26 32 6 43 43 15 15 43 15 9 9 1 1 9 1 43 15 9 1 18 26 32 6 43 15 9 1 18 26 632 626 3218 1543 19 1 915 43 16 9 1518 26 32 43 Original Sequence Sorted Sequence
  • 5. Comp 122- 6 Merge-Sort (A, p, r) INPUT: a sequence of n numbers stored in array A OUTPUT: an ordered sequence of n numbers MergeSort (A, p, r) // sort A[p..r] by divide & conquer 1 if p < r 2 then q ← (p+r)/2 3 MergeSort (A, p, q) 4 MergeSort (A, q+1, r) 5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r] Initial Call: MergeSort(A, 1, n)
  • 6. Comp 122- 7 Procedure Merge Merge(A, p, q, r) 1 n1 ← q – p + 1 2 n2 ← r – q 3 for i ← 1 to n1 4 do L[i] ← A[p + i – 1] 5 for j ← 1 to n2 6 do R[j] ← A[q + j] 7 L[n1+1] ← ∞ 8 R[n2+1] ← ∞ 9 i ← 1 10 j ← 1 11 for k ←p to r 12 do if L[i] ≤ R[j] 13 then A[k] ← L[i] 14 i ← i + 1 15 else A[k] ← R[j] 16 j ← j + 1 Sentinels, to avoid having to check if either subarray is fully copied at each step. Input: Array containing sorted subarrays A[p..q] and A[q+1..r]. Output: Merged sorted subarray in A[p..r].
  • 7. Comp 122- 8 j Merge – Example 6 8 26 32 1 9 42 43… …A k 6 8 26 32 1 9 42 43 k k k k k k k i i i i ∞ ∞ i j j j j 6 8 26 32 1 9 42 43 1 6 8 9 26 32 42 43 k L R
  • 8. Comp 122- 9 Correctness of Merge Merge(A, p, q, r) 1 n1 ← q – p + 1 2 n2 ← r – q 3 for i ← 1 to n1 4 do L[i] ← A[p + i – 1] 5 for j ← 1 to n2 6 do R[j] ← A[q + j] 7 L[n1+1] ← ∞ 8 R[n2+1] ← ∞ 9 i ← 1 10 j ← 1 11 for k ←p to r 12 do if L[i] ≤ R[j] 13 then A[k] ← L[i] 14 i ← i + 1 15 else A[k] ← R[j] 16 j ← j + 1 Loop Invariant for the for loop At the start of each iteration of the for loop: Subarray A[p..k – 1] contains the k – p smallest elements of L and R in sorted order. L[i] and R[j] are the smallest elements of L and R that have not been copied back into A. Initialization: Before the first iteration: •A[p..k – 1] is empty. •i = j = 1. •L[1] and R[1] are the smallest elements of L and R not copied to A.
  • 9. Comp 122- 10 Correctness of Merge Merge(A, p, q, r) 1 n1 ← q – p + 1 2 n2 ← r – q 3 for i ← 1 to n1 4 do L[i] ← A[p + i – 1] 5 for j ← 1 to n2 6 do R[j] ← A[q + j] 7 L[n1+1] ← ∞ 8 R[n2+1] ← ∞ 9 i ← 1 10 j ← 1 11 for k ←p to r 12 do if L[i] ≤ R[j] 13 then A[k] ← L[i] 14 i ← i + 1 15 else A[k] ← R[j] 16 j ← j + 1 Maintenance: Case 1: L[i] ≤ R[j] •By LI, A contains p – k smallest elements of L and R in sorted order. •By LI, L[i] and R[j] are the smallest elements of L and R not yet copied into A. •Line 13 results in A containing p – k + 1 smallest elements (again in sorted order). Incrementing i and k reestablishes the LI for the next iteration. Similarly for L[i] > R[j]. Termination: •On termination, k = r + 1. •By LI, A contains r – p + 1 smallest elements of L and R in sorted order. •L and R together contain r – p + 3 elements. All but the two sentinels have been copied back into A.
  • 10. Comp 122- 11 Analysis of Merge Sort  Running time T(n) of Merge Sort:  Divide: computing the middle takes Θ(1)  Conquer: solving 2 subproblems takes 2T(n/2)  Combine: merging n elements takes Θ(n)  Total: T(n) = Θ(1) if n = 1 T(n) = 2T(n/2) + Θ(n) if n > 1 ⇒ T(n) = Θ(n lg n) (CLRS, Chapter 4)
  • 11. Comp 122, Spring 2004 Recurrences – IRecurrences – I
  • 12. Comp 122- 13 Recurrence Relations  Equation or an inequality that characterizes a function by its values on smaller inputs.  Solution Methods (Chapter 4)  Substitution Method.  Recursion-tree Method.  Master Method.  Recurrence relations arise when we analyze the running time of iterative or recursive algorithms.  Ex: Divide and Conquer. T(n) = Θ(1) if n ≤ c T(n) = a T(n/b) + D(n) + C(n) otherwise
  • 13. Comp 122- 14 Substitution Method  Guess the form of the solution, then use mathematical induction to show it correct.  Substitute guessed answer for the function when the inductive hypothesis is applied to smaller values – hence, the name.  Works well when the solution is easy to guess.  No general way to guess the correct solution.
  • 14. Comp 122- 15 Example – Exact Function Recurrence: T(n) = 1 if n = 1 T(n) = 2T(n/2) + n if n > 1 Guess: T(n) = n lg n + n. Induction: •Basis: n = 1 ⇒ n lgn + n = 1 = T(n). •Hypothesis: T(k) = k lg k + k for all k < n. •Inductive Step: T(n) = 2 T(n/2) + n = 2 ((n/2)lg(n/2) + (n/2)) + n = n (lg(n/2)) + 2n = n lg n – n + 2n = n lg n + n
  • 15. Comp 122- 16 Recursion-tree Method  Making a good guess is sometimes difficult with the substitution method.  Use recursion trees to devise good guesses.  Recursion Trees  Show successive expansions of recurrences using trees.  Keep track of the time spent on the subproblems of a divide and conquer algorithm.  Help organize the algebraic bookkeeping necessary to solve a recurrence.
  • 16. Comp 122- 17 Recursion Tree – Example  Running time of Merge Sort: T(n) = Θ(1) if n = 1 T(n) = 2T(n/2) + Θ(n) if n > 1  Rewrite the recurrence as T(n) = c if n = 1 T(n) = 2T(n/2) + cn if n > 1 c > 0: Running time for the base case and time per array element for the divide and combine steps.
  • 17. Comp 122- 18 Recursion Tree for Merge Sort For the original problem, we have a cost of cn, plus two subproblems each of size (n/2) and running time T(n/2). cn T(n/2) T(n/2) Each of the size n/2 problems has a cost of cn/2 plus two subproblems, each costing T(n/4). cn cn/2 cn/2 T(n/4) T(n/4) T(n/4) T(n/4) Cost of divide and merge. Cost of sorting subproblems.
  • 18. Comp 122- 19 Recursion Tree for Merge Sort Continue expanding until the problem size reduces to 1. cn cn/2 cn/2 cn/4 cn/4 cn/4 cn/4 c c c cc c lg n cn cn cn cn Total : cnlgn+cn
  • 19. Comp 122- 20 Recursion Tree for Merge Sort Continue expanding until the problem size reduces to 1. cn cn/2 cn/2 cn/4 cn/4 cn/4 cn/4 c c c cc c •Each level has total cost cn. •Each time we go down one level, the number of subproblems doubles, but the cost per subproblem halves ⇒ cost per level remains the same. •There are lg n + 1 levels, height is lg n. (Assuming n is a power of 2.) •Can be proved by induction. •Total cost = sum of costs at each level = (lg n + 1)cn = cnlgn + cn = Θ(n lgn).
  • 20. Comp 122- 21 Other Examples  Use the recursion-tree method to determine a guess for the recurrences  T(n) = 3T(n/4) + Θ(n2 ).  T(n) = T(n/3) + T(2n/3) + O(n).
  • 21. Comp 122- 22 Recursion Trees – Caution Note  Recursion trees only generate guesses.  Verify guesses using substitution method.  A small amount of “sloppiness” can be tolerated. Why?  If careful when drawing out a recursion tree and summing the costs, can be used as direct proof.
  • 22. Comp 122- 23 The Master Method  Based on the Master theorem.  “Cookbook” approach for solving recurrences of the form T(n) = aT(n/b) + f(n) • a ≥ 1, b > 1 are constants. • f(n) is asymptotically positive. • n/b may not be an integer, but we ignore floors and ceilings. Why?  Requires memorization of three cases.
  • 23. Comp 122- 24 The Master Theorem Theorem 4.1 Let a ≥ 1 and b > 1 be constants, let f(n) be a function, and Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b. T(n) can be bounded asymptotically in three cases: 1. If f(n) = O(nlogba–ε ) for some constant ε > 0, then T(n) = Θ(nlogba ). 2. If f(n) = Θ(nlogba ), then T(n) = Θ(nlogba lg n). 3. If f(n) = Ω(nlogba+ε ) for some constant ε > 0, and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b) ≤ c f(n), then T(n) = Θ(f(n)). Theorem 4.1 Let a ≥ 1 and b > 1 be constants, let f(n) be a function, and Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b. T(n) can be bounded asymptotically in three cases: 1. If f(n) = O(nlogba–ε ) for some constant ε > 0, then T(n) = Θ(nlogba ). 2. If f(n) = Θ(nlogba ), then T(n) = Θ(nlogba lg n). 3. If f(n) = Ω(nlogba+ε ) for some constant ε > 0, and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b) ≤ c f(n), then T(n) = Θ(f(n)). We’ll return to recurrences as we need them…

Editor's Notes

  1. Talk about how mathematical induction works.