# divide & conquer

Post on 01-Jan-2016

34 views

Category:

## Documents

Embed Size (px)

DESCRIPTION

Divide & Conquer. Themes Reasoning about code (correctness and cost) recursion, induction, and recurrence relations Divide and Conquer Examples sorting (insertion sort & merge sort) computing powers. Sorting. - PowerPoint PPT Presentation

TRANSCRIPT

• Divide & ConquerThemesReasoning about code (correctness and cost)recursion, induction, and recurrence relationsDivide and ConquerExamplessorting (insertion sort & merge sort)computing powers

• SortingGiven a list L = (L1 Ln) of elements from a totally ordered set (e.g. integers with ) return a list M = (M1 Mn) such that(sorted M), i.e. M1 M2 MnM = (permutation L), i.e. Mi = L(i) where is a bijection from {1,,n} to {1,,n}Use divide and conquer to derive several recursive algorithms and the principle of balance to obtain an efficient algorithm

• sortedpPredicate to test to see if a list is sortedShould check to see that L is a list of integers(x1 x2 xn) with x1 x2 xnxi xj for 1 i < j n

(defun sortedp (L) (cond ((equal L nil) t) ((equal (length L) 1) t) ((and (

• Correctness of sortedpL = (x1 x2 xn)By induction on len(L)Base cases L nil or of length 1Show xi xj for 1 i < j nx1 x2 and by induction xi xj for 2 i < j nx1 x2 xj x1 xj for 2 j n

• Precise Proof is DifficultWe take advantage of informal reasoning and names which provide intuitive meaningL = (x1 x2 xn) Prove (fn123 L) = t if xi xj for 1 i < j n and nil otherwise using equational properties of fn13, fn14, fn15, fn17, fn81, fn100 and fn150(defun fn123 (L) (fn13 ((fn81 L nil) t) ((fn81 (fn150 L) 1) t) ((fn82 (fn100 (fn14 L) (fn17 L)) (fn123 (fn15 L))))))

• Insertion SortTo sort x1,,xn, recursively sort x2,,xninsert x1 into x2,,xn(see code for details)Prove correctness by induction

• Insertion Sort(defun insertionsort (L) (if (equal L nil) nil (insert (first L) (insertionsort (rest L)))))

(defun insert (x L) (cond ((equal L nil) (list x)) ((

• Insertion Sort (Example)(insertionsort (7,6,5,4,3,2,1,0))recursive call returns (0,1,2,3,4,5,6)(insert 7 (0,1,2,3,4,5,6))(0,1,2,3,4,5,6,7)Number of comparisons to sort inputs that are in reverse sorted order (worst case) C(n) = C(n-1) + (n-1) C(1) = 0

• Correctness of insertionsort L = (L1 Ln)(insertionsort L) terminates and returns a list of length n(insertionsort L) is sorted(insertionsort L) is a permutation of LProof by induction on nFor n 0, recursive call with smaller nRecursive call produces a sorted list of (rest L) by induction and (insert (first L) (insertionsort (rest L))) returns a sorted list (by induction)Recursive call returns a permutation of (rest L) and insert returns a list containing (first L) and elements of (rest L)

• Solve Recurrence for C(n)

• Merge SortTo sort x1,,xn, recursively sort x1,,xa and xa+1,,xn, where a = n/2merge two sorted lists

Insertion sort is a special case where a=1

• Mergesort(defun mergesort (L) (cond ((equal L nil) nil) ((equal (length L) 1) L) ((merge (mergesort (take (floor (length L) 2) L)) (mergesort (drop (floor (length L) 2) L))))))

• Merge(defun merge (L M) (cond ((equal L nil) M) ((equal M nil) L) ((
• Take and Drop(defun take (n L) (cond ((equal L nil) nil) ((equal n 0) nil) ((cons (first L) (take (- n 1) (rest L))))))

(defun drop (n L) (cond ((equal L nil) nil) ((equal n 0) L) ((drop (- n 1) (rest L)))))

• Merge 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) + n-1 M(1) = 0What is the best case (all in one list > other list)?M(n) = 2M(n/2) + n/2

• Comparison of Insertion and Merge SortCount the number of comparisons for different n=2k M(n)/C(n) 0 as n increasesC(n) grows faster than M(n)C(2n)/C(n) 4So, apparently quadratic.C(n) = (n2)M(2n)/M(n) 2 as n increases

nM(n)C(n)M(n)/C(n)21114460.667812280.42916321200.26732804960.1616419220160.095128448181280.055

• Solve Recurrence for M(n)

• MergesortWhat is wrong with the following?

(defun mergesort (L) (if (equal L nil) nil (merge (mergesort (mytake (floor (length L) 2) L)) (mergesort (drop (floor (length L) 2) L)))))

• Correctness of mergesort L = (L1 Ln)(mergesort L) terminates and returns a list of length n(mergesort L) is sorted(mergesort L) is a permutation of LProof by induction on nFor n 2, recursive calls with smaller nRecursive calls produce sorted lists by induction and merge produces a sorted listRecursive calls contain lists whose concatenation is L and hence by induction return lists whose concatenation is L. merge returns a permutation of L

• Properties of nth L = (L1 Llen(L))0 < n len(L), then (nth n L) = LnProof by induction on nn > len(L), then (nth n L) = nilProof by induction on len(L)

(defun nth (n L) (if (equal L nil) nil (if (equal n 1) (first L) (nth (- n 1) (rest L)))))

• Properties of nth L = (L1 Llen(L))0 < n len(L), then (nth n L) = LnProof by induction on n

Base case. n = 1 return (first L)By induction (nth (- n 1) (rest L)) returns the (n-1)st element of (rest L) which is the nth element of L (n > 1)

• Properties of nth L = (L1 Llen(L))n > len(L), then (nth n L) = nilProof by induction on len(L)

Base case. Len(L) = 0. L = nil and return niln > len(L) n-1 > len((rest L)), therefore by induction (nth (- n 1) (rest L)) returns nil

• Properties of take and drop L = (L1 Llen(L))0 n len(L), then len((take n L)) = nProof by induction on nn > len(L), then (take n L) = LProof by induction on len(L)0 < k n len(L), then (nth k (take n L)) = LkProof by property of nth and induction on k0 n len(L), then len((drop n L)) = len(L) nn > len(L), then (drop n L) = nil0 n len(L), 0 < k len(L)-n, then (nth k (drop n L)) = Ln+k0 n len(L), (append (take n L) (drop n L)) = L

• Properties of merge L = (L1 Lm) and M = (M1 Mn)Assume L is sorted and M is sorted(merge L M) is a list of length m+n(merge L M) is sorted(merge L M) is a permutation of (L1 Lm M1 Mn)

• ExerciseUse check= to verify properties of take and drop for several examples

Use induction to prove the properties

• Solution Property 1 L = (L1 Llen(L))0 n len(L), then len((take n L)) = nProof by induction on n

Base case. n = 0 return nil and len(nil) = 0.Assume n > 0 and show that len((take n L)) = n. Assume that take returns a list of length k for k < n [Inductive hypothesis]In this case take returns (cons (first L) (take (- n 1) (rest L))) and since n-1 len((rest L)) (take (- n 1) (rest L)) by induction is a list of length n-1. Therefore (cons (first L) (take (- n 1) (rest L))) has length (n-1) + 1 = n.

• Solution Property 2 L = (L1 Llen(L))n > len(L), then (take n L) = LProof by induction on len(L)Base case. len(L) = 0 and take returns L = nil independent of n.Assume n > len(L) = and show that (take n L) = L. Assume that take returns M when len(M) < [Inductive hypothesis]In this case take returns (cons (first L) (take (- n 1) (rest L)). By induction, (take (- n 1) (rest L)) returns (rest L) and therefore (take n L) returns (cons (first L) (rest L)) = L.

• Solution Property 3 L = (L1 Llen(L))0 < k n len(L), then (nth k (take n L)) = LkProof by property of nth and induction on kBase case. k = 1and by the first property of nth, (nth 1 (take n L)) = the first element of (take n L) which by the definition of take is the first element of L.Assume the jth element of (take n L) = Lj for j < k [Inductive hypothesis] and show that the kth element of (take n L) = Lk.By the first property of nth (nth k (take n L)) is the kth element of (take n L) and by the definition of take, that is the (k-1)st element of (take (- n 1) (rest L)) which by induction is the (k-1)st element of (rest L) which is the kth element of L.

• Solution of Properties 4-7 L = (L1 Llen(L))Properties 4-6 for drop are analogous to properties 1-3 for take and their proofs are similar to those for properties 1-3.Property 7 follows from properties 3 and 6.

• Computing PowersRecursive definition:an = a an-1, n > 0a0 = 1

Number of multiplicationsM(n) = M(n-1) + 1, M(0) = 0M(n) = n

• ExerciseWrite a recursive ACL program to compute the power of a number using the recursive definition on the previous slideProve by induction on n that (power a n) = an

• Solution; inputs a rational number a and a natural number n; output a rational number equal to a^n

(defun power (a n) (if (= n 0) 1 (* a (power a (- n 1)))))

(check= (power 2 0) 1)(check= (power 2 10) 1024)

• Solution Continued(defun power (a n) (if (= n 0) 1 (* a (power a (- n 1)))))

Base case n = 0(power a 0) = 1 = a0Assume n > 0 and (power a (- n 1)) = an-1 [Inductive Hypothesis](power a n) = (* a (power a (- n 1)) = aan-1 [by induction] = an

• Binary Powering (Recursive)Binary poweringx16 = (((x2)2)2)2, 16 = (10000)2x23 = (((x2)2x)2x)2x, 23 = (10111)2

Recursive (right-left) xn = (xn/2)2 x(n % 2)

M(n) = M(n/2) + [n % 2] + 1

• ExerciseWrite a recursive ACL program to compute the power of a number using the binary powering algorithmProve by induction on n that (fastpower a n) = an

• Solution; inputs a rational number a and a natural number n; output a rational number equal to a^

Recommended