Title: Divide
1Divide Conquer
- Themes
- Reasoning about code (correctness and cost)
- iterative code, loop invariants, and sums
- recursion, induction, and recurrence relations
- Divide and Conquer
- Examples
- sorting (insertion sort merge sort)
- computing powers
- Euclidean algorithm (computing gcds)
2Identities for Sums
3Some Common Sums
4Arithmetic Series
- Sum of consecutive integers (written slightly
differently)
See http//www.cs.drexel.edu/kschmidt/CS520/Lectu
res/2/arithmetic.swf
5Arithmetic Series (Proof)
6Geometric Series
See http//www.cs.drexel.edu/kschmidt/CS520/Lectu
res/2/geometric.swf
7Geometric Series (Proof)
8Floor and Ceiling
- Let x be a real number
- The floor of x, ?x?, is the largest integer less
than or equal to x - If an integer k satisfies k ? x lt k1, k ?x?
- E.G. ?3.7? 3, ?3? 3
- The ceiling of x, ?x?, is the smallest integer
greater than or equal to x - If an integer k satisfies k-1 lt x ? k, k ?x?
- E.G. ?3.7? 4, ?3? 3
9logarithm
- y logb(x) ? by x
- Two important cases
- ln(x) loge(x)
- lg(x) log2(x) frequently occurs in CS
- Properties
- log(cd) log(c) log(d)
- log(cx) xlog(c)
- logb(bx) x blogb(x)
- d ln(x)/dx 1/x
10logarithm
- 2k ?? x lt 2k1 ? k ?lg(x)?
- E.G. 16 ? 25 lt 32 ? 4 ? lg(25) lt 5
- lg(25) ? 4.64
- Change of base
- logc(x) logb(x) / logb(c)
- Proof. y logc(x) ? cy x ? ylogb(c) logb(x)
? y logb(x) / logb(c)
11Insertion Sort
- To sort x0,,xn-1,
- recursively sort x0,,xn-2
- insert xn-1 into x0,,xn-2
- (see code for details)
- Loop invariant (just before test, iltn)
- x0,, xi-1 sorted
- initialize t xi
12Insertion Sort (Example)
- (7,6,5,4,3,2,1,0)
- after recursive call (1,2,3,4,5,6,7,0)
- Insert 0 into sorted subarray
- Let 0 fall as far as it can
- Number of comparisons to sort inputs that are in
reverse sorted order (worst case) - C(n) C(n-1) (n-1)
- C(1) 0
See (http//www.cs.drexel.edu/kschmidt/CS520/Prog
rams/sorts.cc)
13Merge Sort
- To sort x0,,xn-1,
- recursively sort x0,,xa-1 and xa,,xn-1, where a
n/2 - merge two sorted lists
- Insertion sort is a special case where a1
- loop invariant for merge similar to insert
(depends on implementation)
14Merge Sort (Example)
- (7,6,5,4,3,2,1,0)
- after recursive calls (4,5,6,7) and (0,1,2,3)
- Number of comparisons needed to sort, worst case
(the merged lists are perfectly interleaved) - M(n) 2M(n/2) (2n-2)
- M(1) 0
- What is the best case (all in one list gt other
list)? - M(n) 2M(n/2) n/2
15Comparison of Insertion and Merge Sort
- Count the number of comparisons for different
n2k (see and run sort.cpp) - M(n)/C(n) ?0 as n increases
- C(n) is of higher order. I.e., C(n) bounds M(n)
from above, but not tightly - C(2n)/C(n) ? 4
- So, apparently quadratic.
- C(n) T(n2)
- M(2n)/M(n) ? 2 as n increases
16Solve Recurrence for C(n)
See (http//www.cs.drexel.edu/kschmidt/CS520/Lect
ures/2/insertionSortSwaps.swf)
17Solve Recurrence for M(n)
See http//www.cs.drexel.edu/kschmidt/CS520/Lectu
res/2/mergeSortSwaps.swf
18Computing Powers
- Recursive definition, multiplying as we learned
in 4th grade (whatever) - an a ? an-1, n gt 0
- a0 1
- Number of multiplications
- M(n) M(n-1) 1, M(0) 0
- M(n) n
19Binary Powering (Recursive)
- Binary powering
- (see http//www.cs.drexel.edu/kschmidt/CS520/Prog
rams/power.cpp) - x16 (((x2)2)2)2, 16 (10000)2
- x23 (((x2)2x)2x)2x, 23 (10111)2
- Recursive (right-left)
- xn (x?n/2?)2 ? x(n 2)
- M(n) M(?n/2?) n 2
20Binary Powering (Iterative)
(See http//www.cs.drexel.edu/kschmidt/CS520/Prog
rams/power.cpp)
- Example
- N y z
- 1 x
- 11 x x2
- 5 x3 x4
- 2 x7 x8
- 1 x7 x16
- 0 x23 x32
- Loop invariant
- xn y ? zN
- N n y 1 z x
- while (N ! 0)
- if (N 2 1)
- y zy
- z zz N N/2
21Binary Powering
- Number of multiplications
- Let ?(n) number of 1bits in binary
representation of n - num bits ?lg(n)? 1
- M(n) ?lg(n)? ?(n)
- ?(n) lg(n) gt
- M(n) 2lg(n)
22Greatest Common Divisors
- g gcd(a,b) ? ga and gb
- if ea and eb ? eg
- gcd(a,0) a
- gcd(a,b) gcd(b,ab)
- since if ga and gb then gab and if gb and
gab then ga
23Euclidean Algorithm (Iterative)
- (see http//www.cs.drexel.edu/kschmidt/CS520/Prog
rams/gcd.cpp) - a0 a, a1 b
- ai qi ai1 ai2 , 0 ? ai2 lt ai1
-
- an qn an1
- g an1
24Number of Divisions
- ai qi ai1 ai2 , 0 ? ai2 lt ai1 gt
- ai ? qi ai2 ai2 (qi 1)ai2 ? 2ai2
- gt an ? 2an2 gt an / an2 ? 2
- a1 / a3 ? a2 / a4 ? ? an-1 / an1 ? an / an2 ?
2n - n ? lg(a1a2 /g2)
- if a,b ? N, n ? 2lg(N)
25(blank, for notes)