2IL50 Data Structures Fall 2015 Lecture 3: Heaps.

Slides:



Advertisements
Similar presentations
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Advertisements

Analysis of Algorithms
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
David Luebke 1 5/20/2015 CS 332: Algorithms Quicksort.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 7 Heapsort and priority queues Motivation Heaps Building and maintaining heaps.
Data Structures Dynamic Sets Heaps Binary Trees & Sorting Hashing.
COMP 171 Data Structures and Algorithms Tutorial 5 Heapsort.
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
Sorting Algorithms (Part II) Slightly modified definition of the sorting problem: input: A collection of n data items where data item a i has a key, k.
2IL50 Data Structures Spring 2015 Lecture 3: Heaps.
Ch. 6: Heapsort n nodes. Heap -- Nearly binary tree of these n nodes (not just leaves) Heap property If max-heap, the max-heap property is that for every.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
Fall 2015 Lecture 4: Sorting in linear time
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Heaps. What is a heap? Like a binary search tree, but less structure within each level. Guarantees: – Parent better than child – That’s it! What does.
Prepared by- Jatinder Paul, Shraddha Rumade
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
Introduction to Algorithms
1 Analysis of Algorithms Chapter - 03 Sorting Algorithms.
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap”
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Spring 2015 Lecture 2: Analysis of Algorithms
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
COSC 3101A - Design and Analysis of Algorithms 3 Recurrences Master’s Method Heapsort and Priority Queue Many of the slides are taken from Monica Nicolescu’s.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.
CSE 310 Review 2/17/2016 Patrick Michaelson Ian Nall.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
Heapsort Lecture 4 Asst. Prof. Dr. İlker Kocabaş.
6.Heapsort. Computer Theory Lab. Chapter 6P.2 Why sorting 1. Sometimes the need to sort information is inherent in a application. 2. Algorithms often.
1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.
"Teachers open the door, but you must enter by yourself. "
Heaps, Heapsort, and Priority Queues
Heaps, Heap Sort and Priority Queues
Introduction to Algorithms
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
Heaps, Heapsort, and Priority Queues
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
Heapsort.
"Teachers open the door, but you must enter by yourself. "
Design and Analysis of Algorithms
HEAPS.
Heapsort Sorting in place
Solving Recurrences Continued The Master Theorem
Algorithms CSCI 235, Spring 2019 Lecture 14 Heap Sort Read: Ch. 6
Presentation transcript:

2IL50 Data Structures Fall 2015 Lecture 3: Heaps

Solving recurrences one more time …

Solving recurrences  Easiest: Master theorem caveat: not always applicable  Alternatively: Guess the solution and use the substitution method to prove that your guess it is correct.  How to guess: 1. expand the recursion 2. draw a recursion tree

Example(A) ► A is an array of length n 1. n = A.length 2. if n==1 3. then return A[1] 4. else begin 5. Copy A[1… n/2 ] to auxiliary array B[1... n/2 ] 6. Copy A[1… n/2 ] to auxiliary array C[1… n/2 ] 7. b = Example(B); c = Example(C) 8. for i = 1 to n 9. do for j = 1 to i 10. do A[i] = A[j] 11. return end Example

Example(A) ► A is an array of length n 1. n = A.length 2. if n==1 3. then return A[1] 4. else begin 5. Copy A[1… n/2 ] to auxiliary array B[1... n/2 ] 6. Copy A[1… n/2 ] to auxiliary array C[1… n/2 ] 7. b = Example(B); c = Example(C) 8. for i = 1 to n 9. do for j = 1 to i 10. do A[i] = A[j] 11. return end Example Let T(n) be the worst case running time of Example on an array of length n. Lines 1,2,3,4,11, and 12 take Θ(1) time. Lines 5 and 6 take Θ(n) time. Line 7 takes Θ(1) + 2 T( n/2 ) time. Lines 8 until 10 take time. If n=1 lines 1,2,3 are executed, else lines 1,2, and 4 until 12 are executed. ➨ T(n): ➨ use master theorem … Θ(1) if n=1 2T(n/2) + Θ(n 2 ) if n>1

Let a and b be constants, let f(n) be a function, and let T(n) be defined on the nonnegative integers by the recurrence T(n) = aT(n/b) + f(n) Then we have: 1.If f(n) = O(n log a – ε ) for some constant ε > 0, then T(n) = Θ(n log a ). 2.If f(n) = Θ(n log a ), then T(n) = Θ(n log a log n) 3.If f(n) = Ω(n log a + ε ) for some constant ε > 0, and if af(n/b) ≤ cf(n) for some constant c < 1 and all sufficiently large n, then T(n) = Θ(f(n)) The master theorem b b b b b

