Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Binary Heap.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
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.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
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.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Advanced Sorting.
Sorting With Priority Queue In-place Extra O(N) space
"Teachers open the door, but you must enter by yourself. "
CS 201 Data Structures and Algorithms
May 17th – Comparison Sorts
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Searching – Linear and Binary Searches
CSE 373: Data Structure & Algorithms Comparison Sorting
October 30th – Priority QUeues
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Priority Queues (Heaps)
ADT Heap data structure
CMSC 341: Data Structures Priority Queues – Binary Heaps
Data Structures and Algorithms
Data Structures & Algorithms Priority Queues & HeapSort
CSE 373: Data Structures and Algorithms
Hassan Khosravi / Geoffrey Tien
B-Tree Insertions, Intro to Heaps
Heaps and the Heapsort Heaps and priority queues
Priority Queues.
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Ch. 8 Priority Queues And Heaps
Data Structures and Algorithms
8/04/2009 Many thanks to David Sun for some of the included slides!
Data Structures and Algorithms
"Teachers open the door, but you must enter by yourself. "
CE 221 Data Structures and Algorithms
CS Data Structure: Heaps.
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Sub-Quadratic Sorting Algorithms
Sorting and recurrence analysis techniques
CSE 326: Data Structures Sorting
CSE 373: Data Structures and Algorithms
CSE 332: Data Abstractions Sorting I
CSE 373 Data Structures and Algorithms
Priority Queues (Heaps)
The Selection Problem.
Lecture 15: Sorting Algorithms
Lecture 9: Intro to Trees
CSE 373: Data Structures and Algorithms
Heaps & Multi-way Search Trees
Lecture 19: Sorting Algorithms
Lecture 16: Midterm recap + Heaps ii
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
CSE 373: Data Structures and Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

Data Structures and Algorithms Happy Star Wars Day Implementing Heaps Data Structures and Algorithms CSE 373 SP 18 - Kasey Champion

Warm Up Construct a Min Binary Heap by inserting the following values in this order: 5, 10, 15, 20, 7, 2 Min Priority Queue ADT removeMin() – returns the element with the smallest priority, removes it from the collection state behavior Set of comparable values - Ordered based on “priority” peekMin() – find, but do not remove the element with the smallest priority insert(value) – add a new element to the collection Min Binary Heap Invariants Binary Tree – each node has at most 2 children Min Heap – each node’s children are larger than itself Level Complete - new nodes are added from left to right completely filling each level before creating a new one 2 5 7 10 5 15 2 percolateUp! 20 7 10 2 15 percolateUp! percolateUp! CSE 373 SP 18 - Kasey Champion

Midterm Grades Posted! Statistics: Minimum: 22.0 Maximum: 85.0 Mean: 67.28 (79.1%) Median: 69.0 (81.1%) Standard Deviation: 10.05 CSE 373 SP 18 - Kasey Champion

Implementing Heaps How do we find the minimum node? How do we find the last node? How do we find the next open space? How do we find a node’s left child? How do we find a node’s right child? How do we find a node’s parent? 𝑝𝑒𝑒𝑘𝑀𝑖𝑛()=𝑎𝑟𝑟[0] I A B D H C K E J F L G 𝑙𝑎𝑠𝑡𝑁𝑜𝑑𝑒()=𝑎𝑟𝑟[𝑠𝑖𝑧𝑒 −1] 𝑜𝑝𝑒𝑛𝑆𝑝𝑎𝑐𝑒()=𝑎𝑟𝑟[𝑠𝑖𝑧𝑒] 𝑙𝑒𝑓𝑡𝐶ℎ𝑖𝑙𝑑 𝑖 =2𝑖+1 Gives us fast insertions and removals Completeness forces height to be log(n) Gives us contiguious memory 𝑟𝑖𝑔ℎ𝑡𝐶ℎ𝑖𝑙𝑑 𝑖 =2𝑖+2 Fill array in level-order from left to right 𝑝𝑎𝑟𝑒𝑛𝑡 𝑖 = 𝑖 2 𝑝𝑎𝑟𝑒𝑛𝑡 𝑖 = 𝑖−1 2 1 2 3 4 5 6 7 8 9 10 11 12 13 A B C D E F G H I J K L 1 2 3 4 5 6 7 8 9 10 11 12 13 A B C D E F G H I J K L CSE 373 SP 18 - Kasey Champion

