Download presentation
Presentation is loading. Please wait.
Published byNorman Williams Modified over 9 years ago
1
Divide and Conquer
2
Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive
3
Today Topic Divide and Conquer
4
DIVIDE AND CONQUER Algorithm Design Technique
5
Divide and Conquer
6
Induction
7
Induction Example
8
The Inductive Step The inductive step – Assume that it is true for (n-1) – Check the case (n) 1 + 2 + 3 +… + n = (1 + 2 + 3 + … + (n-1) ) + n = (n-1)(n) / 2 + n = n((n-1)/2 + 1) = n(n/2-1/2 + 1) = n(n/2 + 1/2) = n(n+1)/2
9
The Inductive Step The inductive step – Assume that it is true for (n-1) – Check the case (n) 1 + 2 + 3 +… + n = (1 + 2 + 3 + … + (n-1) ) + n = (n-1)(n) / 2 + n = n((n-1)/2 + 1) = n(n/2-1/2 + 1) = n(n/2 + 1/2) = n(n+1)/2 By using (n-1)
10
The Induction By using the result of the smaller case We can proof the larger case
11
Key of Divide and Conquer
12
Steps in D&C
13
Code Example ResultType DandC(Problem p) { if (p is trivial) { solve p directly return the result } else { divide p into p 1,p 2,...,p n for (i = 1 to n) r i = DandC(p i ) combine r 1,r 2,...,r n into r return r }
14
Code Example ResultType DandC(Problem p) { if (p is trivial) { solve p directly t s return the result } else { divide p into p 1,p 2,...,p n t d for (i = 1 to n) t r r i = DandC(p i ) combine r 1,r 2,...,r n into r t c return r }
15
Examples Let’s see some examples
16
MERGE SORT
17
The Sorting Problem Given a sequence of numbers – A = [a 1,a 2,a 3,…,a n ] Output – The sequence A that is sorted from min to max
18
The Question What if we know the solution of the smaller problem? – What is the smaller problem? Try the same problem sorting What if we know the result of the sorting of some elements?
19
The Question How to divide? – Let’s try sorting of the smaller array Divide exactly at the half of the array How to conquer? – Merge the result directly
20
Idea Simplest dividing – Divide array into two array of half size Laborious conquer – Merge the result
21
Divide
24
Merge
26
Analysis T(n) = 2T(n/2) + O(n) Master method – T(n) = O(n lg n)
27
QUICK SORT
28
The Sorting Problem (again) Given a sequence of numbers – A = [a 1,a 2,a 3,…,a n ] Output – The sequence A that is sorted from min to max
29
Problem of Merge Sort Need the use of external memory for merging Are there any other way such that conquering is not that complex?
30
The Question How to divide? – Try doing the same thing as the merge sort – Add that every element in the first half is less than the second half Can we do that? How to conquer? – The sorted result should be easier to merge?
31
Idea Laborious dividing – Divide array into two arrays Add the requirement of value of the array Simplest conquer – Simply connect the result
32
Is dividing scheme possible? Can we manage to have two subproblems of equal size – That satisfy our need? Any trade off?
33
The Median
34
Simplified Division
35
partitioning First k element Other n- k element
36
Solve by Recursion sorting
37
Do nothing!
38
Analysis T(n) = T(k) + T(n – k) + Θ(n) There can be several cases – Up to which K that we chosen
39
Analysis : Worst Case K always is 1 st What should be our T(N) ? What is the time complexity
40
Analysis : Worst Case K always is 1 st T(n) = T(1) + T(n – 1) + Θ(n) = T(n – 1) + Θ(n) = Σ Θ(i) = Θ(n 2 ) Not good
41
Analysis : Best Case K always is the median What should be our T(N) ? What is the time complexity
42
Analysis : Best Case K always is the median T(n) = 2T(n/2) + Θ(n) = Θ(n log n) The same as the merge sort (without the need of external memory)
43
Fixing the worst case When will the worst case happen? – When we always selects the smallest element as a pivot – Depends on pivot selection strategy If we select the first element as a pivot – What if the data is sorted?
44
Fixing the worst case Select wrong pivot leads to worst case. There will exist some input such that for any strategy of “deterministic” pivot selection leads to worst case.
45
Fixing the worst case Use “non-deterministic” pivot selection – i.e., randomized selection Pick a random element as a pivot It is unlikely that every selection leads to worst case We can hope that, on average, – it is O(n log n)
46
MODULO EXPONENTIAL
47
The Problem Given x, n and k Output: the value of x n mod k
48
Naïve method res = x mod k; for (i = 2 to n) do { res = res * x; res = res mod k; } Θ(n) Using basic facts: (a * b) mod k = ((a mod k) * (b mod k)) mod k
49
The Question What if we knows the solution to the smaller problem? – Smaller problem smaller N – x n ?? from x (n-e) ? Let’s try x (n/2)
50
The Question How to divide? – x n = x n/2 * x n/2 (n is even) – x n = x n/2 * x n/2 * x (n is odd) How to conquer? – Compute x n/2 mod k Square and times with x, if needed, mod k afterward
51
Analysis T(n) = T(n/2) + Θ(1) = O(log n)
52
Example 2 92 mod 10 2 92 = 2 46 × 2 46 2 46 = 2 23 × 2 23 2 23 = 2 11 × 2 11 × 2 2 11 = 2 5 × 2 5 × 2 2 5 = 2 2 × 2 2 × 2 2 2 = 2 1 × 2 1 = 4×4 mod 10 = 6 = 8×8 mod 10 = 4 = 8×8×2 mod 10 = 8 = 2×2×2 mod 10 = 8 = 4×4×2 mod 10 = 2 = 2×2 mod 10 = 4
53
MAXIMUM CONTIGUOUS SUM OF SUBSEQUENCE (MCS)
54
The MCS Problem Given a sequence of numbers – A = [a 1,a 2,a 3,…,a n ] There are n members Find a subsequence s = [a i,a i+1,a i+2,…,a k ] – Such that the sum of the element in s is maximum
55
Example Sum = 11
56
Naïve approach Try all possible sequences – How many sequence? – How much time does it use to compute the sum?
57
Naïve approach
58
The DC Approach What if we know the solution of the smaller problem – What if we know MSS of the first half and the second half?
59
The Question How to divide? – By half of the member How to conquer? – Does the result of the subproblems can be used to compute the solution? – Let’s see
60
Combining the result from sub MSS
61
4-35-226-2
62
4-35-226-2 Combining the result from sub MSS Sum = 6Sum = 8 Shall this be our answer?
63
Combining the result from sub MSS Do we consider all possibilities? 4-35-2 26-2
64
Combining the result from sub MSS Considered all pairs in each half But not the pair including member from both half There are (n/2) 2 additional pairs 4-35-2 26-2
65
Compute max of cross over Consider this The max sequence from the green part is always the same, regardless of the pink part The sequence always start at the left border 4-35-2 26-2
66
Compute max of cross over Similarly The max sequence from the pink part is always the same, regardless of the green part The sequence always start at the right border 4-35-2 26-2
67
4-35-2 26-2 Compute max of cross over Just find the marginal max from each part Sum = -1
68
Compute max of cross over Just find the marginal max from each part 4-35-2 26-2 Sum = 1
69
Compute max of cross over Just find the marginal max from each part 4-35-2 26-2 Sum = 7Sum = 7
70
Compute max of cross over Just find the marginal max from each part Less than previous 4-35-2 26-2 Sum = 5Sum = 5
71
Compute max of cross over Just find the marginal max from each part 4-35-2 26-2 Sum = -2
72
Compute max of cross over Just find the marginal max from each part 4-35-2 26-2 Sum = 3Sum = 3
73
4-35-2 26-2 Compute max of cross over Just find the marginal max from each part Sum = 0Sum = 0
74
4-35-2 26-2 Compute max of cross over Just find the marginal max from each part Sum = 4Sum = 4 This is max
75
4-35-2 26-2 Compute max of cross over Just find the marginal max from each part Sum = 7Sum = 7Sum = 4Sum = 4 Total = 11 takes Θ(n/2)
76
Combine Max from the three parts – The left half – The right half – The cross over part Use the one that is maximum
77
Analysis T(n) = 2 T(n/2) + Θ(n) = Θ(n log n) a = 2, b = 2, c = log 2 2=1, f(n) = O(n) n c =n 2 = Θ(n) – f(n) = Θ(n c )this is case 1 of the master’s method Hence, T(n) = Θ(n log n) Left and right parts Cross over part
78
CLOSEST PAIR
79
The Problem Given – N points in 2D (x 1,y 1 ), (x 2,y 2 ), …,(x n,y n ) Output – A pair of points from the given set (x a,y a ), (x b,y b ) 1 <= a,b <= n Such that the distance between the points is minimal
80
Input Example
81
Output Example
82
The Naïve Approach
83
DC approach What if we know the solution of the smaller problem – What if we know the Closest Pair of half of the points? Which half?
84
Divide by X axis Find closest pair of the left side Find closest pair of the right side
85
Conquer Like the MSS problem – Solutions of the subproblems do not cover every possible pair of points – Missing the pairs that “span” over the boundary
86
Divide by X axis Find closest pair of the left side Find closest pair of the right side
87
Conquer
88
Find Closest Spanning Pair Should we consider this one? Why?
89
Find Closest Spanning Pair
90
Possible Spanning Pair Consider pairs only in this strip One point from the left side Another point from the right side Consider pairs only in this strip One point from the left side Another point from the right side Any point outside the strip, if paired, its distance will be more than b
91
Point in Strips
92
Point in Strips is O(N)
93
The Solution
94
Spanning Pair to be considered
95
Question is still remains How many pair to be checked? A point to check
96
Implementation Detail
97
Naïve approach
98
Better Approach
99
Analysis T(n) = 2 T(n/2) + 4O(n) + O(n) = Θ(n log n) a = 2, b = 2, c = log 2 2=1, f(n) = O(n) n c =n 2 = Θ(n) – f(n) = Θ(n c )this is case 1 of the master’s method Hence, T(n) = Θ(n log n) Left and right parts Point in strip Divide the list
100
MATRIX MULTIPLICATION
101
The Problem Given two square matrix – A n x n and B n x n – A R n x n and B R n x n Produce – C = AB
102
Multiplying Matrix c i,j = Σ(a i,k *b k,j )
103
Naïve Method for (i = 1; i <= n;i++) { for (j = 1; j <= n;j++) { sum = 0; for (k = 1;k <= n;k++) { sum += a[i][k] * b[k][j]; } c[i][j] = sum; } O(N 3 )
104
Simple Divide and Conquer Divide Matrix into Block Multiply the block
105
Simple Divide and Conquer What is the complexity? T(n) = 8T(n/2) + O(n 2 ) Master method gives O(n 3 ) – Still the same
106
Strassen’s Algorithm We define Note that each M can be computed by one single multiplication of n/2 * n/2 matrices The number of addition is 10(n/2) 2
107
Strassen’s Algorithm Compute the result 8 more additions of n/2 * n/2 matrices Hence, total addition is 18 (n/2) 2
108
Analysis T(n) = 7T(n/2) + O(n 2 ) – Using Master’s method – a = 7, b = 2, f(n) = O(n 2 ) c = log 2 7 ≈ 2.807 – Hence, f(n) = O(n 2.807 ) this is case 1 of the master method So, T(n) = O(n 2.807 )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.