Heapsort Algorithm : Design & Analysis [8]. In the last class … Mergesort Worst Case Analysis of Mergesort Lower Bound for Average Behavior Worst Case.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
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)
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.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
TECH Computer Science Problem: Sorting  arranging elements of set into order Algorithm design technique:  Divide and Conquer Solution:  Insertion Sort.
Chapter 6: Transform and Conquer
Using Divide and Conquer for Sorting
Insertion Sort Algorithm : Design & Analysis [5].
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.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
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:
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Heap Sort.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 7 Heapsort and priority queues Motivation Heaps Building and maintaining heaps.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Analysis of Algorithms CS 477/677
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
2IL50 Data Structures Spring 2015 Lecture 3: Heaps.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
Binary Heap.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
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.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Chapter 21 Binary Heap.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
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.
Lecture 8COMPSCI.220.FS.T Algorithm HeapSort J. W. J. Williams (1964): a special binary tree called heap to obtain an O(n log n) worst-case sorting.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Recursion Algorithm : Design & Analysis [3]. In the last class… Asymptotic growth rate The Sets ,  and  Complexity Class An Example: Maximum Subsequence.
Priority Queues (Heaps)
+ 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,
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University.
Asymptotic Behavior Algorithm : Design & Analysis [2]
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Search Trees (BST)
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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:
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
The Selection Algorithm : Design & Analysis [10].
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
Insertion Sort Algorithm : Design & Analysis [4].
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Binary Search Trees.
Description Given a linear collection of items x1, x2, x3,….,xn
ICS 353: Design and Analysis of Algorithms
Topic 5: Heap data structure heap sort Priority queue
Algorithm : Design & Analysis [5]
Presentation transcript:

Heapsort Algorithm : Design & Analysis [8]

In the last class … Mergesort Worst Case Analysis of Mergesort Lower Bound for Average Behavior Worst Case Average Behavior Lower Bound of Key Comparison Based Sorting Algorithm

Heapsort Heap Structure and Patial Order Tree Property The Strategy of Heapsort Keep the Partial Order Tree Property after the maximal element is removed Constructing the Heap Complexity of Heapsort Accelerated Heapsort

Heap: a Data Structure A binary tree T is a heap structure if: T is complete at least through depth h-1 All leaves are at depth h or h-1 All path to a leaf of depth h are to the left of all path to a leaf of depth h-1 Partial order tree property A tree T is a (maximizing) partial order tree if and only if the key at any node is greater than or equal to the keys at each of its children(if it has any). A heap is: A heap structure, satisfying Partial order tree property A heap is: A heap structure, satisfying Partial order tree property

Heap: Examples The maximal key is always with the root

Heapsort: the Strategy heapSort(E,n) Construct H from E, the set of n elements to be sorted; for (i=n;i  1;i--) curMax = getMax(H); deleteMax(H); E[i] = curMax deleteMax(H) Copy the rightmost element on the lowest level of H into K; Delete the rightmost element on the lowest level of H; fixHeap(H,K)

FixHeap: Keeping the Partial Order Tree Property Input: A nonempty binary tree H with a “ vacant ” root and its two subtrees in partial order. An element K to be inserted. Output: H with K inserted and satisfying the partial order tree property. Procesure: fixHeap(H,K) if (H is a leaf) insert K in root(H); else Set largerSubHeap; if (K.key  root(largerSubHeap).key) insert K in root(H) else insert root(largerSubHeap) in root(H); fixHeap(largerSubHeap, K); return Recursion One comparison: largerSubHeap is left- or right- Subtree(H), the one with larger key at its root. Special case: rightSubtree is empty “Vacant” moving down

fixHeap: an Example K=6

Worst Case Analysis for fixHeap 2 comparisons at most in one activation of the procedure The tree height decreases by one in the recursive call So, 2h comparisons are needed in the worst case, where h is the height of the tree Procesure: fixHeap(H,K) if (H is a leaf) insert K in root(H); else Set largerSubHeap; if (K.key  root(largerSubHeap).key) insert K in root(H) else insert root(largerSubHeap) in root(H); fixHeap(largerSubHeap, K); return Recursion One comparison: largerSubHeap is left- or right- Subtree(H), the one with larger key at its root. Special case: rightSubtree is empty “Vacant” moving down

Heap Construction Note: if left subtree and right subtree both satisfy the patial order tree property, then fixHeap(H,root(H)) gets the thing done. We begin from a Heap Structure H: void constructHeap(H) if (H is not a leaf) constructHeap(left subtree of H); constructHeap(right subtree of H); Element K=root(H); fixHeap(H,K) return leftright root Post-order Traversal

Specification Input: A heap structure H, not necessarily having the partial order tree property. Output: H with the same nodes rearranged to satisfy the partial order tree property. void constructHeap(H) if (H is not a leaf) constructHeap(left subtree of H); constructHeap(right subtree of H); Element K=root(H); fixHeap(H,K) return Correctness of constructHeap H is a leaf: base case, satisfied trivially. Preconditions hold respectively? Postcondition of constructHeap satisfied?

The Heap Is Constructed in Linear Time! The recursion equation: W(n)=W(n-r-1)+W(r)+2lg(n) A special case: H is a complete binary tree: The size N=2 d -1, and N/2<n  N, so W(n)  W(N), Note: W(N)=2W((N-1)/2)+2lg(N) The Master Theorem applys, with b=c=2, and the critical exponent E=1, f(N)=2lg(N) Note: When 0<  <1, this limit is equal to zero So, 2lg(N)  (N E-  ), case 1 satisfied, we have W(N)  (N), so, W(n)  (n) Number of nodes in right subheap Cost of fixHeap L’Hôpital’s Rule

