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.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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)
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.
Analysis of Algorithms
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
David Luebke 1 5/20/2015 CS 332: Algorithms Quicksort.
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Analysis of Algorithms CS 477/677
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
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.
Binary Search Trees vs. Binary Heaps. Binary Search Tree Condition Given a node i –i.value is the stored object –i.left and i.right point to other nodes.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
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.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
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.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
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.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
David Luebke 1 6/3/2016 CS 332: Algorithms Heapsort Priority Queues Quicksort.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
1 Analysis of Algorithms Chapter - 03 Sorting Algorithms.
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.
ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ
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.
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:
Internal and External Sorting External Searching
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
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.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
Heaps, Heapsort, and Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
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.
Priority Queues.
Heaps, Heapsort, and Priority Queues
Ch 6: Heapsort Ming-Te Chi
Heaps 11/27/ :05 PM Heaps Heaps.
Priority Queues.
Heapsort.
"Teachers open the door, but you must enter by yourself. "
HEAPS.
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

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. No relationship between keys at the same level (unlike BST).

Types of heaps Min heap: priority 1 more important than 100 Max heap: 100 more important than 1 – We are going to talk about max heaps. Max heap-order property – Look at any node u, and its parent p. – p.priority ≥ u.priority p:7 u:3

Abstract data type (ADT) We are going to use max-heaps to implement the (max) priority queue ADT A priority queue Q offers (at least) 2 operations: – Extract-max(Q): returns the highest priority element – Insert(Q, e): inserts e into Q Every time an Insert or Extract-max changes the heap, it must restore the max-heap order property. (  Prove by induction on the sequence of inserts and extract-maxes that occur.)

What can we do with a heap Can do same stuff with a BST… why use a heap? – BST extract-max is O(depth); heap is O(log n)! When would we use a BST? – When we need to search for a particular key.

Heaps are typically implemented with arrays. The array is just a level-order traversal of the heap. The children of the node at index i are at 2i and 2i+1. Storing a heap in memory

Example time Interactive heap visualization Insert places a key in a new node that is the last node in a level-order-traversal of the heap. The inserted key is then “bubbled” upwards until the heap property is satisfied. Extract-max removes the last node in a level- order-traversal and moves its key into the root. The new key at the root is then bubbled down until the heap property is satisfied. Bubbling down is also called heapifying.

Suppose we want to build a heap from an unsorted array: 10, 2, 7, 8, 6, 5, 9, 4, 3, 11. We start by interpreting the array as a tree. Building a max-heap in O(n) time

Building a heap: a helper function Precondition: trees rooted at L and R are heaps Postcondition: tree rooted at I is a heap MaxHeapify(A,I): L = LEFT(I) R = RIGHT(I) If L ≤ heap_size(A) and A[L] > A[I] then max = L else max = I If R ≤ heap_size(A) and A[R] > A[max] then max = R If max is L or R then swap(A[I],A[max]) MaxHeapify(A,max) I:3 L:7R:5 Case 1: max = L Need to fix… I:7 L:3R:5 Case 2: max = I Heap OK! I:5 L:3R:7 Case 3: max = R Need to fix…

Proving MaxHeapify is correct How would you formally prove that MaxHeapify is correct? Goal: Prove MaxHeapify is correct for all inputs. “Correct” means: “if the precondition is satisfied when MaxHeapify is called, then the postcondition will be satisfied when it finishes.” How do we prove a recursive function correct? – Define a problem size, and prove correctness by induction on problem size. – Base case: show function is correct for any input of the smallest problem size. – Inductive step: assume function is correct for problem size j; show it is correct for problem size j+1.

Proving MaxHeapify is correct - 2 Let’s apply this to MaxHeapify. Problem size: height of node I. Base case: Prove MaxHeapify is correct for every input with height(I) = 0. Inductive step: Let A and I be any input parameters that satisfy the precondition. Assume MaxHeapify is correct when the problem size is j. Prove MaxHeapify is correct when the problem size is j+1.

Proving MaxHeapify is correct - 3 Precondition: trees rooted at L and R are heaps Postcondition: tree rooted at I is a heap MaxHeapify(A,I): L = LEFT(I) R = RIGHT(I) If L ≤ heap_size(A) and A[L] > A[I] then max = L else max = I If R ≤ heap_size(A) and A[R] > A[max] then max = R If max is L or R then swap(A[I],A[max]) MaxHeapify(A,max) I:3 L:7R:5 Case 1: max = L Need to fix… I:7 L:3R:5 Case 2: max = I Heap OK! I:5 L:3R:7 Case 3: max = R Need to fix…

The main function BUILD-MAX-HEAP(A): for i = heap_size(A)/2 down to 1 MaxHeapify(A,i)

Analyzing worst-case complexity

Analyzing worst-case complexity

≤ 2 d nodes at depth d Node at depth d has height ≤ h-d Cost to “heapify” one node at depth d is ≤ c(h-d) – Don’t care about constants... Ignoring them below… Cost to heapify all nodes at depth d is ≤ 2 d (h-d)

So, cost to heapify all nodes over all depths is: