Heap Sort The Heap Data Structure

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)
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
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.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Heaps Heaps are used to efficiently implement two operations:
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
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.
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.
Binary Heap.
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.
Chapter 21 Binary Heap.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
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.
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.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
CSE 5392 Fall 2005 Week 4 Devendra Patel Subhesh Pradhan.
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:
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ş.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
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:
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
Heaps (8.3) CSE 2011 Winter May 2018.
Heaps, Heapsort, and Priority Queues
Heaps, Heap Sort and Priority Queues
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Heapsort Chapter 6 Lee, Hsiu-Hui
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heapsort.
Heap Sort Example Qamar Abbas.
Introduction to Algorithms
The Heap Data Structure
7/23/2009 Many thanks to David Sun for some of the included slides!
CMSC 341: Data Structures Priority Queues – Binary Heaps
Heaps, Heapsort, and Priority Queues
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
CMSC 341 Lecture 14 Priority Queues & Heaps
Binary Tree Application Operations in Heaps
Heapsort.
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Heaps A heap is a binary tree.
Lecture 3 / 4 Algorithm Analysis
ICS 353: Design and Analysis of Algorithms
"Teachers open the door, but you must enter by yourself. "
ICS 353: Design and Analysis of Algorithms
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Topic 5: Heap data structure heap sort Priority queue
HEAPS.
Heapsort Sorting in place
Algorithms: Design and Analysis
Priority Queues (Heaps)
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps & Multi-way Search Trees
Asst. Prof. Dr. İlker Kocabaş
Presentation transcript:

Heap Sort The Heap Data Structure

Basic Heap Property Complete binary tree All internal nodes have two children All leaves have depth d Essentially complete binary tree It is a complete binary tree down to a depth of d-1 The nodes with depth d are as far to the left as possible

Basic Heap Property Depth of a tree The depth of a node in a tree is the number of edges in the unique path from the root to that node The depth of a tree is the maximum depth of all nodes in the tree A leaf in a tree is any node with no children Internal node is any node that has at least one child

Basic Heap Property Heap A heap is an essentially complete binary tree such that The values stored at the nodes come from an ordered set The value stored at each node is greater than or equal to the values stored at its children.

Procedure Siftdown Siftdown Procedure to sift the key at the root down the heap until the heap property is satisfied. Comparing the parent key with the larger key at the children. If smaller  exchange. Repeat until no smaller key than the larger of its children.

Heap data structure root = 1 Parent(i) = i/2 Left(i)=2i 3 2 8 7 18 14 9 29 6 1 4 5 10 root = 1 Parent(i) = i/2 Left(i)=2i Right(i)=2i+1 last array 1 3 2 8 7 29 6 4 5 9 18 14 10

Heap Sort Makeheap From nodes in the depth level of d-1 to level 0 Each depth level, from right node to left node to make heap Removekeys Move root node value into the array S Move leaf node (far right) into the root and make a heap, delete that leaf node Repeat the process until all the node values are moved into the array S, which is formed the sorted sequence.

Overview Usage of a heap HeapSort Priority queue Definitions: height, depth, full binary tree, complete binary tree Definition of a heap Methods of a heap

Priority Queue A priority queue is a collection of zero or more items, associated with each item is a priority Operations: insert a new item delete item with the highest priority find item with the highest priority

Worst case time complexity for heaps Build heap with n items - (n) insert() into a heap with n items - (lg n) deleteMin() from a heap with n items- (lg n) findMin() - (1)

Depth of tree nodes Depth of a node is: If node is the root --- 0 Otherwise - (depth of its parent + 1) Depth of a tree is maximum depth of its leaves. 1 1 2 2 A tree of depth 2

Height of tree nodes Height of a node is: If node is a leaf --- 0 Otherwise - (maximum height of its children +1) Height of a tree is the height of the root. 2 1 A tree of height 2

A full binary tree A full binary tree is a binary tree such that: All internal nodes have 2 children All leaves have the same depth d The number of nodes is n = 2d+1 - 1 7 = 22+1 - 1 A full binary tree of depth = height = 2

A full binary tree - cont. Number the nodes of a full binary tree of depth d: The root at depth 0 is numbered ---- 1 The nodes at depth 1, …, d are numbered consecutively from left to right, in increasing depth. 1 2 3 4 5 6 7

Essential complete binary tree An essential complete binary tree of depth d and n nodes is a binary tree such that its nodes would have the numbers 1, …, n in a binary tree of depth d. The number of nodes 2d n  2d+1 -1 1 1 2 3 2 3 4 5 6 4 5 6 7

