cs16: data structures & algorithms | spring 2014 midterm review 3/16/15 2015
TRANSCRIPT
CS16: Data Structures & Algorithms | Spring 2014
Midterm Review
3/16/15 2015
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
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
mergesort(list)
1 def mergesort(list):2 """mergesort: list -> list3 Consumes: a list of numbers, list4 Produces: a sorted list of numbers5 """6 if (len(list) == 1):7 return list8 m = len(list)/29 L1 = mergesort(list[:m])10 L2 = mergesort(list[m:])11 return merge(L1, L2)
mergesort(list)
1 def mergesort(list):2 """mergesort: list -> list3 Consumes: a list of numbers, list4 Produces: a sorted list of numbers5 """6 if (len(list) == 1):7 return list8 m = len(list)/2 c39 L1 = mergesort(list[:m]) T(n/2)10 L2 = mergesort(list[m:]) T(n/2)11 return merge(L1, L2) c5 n + c6
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)
Have recurrence relation, now plug-and-chugT(1) = c3T(n) = 2 T(n/2) + n*c5 + c1
T(1) = c3T(2) = 2*T(1) + 2 c5 + c1 = 2 c3 + 2c5 + c1T(4) = 2*(2*c3 + 2c5 + c1) + 4c5 + c1 = 4c3 + 4c5 + 2c1 + 4c5 + c1 = 4 c3 + 8c5 + 3c1T(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
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= 2kTherefore, true for all n positive powers of 2
Master Theorem
Let T(n) be a monotonically increasing function that satisfies:
T(n) = aT(n/b) + Θ(nd)Where a≥1, b>1, d≥0.
T(n) is Θ(nd) if a < bd
T(n) is Θ(ndlog n) if a = bd
T(n) is Θ(nlog a) if a > bd
MS: T(n) = 2 T(n/2) + c5 n + c1 a b d=1
b
a is number of sub-problems1/b is size of sub-problemsnd is cost within a given level
Amortized Analysis
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
Recurrence Relations?
T(n) =3n3 + n log n
T(n) =2n!
T(n) =n + 2 T(n-1)
T(n) =3n2 + n +
T(n/3)
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
Master Theorem
Let T(n) be a monotonically increasing function that satisfies:
T(n) = aT(n/b) + Θ(nd)Where a≥1, b>1, d≥0.
T(n) is Θ(nd) if a < bd
T(n) is Θ(ndlog n) if a = bd
T(n) is Θ(nlog a) if a > bd
What is O() of T(n) = n + T(n-2), T(2)=2 (n even)
b
a is number of sub-problems1/b is size of sub-problemsnd is cost within a given level
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
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.
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
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
Quicksort(list)
Pick random pivot, v Construct 3 lists, SL, SV, SR
Quicksort(SL) Quicksort(SR) Return(SL,SV,SR) append now-sorted
lists
quicksort(list)
1 def quicksort(list):2 """quicksort: list -> list3 Consumes: list of numbers4 Produces: sorted list5 """6 if(len(list)==1 or
len(list)==0):7 return list8 pivot = random.choice(list)
9 small = []10 npivot = 011 big = []
12 for e in list:13 if (e < pivot):14 small.append(e)15 else:16 if (e > pivot):17 big.append(e)18 else:19 npivot = npivot + 120 small = quicksort(small)21 big = quicksort(big)22 while (npivot):23 small.append(pivot)24 npivot = npivot - 125 return small + big
dqsort(list)
1 def dqsort(list):2 # dqsort: list -> list3 # Consumes: a list of numbers, list4 # Produces: a sorted list of numbers5 if (len(list) == 1):6 return list7 m = len(list)/28 p = randSelect(m, list)9 for e in list:10 if (e<p) add e to L1 11 else add e to L212 L1 = dqsort(L1) 13 L2 = dqsort(L2)14 return (L1 + L2) # concat
HullSort(L) Reduction! Create list of points, M = {(Li, (Li)2)} H = ConvexHull(M) Return list of first coordinates of Hi
(start at smallest one)
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