SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
Lecture 7:
  Heapsort / Priority Queues
         Steven Skiena

Department of Computer Science
 State University of New York
 Stony Brook, NY 11794–4400

http://www.cs.sunysb.edu/∼skiena
Problem of the Day
Take as input a sequence of 2n real numbers. Design an
O(n log n) algorithm that partitions the numbers into n pairs,
with the property that the partition minimizes the maximum
sum of a pair.
For example, say we are given the numbers (1,3,5,9).
The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and
((1,9),(3,5)). The pair sums for these partitions are (4,14),
(6,12), and (10,8). Thus the third partition has 10 as
its maximum sum, which is the minimum over the three
partitions.
Solution
Importance of Sorting
Why don’t CS profs ever stop talking about sorting?
1. Computers spend more time sorting than anything else,
   historically 25% on mainframes.
2. Sorting is the best studied problem in computer science,
   with a variety of different algorithms known.
3. Most of the interesting ideas we will encounter in the
   course can be taught in the context of sorting, such as
   divide-and-conquer, randomized algorithms, and lower
   bounds.
You should have seen most of the algorithms - we will
concentrate on the analysis.
Efficiency of Sorting
Sorting is important because that once a set of items is sorted,
many other problems become easy.
Further, using O(n log n) sorting algorithms leads naturally to
sub-quadratic algorithms for these problems.
                    n            n2 /4      n lg n
                   10               25         33
                  100           2,500         664
                1,000        250,000        9,965
               10,000     25,000,000 132,877
              100,000 2,500,000,000 1,660,960
Large-scale data processing would be impossible if sorting
took Ω(n2 ) time.
Application of Sorting: Searching
Binary search lets you test whether an item is in a dictionary
in O(lg n) time.
Search preprocessing is perhaps the single most important
application of sorting.
Application of Sorting: Closest pair
Given n numbers, find the pair which are closest to each other.
Once the numbers are sorted, the closest pair will be next to
each other in sorted order, so an O(n) linear scan completes
the job.
Application of Sorting: Element Uniqueness
Given a set of n items, are they all unique or are there any
duplicates?
Sort them and do a linear scan to check all adjacent pairs.
This is a special case of closest pair above.
Application of Sorting: Mode
Given a set of n items, which element occurs the largest
number of times? More generally, compute the frequency
distribution.
Sort them and do a linear scan to measure the length of all
adjacent runs.
The number of instances of k in a sorted array can be found in
O(log n) time by using binary search to look for the positions
of both k − and k + .
Application of Sorting: Median and Selection
What is the kth largest item in the set?
Once the keys are placed in sorted order in an array, the kth
largest can be found in constant time by simply looking in the
kth position of the array.
There is a linear time algorithm for this problem, but the idea
comes from partial sorting.
Application of Sorting: Convex hulls
Given n points in two dimensions, find the smallest area
polygon which contains them all.




The convex hull is like a rubber band stretched over the
points.
Convex hulls are the most important building block for more
sophisticated geometric algorithms.
Finding Convex Hulls
Once you have the points sorted by x-coordinate, they can be
inserted from left to right into the hull, since the rightmost
point is always on the boundary.




Sorting eliminates the need check whether points are inside
the current hull.
Adding a new point might cause others to be deleted.
Pragmatics of Sorting: Comparison Functions
Alphabetizing is the sorting of text strings.
Libraries have very complete and complicated rules con-
cerning the relative collating sequence of characters and
punctuation.
Is Skiena the same key as skiena?
Is Brown-Williams before or after Brown America before or
after Brown, John?
Explicitly controlling the order of keys is the job of the
comparison function we apply to each pair of elements.
This is how you resolve the question of increasing or
decreasing order.
Pragmatics of Sorting: Equal Elements
Elements with equal key values will all bunch together in any
total order, but sometimes the relative order among these keys
matters.
Sorting algorithms that always leave equal items in the same
relative order as in the original permutation are called stable.
Unfortunately very few fast algorithms are stable, but
Stability can be achieved by adding the initial position as a
secondary key.
Pragmatics of Sorting: Library Functions
Any reasonable programming language has a built-in sort
routine as a library function.
You are almost always better off using the system sort than
writing your own routine.
For example, the standard library for C contains the function
qsort for sorting:
void qsort(void *base, size t nel, size t width,
      int (*compare) (const void *, const void *));
Selection Sort
Selection sort scans throught the entire array, repeatedly
finding the smallest remaining element.
For i = 1 to n
A: Find the smallest of the first n − i + 1 items.
B:    Pull it out of the array and put it first.


Selection sort takes O(n(T (A) + T (B)) time.
The Data Structure Matters
Using arrays or unsorted linked lists as the data structure,
operation A takes O(n) time and operation B takes O(1), for
an O(n2 ) selection sort.
Using balanced search trees or heaps, both of these operations
can be done within O(lg n) time, for an O(n log n) selection
sort, balancing the work and achieving a better tradeoff.
Key question: “Can we use a different data structure?”
Heap Definition
A binary heap is defined to be a binary tree with a key in each
node such that:
1. All leaves are on, at most, two adjacent levels.
2. All leaves on the lowest level occur to the left, and all
   levels except the lowest one are completely filled.
3. The key in root is ≤ all its children, and the left and right
   subtrees are again binary heaps.
Conditions 1 and 2 specify shape of the tree, and condition 3
the labeling of the tree.
Binary Heaps
Heaps maintain a partial order on the set of elements which
is weaker than the sorted order (so it can be efficient to
maintain) yet stronger than random order (so the minimum
element can be quickly identified).
                                                                             1    1492
                                                 1492                        2    1783
                                                                             3    1776

                                 1783                          1776          4    1804
                                                                             5    1865
                                                                             6    1945
                   1804                   1865          1945          1963   7    1963
                                                                             8    1918
            1918          2001     1941                                      9    2001
                                                                             10   1941




A heap-labeled tree of important years (l), with the corre-
sponding implicit heap representation (r)
Array-Based Heaps
The most natural representation of this binary tree would
involve storing each key in a node with pointers to its two
children.
However, we can store a tree as an array of keys, usiing
the position of the keys to implicitly satisfy the role of the
pointers.
The left child of k sits in position 2k and the right child in
2k + 1.
The parent of k is in position n/2 .
Can we Implicitly Represent Any Binary Tree?
The implicit representation is only efficient if the tree is
sparse, meaning that the number of nodes n < 2h .
All missing internal nodes still take up space in our structure.
This is why we insist on heaps as being as balanced/full at
each level as possible.
The array-based representation is also not as flexible to
arbitrary modifications as a pointer-based tree.
Constructing Heaps
Heaps can be constructed incrementally, by inserting new
elements into the left-most open spot in the array.
If the new element is greater than its parent, swap their
positions and recur.
Since all but the last level is always filled, the height h of an
n element heap is bounded because:
                     h
                          2i = 2h+1 − 1 ≥ n
                    i=1

so h = lg n .
Doing n such insertions takes Θ(n log n), since the last n/2
insertions require O(log n) time each.
Heap Insertion

pq insert(priority queue *q, item type x)
{
      if (q->n >= PQ SIZE)
             printf(”Warning: overflow insert”);
      else {
             q->n = (q->n) + 1;
             q->q[ q->n ] = x;
             bubble up(q, q->n);
      }
}
Bubble Up

bubble up(priority queue *q, int p)
{
     if (pq parent(p) == -1) return;

      if (q->q[pq parent(p)] > q->q[p]) {
             pq swap(q,p,pq parent(p));
             bubble up(q, pq parent(p));
      }
}
Bubble Down or Heapify
Robert Floyd found a better way to build a heap, using a
merge procedure called heapify.
Given two heaps and a fresh element, they can be merged into
one by making the new one the root and bubbling down.
Build-heap(A)
      n = |A|
      For i = n/2 to 1 do
            Heapify(A,i)
Bubble Down Implementation

bubble down(priority queue *q, int p)
{
     int c; (* child index *)
     int i; (* counter *)
     int min index; (* index of lightest child *)

      c = pq young child(p);
      min index = p;

      for (i=0; i<=1; i++)
             if ((c+i) <= q->n) {
                    if (q->q[min index] > q->q[c+i]) min index = c+i;
             }

      if (min index ! = p) {
      pq swap(q,p,min index);
      bubble down(q, min index);
      }
}
Exact Analysis of Heapify
In fact, heapify performs better than O(n log n), because most
of the heaps we merge are extremely small.
It follows exactly the same analysis as with dynamic arrays
(Chapter 3).
In a full binary tree on n nodes, there are at most n/2h+1
nodes of height h, so the cost of building a heap is:
            lg n                            lg n
                         h+1
                   n/2         O(h) = O(n          h/2h)
            h=0                             h=0
Since this sum is not quite a geometric series, we can’t apply
the usual identity to get the sum. But it should be clear that
the series converges.
Proof of Convergence (*)
The identify for the sum of a geometric series is
                          ∞ k       1
                             x =
                         k=0      1−x
If we take the derivative of both sides, . . .
                      ∞               1
                         kxk−1 =
                     k=0          (1 − x)2
Multiplying both sides of the equation by x gives:
                       ∞             x
                          kxk =
                      k=0        (1 − x)2
Substituting x = 1/2 gives a sum of 2, so Build-heap uses at
most 2n comparisons and thus linear time.
Is our Analysis Tight?
“Are we doing a careful analysis? Might our algorithm be
faster than it seems?”
Doing at most x operations of at most y time each takes total
time O(xy).
However, if we overestimate too much, our bound may not be
as tight as it should be!
Heapsort
Heapify can be used to construct a heap, using the observation
that an isolated element forms a heap of size 1.
Heapsort(A)
     Build-heap(A)
     for i = n to 1 do
            swap(A[1],A[i])
            n=n−1
            Heapify(A,1)

Exchanging the maximum element with the last element
and calling heapify repeatedly gives an O(n lg n) sorting
algorithm. Why is it not O(n)?
Priority Queues
Priority queues are data structures which provide extra
flexibility over sorting.
This is important because jobs often enter a system at
arbitrary intervals. It is more cost-effective to insert a new
job into a priority queue than to re-sort everything on each
new arrival.
Priority Queue Operations
The basic priority queue supports three primary operations:
 • Insert(Q,x): Given an item x with key k, insert it into the
   priority queue Q.
 • Find-Minimum(Q) or Find-Maximum(Q): Return a
   pointer to the item whose key value is smaller (larger)
   than any other key in the priority queue Q.
 • Delete-Minimum(Q) or Delete-Maximum(Q) – Remove
   the item from the priority queue Q whose key is minimum
   (maximum).
Each of these operations can be easily supported using heaps
or balanced binary trees in O(log n).
Applications of Priority Queues: Dating
What data structure should be used to suggest who to ask out
next for a date?
It needs to support retrieval by desirability, not name.
Desirability changes (up or down), so you can re-insert the
max with the new score after each date.
New people you meet get inserted with your observed
desirability level.
There is never a reason to delete anyone until they arise to the
top.
Applications of Priority Queues: Discrete Event
Simulations
In simulations of airports, parking lots, and jai-alai – priority
queues can be used to maintain who goes next.
The stack and queue orders are just special cases of orderings.
In real life, certain people cut in line.
Applications of Priority Queues: Greedy
Algorithms
In greedy algorithms, we always pick the next thing which
locally maximizes our score. By placing all the things in a
priority queue and pulling them off in order, we can improve
performance over linear search or sorting, particularly if the
weights change.
War Story: sequential strips in triangulations

Contenu connexe

Tendances

A note on arithmetic progressions in sets of integers
A note on arithmetic progressions in sets of integersA note on arithmetic progressions in sets of integers
A note on arithmetic progressions in sets of integersLukas Nabergall
 
A Short Study of Galois Field
A Short Study of Galois FieldA Short Study of Galois Field
A Short Study of Galois FieldHazratali Naim
 
Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)Shiang-Yun Yang
 
Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)Shiang-Yun Yang
 
Elliptic Curves and Elliptic Curve Cryptography
Elliptic Curves and Elliptic Curve CryptographyElliptic Curves and Elliptic Curve Cryptography
Elliptic Curves and Elliptic Curve CryptographyMd. Al-Amin Khandaker Nipu
 
minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm sachin varun
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Abhimanyu Mishra
 
Elliptic Curve Cryptography: Arithmetic behind
Elliptic Curve Cryptography: Arithmetic behindElliptic Curve Cryptography: Arithmetic behind
Elliptic Curve Cryptography: Arithmetic behindAyan Sengupta
 
elliptic-curves-modern
elliptic-curves-modernelliptic-curves-modern
elliptic-curves-modernEric Seifert
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)Madhu Bala
 
Horn Concerto – AKSW Colloquium
Horn Concerto – AKSW ColloquiumHorn Concerto – AKSW Colloquium
Horn Concerto – AKSW ColloquiumTommaso Soru
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmAcad
 
Applications of datastructures
Applications of datastructuresApplications of datastructures
Applications of datastructuresRatsietsi Mokete
 

Tendances (20)

Galois theory
Galois theoryGalois theory
Galois theory
 
A note on arithmetic progressions in sets of integers
A note on arithmetic progressions in sets of integersA note on arithmetic progressions in sets of integers
A note on arithmetic progressions in sets of integers
 
A Short Study of Galois Field
A Short Study of Galois FieldA Short Study of Galois Field
A Short Study of Galois Field
 
Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)Aaex5 group2(中英夾雜)
Aaex5 group2(中英夾雜)
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)Aaex4 group2(中英夾雜)
Aaex4 group2(中英夾雜)
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
 
Galois theory andrew hubery
Galois theory andrew huberyGalois theory andrew hubery
Galois theory andrew hubery
 
Elliptic Curves and Elliptic Curve Cryptography
Elliptic Curves and Elliptic Curve CryptographyElliptic Curves and Elliptic Curve Cryptography
Elliptic Curves and Elliptic Curve Cryptography
 
Binsort
BinsortBinsort
Binsort
 
minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3
 
Elliptic Curve Cryptography: Arithmetic behind
Elliptic Curve Cryptography: Arithmetic behindElliptic Curve Cryptography: Arithmetic behind
Elliptic Curve Cryptography: Arithmetic behind
 
elliptic-curves-modern
elliptic-curves-modernelliptic-curves-modern
elliptic-curves-modern
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
Presentation1
Presentation1Presentation1
Presentation1
 
Horn Concerto – AKSW Colloquium
Horn Concerto – AKSW ColloquiumHorn Concerto – AKSW Colloquium
Horn Concerto – AKSW Colloquium
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithm
 
Applications of datastructures
Applications of datastructuresApplications of datastructures
Applications of datastructures
 

En vedette

Agile for Embedded & System Software Development : Presented by Priyank KS
Agile for Embedded & System Software Development : Presented by Priyank KS Agile for Embedded & System Software Development : Presented by Priyank KS
Agile for Embedded & System Software Development : Presented by Priyank KS oGuild .
 
iBeacons: Security and Privacy?
iBeacons: Security and Privacy?iBeacons: Security and Privacy?
iBeacons: Security and Privacy?Jim Fenton
 
Interfaces to ubiquitous computing
Interfaces to ubiquitous computingInterfaces to ubiquitous computing
Interfaces to ubiquitous computingswati sonawane
 
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...Paolo Sammicheli
 
Demystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and ToothpickDemystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and ToothpickDanny Preussler
 
Agile Methodology PPT
Agile Methodology PPTAgile Methodology PPT
Agile Methodology PPTMohit Kumar
 
HUG Ireland Event Presentation - In-Memory Databases
HUG Ireland Event Presentation - In-Memory DatabasesHUG Ireland Event Presentation - In-Memory Databases
HUG Ireland Event Presentation - In-Memory DatabasesJohn Mulhall
 
Privacy Concerns and Social Robots
Privacy Concerns and Social Robots Privacy Concerns and Social Robots
Privacy Concerns and Social Robots Christoph Lutz
 
ScrumGuides training: Agile Software Development With Scrum
ScrumGuides training: Agile Software Development With ScrumScrumGuides training: Agile Software Development With Scrum
ScrumGuides training: Agile Software Development With ScrumAlexey Krivitsky
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesFellowBuddy.com
 
09 Machine Learning - Introduction Support Vector Machines
09 Machine Learning - Introduction Support Vector Machines09 Machine Learning - Introduction Support Vector Machines
09 Machine Learning - Introduction Support Vector MachinesAndres Mendez-Vazquez
 
Final Year Project-Gesture Based Interaction and Image Processing
Final Year Project-Gesture Based Interaction and Image ProcessingFinal Year Project-Gesture Based Interaction and Image Processing
Final Year Project-Gesture Based Interaction and Image ProcessingSabnam Pandey, MBA
 
Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++Daniele Pallastrelli
 
In-Memory Database Performance on AWS M4 Instances
In-Memory Database Performance on AWS M4 InstancesIn-Memory Database Performance on AWS M4 Instances
In-Memory Database Performance on AWS M4 InstancesSingleStore
 
Machine learning support vector machines
Machine learning   support vector machinesMachine learning   support vector machines
Machine learning support vector machinesSjoerd Maessen
 
Sap technical deep dive in a column oriented in memory database
Sap technical deep dive in a column oriented in memory databaseSap technical deep dive in a column oriented in memory database
Sap technical deep dive in a column oriented in memory databaseAlexander Talac
 

En vedette (20)

Linklist
LinklistLinklist
Linklist
 
Agile for Embedded & System Software Development : Presented by Priyank KS
Agile for Embedded & System Software Development : Presented by Priyank KS Agile for Embedded & System Software Development : Presented by Priyank KS
Agile for Embedded & System Software Development : Presented by Priyank KS
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 
circular linked list
circular linked listcircular linked list
circular linked list
 
iBeacons: Security and Privacy?
iBeacons: Security and Privacy?iBeacons: Security and Privacy?
iBeacons: Security and Privacy?
 
Interfaces to ubiquitous computing
Interfaces to ubiquitous computingInterfaces to ubiquitous computing
Interfaces to ubiquitous computing
 
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...
Agile London: Industrial Agility, How to respond to the 4th Industrial Revolu...
 
Demystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and ToothpickDemystifying dependency Injection: Dagger and Toothpick
Demystifying dependency Injection: Dagger and Toothpick
 