Heap Implementation Runtimes char peekMin() timeToFindMin Tree Array char removeMin() findLastNodeTime + removeRootTime + numSwaps * swapTime void insert(char) findNextSpace + addValue + numSwaps * swapTime A Θ(1) Θ(1) B C n + 1 + log(n) * 1 Θ(n) D E F 1 + 1 + log(n) * 1 Θ(log(n)) 1 2 3 4 5 6 7 A B C D E F n + 1 + log(n) * 1 Θ(n) 1 + 1 + log(n) * 1 Θ(log(n)) CSE 373 SP 18 - Kasey Champion

Building a Heap Insert has a runtime of Θ(log(n)) If we want to insert a n items… Building a tree takes O(nlog(n)) Add a node, fix the heap, add a node, fix the heap Can we do better? Add all nodes, fix heap all at once! CSE 373 SP 18 - Kasey Champion

Cleaver building a heap – Floyd’s Method Facts of binary trees Increasing the height by one level doubles the number of possible nodes A complete binary tree has half of its nodes in the leaves A new piece of data is much more likely to have to percolate down to the bottom than be the smallest element in heap 1. Dump all the new values into the bottom of the tree Back of the array 2. Traverse the tree from bottom to top Reverse order in the array 3. Percolate Down each level moving towards overall root CSE 373 SP 18 - Kasey Champion

Floyd’s buildHeap algorithm Build a tree with the values: 12, 5, 11, 3, 10, 2, 9, 4, 8, 1, 7, 6 12 Add all values to back of array percolateDown(parent) starting at last index 5 11 3 10 2 9 4 8 1 7 6 1 2 3 4 5 6 7 8 9 10 11 12 13 12 5 11 3 10 2 9 4 8 1 7 6 CSE 373 SP 18 - Kasey Champion

Floyd’s buildHeap algorithm Build a tree with the values: 12, 5, 11, 3, 10, 2, 9, 4, 8, 1, 7, 6 12 2 Add all values to back of array percolateDown(parent) starting at last index percolateDown level 4 percolateDown level 3 percolateDown level 2 percolateDown level 1 5 3 11 11 2 3 10 2 5 7 11 9 4 8 1 7 6 10 1 2 3 4 5 6 7 8 9 10 11 12 13 12 5 11 3 10 2 9 4 8 1 7 6 CSE 373 SP 18 - Kasey Champion

Floyd’s Heap Runtime We step through each node – n We call percolateDown() on each n – log n thus it’s O(nlogn) … let’s look closer… Are we sure percolateDown() runs log n each time? Half the nodes of the tree are leaves Leaves run percolate down in constant time ¼ the nodes have at most 1 level to travel 1/8 the nodes have at most 2 levels to travel etc… work(n) ≈ n/2 * 1 + n/4 * 2 + n/8 * 3 + … CSE 373 SP 18 - Kasey Champion

Closed form Floyd’s buildHeap work(n) ≈ 𝑛 2 * 1 + 𝑛 4 * 2 + 𝑛 8 * 3 + … factor out n work(n) ≈ n( 1 2 + 2 4 + 3 8 + …) work(n) ≈ n( 1 2 1 + 2 2 2 + 3 2 3 + …) find a pattern -> powers of 2 Summation! 𝑤𝑜𝑟𝑘 𝑛 ≈𝑛 𝑖=1 ? 𝑖 2 𝑖 ? = how many levels = height of tree = log(n) Infinite geometric series Because 1 2 the nodes are leaves, design algorithms that do less work closer to the bottom! More careful analysis can reveal tighter bounds Math strategy: rather than show a <= b directly, it can be simpler to show a <= t then t<= b (a la finding c and n0) 𝑤𝑜𝑟𝑘 𝑛 ≈𝑛 𝑖=1 logn 𝑖 2 𝑖 𝑖𝑓−1<𝑥<1 𝑡ℎ𝑒𝑛 𝑖=0 ∞ 𝑥 𝑖 = 1 1 −𝑥 =𝑥 𝑤𝑜𝑟𝑘 𝑛 ≈𝑛 𝑖=1 logn 𝑖 2 𝑖 ≤𝑛 𝑖=0 ∞ 𝑖 2 𝑖 =𝑛 ∗2 Floyd’s buildHeap runs in O(n) time! CSE 373 SP 18 - Kasey Champion

Sorting CSE 373 SP 18 - Kasey Champion

Types of Sorts Niche Sorts aka “linear sorts” Comparison Sorts Compare two elements at a time General sort, works for most types of elements Element must form a “consistent, total ordering” For every element a, b and c in the list the following must be true: If a <= b and b <= a then a = b If a <= b and b <= c then a <= c Either a <= b is true or <= a What does this mean? compareTo() works for your elements Comparison sorts run at fastest O(nlog(n)) time Niche Sorts aka “linear sorts” Leverages specific properties about the items in the list to achieve faster runtimes niche sorts typically run O(n) time In this class we’ll focus on comparison sorts CSE 373 SP 18 - Kasey Champion

Sort Approaches [(8, “fox”), (9, “dog”), (4, “wolf”), (8, “cow”)] In Place sort A sorting algorithm is in-place if it requires only O(1) extra space to sort the array Typically modifies the input collection Useful to minimize memory usage Stable sort A sorting algorithm is stable if any equal items remain in the same relative order before and after the sort Why do we care? Sometimes we want to sort based on some, but not all attributes of an item Items that “compareTo()” the same might not be exact duplicates Enables us to sort on one attribute first then another etc… [(8, “fox”), (9, “dog”), (4, “wolf”), (8, “cow”)] [(4, “wolf”), (8, “fox”), (8, “cow”), (9, “dog”)] Stable [(4, “wolf”), (8, “cow”), (8, “fox”), (9, “dog”)] Unstable CSE 373 SP 18 - Kasey Champion

SO MANY SORTS Quicksort, Merge sort, in-place merge sort, heap sort, insertion sort, intro sort, selection sort, timsort, cubesort, shell sort, bubble sort, binary tree sort, cycle sort, library sort, patience sorting, smoothsort, strand sort, tournament sort, cocktail sort, comb sort, gnome sort, block sort, stackoverflow sort, odd-even sort, pigeonhole sort, bucket sort, counting sort, radix sort, spreadsort, burstsort, flashsort, postman sort, bead sort, simple pancake sort, spaghetti sort, sorting network, bitonic sort, bogosort, stooge sort, insertion sort, slow sort, rainbow sort… CSE 373 SP 18 - Kasey Champion

Insertion Sort https://www.youtube.com/watch?v=ROalU379l3U 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10 Sorted Items Unsorted Items Current Item 1 2 3 4 5 6 7 8 9 10 Sorted Items Unsorted Items Current Item 1 2 3 4 5 6 7 8 9 10 Sorted Items Unsorted Items Current Item