Quiz Recurrence 1.T(n) = 4 T(n/2) + Θ(n 3 ) 2.T(n) = 4 T(n/2) + Θ(n) 3.T(n) = T(n/2) T(n) = T(n/3) + T(2n/3) + n 5.T(n) = 9 T(n/3) + Θ(n 2 ) 6.T(n) = √n T(√n) + n Master theorem? yes no yes no T(n) = Θ(n 3 ) T(n) = Θ(n 2 ) T(n) = Θ(log n) T(n) = Θ(n log n) T(n) = Θ(n 2 log n) T(n) = Θ(n log log n)

Tips  Analysis of recursive algorithms: find the recursion and solve with master theorem if possible  Analysis of loops: summations  Some standard recurrences and sums: T(n) = 2T(n/2) + Θ(n) ➨ ½ n(n+1) = Θ(n 2 ) Θ(n 3 ) T(n) = Θ(n log n)

Heaps

Event-driven simulation  Stores a set of events, processes first event (highest priority)  Supporting data structure: insert event find (and extract) event with highest priority change the priority of an event

Priority queue Max-priority queue abstract data type (ADT) that stores a set S of elements, each with an associated key (integer value). Operations Insert(S, x): inserts element x into S, that is, S ← S ⋃ {x} Maximum(S): returns the element of S with the largest key Extract-Max(S): removes and returns the element of S with the largest key Increase-Key(S, x, k): give key[x] the value k condition: k is larger than the current value of key[x] Min-priority queue …

Implementing a priority queue Today InsertMaximumExtract-MaxIncrease-Key sorted list sorted array Θ(1) Θ(n) Θ(n)? Θ(n) InsertMaximumExtract-MaxIncrease-Key heap Θ(1)Θ(log n)

Max-heap Heap nearly complete binary tree, filled on all levels except possibly the lowest. (lowest level is filled from left to right) Max-heap property: for every node i other than the root key[Parent(i)] ≥ key[i]

Tree terminology Binary tree: every node has 0, 1, or 2 children Root: top node (no parent) Leaf: node without children Subtree rooted at node x: all nodes below and including x Depth of node x: length of path from root to x Depth of tree: max. depth over all nodes Height of node x: length longest path from x to leaf Height of tree: height of root Level: set of nodes with same depth Family tree terminology… Left/right child x

Max-heap Heap nearly complete binary tree, filled on all levels except possibly the lowest. (lowest level is filled from left to right) Max-heap property: for every node i other than the root key[Parent(i)] ≥ key[i]

Properties of a max-heap Lemma The largest element in a max-heap is stored at the root. Proof:

Properties of a max-heap Lemma The largest element in a max-heap is stored at the root. Proof:x root y arbitrary node z 1, z 2, …, z k nodes on path between x and y max-heap property ➨ key[x] ≥ key[z 1 ] ≥ … ≥ key[z k ] ≥ key[y] ➨ the largest element is stored at x ■ x y z1z1 z2z2

Storing a heap How to store a heap? Tree structure? In an array?

Implementing a heap with an array A.length = length of array A heap-size[A] =number of elements in the heap heap-size[A] 4 array A[1 … A. length ]

Implementing a heap with an array k th node on level j is stored at position left child of node at position i = Left(i) = right child of node at position i = Right(i) = parent of node at position i = Parent(i) = 123 heap-size[A] 4 array A[1 … A.length] level 0 level 1 level 2, position 3 A[2 j +k-1] 2i 2i + 1 i / 2

Priority queue Max-priority queue abstract data type (ADT) that stores a set S of elements, each with an associated key (integer value). Operations Insert(S, x): inserts element x into S, that is, S ← S ⋃ {x} Maximum(S): returns the element of S with the largest key Extract-Max(S): removes and returns the element of S with the largest key Increase-Key(S, x, k): give key[x] the value k condition: k is larger than the current value of key[x]

Implementing a max-priority queue Set S is stored as a heap in an array A. Operations: Maximum, Extract-Max, Insert, Increase-Key heap-size[A] 4

Implementing a max-priority queue Set S is stored as a heap in an array A. Operations: Maximum, Extract-Max, Insert, Increase-Key. Maximum(A) 1. if heap-size[A] < 1 2. then error 3. else return A[1] running time: O(1) heap-size[A] 4

Implementing a max-priority queue Set S is stored as a heap in an array A. Operations: Maximum, Extract-Max, Insert, Increase-Key heap-size[A] 4

Implementing a max-priority queue Set S is stored as a heap in an array A. Operations: Maximum, Extract-Max, Insert, Increase-Key