Implementing Heap Using Array

Looking for the Children Quickly Starting from 1, not zero, then the ith level has 2 j-1 elements. and there are 2 j-1 -1 elements in the preceeding i-1 levels altogether. So, If E[i] is the kth element at level j, then i=(2 j-1 -1)+k, and the index of its left child (if existing) is i+(2 j-1 -k)+2(k-1)+1=2i For E[i]: Left subheap: E[2i] right subheap: E[2i+1] For E[i]: Left subheap: E[2i] right subheap: E[2i+1]

In-space Implementation of Heapsort E[1]: The largest key to be moved to E[n] E[n]  K: removed to be inserted Heap implemented as a array (initial) E[heapsize]  K: removed to be inserted E[1]: The largest key in current heap, to be moved to E[heapsize] Sorted portion Current heap: processed by fixHeap Current heap: processed by fixHeap

Heapsort: the Algorithm Input: E, an unsorted array with n(>0) elements, indexed from 1 Sorted E, in nondecreasing order Procedure: void heapSort(Element[] E, int n) int heapsize constructHeap(E,n,root) for (heapsize=n; heapsize  2; heapsize--;) Element curMax=E[1]; Element K=E[heapsize]; fixHeap(E,heapsize-1,1,K); E[heapsize]=cueMax; return; New Version

Worst Case Analysis of Heapsort We have: It has been known that: Recall that: So, W(n)  2nlgn +  (n), that is W(n)  (nlogn) Coefficient doubles that of mergeSort approximately

heapSort: the Right Choice For heapSort, W(n)   (nlgn) Of course, A(n)   (nlgn) More good news: heapSort is an in-space algorithm It’ll be more competitive if only the coefficient of the leading term can be decreased to 1

Number of Comparisons in fixHeap Procesure: fixHeap(H,K) if (H is a leaf) insert K in root(H); else Set largerSubHeap; if (K.key  root(largerSubHeap).key) insert K in root(H) else insert root(largerSubHeap) in root(H); fixHeap(largerSubHeap, K); return 2 comparisons are done in filtering down for one level.

A One-Comparison-per-Level Fixing Bubble-Up Heap Algorithm: void bubbleUpHeap(Element []E, int root, Element K, int vacant) if (vacant==root) E[vancant]=K; else int parent=vacant/2; if (K.key  E[parent].key) E[vacant]=K else E[vacant]=E[parent]; bubbleUpHeap(E,root,K,parent); Bubbling up from vacant through to the root, recursively

In fact, the “risk” is no more than “no improvement” Element(55) to be inserted bubling up: element vs. parent “Vacant” filtering down: left vs. right “Vacant” filtering down: left vs. right Risky FixHeap

If the element is smaller, filtering down half-half-way “Vacant” filtering down only half- way Improvement by Divide-and-Conquer The bubbling up will not beyond last vacStop

Depth Bounded Filtering Down int promote(Element [ ] E, int hStop, int vacant, int h) int vacStop; if (h  hStop) vacStop=vacant; else if (E[2*vacant].key  E[2*vacant+1].key) E[vacant]=E[2*vacant+1]; vacStop=promote(E, hStop, 2*vacan+1, h-1); else E[vacant]=E[2*vacant]; vacStop=promote(E, hStop, 2*vacant, h-1); return vacStop Depth Bound

fixHeap Using Divide-and-Conquer void fixHeapFast(Element [ ] E, Element K, int vacant, int h) //h=  lg(n+1)/2  in uppermost call if (h  1) Process heap of height 0 or 1; else int hStop=h/2; int vacStop=promote(E, hStop, vacant, h); int vacParent=vacStop/2; if (E[vacParent].key  K.key) E[vacStop]=E[vacParent]; bubbleUpHeap(E, vacant, K, vacParent); else fixHeapFast(E, K, vacStop, hStop)

Number of Comparisons in the Worst Case for Accelerated fixHeap Moving the vacant one level up or down need one comparison exactly in promote or bubbleUpHeap. In a cycle, t calls of promote and 1 call of bubbleUpHeap are excuted at most. So, the number of comparisons in promote and bubbleUpHeap calls are: At most, lg(h) checks for reverse direction are excecuted. So, the number of comparisons in a cycle is at most h+lg(h) So, for accelerated heapSort: W(n)=nlgn+  (nloglogn)

Recursion Equation of Accelerated heapSort The recurrence equation about h, which is about lg(n+1) Assuming T(h)  h, then:

For sorting a sequence of size n, n cycles of fixHeap are executed, so: n.(h+  lg(h+1)  ) For sorting a sequence of size n, n cycles of fixHeap are executed, so: n.(h+  lg(h+1)  ) Solving the Recurrence Equation by Recursive Tree T(n)T(n) T(  n/2  ) T(  n/4  )  h/2  +1  h/4  +1  h/8  +1 T(1)1+1  lg(h+1)  levels So, the total cost is: h+  lg(h+1)  So, the total cost is: h+  lg(h+1) 

Inductive Proof The recurrence equation for fixHeapFast: Proving the following solution by induction: T(h) = h+  lg(h+1)  According to the recurrence equation: T(h+1)=  (h+1)/2  +1+T(  (h+1)/2  ) Applying the inductive assumption to the last term: T(h+1)=  (h+1)/2  +1+  (h+1)/2  +  lg(  (h+1)/2  +1)  (It can be proved that for any positive integer:  lg(  (h)/2  +1)  +1=  lg(h+1)  ) The sum is h+1

Home Assignment pp