Insertion Sort 1 2 3 4 5 6 7 8 9 10 Sorted Items Unsorted Items 1 2 3 4 5 6 7 8 9 10 Sorted Items Unsorted Items Current Item public void insertionSort(collection) { for (entire list) if(currentItem is bigger than nextItem) int newIndex = findSpot(currentItem); shift(newIndex, currentItem); } public int findSpot(currentItem) { for (sorted list) if (spot found) return public void shift(newIndex, currentItem) { for (i = currentItem > newIndex) item[i+1] = item[i] item[newIndex] = currentItem Worst case runtime? Best case runtime? Average runtime? Stable? In-place? O(n2) O(n) O(n2) Yes Yes CSE 373 SP 18 - Kasey Champion

Selection Sort https://www.youtube.com/watch?v=Ns4TPTC8whw 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 18 10 14 11 15 Sorted Items Unsorted Items Current Item 1 2 3 4 5 6 7 8 9 10 14 18 11 15 Sorted Items Unsorted Items Current Item 1 2 3 4 5 6 7 8 9 10 18 14 11 15 Sorted Items Unsorted Items Current Item

Selection Sort 1 2 3 4 5 6 7 8 9 18 10 14 11 15 Sorted Items 1 2 3 4 5 6 7 8 9 18 10 14 11 15 Sorted Items Current Item Unsorted Items public void selectionSort(collection) { for (entire list) int newIndex = findNextMin(currentItem); swap(newIndex, currentItem); } public int findNextMin(currentItem) { min = currentItem for (unsorted list) if (item < min) return min public int swap(newIndex, currentItem) { temp = currentItem currentItem = newIndex newIndex = currentItem Worst case runtime? Best case runtime? Average runtime? Stable? In-place? O(n2) O(n2) O(n2) Yes Yes CSE 373 SP 18 - Kasey Champion

Heap Sort 1. run Floyd’s buildHeap on your data https://www.youtube.com/watch?v=Xw2D9aJRBY4 1. run Floyd’s buildHeap on your data 2. call removeMin n times Worst case runtime? Best case runtime? Average runtime? Stable? In-place? O(nlogn) public void heapSort(collection) { E[] heap = buildHeap(collection) E[] output = new E[n] for (n) output[i] = removeMin(heap) } O(nlogn) O(nlogn) No No CSE 373 SP 18 - Kasey Champion

In Place Heap Sort 1 2 3 4 5 6 7 8 9 14 15 18 16 17 20 22 Heap Sorted Items Current Item 1 2 3 4 5 6 7 8 9 22 14 15 18 16 17 20 percolateDown(22) Heap Sorted Items Current Item 1 2 3 4 5 6 7 8 9 16 14 15 18 22 17 20 Heap Sorted Items Current Item CSE 373 SP 18 - Kasey Champion

In Place Heap Sort 1 2 3 4 5 6 7 8 9 15 17 16 18 20 22 14 Heap Sorted Items Current Item public void inPlaceHeapSort(collection) { E[] heap = buildHeap(collection) for (n) output[n – i - 1] = removeMin(heap) } Worst case runtime? Best case runtime? Average runtime? Stable? In-place? O(nlogn) O(nlogn) O(nlogn) Complication: final array is reversed! Run reverse afterwards (O(n)) Use a max heap Reverse compare function to emulate max heap No Yes CSE 373 SP 18 - Kasey Champion

Divide and Conquer Technique 1. Divide your work into smaller pieces recursively Pieces should be smaller versions of the larger problem 2. Conquer the individual pieces Base case! 3. Combine the results back up recursively divideAndConquer(input) { if (small enough to solve) conquer, solve, return results else divide input into a smaller pieces recurse on smaller piece combine results and return } CSE 373 SP 18 - Kasey Champion

Merge Sort https://www.youtube.com/watch?v=XaqR3G_NVoo Divide 1 2 3 4 1 2 3 4 5 6 7 8 9 91 22 57 10 1 2 3 4 8 91 22 57 5 6 7 8 9 1 10 4 8 Conquer 8 Combine 1 2 3 4 8 22 57 91 5 6 7 8 9 1 4 10 1 2 3 4 5 6 7 8 9 10 22 57 91 CSE 373 SP 18 - Kasey Champion

1 2 3 4 8 57 91 22 Merge Sort 1 8 2 1 2 57 91 22 mergeSort(input) { if (input.length == 1) return else smallerHalf = mergeSort(new [0, ..., mid]) largerHalf = mergeSort(new [mid + 1, ...]) return merge(smallerHalf, largerHalf) } 8 2 57 1 91 22 91 22 1 22 91 Worst case runtime? Best case runtime? Average runtime? Stable? In-place? 1 if n<= 1 2T(n/2) + n otherwise T(n) = 1 2 8 1 2 22 57 91 No 1 2 3 4 22 57 91 No CSE 373 SP 18 - Kasey Champion