5 views

Uploaded by anon_112800657

aaa

- Big o Cheat Sheet
- Sorting
- Algorithms Data
- Assignment 02
- How to Use Hierachical Tree Control
- Geometric Search Coursenotes
- GATE Questions
- hw05
- Arrays in PHP
- UNIT v Programs
- IDE.pdf
- Thought Work
- 36-H-Graph Traversal Technique for Grid Based Robot Navigation
- DAA-2011-Syll
- Scalable VANET Simulations With NS3
- ScottKnott
- Pig Latin Users Guide
- Binomial Sli
- CN2
- 3rd_4th_Scheme__Syllabus_CSE

You are on page 1of 25

2

5

9

Heaps

6

7

stores a collection of

entries

Typically, an entry is a pair

(key, value), where the

key indicates the priority

Main methods of the

Priority Queue ADT

e

removeMin()

removes the entry with

smallest key

Heaps

Additional methods

Applications:

Standby flyers

Auctions

Stock market

implementations of PQ

PQ Sorting

on the priority queue

implementation:

series of insert operations

Remove the elements in

sorted order with a series

of removeMin operations

selection sort: O(n2) time

Sorted sequence using

insertion sort: O(n2) time

Can we do better?

Algorithm PQ-Sort(S, C)

Input sequence S, comparator C

for the elements of S

Output sequence S sorted in

increasing order according to C

P priority queue with

comparator C

while S.empty ()

e S.front(); S.eraseFront()

P.insert (e, )

while P.empty()

e P.removeMin()

S.insertBack(e)

Heaps

Two Paradigms of PQ

Sorting

PQ via selection

sort

Heaps

PQ via insertion

sort

Heaps

keys at its nodes and

satisfying the following

properties:

node v other than the root,

key(v) key(parent(v))

Min heap

the height of the heap

nodes of depth i

at depth h, the internal nodes

are to the left of the external

nodes

Heaps

heap is the rightmost

node of maximum

depth

2

5

9

6

7

last node

5

Height of a Heap

Proof: (we apply the complete binary tree property)

Let h be the height of a heap storing n keys

Since there are 2i keys at depth i 0, , h 1 and at least

one key at depth h, we have n 1 2 4 2h1 1

Thus, n 2h , i.e., h log n

depth keys

0

h1

2h1

Heaps

We store a (key, element) item at each internal

node

We keep track of the position of the last node

(2, Sue)

(5, Pat)

(9, Jeff)

(6, Mark)

(7, Anna)

Heaps

Method insertItem of

the priority queue ADT

corresponds to the

insertion of a key k to

the heap

The insertion algorithm

consists of three steps

(the new last node)

Store k at z

Restore the heap-order

property (discussed

next)

Heaps

2

5

9

z

7

insertion node

2

5

9

6

7

Upheap

may be violated

Algorithm upheap restores the heap-order property by

swapping k along an upward path from the insertion node

Upheap terminates when the key k reaches the root or a

node whose parent has a key smaller than or equal to k

Since a heap has height O(log n), upheap runs in O(log n) time

2

5

9

1

7

Heaps

2

7

Newly added node

Up-heap

bubbling

Heaps

10

Method removeMin of

the priority queue ADT

corresponds to the

removal of the root key

from the heap

The removal algorithm

consists of three steps

the key of the last node w

Remove w

Restore the heap-order

property (discussed next)

2

5

9

6

7

w

last node

7

2010 Goodrich, Tamassia

Heaps

11

Downheap

After replacing the root key with the key k of the last node, the

heap-order property may be violated

Algorithm downheap restores the heap-order property by

swapping key k along a downward path from the root

Upheap terminates when key k reaches a leaf or a node whose

children have keys greater than or equal to k

Since a heap has height O(log n), downheap runs in O(log n) time

7

5

5

6

Heaps

12

to top

Down-heap

bubbling

Heaps

13

O(log n) nodes

If a left child is reached, go to the right child

Go down left until a leaf is reached

Heaps

14

Heap-Sort

Consider a priority

queue with n items

implemented by a

heap

methods insert and

removeMin take O(log

n) time

methods size, empty,

and min take time

O(1) time

Heap-sort

Heaps

Sort a sequence of n

elements in O(n log n)

time using a heap-based

PQ

Much faster than

quadratic sorting

algorithms, such as

insertion-sort and

selection-sort

15

Vector-based Heap

Implementation

keys by means of a vector of

length n 1

For the node at index i

the right child is at index 2i 1

explicitly stored

The cell of at index 0 is not used

Operation insert corresponds to

inserting at index n1

Operation removeMin

corresponds to removing at

index 0

Yields in-place heap-sort

Heaps

2

5

16

In heap

Stage 1:

Heap

construction

Sorted

In heap

Sorted

Sorted

In heap

Sorted

In heap

Stage 2:

output

Heaps

17

and a key k

Create a new heap

with the root node

storing k and the

two heaps as

subtrees

Perform downheap

to restore the heaporder property

3

8

Heaps

7

3

8

2

5

2

3

8

4

5

6

18

Bottom-up Heap

Construction

Goal: Construct a

heap of n keys using

a bottom-up method

with log n phases

In phase i, pairs of

heaps with 2i 1 keys

are merged into

heaps with 2i11 keys

2i 1

2i 1

2i11

Heaps

19

Example

16

15

25

16

12

5

15

23

11

12

Heaps

20

27

7

23

20

20