Height (depth) of a complete binary tree Number of nodes n satisfy: 2h  n and (n + 1) 2h+1 Taking the log base 2 we get: h  lg n and lg(n + 1)  h + 1 or lg(n + 1)-1  h  lg n Since h is integer and lg(n + 1) -1  =  lg n  h = lg(n + 1) - 1=  lg n 

Definition of a heap A heap is an essential complete binary tree that satisfies the heap property. Minimum Heap Property: The value stored at each node is less than or equal to the values stored at its children. OR Maximum Heap Property: greater

Heap and its (dynamic) array implementation 3 2 8 7 18 14 9 29 6 1 4 5 10 root = 1 Parent(i) = i/2 Left(i)=2i Right(i)=2i+1 last array (bt) 1 3 2 8 7 29 6 4 5 9 18 14 10

Methods insert deleteMin percolate (or siftUp) siftDown buildHeap Other methods size, isEmpty, findMin, decreaseKey Assume that bt is an array that is used to “store” the heap and is visible to all methods.

insert(v) Item inserted as new last item in the heap 1 10 Item inserted as new last item in the heap Heap property may be violated Percolate to restore heap property 3 2 30 20 4 7 5 6 80 6 70 29 last Last after insert 6

Percolate Start at index to Re-establish MinHeap Property procedure percolate (index ) if index >root // root = 1 p = parent(index) if bt [p].key > bt [ index ].key swap( index, p) percolate(p) The worst case growth rate of percolate is (d(index)) where d(index) denotes the depth of node index or O(lg n).

Time analysis for Percolate(index) 1 3 2 d 4 7 5 6 lg n (d(index)) n O(lg n) for index < n (lg n) for index = n

insert(v) insert( v ) last =last+1 bt[last] ¬ v //insert at new last position of tree 3. percolate ( last ) The worst case time of insert is (d(last)), or (lg n)

percolate(last) last last 6 10 30 10 30 6 80 20 70 29 80 20 70 29 1 1 4 7 5 6 4 7 5 6 80 20 70 29 80 20 70 29 last last

deleteMin() Save root object (1) 10 10 2 3 30 20 4 Save root object (1) Remove last element and store in root (1) siftDown(1) 80 1 last 80 2 3 30 20 1 After siftDown(1) 20 2 3 30 80

Delete minimum deleteMin () 1. minKeyItem = bt [root] //root = 1 2. swap(root, last) 3. last = last - 1 // decrease last by 1 4. if (notEmpty()) // last > 1 5. siftDown(root) 6. return minKeyItem Worst case time is dominated by time for siftDown(root) is (h(root)) or (lg n). h(root) denotes the height of the tree

SiftDown(bt, i) lC = Left[i] rC = Right[i] smallest = i //smallest = index of min{bt[i], bt[lC], bt[rC]} if (lC <= last) and (bt[lC] < bt[i]) smallest = lC if (rC <= last) and (bt[rC] < bt[smallest]) smallest = rC if (smallest != i) // Otherwise bt is already a heap swap bt[i] and bt[smallest] SiftDown(bt, smallest) //Continue to sift down

Time analysis for Siftdown(i) 1 O(lg n) for i >1 (lgn) for i=1 3 2 4 7 5 6 lg n (h(i)) h n

4 3 8 17 12 14 19 6 13 1 siftDown(1) New value at root. 5 7 9 10 siftDown(1) New value at root. Right Child is smaller Exchange root and right child Satisfy the Heap property.

4 9 8 17 12 14 19 6 13 1 3 2 Parent Left Child is smaller 5 7 10 Parent Left Child is smaller Exchange parent and left child

The worst case run time to do siftDown(index) is 4 6 8 17 12 14 19 9 13 1 2 3 5 7 10 The worst case run time to do siftDown(index) is (h(index)) where h(index) is the height of node index or O(lg n)

Building a Heap: Method 1 Assume that array bt has n elements, and needs to be converted into a heap. slow-make-heap() { for i ¬ 2 to last do percolate ( i ) } The time is

Percolate(2) 1 swap Percolate(3) 1 swap 9 8 7 6 3 2 1 5 4 10 8 7 6 3 2

Percolate(4) 2 swaps Percolate(5) 2 swaps 10 9 7 6 3 2 1 5 4 8 9 10 6

Percolate(6) 2 swaps Percolate(7) 2 swaps 7 9 10 8 3 2 1 5 4 7 6 10 8

Percolate(8) 3 swaps Percolate(9) 3 swaps 7 5 10 8 3 2 1 9 6 4 5 7 8

Percolate(10) 3 swaps The heap 3 5 4 8 10 7 1 9 6 2 5 4 3 10 7 8 9 6 1

Time for slow make heap The depth of node i for a current heap with i nodes is lg i. For simplicity we assume that the time of percolate is lg i . So time of slow make heap is

