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

Slides:



Advertisements
Similar presentations
INTRODUCTION TO CS16 CS16: Introduction to Algorithms and Data Structures Tu/Th 10:30-11:50 Metcalf Auditorium David Laidlaw Thursday, January 23, 2014.
Advertisements

Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
Chapter 7 Sorting Part I. 7.1 Motivation list: a collection of records. keys: the fields used to distinguish among the records. One way to search for.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
Review. What to know You are responsible for all material covered in lecture, the readings, or the programming assignments There will also be some questions.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
1 Advanced Data Structures. 2 Topics Data structures (old) stack, list, array, BST (new) Trees, heaps, union-find, hash tables, spatial, string Algorithm.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Analysis of Algorithms CS 477/677
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Data Structures, Spring 2004 © L. Joskowicz 1 DAST – Final Lecture Summary and overview What we have learned. Why it is important. What next.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
EXAM REVIEW CSC 172 SPRING 2004 LECTURE 26. Want to TA for next semester?
CSC 213 Lecture 12: Quick Sort & Radix/Bucket Sort.
Recitation 11 Analysis of Algorithms and inductive proofs 1.
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
Chapter 21 Binary Heap.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
CompSci 102 Discrete Math for Computer Science April 17, 2012 Prof. Rodger.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Sorting preparation for searching. Overview  levels of performance  categories of algorithms  Java class Arrays.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
بسم الله الرحمن الرحيم شرح جميع طرق الترتيب باللغة العربية
Lecture 2 Sorting.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Data Structures Using C++ 2E
Divide and Conquer.
CS 3343: Analysis of Algorithms
Description Given a linear collection of items x1, x2, x3,….,xn
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
CS 3343: Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
CS200: Algorithm Analysis
Sorting.
CSE 326: Data Structures Sorting
Divide-and-Conquer 7 2  9 4   2   4   7
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
CS 583 Analysis of Algorithms
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Algorithms CSCI 235, Spring 2019 Lecture 26 Midterm 2 Review
Tables and Priority Queues
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

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

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

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,

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 -> 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)

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

 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)

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 = i else: 14 out[i1+i2] = L2[i2] 15 i2 = i return out

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

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

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 

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

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) = 3n 3 + n log n T(n) = 2 n! T(n) = n + 2 T(n-1) T(n) = 3n 2 + 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) + Θ (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

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 -> 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 small = quicksort(small) 10 big = quicksort(big) 11 while (npivot): 12 small.append(pivot) 13 npivot = npivot return small + big

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<p) add e to L1 11 else add e to L2 12 L1 = dqsort(L1) 13 L2 = dqsort(L2) 14 return (L1 + L2)# concat

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)

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