Dependency Injection with Apex
Dependency Injection with ApexDependency Injection with Apex
Dependency Injection with Apex
 
Agile Methodology PPT
Agile Methodology PPTAgile Methodology PPT
Agile Methodology PPT
 
HUG Ireland Event Presentation - In-Memory Databases
HUG Ireland Event Presentation - In-Memory DatabasesHUG Ireland Event Presentation - In-Memory Databases
HUG Ireland Event Presentation - In-Memory Databases
 
Privacy Concerns and Social Robots
Privacy Concerns and Social Robots Privacy Concerns and Social Robots
Privacy Concerns and Social Robots
 
ScrumGuides training: Agile Software Development With Scrum
ScrumGuides training: Agile Software Development With ScrumScrumGuides training: Agile Software Development With Scrum
ScrumGuides training: Agile Software Development With Scrum
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
09 Machine Learning - Introduction Support Vector Machines
09 Machine Learning - Introduction Support Vector Machines09 Machine Learning - Introduction Support Vector Machines
09 Machine Learning - Introduction Support Vector Machines
 
Final Year Project-Gesture Based Interaction and Image Processing
Final Year Project-Gesture Based Interaction and Image ProcessingFinal Year Project-Gesture Based Interaction and Image Processing
Final Year Project-Gesture Based Interaction and Image Processing
 
Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++
 
In-Memory Database Performance on AWS M4 Instances
In-Memory Database Performance on AWS M4 InstancesIn-Memory Database Performance on AWS M4 Instances
In-Memory Database Performance on AWS M4 Instances
 
Machine learning support vector machines
Machine learning   support vector machinesMachine learning   support vector machines
Machine learning support vector machines
 
Sap technical deep dive in a column oriented in memory database
Sap technical deep dive in a column oriented in memory databaseSap technical deep dive in a column oriented in memory database
Sap technical deep dive in a column oriented in memory database
 

Similaire à Skiena algorithm 2007 lecture07 heapsort priority queues

Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
lecture 10
lecture 10lecture 10
lecture 10sajinsc
 
lecture 9
lecture 9lecture 9
lecture 9sajinsc
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxkassahungebrie
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 
Review session2
Review session2Review session2
Review session2NEEDY12345
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdfNash229987
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbounddespicable me
 
DSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsDSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsRESHAN FARAZ
 

Similaire à Skiena algorithm 2007 lecture07 heapsort priority queues (20)

Sorting
SortingSorting
Sorting
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
lecture 10
lecture 10lecture 10
lecture 10
 
Q
QQ
Q
 
searching.pdf
searching.pdfsearching.pdf
searching.pdf
 
lecture 9
lecture 9lecture 9
lecture 9
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
Algorithms Design Homework Help
Algorithms Design Homework HelpAlgorithms Design Homework Help
Algorithms Design Homework Help
 
Review session2
Review session2Review session2
Review session2
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
 
Sorting2
Sorting2Sorting2
Sorting2
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Algorithms Design Exam Help
Algorithms Design Exam HelpAlgorithms Design Exam Help
Algorithms Design Exam Help
 
220exercises2
220exercises2220exercises2
220exercises2
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
 
DSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsDSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) Questions
 

Plus de zukun

My lyn tutorial 2009
My lyn tutorial 2009My lyn tutorial 2009
My lyn tutorial 2009zukun
 
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVzukun
 
ETHZ CV2012: Information
ETHZ CV2012: InformationETHZ CV2012: Information
ETHZ CV2012: Informationzukun
 
Siwei lyu: natural image statistics
Siwei lyu: natural image statisticsSiwei lyu: natural image statistics
Siwei lyu: natural image statisticszukun
 
Lecture9 camera calibration
Lecture9 camera calibrationLecture9 camera calibration
Lecture9 camera calibrationzukun
 
Brunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionBrunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionzukun
 
Modern features-part-4-evaluation
Modern features-part-4-evaluationModern features-part-4-evaluation
Modern features-part-4-evaluationzukun
 
Modern features-part-3-software
Modern features-part-3-softwareModern features-part-3-software
Modern features-part-3-softwarezukun
 
Modern features-part-2-descriptors
Modern features-part-2-descriptorsModern features-part-2-descriptors
Modern features-part-2-descriptorszukun
 
Modern features-part-1-detectors
Modern features-part-1-detectorsModern features-part-1-detectors
Modern features-part-1-detectorszukun
 
Modern features-part-0-intro
Modern features-part-0-introModern features-part-0-intro
Modern features-part-0-introzukun
 
Lecture 02 internet video search
Lecture 02 internet video searchLecture 02 internet video search
Lecture 02 internet video searchzukun
 