Example (contd.)

25

16

5

15

15

16

11

12

4

25

27

9

23

6

12

11

Heaps

20

23

9

27

20

21

Example (contd.)

7

15

16

4

25

6

12

11

23

9

4

5

25

20

15

16

27

8

12

11

Heaps

23

9

27

20

22

Example (end)

10

4

15

16

5

25

8

12

11

23

9

27

20

4

5

15

16

7

25

10

8

12

11

Heaps

23

9

27

20

23

path that goes first right and then repeatedly goes left until the

bottom of the heap (this path may differ from the actual

downheap path)

Since each node is traversed by at most two proxy paths, the

total number of nodes of the proxy paths is O(n)

Thus, bottom-up heap construction runs in O(n) time

Bottom-up heap construction is faster than n successive

insertions and speeds up the first phase of heap-sort

Heaps

24

d=0

d=1

d=2

d=h-1

d=h

T 2 h 1 2 h 1 2 2 h 2 3 20 h 1

2T 2 h 1 1 2 h 2 2 h 1 3 21 h 1

2T T 2 h 1 2 h 2 h 1 21 h 1

T 2 h 2 1 h 1 2 2 h 1 1 h 1 2n 1 log 2 n 1

T O n

Heaps

25

- Big o Cheat SheetUploaded byXinyu Hu
- SortingUploaded byGio Rabara
- Algorithms DataUploaded bynulatata
- Assignment 02Uploaded byShariq Masood
- How to Use Hierachical Tree ControlUploaded byMuhammad Shoaib Mahmood
- Geometric Search CoursenotesUploaded bybsitler
- GATE QuestionsUploaded byDebayan Biswas
- hw05Uploaded byNico Scheinost
- Arrays in PHPUploaded byNaveen Jamdagani
- UNIT v ProgramsUploaded byAnonymous MQjBLReGmX
- IDE.pdfUploaded byIvan Bancev
- Thought WorkUploaded byBinit Agarwal
- 36-H-Graph Traversal Technique for Grid Based Robot NavigationUploaded byNaveed Malik
- DAA-2011-SyllUploaded byShashank Kumar Mittal
- Scalable VANET Simulations With NS3Uploaded byJayavel Janardhanan
- ScottKnottUploaded byMárcio Sousa Rocha
- Pig Latin Users GuideUploaded bymahdimax
- Binomial SliUploaded byPrakherGupta
- CN2Uploaded byАлександр Гринчук
- 3rd_4th_Scheme__Syllabus_CSEUploaded byMehul Agarwal
- module aUploaded byJane Michelle Eman
- sample4Uploaded byAjeya Shetti
- algo.pptUploaded byraj
- Complexity of Metareasoning.ijcai03Uploaded byJonathan Mad
- Elementary Data Structures - Stacks, Queues, & Lists, Amortized analysis Trees.pptUploaded byedwin5203839
- Programming League Round2 Set1Uploaded byShanky Nagpal
- 6_BBUploaded byMiskir Be
- Review - Improve Performance of Efficient Aggregation Scheduling In Multihop Wireless Sensor NetworkUploaded byEditor IJRITCC
- [2011] Data StructuresUploaded byMd Isfaque
- PEGA 6.2 Course ContentUploaded byDinesh Reddy G

- NAGDAR FEASIBILITY REPORT.pdfUploaded byasim469
- Ct Objective (1)Uploaded bySainath Vanakuri
- Derivation of Formula for Volume of a Frustum of Pyramid_cone _ Derivation of Formulas ReviewUploaded byimrancenakk
- lab 2Uploaded byapi-383698554
- Columns,Rankine Theory,Energy MethodUploaded bybasit_ehsan
- PHD_2013Uploaded byRajiv Kumar
- 1021 Brochure_operating TemperatureUploaded byNisa_nishe
- SeminarUploaded byAnvay Pandit
- CARGAS CONCENTRADAS EM LAJES.pdfUploaded byRicardo Martins
- Lista 01 (Rev 01)Uploaded byNubia Bergamini
- various stages in stress strain curveUploaded byganesh_the_aviator
- Power - June 2011Uploaded byegman7305
- Chapter 4Uploaded bysadaram3434
- 8. EDS XRF OES Characterisation.pdfUploaded byKevin Abram
- Balancing Chemical Equations.pdfUploaded byLeroyJones
- 221127823 Electric Power Generation Transmission and DistriElectric Power Generation Transmission and DistributionbutionUploaded byamrit403
- Homework 4Uploaded byscribdrnv
- D 3794 - 00 _RDM3OTQ_Uploaded byGuy Peled
- International System of UnitsUploaded byjbahalkeh
- sentence definition memorandumUploaded byapi-242284975
- 11 Chemistry NcertSolutions Chapter 13 ExercisesUploaded byAnonymous EafYD0
- Fibre Optic Cable AccessoriesUploaded byIdajet Projko
- 1174rr320801 Mass Transfer Operations IIUploaded bysobichem
- What-Will-MV-Switchgear-Look-Like-in-the-Future.pdfUploaded bySofyan Shah
- Lab3_FI204A_2018-1.docxUploaded byJefry Farro Levano
- Wire Ropes01Uploaded byRakesh Roushan
- Comsol TutorialUploaded bydringcasu
- Eugen OlUploaded byShimaKenthiz
- Rapport fra UL International GermanyUploaded byIngenioeren
- 1276Uploaded byAditya Ace Pumpist