Title: Five basic algorithm design methods:
1Chapter 10Algorithm Design Techniques
- Five basic algorithm design methods
- Greedy Method
- Divide and Conquer
- Dynamic Programming
- Backtracking
- Branch and Bound
-
-
2Chapter 10Divide and Conquer
- Three Step Approach
- 1. Divide problem into two or more smaller
instances - 2. Solve each of these smaller instances
- 3. Combine the solutions to obtain the solution
for the larger problem - Example Detecting a counterfeit coin
- Bag with 16 coins
- One of them may be counterfeit
- Counterfeit coins are lighter
- Determine whether bag contains a counterfeit coin
3Chapter 10Divide and Conquer
- Three Step Approach
- 1. Divide problem into two or more smaller
instances - 2. Solve each of these smaller instances
- 3. Combine the solutions to obtain the solution
for the larger problem - Example Gold Nuggets
- Given 8 nuggets, find the heaviest
- Find the lightest
4Chapter 10Divide and Conquer
- Matrix Multiplication
- 1 2 5 5 6 1 15 22 59 16 21
52 - 4 1 3 X 2 1 5 45 12 39 46
11 32 - 3 5 2 9 2 4 35 52 29 36 51
22 - C(i, j)
-
5Chapter 10Divide and Conquer
6Chapter 10Divide and Conquer
-
-
- C1 A1 B1 A2 B2
- C2 A1 B2 A2 B4
- C3 A3 B1 A4 B3
- C4 A3 B2 A4 B4
- Eight multiplications, four additions
7Chapter 10Divide and Conquer
templateltclass Tgt void InsertionSort(T a, int
n) for (int i 1 i lt n i) //insert
ai into a0i-1 T t ai int j for
(j i - 1 j gt 0 t lt aj j--) aj1
aj aj 1 t
8Chapter 10Divide and Conquer
- Partition elements into 2 or more subcollections
- Sort each
- Combine the solution
- Example 4 2 7 10 9 1 8 5 3
- How to partition? How to sort?
- One possibility
- First n-1 elements in one subcollection,
- Last element in the second
- A B
- 2 7 10 9 1 8 5 3
- But this is just a recursive version of
InsertionSort().O(n) calls to Sort(), O(n) merges
9Chapter 10Divide and Conquer
- Divide and conquer approach divide the list
into two equal groups. This avoids the O(n2)
complexity - 4 6 3 8 2 5 7
- 10 4 6 3 8 2 5 7
- 10 4 6 3 8 2 5 7
- 10 4 6 3 8
2 5 7 - Then merge the groups.
10Chapter 10Divide and Conquer
templateltclass Tgt void MergeSort(T a, int
n) T b new T n int s 1 while
(sltn) MergePass(a, b, s, n) ss Merge
Pass(b,a,s,n) ss
11Chapter 10Divide and Conquer
templateltclass Tgt void MergePass(T x, T y,
int s, int n) int i 0 while (i lt n - 2s)
Merge(x, y, i, i s - 1, i 2s - 1) i
i 2s if (i s lt n) Merge(x, y, i,
is-1, n-1) else for (int j i j lt n 1
j) yj xj
12Chapter 10Divide and Conquer
templateltclass Tgt void Merge(T c, T d, int l,
int m, int r) int i l, j m1, k
l while ((i lt m) (j lt r)) if (ci lt
cj) dk ci else dk
cj if (i gt m) for (int q j q ltr
q) dk cq else for (int q i q lt
m q) dk cq
13Chapter 10Divide and Conquer
- n elements to be sorted
- Partition into 3 groups
- left middle right
- Exactly one element in the middle The pivot,
or index, or partitioning element. - Select an element from a0n-1 for middle. This
is the pivot - Partition the remaining elements into segments
left and right. Partition s.t. no element in the
left is gt pivot and no element in the right is lt
pivot. - Sort left using QuickSort recursively
- Sort right using QuickSort recursively
- The answer is left, middle, right
14Chapter 10Divide and Conquer
templateltclass Tgt void QuickSort(T a, int n)
quickSort(a, 0, n-1) templateltclass Tgt void
quickSort(T a, int l, int r) if (l gt r)
return int i l, j r1 T pivot
ai while (true) do i i 1 while
(ai lt pivot) do j j - 1 while (aj gt
pivot) if (i gt j) break Swap(ai,
aj) al aj aj pivot quickSort
(a, l, j-1) quickSort(a, j1, r)
15Chapter 10Divide and Conquer
- n elements to be sorted
- Partition into 3 groups
- left middle right
- Exactly one element in the middle The pivot,
or index, or partitioning element. - Select an element from a0n-1 for middle. This
is the pivot - Partition the remaining elements into segments
left and right. Partition s.t. no element in the
left is gt pivot and no element in the right is lt
pivot. - Sort left using QuickSort recursively
- Sort right using QuickSort recursively
- The answer is left, middle, right