Lec04 Divide and Conquer (Part 1)
Lec04 Divide and Conquer (Part 1)
DIVIDE-AND-CONQUER (PART 1)
A Technique of Algorithm Design: Divide
and Conquer
Problems
subproblem Subproblems…
1 n
subproblems
2
Solution to Solution to
subproblem1 Solution to subproblem3
subproblem2
Solution to
problem
Divide and Conquer
18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43
18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9
18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
Merging Two Sorted Sequences
S.insertLast(A.remove(A.first()))
else
S.insertLast(B.remove(B.first()))
while A.isEmpty()
S.insertLast(A.remove(A.first()))
while B.isEmpty()
S.insertLast(B.remove(B.first()))
Merging Two Sorted Sequences
7 29 4 2 4 7 9
72 2 7 94 4 9
Partition
7 2 9 43 8 6 1
15
Mergesort Tree Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1
7 29 4
16
Mergesort Tree Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1
7 29 4
7|2
17
Mergesort Tree Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1
7 29 4
72
77
18
Mergesort Tree Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1
7 29 4
72
77 22
19
Mergesort Tree Example (cont.)
Merge
7 2 9 43 8 6 1
7 29 4
722 7
77 22
20
Mergesort Tree Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1
7 29 4
722 7 9|4
77 22
21
Mergesort Tree Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1
7 29 4
722 7 9|4
22
Mergesort Tree Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1
7 29 4
722 7 9|4
23
Mergesort Tree Example (cont.)
Merge
7 2 9 43 8 6 1
7 29 4
722 7 9|44 9
24
Mergesort Tree Example (cont.)
Merge
7 2 9 43 8 6 1
7 29 42 4 7 9
722 7 9|44 9
25
Mergesort Tree Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1
722 7 9|44 9
26
Mergesort Tree Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1
27
Mergesort Tree Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1
28
Mergesort Tree Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1
29
Mergesort Tree Example (cont.)
Merge
7 2 9 43 8 6 1
30
Mergesort Tree Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1
31
Mergesort Tree Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1
32
Mergesort Tree Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1
33
Mergesort Tree Example (cont.)
Merge
7 2 9 43 8 6 1
34
Mergesort Tree Example (cont.)
Merge
7 2 9 43 8 6 1
35
Mergesort Tree Example (cont.)
Merge
7 2 9 43 8 6 11 2 3 4 6 7 8 9
36
Array Output
Mergesort recurrence.
0 if n 1
T(n) T n /2 T n/2 n otherwise
merging
solve left half solve right half
Solution. T(n) = O(n log2 n).
This gives us
k=
Use the iterative method:
Thus, T(n) = n +
Since n< , so we have
O()
Analysis of Mergesort: Proof by
Recursion Tree
0 if n 1
T(n) 2T(n /2) n otherwise
sorting both halves merging
T(n) n
2(n/2)
T(n/2) T(n/2)
...
T(2 T(2 T(2 T(2) T(2 T(2 T(2 T(2 n/2 (2)
) ) ) ) ) ) )
Kleinberg & Tados (2014) n log2n
Analysis of Mergesort: Proof by Induction
Claim. If T(n) satisfies this recurrence, then T(n) = n log2
n. assumes n is a power of 2
0 if n 1
T(n) 2T(n /2) n otherwise
sorting both halves merging
Pf. (by induction on n)
Base case: n = 1.
Inductive
hypothesis: T(n) = n log2 n. T(1) = 1
Goal: show that T(2n) = 2n log2 (2n).
T(2n) 2T(n) 2n
2n log2 n 2n =
2nlog2 (2n) 1 2n Note that: =
2n log2 (2n)
Kleinberg & Tados (2014)
Analysis of Mergesort (summary)
2h = n (eg. 23 = 8)
h = log2 n (eg. 3 = log2 8)
7 2 9 43 8 6 11 2 3 4 6 7 8 9
Mergesort:
It has O(n log n) running time
This is better than quicksort which has O(n2) at
worst case
It is stable
It is not-in-place
QUICK-SORT
L E G
47
Quick-Sort
L E G
48
Algorithm QuickSort
1 quicksort( arr, p, r )
2 if p < r
3 pi = partition( arr, p, r )
4 quicksort( arr, p, pi – 1 )
5 quicksort( arr, pi + 1, r )
49
Partition
The partition step of quick-sort Algorithm partition(S, p, r)
takes O(n) time Input sequence S, start index p,
end index r
Output subsequences L, E, G
of the
Method to select a pivot: elements of S less
1. Always choose the than, equal to,
first element or greater than the
2. Always choose the pivot, resp.
last element i=p–1
3. Choose the median for all j from p to r – 1
4. Randomly choose an if S[j] <= S[r]
element i++
swap (S[i], S[j])
swap (S[i+1], S[r])
return i + 1
50
Execution Example 1
24316 7 7 9
51
Execution Example 2
7 2 9 4 3 7 6 1
52
Execution Example 2 (cont.)
7 2 9 4 3 7 6 1
2 4 3 1 7 9 7
53
Execution Example 2(cont.)
7 2 9 4 3 7 6 1
2 4 3 1 7 9 7
11 4 3
54
Execution Example 2(cont.)
7 2 9 4 3 7 6 1
2 4 3 1 7 9 7
11 4 3
55
Execution Example 2(cont.)
7 2 9 4 3 7 6 1
2 4 3 1 7 9 7
11 4 3
44
56
Execution Example 2(cont.)
Join, join
7 2 9 4 3 7 6 1
2 4 3 1 1 2 3 4 7 9 7
11 4 3 3 4
44
57
Execution Example 2(cont.)
7 2 9 4 3 7 6 1
2 4 3 1 1 2 3 4 7 9 7
11 4 3
44
58
Execution Example 2(cont.)
7 2 9 4 3 7 6 1
2 4 3 1 1 2 3 4 7 9 7
44
59
Execution Example 2(cont.)
Join, join
7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
2 4 3 1 1 2 3 4 7 9 7 7 7 9
44
60
Worst-case Running Time
The worst case for quick-sort occurs when the pivot is the unique
minimum or maximum element (already sorted)
One of L and G has size n - 1 and the other has size 0
The running time is proportional to the sum
n + (n - 1) + (n - 2) +… + 2 + 1
Thus, the worst-case running time of quick-sort is O(n2)
depth time
0 n
1 n-1
…
… …
n-1 1
61
Worst-case Running Time
63