Lecture 01 internet video search
Lecture 01 internet video searchLecture 01 internet video search
Lecture 01 internet video searchzukun
 
Lecture 03 internet video search
Lecture 03 internet video searchLecture 03 internet video search
Lecture 03 internet video searchzukun
 
Icml2012 tutorial representation_learning
Icml2012 tutorial representation_learningIcml2012 tutorial representation_learning
Icml2012 tutorial representation_learningzukun
 
Advances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionAdvances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionzukun
 
Gephi tutorial: quick start
Gephi tutorial: quick startGephi tutorial: quick start
Gephi tutorial: quick startzukun
 
EM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysisEM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysiszukun
 
Object recognition with pictorial structures
Object recognition with pictorial structuresObject recognition with pictorial structures
Object recognition with pictorial structureszukun
 
Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities zukun
 

Plus de zukun (20)

My lyn tutorial 2009
My lyn tutorial 2009My lyn tutorial 2009
My lyn tutorial 2009
 
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCVETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCV
 
ETHZ CV2012: Information
ETHZ CV2012: InformationETHZ CV2012: Information
ETHZ CV2012: Information
 
Siwei lyu: natural image statistics
Siwei lyu: natural image statisticsSiwei lyu: natural image statistics
Siwei lyu: natural image statistics
 
Lecture9 camera calibration
Lecture9 camera calibrationLecture9 camera calibration
Lecture9 camera calibration
 
Brunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer visionBrunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer vision
 
Modern features-part-4-evaluation
Modern features-part-4-evaluationModern features-part-4-evaluation
Modern features-part-4-evaluation
 
Modern features-part-3-software
Modern features-part-3-softwareModern features-part-3-software
Modern features-part-3-software
 
Modern features-part-2-descriptors
Modern features-part-2-descriptorsModern features-part-2-descriptors
Modern features-part-2-descriptors
 
Modern features-part-1-detectors
Modern features-part-1-detectorsModern features-part-1-detectors
Modern features-part-1-detectors
 
Modern features-part-0-intro
Modern features-part-0-introModern features-part-0-intro
Modern features-part-0-intro
 
Lecture 02 internet video search
Lecture 02 internet video searchLecture 02 internet video search
Lecture 02 internet video search
 
Lecture 01 internet video search
Lecture 01 internet video searchLecture 01 internet video search
Lecture 01 internet video search
 
Lecture 03 internet video search
Lecture 03 internet video searchLecture 03 internet video search
Lecture 03 internet video search
 
Icml2012 tutorial representation_learning
Icml2012 tutorial representation_learningIcml2012 tutorial representation_learning
Icml2012 tutorial representation_learning
 
Advances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer visionAdvances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer vision
 
Gephi tutorial: quick start
Gephi tutorial: quick startGephi tutorial: quick start
Gephi tutorial: quick start
 
EM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysisEM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysis
 
Object recognition with pictorial structures
Object recognition with pictorial structuresObject recognition with pictorial structures
Object recognition with pictorial structures
 
Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities
 

Dernier

Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 

Dernier (20)

Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 