Why is So lg n! <= lg nn = n lg n for all n >= 1 n! = n*(n-1)*(n-2)*…*3* 2 *1 <= n*n*n*…*n* n *n =nn So lg n! <= lg nn = n lg n for all n >= 1 To show BigOh. We choose a c = 1 and N=1.

Why is n! = n*(n-1)*…n/2*…*2 *1 >= n/2*n/2*n/2*…*n/2 = = (n/2)n/2 for all n>=1. (We neglect floors) So lg n! >= lg (n/2)n/2 = n/2(lg n – 1) = 1/2(nlg n) – n/2 = ¼(nlgn) + (1/4(nlgn) – n/2) >= ¼(nlgn) provided that ¼(nlgn) – n/2 >= 0 Dividing by n>0 we get ¼(lg n) >= 1/2 and lg n >=2. So n >= 4 To show Omega. We choose c = 1/4 and N=4. Clearly,

Build the Heap:Method 2 make-heap //- done in constructor. 1. for i ¬ last downto 1 2. do siftDown( i ) The time is

siftDown(5) makes it a min heap 1 swap 8 12 9 7 10 21 1 2 3 4 6 14 8 12 9 7 6 14 4 10 21 1 2 3 5 siftDown(5) makes it a min heap 1 swap

siftDown(4) makes this into heap 1 swap 8 12 9 4 6 14 7 10 21 1 2 3 5 this is a heap siftDown(4) makes this into heap 1 swap

siftDown(3) 1 swap makes heap 8 12 6 4 9 14 7 10 21 1 2 3 5 i = 3 siftDown(3) 1 swap makes heap These are heaps

Siftdown(2) 2 swaps After second After first siftDown 10 12 21 5 8 4 9 3 6 7 5 8 4 9 14 2 Siftdown(2) 2 swaps After second After first 4 6 7 9 14 8 2 5 10 4 6 8 9 14 7 2 5 10 siftDown

4 10 6 7 9 14 8 12 21 1 2 3 5 Siftdown(1) 1 swap 10 6 7 9 14 8 12 21 1 2 3 4 5 5

Example The following slide shows an example of a worst case computation done by slow-make-heap, and fast make- heap The heap contains 7000 nodes The height is 12 73% of the nodes are in the bottom 3 levels of the tree slow-make-heap requires 75822 swaps in the worst case, and an average of 11.3 swaps for 73% of the nodes (~10 for 100%) Fast make-heap requires <8178 swaps in the worst case, and an average of .68 swaps for 73% of the nodes (~1.1 for 100%)

Tight analysis of Method 2 Notice we are building the heap “bottom up” . The most amount of work is done for the fewest nodes. height h height h-1 height 1 height 0 height 0 “path” of siftDowns

Cost of fast make heap Depth Number Nodes Sift Count 1 h+1-0 20( h+1) . . . . . 2i (h+1- i) 2i (h+1 -i) 2h-1  (h+1-(h-1)) ...  2h 1  2h(1) 1 2 h-1 h . . . . . . . . . . . . . . . . . . . . . .

The total cost

i = 0 ¥ å x i = 1 (1-x ) for x < 1 Basic Geometric Progression 1) ¥ å (-1)( -1) (1-x ) 2 = 1 (1-x ) 2 2) i x i-1 = Derivative of (1) i = 1 å i x i i = 1 ¥ 3) = x (1-x ) 2 Multiply (2) by x ¥ å 1/2 (1-(1/2)) 2 4) i (1/2) i = 2 = Substitute x=1/2 in (3) i = 1 Therefore å i 2 i i = 1 h+1 ¥ < = 2 We get the total cost S< 4*n = O ( n )

Improved Build the Heap:Method 2 make-heap //- done in constructor. 1. for i ¬ (last /2) downto 1 2. do siftDown( i ) The next foil explains that we can start siftDown at last/2, because we : need to siftDown only parents the rest of the nodes are leaves and leaves satisfy the heap property There are at most n/2 parents stored in bt[1..last/2]

Leaves and “parents” in a Complete Binary Tree We show: (n-1)/2  #parents  n/2, (n+1)/2  # leaves  n/2 C/P2 C/P2 C/P2 C/P1 C/_ C/_ C/_ C/_ C/_ Case A: every parent has 2 children #P = (n -1) / 2 #leave = n-(n-1)/2= (n+1)/2 Case B: 1 parent has only 1 child #P = n/2 #leave= n-n/2 = n/2

HEAPSORT(A) 1. fast-build-Maxheap(A) //max heap if in-place 2. for i = last downto 2 3. swap A[i] and A[1] 4. last = last –1 5. siftDown(1) Analysis: Lines 2-5 are O(nlg n) (line 1 is O(n))