Download presentation

Presentation is loading. Please wait.

Published bySerena Privott Modified over 2 years ago

1
CS16: Data Structures & Algorithms | Spring 2014 Midterm Review 3/16/15 2015

2
Structures/Algorithms Analysis list, array, queue, stack, priority queue, heap, hash table, binary search tree, tree binary search (BS), rand select (median), seamcarve quicksort (QS), mergesort (MS), heapsort, insertion sort, selection sort, radix sort Op “counting” Loops, recursion Recurrence relations Inductive proofs O(), Ω(), Θ() Worst case, average case, expected case Master theorem

3
Selection Pseudocode select(list, k): pivot = list[rand(0, list.size)] L = [] E = [] G = [] for x in list: if x < pivot: L.append(x) if x == pivot: E.append(x) if x > pivot: G.append(x) if k < L.size: return select(L, k) else if k < (L.size + E.size) return pivot else return select(G, k – (L.size + E.size)) Tuesday, March 3, 2015 3

4
Structures/Algorithms Analysis list, array, queue, stack, priority queue, heap, hash table, binary search tree, tree binary search (BS), rand select (median), seamcarve quicksort (QS), mergesort (MS), heapsort, insertion sort, selection sort, radix sort Op “counting” Loops, recursion Recurrence relations Inductive proofs O(), Θ(), Ω() Worst case, average case, expected case Master theorem

5
mergesort(list) 1 def mergesort(list): 2 """mergesort: list -> list 3 Consumes: a list of numbers, list 4 Produces: a sorted list of numbers 5 """ 6 if (len(list) == 1): 7 return list 8 m = len(list)/2 9 L1 = mergesort(list[:m]) 10 L2 = mergesort(list[m:]) 11 return merge(L1, L2)

6
mergesort(list) 1 def mergesort(list): 2 """mergesort: list -> list 3 Consumes: a list of numbers, list 4 Produces: a sorted list of numbers 5 """ 6 if (len(list) == 1): 7 return list 8 m = len(list)/2c3 9 L1 = mergesort(list[:m])T(n/2) 10 L2 = mergesort(list[m:])T(n/2) 11 return merge(L1, L2)c5 n + c6

7
T(1) = c3 (base case) T(n) = c4 + T(n/2) + T(n/2) + c5*n + c6 T(n) = 2 T(n/2) + c5 n + c1 (c1 = c6 + c4)

8
merge(L1, L2) 1 def merge(L1, L2): 2 """merge: list, list -> list 3 Consumes: two sorted lists of numbers, L1 and L2 4 Produces: a single merged sorted list of numbers 5 """ 6 i1 = 0 # index into L1 7 i2 = 0 # index into L2 8 out = zeros(len(L1)+len(L2)) 9 while (i1 < len(L1) or i2 < len(L2)) : 10 if (i2 == len(L2) or (i1 < len(L1) and L1[i1] < L2[i2])): 11 out[i1+i2] = L1[i1] 12 i1 = i1 + 1 13 else: 14 out[i1+i2] = L2[i2] 15 i2 = i2 + 1 16 return out

9
First 3 lines: c6 While: n*c5 n*c5 + c6 for merge

10
Have recurrence relation, now plug-and-chug T(1) = c3 T(n) = 2 T(n/2) + n*c5 + c1 T(1) = c3 T(2) = 2*T(1) + 2 c5 + c1 = 2 c3 + 2c5 + c1 T(4) = 2*(2*c3 + 2c5 + c1) + 4c5 + c1 = 4c3 + 4c5 + 2c1 + 4c5 + c1 = 4 c3 + 8c5 + 3c1 T(8) = 2*(4 c3 + 8c5 + 3c1) + 8c5 + c1 = 8c3 + 16c5 + 6c1 + 8c5 + c1 = 8 c3 + 24c5 + 7c1 T(n) = n c3 + ? c5 + (n-1) c1 ? = (0,2,8,24) n log n = (0, 2, 8, 24) T(n) = n c3 + (n log n) c5 + (n-1) c1

11
Prove T(n) = c5 n log n + c3 n + c1 (n-1) T(1) = c3 // def’n base case T(k) = c5 k log k + c3 k + c1 (k-1) // inductive assump. T(2k) = 2T(k) + c5 2k + c1 // def’n rec. rel. = 2 (c5 k log k + c3 k + c1(k-1)) + c5 2k + c1 = 2 c5 k log k + 2 c3 k + 2 c1(k-1) + c5 2k + c1 = c5 2k (log k + 1) + 2k c3 + c1 (2k-2+1) = c5 2k (log k + log 2) + 2k c3 + c1(2k-1) = c5 2k log 2k + c3 2k + c1(2k-1) Shown for n=1; shown if true for n=k, also true for n= 2k Therefore, true for all n positive powers of 2

12
Master Theorem Let T(n) be a monotonically increasing function that satisfies: T(n) = aT(n/b) + Θ (n d ) Where a≥1, b>1, d≥0. T(n) is Θ (n d ) if a < b d T(n) is Θ (n d log n)if a = b d T(n) is Θ (n log a )if a > b d MS: T(n) = 2 T(n/2) + c5 n + c1 a b d=1 b a is number of sub-problems 1/b is size of sub-problems n d is cost within a given level

14
Amortized Analysis

15
Structures/Algorithms Analysis list, array, queue, stack, priority queue, heap, hash table, binary search tree, tree, graph, directed graph, directed acyclic graph binary search (BS), rand select (median), topological sort, convex hull, seamcarve quicksort (QS), mergesort (MS), heapsort, insertion sort, selection sort, radix sort Op counting Loops, recursion Recurrence relations Inductive proofs O(), Θ(), Ω() Worst case, average case, expected case Master theorem

16
Recurrence Relations? T(n) = 3n 3 + n log n T(n) = 2 n! T(n) = n + 2 T(n-1) T(n) = 3n 2 + n + T(n/3)

17
Structures/Algorithms Analysis list, array, queue, stack, priority queue, heap, hash table, binary search tree, tree, graph, directed graph, directed acyclic graph binary search (BS), rand select (median), topological sort, convex hull, seamcarve quicksort (QS), mergesort (MS), heapsort, insertion sort, selection sort, radix sort Op counting Loops, recursion Recurrence relations Inductive proofs O(), Θ(), Ω() Worst case, average case, expected case Master theorem

18
Master Theorem Let T(n) be a monotonically increasing function that satisfies: T(n) = aT(n/b) + Θ (n d ) Where a≥1, b>1, d≥0. T(n) is Θ (n d ) if a < b d T(n) is Θ (n d log n)if a = b d T(n) is Θ (n log a )if a > b d What is O() of T(n) = n + T(n-2), T(2)=2 (n even) b a is number of sub-problems 1/b is size of sub-problems n d is cost within a given level

19
Structures/Algorithms Analysis list, array, queue, stack, priority queue, heap, hash table, binary search tree, tree, graph, directed graph, directed acyclic graph binary search (BS), rand select (median), topological sort, convex hull, seamcarve quicksort (QS), mergesort (MS), heapsort, insertion sort, selection sort, radix sort Op counting Loops, recursion Recurrence relations Inductive proofs O(), Θ(), Ω() Worst case, average case, expected case Master theorem

20
Binary Tree A set of nodes that are connected with directed connections from parents to children so that each node has one parent, each parent has 0, 1, or 2 children, there are no cycles, and all nodes are directly or indirectly parented by a single root node. Data structure that has 0, 1, or 2 children that are binary trees.

21
Ok in pseudocode? merge(L1,L2): merge L1 and L2 return result insertionSort(L) insert elements into L2 return L2 qsort(L): pick random pivot. selectionSort(L): while (len(L) > 0): find and nuke min in L append min to L2 return L2

22
Structures/Algorithms Analysis list, array, queue, stack, priority queue, heap, hash table, binary search tree, tree, graph, directed graph, directed acyclic graph binary search (BS), rand select (median), topological sort, convex hull, seamcarve quicksort (QS), mergesort (MS), heapsort, insertion sort, selection sort, radix sort Op counting Loops, recursion Recurrence relations Inductive proofs O(), Ω(), Θ() Worst case, average case, expected case Master theorem

24
Quicksort(list) Pick random pivot, v Construct 3 lists, SL, SV, SR Quicksort(SL) Quicksort(SR) Return(SL,SV,SR)append now-sorted lists

25
quicksort(list) 1 def quicksort(list): 2 """quicksort: list -> list 3 Consumes: list of numbers 4 Produces: sorted list 5 """ 6 if(len(list)==1 or len(list)==0): 7 return list 8 pivot = random.choice(list) 9 small = [] 10 npivot = 0 11 big = [] 1 for e in list: 2 if (e < pivot): 3 small.append(e) 4 else: 5 if (e > pivot): 6 big.append(e) 7 else: 8 npivot = npivot + 1 9 small = quicksort(small) 10 big = quicksort(big) 11 while (npivot): 12 small.append(pivot) 13 npivot = npivot - 1 14 return small + big

26 dqsort(list) 1 def dqsort(list): 2 # dqsort: list -> list 3 # Consumes: a list of numbers, list 4 # Produces: a sorted list of numbers 5 if (len(list) == 1): 6 return list 7 m = len(list)/2 8 p = randSelect(m, list) 9 for e in list: 10 if (e

{ "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3265403/slides/slide_26.jpg", "name": "dqsort(list) 1 def dqsort(list): 2 # dqsort: list -> list 3 # Consumes: a list of numbers, list 4 # Produces: a sorted list of numbers 5 if (len(list) == 1): 6 return list 7 m = len(list)/2 8 p = randSelect(m, list) 9 for e in list: 10 if (e

list 3 # Consumes: a list of numbers, list 4 # Produces: a sorted list of numbers 5 if (len(list) == 1): 6 return list 7 m = len(list)/2 8 p = randSelect(m, list) 9 for e in list: 10 if (e

28
HullSort(L) Reduction! Create list of points, M = {(L i, (L i ) 2 )} H = ConvexHull(M) Return list of first coordinates of H i (start at smallest one)

30
Induction Prove 2n > n for all positive integers Base case n=1, 2 > 1 Inductive step: if true for n=k, true for n=k+1 assume 2k > k Add 2 to the left side and 1 to the right side, preserving the inequality 2k + 2 > k + 1 2(k+1) > (k+1) True for all n

Similar presentations

OK

Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)

Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google