Set S is stored as a heap in an array A. Operations: Maximum, Extract-Max, Insert, Increase-Key. Heap-Extract-Max(A) 1. if heap-size[A] < 1 2. then error 3. max = A[1] 4. A[1] = A [heap-size[A]] 5. heap-size[A] = heap-size[A]–1 6. Max-Heapify(A,1) 7. return max Implementing a max-priority queue restore heap property

Max-Heapify Max-Heapify(A, i) ► ensures that the heap whose root is stored at position i has the max-heap property ► assumes that the binary subtrees rooted at Left(i) and Right(i) are max-heaps

Max-Heapify Max-Heapify(A, i) ► ensures that the heap whose root is stored at position i has the max-heap property ► assumes that the binary subtrees rooted at Left(i) and Right(i) are max-heaps  Max-Heapify(A,1)  exchange A[1] with largest child  Max-Heapify(A,2)  exchange A[2] with largest child  Max-Heapify(A,5)  A[5] larger than its children ➨ done

Max-Heapify Max-Heapify(A, i) ► ensures that the heap whose root is stored at position i has the max-heap property ► assumes that the binary subtrees rooted at Left(i) and Right(i) are max-heaps 1. if Left(i) ≤ heap-size[A] and A[Left(i)] > A[i] 2. then largest = Left(i) 3. else largest = i 4. if Right(i) ≤ heap-size[A] and A[Right(i)] > A[largest] 5. then largest = Right(i) 6. if largest ≠ i 7. then exchange A[i] and A[largest] 8. Max-Heapify(A, largest) running time? O(height of the subtree rooted at i) = O(log n)

Implementing a max-priority queue Set S is stored as a heap in an array A. Operations: Maximum, Extract-Max, Insert, Increase-Key. Insert (A, key) 1. heap-size[A] = heap-size[A] A[heap-size[A]] = - infinity 3. Increase-Key(A, heap-size[A], key)

Implementing a max-priority queue Set S is stored as a heap in an array A. Operations: Maximum, Extract-Max, Insert, Increase-Key.

Implementing a max-priority queue Set S is stored as a heap in an array A. Operations: Maximum, Extract-Max, Insert, Increase-Key. Increase-Key(A, i, key) 1. if key < A[i] 2. then error 3. A[i] = key 4. while i>1 and A[parent(i)] < A[i] 5. do exchange A[parent(i)] and A[i] 6. i = parent(i) running time: O(log n)

Building a heap Build-Max-Heap(A) ► Input: array A[1..n] of numbers ► Output: array A[1..n] with the same numbers, but rearranged, such that the max-heap property holds 1.heap-size = A.length 2. for i = A.length downto 1 3. do Max-Heapify(A,i) Loop Invariant P(i): nodes i+1, …, n are each the root of a max-heap Maintenance P(i) holds before line 3 is executed, P(i – 1) holds afterwards starting at A.length / 2 is sufficient

Building a heap Build-Max-Heap(A) 1.heap-size = A.length 2. for i = A.length downto 1 3. do Max-Heapify(A,i) ∑ i O(1 + height of i) = ∑ 0 ≤ h ≤ log n (# nodes of height h) ∙ O(1+h) ≤ ∑ 0 ≤ h ≤ log n (n / 2 h+1 ) ∙ O(1+h) = O(n) ∙ ∑ 0 ≤ h ≤ log n h / 2 h+1 = O(n) h=0h=0 h=1h=1 h=2h=2 O(height of node i) height of node i # edges longest simple downward path from i to a leaf.

The sorting problem Input: a sequence of n numbers ‹a 1, a 2, …, a n › Output: a permutation of the input such that ‹ a i1 ≤ … ≤ a in › Important properties of sorting algorithms: running time: how fast is the algorithm in the worst case in place: only a constant number of input elements are ever stored outside the input array

Sorting with a heap: Heapsort HeapSort(A) 1. Build-Max-Heap(A) 2. for i = A.length downto 2 3. do exchange A[1] and A[i] 4. heap-size[A] = heap-size[A] – 1 5. Max-Heapify(A,1) Loop invariant P(i): A[i+1... n] is sorted and contains the n – i largest elements, A[1…i] is a max-heap on the remaining elements Maintenance P(i) holds before lines 3-5 are executed, P(i – 1) holds afterwards Running time: O(n log n)

Sorting algorithms worst case running timein place InsertionSort MergeSort HeapSort Θ(n 2 ) yes Θ(n log n)no yes Θ(n log n)

Announcements Today 7+8 Reading and writing mathematical proofs (final lecture) Wednesday 3+4 General tutorial