Skiena algorithm 2007 lecture07 heapsort priority queues

  • 1. Lecture 7: Heapsort / Priority Queues Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794–4400 http://www.cs.sunysb.edu/∼skiena
  • 2. Problem of the Day Take as input a sequence of 2n real numbers. Design an O(n log n) algorithm that partitions the numbers into n pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus the third partition has 10 as its maximum sum, which is the minimum over the three partitions.
  • 4. Importance of Sorting Why don’t CS profs ever stop talking about sorting? 1. Computers spend more time sorting than anything else, historically 25% on mainframes. 2. Sorting is the best studied problem in computer science, with a variety of different algorithms known. 3. Most of the interesting ideas we will encounter in the course can be taught in the context of sorting, such as divide-and-conquer, randomized algorithms, and lower bounds. You should have seen most of the algorithms - we will concentrate on the analysis.
  • 5. Efficiency of Sorting Sorting is important because that once a set of items is sorted, many other problems become easy. Further, using O(n log n) sorting algorithms leads naturally to sub-quadratic algorithms for these problems. n n2 /4 n lg n 10 25 33 100 2,500 664 1,000 250,000 9,965 10,000 25,000,000 132,877 100,000 2,500,000,000 1,660,960 Large-scale data processing would be impossible if sorting took Ω(n2 ) time.
  • 6. Application of Sorting: Searching Binary search lets you test whether an item is in a dictionary in O(lg n) time. Search preprocessing is perhaps the single most important application of sorting.
  • 7. Application of Sorting: Closest pair Given n numbers, find the pair which are closest to each other. Once the numbers are sorted, the closest pair will be next to each other in sorted order, so an O(n) linear scan completes the job.
  • 8. Application of Sorting: Element Uniqueness Given a set of n items, are they all unique or are there any duplicates? Sort them and do a linear scan to check all adjacent pairs. This is a special case of closest pair above.
  • 9. Application of Sorting: Mode Given a set of n items, which element occurs the largest number of times? More generally, compute the frequency distribution. Sort them and do a linear scan to measure the length of all adjacent runs. The number of instances of k in a sorted array can be found in O(log n) time by using binary search to look for the positions of both k − and k + .
  • 10. Application of Sorting: Median and Selection What is the kth largest item in the set? Once the keys are placed in sorted order in an array, the kth largest can be found in constant time by simply looking in the kth position of the array. There is a linear time algorithm for this problem, but the idea comes from partial sorting.
  • 11. Application of Sorting: Convex hulls Given n points in two dimensions, find the smallest area polygon which contains them all. The convex hull is like a rubber band stretched over the points. Convex hulls are the most important building block for more sophisticated geometric algorithms.
  • 12. Finding Convex Hulls Once you have the points sorted by x-coordinate, they can be inserted from left to right into the hull, since the rightmost point is always on the boundary. Sorting eliminates the need check whether points are inside the current hull. Adding a new point might cause others to be deleted.
  • 13. Pragmatics of Sorting: Comparison Functions Alphabetizing is the sorting of text strings. Libraries have very complete and complicated rules con- cerning the relative collating sequence of characters and punctuation. Is Skiena the same key as skiena? Is Brown-Williams before or after Brown America before or after Brown, John? Explicitly controlling the order of keys is the job of the comparison function we apply to each pair of elements. This is how you resolve the question of increasing or decreasing order.
  • 14. Pragmatics of Sorting: Equal Elements Elements with equal key values will all bunch together in any total order, but sometimes the relative order among these keys matters. Sorting algorithms that always leave equal items in the same relative order as in the original permutation are called stable. Unfortunately very few fast algorithms are stable, but Stability can be achieved by adding the initial position as a secondary key.
  • 15. Pragmatics of Sorting: Library Functions Any reasonable programming language has a built-in sort routine as a library function. You are almost always better off using the system sort than writing your own routine. For example, the standard library for C contains the function qsort for sorting: void qsort(void *base, size t nel, size t width, int (*compare) (const void *, const void *));
  • 16. Selection Sort Selection sort scans throught the entire array, repeatedly finding the smallest remaining element. For i = 1 to n A: Find the smallest of the first n − i + 1 items. B: Pull it out of the array and put it first. Selection sort takes O(n(T (A) + T (B)) time.
  • 17. The Data Structure Matters Using arrays or unsorted linked lists as the data structure, operation A takes O(n) time and operation B takes O(1), for an O(n2 ) selection sort. Using balanced search trees or heaps, both of these operations can be done within O(lg n) time, for an O(n log n) selection sort, balancing the work and achieving a better tradeoff. Key question: “Can we use a different data structure?”
  • 18. Heap Definition A binary heap is defined to be a binary tree with a key in each node such that: 1. All leaves are on, at most, two adjacent levels. 2. All leaves on the lowest level occur to the left, and all levels except the lowest one are completely filled. 3. The key in root is ≤ all its children, and the left and right subtrees are again binary heaps. Conditions 1 and 2 specify shape of the tree, and condition 3 the labeling of the tree.
  • 19. Binary Heaps Heaps maintain a partial order on the set of elements which is weaker than the sorted order (so it can be efficient to maintain) yet stronger than random order (so the minimum element can be quickly identified). 1 1492 1492 2 1783 3 1776 1783 1776 4 1804 5 1865 6 1945 1804 1865 1945 1963 7 1963 8 1918 1918 2001 1941 9 2001 10 1941 A heap-labeled tree of important years (l), with the corre- sponding implicit heap representation (r)
  • 20. Array-Based Heaps The most natural representation of this binary tree would involve storing each key in a node with pointers to its two children. However, we can store a tree as an array of keys, usiing the position of the keys to implicitly satisfy the role of the pointers. The left child of k sits in position 2k and the right child in 2k + 1. The parent of k is in position n/2 .
  • 21. Can we Implicitly Represent Any Binary Tree? The implicit representation is only efficient if the tree is sparse, meaning that the number of nodes n < 2h . All missing internal nodes still take up space in our structure. This is why we insist on heaps as being as balanced/full at each level as possible. The array-based representation is also not as flexible to arbitrary modifications as a pointer-based tree.
  • 22. Constructing Heaps Heaps can be constructed incrementally, by inserting new elements into the left-most open spot in the array. If the new element is greater than its parent, swap their positions and recur. Since all but the last level is always filled, the height h of an n element heap is bounded because: h 2i = 2h+1 − 1 ≥ n i=1 so h = lg n . Doing n such insertions takes Θ(n log n), since the last n/2 insertions require O(log n) time each.
  • 23. Heap Insertion pq insert(priority queue *q, item type x) { if (q->n >= PQ SIZE) printf(”Warning: overflow insert”); else { q->n = (q->n) + 1; q->q[ q->n ] = x; bubble up(q, q->n); } }
  • 24. Bubble Up bubble up(priority queue *q, int p) { if (pq parent(p) == -1) return; if (q->q[pq parent(p)] > q->q[p]) { pq swap(q,p,pq parent(p)); bubble up(q, pq parent(p)); } }
  • 25. Bubble Down or Heapify Robert Floyd found a better way to build a heap, using a merge procedure called heapify. Given two heaps and a fresh element, they can be merged into one by making the new one the root and bubbling down. Build-heap(A) n = |A| For i = n/2 to 1 do Heapify(A,i)
  • 26. Bubble Down Implementation bubble down(priority queue *q, int p) { int c; (* child index *) int i; (* counter *) int min index; (* index of lightest child *) c = pq young child(p); min index = p; for (i=0; i<=1; i++) if ((c+i) <= q->n) { if (q->q[min index] > q->q[c+i]) min index = c+i; } if (min index ! = p) { pq swap(q,p,min index); bubble down(q, min index); } }
  • 27. Exact Analysis of Heapify In fact, heapify performs better than O(n log n), because most of the heaps we merge are extremely small. It follows exactly the same analysis as with dynamic arrays (Chapter 3). In a full binary tree on n nodes, there are at most n/2h+1 nodes of height h, so the cost of building a heap is: lg n lg n h+1 n/2 O(h) = O(n h/2h) h=0 h=0 Since this sum is not quite a geometric series, we can’t apply the usual identity to get the sum. But it should be clear that the series converges.
  • 28. Proof of Convergence (*) The identify for the sum of a geometric series is ∞ k 1 x = k=0 1−x If we take the derivative of both sides, . . . ∞ 1 kxk−1 = k=0 (1 − x)2 Multiplying both sides of the equation by x gives: ∞ x kxk = k=0 (1 − x)2 Substituting x = 1/2 gives a sum of 2, so Build-heap uses at most 2n comparisons and thus linear time.
  • 29. Is our Analysis Tight? “Are we doing a careful analysis? Might our algorithm be faster than it seems?” Doing at most x operations of at most y time each takes total time O(xy). However, if we overestimate too much, our bound may not be as tight as it should be!
  • 30. Heapsort Heapify can be used to construct a heap, using the observation that an isolated element forms a heap of size 1. Heapsort(A) Build-heap(A) for i = n to 1 do swap(A[1],A[i]) n=n−1 Heapify(A,1) Exchanging the maximum element with the last element and calling heapify repeatedly gives an O(n lg n) sorting algorithm. Why is it not O(n)?
  • 31. Priority Queues Priority queues are data structures which provide extra flexibility over sorting. This is important because jobs often enter a system at arbitrary intervals. It is more cost-effective to insert a new job into a priority queue than to re-sort everything on each new arrival.
  • 32. Priority Queue Operations The basic priority queue supports three primary operations: • Insert(Q,x): Given an item x with key k, insert it into the priority queue Q. • Find-Minimum(Q) or Find-Maximum(Q): Return a pointer to the item whose key value is smaller (larger) than any other key in the priority queue Q. • Delete-Minimum(Q) or Delete-Maximum(Q) – Remove the item from the priority queue Q whose key is minimum (maximum). Each of these operations can be easily supported using heaps or balanced binary trees in O(log n).
  • 33. Applications of Priority Queues: Dating What data structure should be used to suggest who to ask out next for a date? It needs to support retrieval by desirability, not name. Desirability changes (up or down), so you can re-insert the max with the new score after each date. New people you meet get inserted with your observed desirability level. There is never a reason to delete anyone until they arise to the top.
  • 34. Applications of Priority Queues: Discrete Event Simulations In simulations of airports, parking lots, and jai-alai – priority queues can be used to maintain who goes next. The stack and queue orders are just special cases of orderings. In real life, certain people cut in line.
  • 35. Applications of Priority Queues: Greedy Algorithms In greedy algorithms, we always pick the next thing which locally maximizes our score. By placing all the things in a priority queue and pulling them off in order, we can improve performance over linear search or sorting, particularly if the weights change. War Story: sequential strips in triangulations