Attribution Non-Commercial (BY-NC)

5 views

Attribution Non-Commercial (BY-NC)

- 8-Queen problem
- Fibonacci Heaps
- Lecture 03
- Realtime Task Scheduling 2
- Searching
- Ada Lab Progs
- jeremia lalputara
- Converted
- Data Structures
- Hash Tables
- ie623-note3
- Study Guide
- Algorithm Ch6 Heapsort
- IJA new tree data structure to extract frequent pattern from transactional databaseERTV1IS3081
- daa file
- Aco
- ghd
- undertheseacode
- Genetic Algorithm Maze Solving Program
- gradiente reducido

You are on page 1of 13

(or key-element pairs) at its internal nodes and that satisfies

• Heaps the additional property:

• Properties REMEMBER: key(parent) ≤ key(child)

• Deletion, Insertion, Construction complete binary tree 4

all levels are full, except the

• Implementation of the Heap last one, which is left-filled 5 6

15 9 7 20

using a Heap

• An application: HeapSort 16 25 14 12 11 8

1 2

Max-heap

We store the keys in the internal nodes only

key(parent) ≥ key(child)

5

40

15 9

35 26

16

15 19 17 20

1 13 14 12 11 8

3 4

1

Height of a Heap Notice that ….

• Theorem: A heap storing n keys has height O (log n) • We store a (key, element) item at each internal

Proof: node

– Let h be the height of a heap storing n keys

removeMin():

– Since there are 2i keys at depth i = 0, … , h - 2 and at least one Remove the root

key at depth h - 1, we have n ≥ 1 + 2 + 4 + … + 2h-2 + 1

– Thus, n ≥ 2h-1 , i.e., h ≤ log n + 1 Re-arrange the heap!

depth keys

0 1 (2, Sue)

h-2 2h-2

(9, Jeff) (7, Anna)

h-1 at least 1

h

5 6

Downheap

Removal From a Heap

top key leaves a hole

heap

with the last key in

the heap

7

• Downheap compares the parent with the smallest 8

… child. If the child is smaller, it switches the two.

2

Downheap Continues Downheap Continues

9 10

keys of both its children or the bottom of the heap is

reached. 11 12

3

Heap Insertion

Upheap

• Swap parent-child keys out of order

13 14

15 of its parent or the top of the heap is reached 16

4

Heap Construction Bottom-up Heap

Construction

We could insert the Items one at the time with

a sequence of Heap Insertions:

n • We can construct a heap

Σ log k = O(n log n) storing n given keys using

k=1

a bottom-up construction

17 18

2

Idea: Recursively re-arrange each sub- 4 5

1 9 6 3

6th 5th 3↔6 leaves anymore here

begin here

4th 3rd 2nd 1st

begin here

2 2 2

4 5 4 5 4 10

7 6 10 8 9 6 10 8 9 6 5 8

HEAP 1 9 3 1 7 3 1 7 3

HEAP

7↔9 5 ↔ 10

HEAP

19 20

5

Example 1

2 Example 1

2

4 10

! !

9 10

9 6 5 8

10

7 6 5 8

1 7 3 2

Finally: 2 ↔ 10

9 1 4 3

9 8

5

4↔9

4

7

This is not a heap ! 4↔7 10

1 7

1 4

2↔8

2

5 8

10

2

! ! 9 8

9 10

7 6 5 2

7 6 5 8

21 1 4 3

22

1 4 3

Example 2 (min-heap) 20,23,7,6,12,4,15,16,27,11,5,25,8,7,10]

[20,23,7,6,12,4,15,16,27,11,5,25,8,7,10]

25 5 11 27

16 15 4 12 6 7 23 20 16 15 4 12 6 9 23 20

25 5 11 27 15 4 6 23

16 15 4 12 6 7 23 20 16 25 5 12 11 9 27 20

23 24

6

Example 2 Example 2

20,23,7,6,12,4,15,16,27,11,5,25,8,7,10]

20,23,7,6,12,4,15,16,27,11,5,25,8,7,10]

10

7 8 4 6

15 4 6 23 15 5 8 23

16 25 5 12 11 9 27 20 16 25 7 12 11 9 27 20

4 6 5 6

15 5 8 23 15 7 8 23

16 25 7 12 11 9 27 20 16 25 10 12 11 9 27 20

25 26

Analysis of Heap Construction

(let us not consider the dummy leaves)

level 0

Number of swaps Number of swaps 1

h=4

3 swaps 2

level 0

at most

2 swaps level 1 h

At level i the number of swaps is

4 5

1 swap 7 3 10 8

level 2

≤ h–i for each node

0 swaps level 3

1 9 6

h

h is the max level

Total: ≤ Σ(h – i)·2i

level i -------- h-i swaps i=0

27 28

7

h

≤ 2h+1

j=1

h h h

j=0 Where h is O(log n)

i=0 j=0

Consider Σ j 2-j :

Σ j 2-j = 1/2 + 2 1/4 + 3 1/8 + 4 1/16 + … So, the number of swaps is ≤ O(n)

= 1/2 + 1/4 + 1/8 + 1/16 + … <= 1

+ 1/4 + 1/8 + 1/16 + … <= 1/2

+ + 1/8 + 1/16 + … <= 1/4

Σ j 2-j <= 2

29 30

So 2h Σ j 2-j <= 2. 2h = 2 n O(n)

Implementing a Heap

with an Array Example

A heap can be nicely represented by an array list 1

H

(array-based), 1

2

where the node at rank i has 2 3 D I

3

4 5 6 4

7 B

5

E

6

L

7

O

A C F G H N

and i

2i 2i+1

1 2 3 4 5 6 7 8 9 10 11 12 13

1 2 3 4 5 6 7 8 H D I B E L O A C F G H N

8

Reminder ….. Implementation of a Priority

Left child of T[i] T[2i] if 2i ≤ n Queue with a Heap

Right child of

T[2i+1] if 2i + 1 ≤ n

T[i]

Parent of T[i] T[i div 2] if i>1

The Root T[1] if T≠0

Leaf? T[i] TRUE if 2i > n

1 n = 11

2 3

I

4 5 6 7

8 9 10 11

33 34

Algorithm PriorityQueueSort(S, P):

Input: A sequence S storing n elements, on which a

O(log n) total order relation is defined, and a Priority

O(log 1) Queue P that compares keys with the same relation

Output: The Sequence S sorted by the total

O(log n) order relation

while ¬ S.isEmpty() do

e ← S.removeFirst() Build Heap

P.insertItem(e, e)

while ¬ P.isEmpty() do

(remove root + downheap) e ← P.removeMin() Remove from heap

S.insertLast(e)

35 36

9

Application: Sorting

Heap Sort When there are i nodes left in the PQ: log i

n

TOT = Σ log i

i=1

= O(n log n)

remove root O(1)

n re-arrange O(log n)

times

remove root O(1)

L M

37 38

L

HeapSort in Place

With a Min-heap: With a Max-Heap:

39 40

10

Another type of Heap … removeMin()

smaller (greater) then or equal to the values of their 5

descendants 65 80

25 37 8 15

5 57 36 45 59 20 14 32 18

65 80 28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17

25 37 8 15

57 36 45 59 20 14 32 18

28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17

41 42

removeMin() removeMin()

smallest 17 8

65 80 65 80

25 37 8 15 25 37 17 15

57 36 45 59 20 14 32 18 57 36 45 59 20 14 32 18

28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 28 30 34 27 39 38 45 50 15 12 13 10 30 31 16

smallest

43 44

11

5

removeMin() Insert(x)

65 80

25 37 8 15

57 36 45 59 20 14 32 18

28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17

8

x

65 80 If in Max-level:

25 37 10 15 If parent is greater, swap and BubbleUpMin

If parent is smaller, BubbleUpMax

57 36 45 59 20 14 32 18

BubbleUpMin

28 30 34 27 39 38 45 50 15 12 13 17 30 31 16

If grandparent is greater, swap

BubbleUpMin(grandparent)

BubbleUpMax

If grandparent is smaller, swap

Check parent BubbleUpMax(grandparent)

45 46

5 5

Insert(13) Insert(13)

65 80 65 80

25 37 8 15 25 37 8 15

57 36 45 59 20 14 32 18 57 36 45 59 20 14 32 18

28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17 13 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17

13 28

If in Max-level:

If parent is greater, swap and BubbleUpMin

If parent is smaller, BubbleUpMax

BubbleUpMin

If grandparent is greater, swap

BubbleUpMin(grandparent)

47 48

12

5 5

Insert(13) Insert(61)

65 80 65 80

13 37 8 15 25 37 8 15

57 36 45 59 20 14 32 18 57 36 45 59 20 14 32 18

25 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17 28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17

28 61

If in Max-level:

If parent is greater, swap and BubbleUpMin

If parent is smaller, BubbleUpMax

BubbleUpMax

If grandparent is smaller, swap

BubbleUpMax(grandparent)

49 50

5

Insert(61)

65 80

25 37 8 15

61 36 45 59 20 14 32 18

28 30 34 27 39 38 45 50 15 12 13 10 30 31 16 17

What is the cost of

57

If in Max-level: removeMin() AND removeMax()

If parent is greater, swap and BubbleUpMin

If parent is smaller, BubbleUpMax With this heap ?

BubbleUpMax

If grandparent is smaller, swap

BubbleUpMax(grandparent)

51 52

13

- 8-Queen problemUploaded byPratik Gandhi
- Fibonacci HeapsUploaded byapi-19981779
- Lecture 03Uploaded bysamoray1xp
- Realtime Task Scheduling 2Uploaded byPuspala Manojkumar
- SearchingUploaded byVikrant Sahu
- jeremia lalputaraUploaded byChin ObZen Munsong
- ConvertedUploaded byrajatdoshi1
- Hash TablesUploaded byfofoa16
- Ada Lab ProgsUploaded bykanth10
- Data StructuresUploaded byNuha Noor
- ie623-note3Uploaded byayush1313
- Study GuideUploaded byBrian Pace
- Algorithm Ch6 HeapsortUploaded bymao
- IJA new tree data structure to extract frequent pattern from transactional databaseERTV1IS3081Uploaded byhamecky
- daa fileUploaded byYog Kunwar
- AcoUploaded bymohits2908
- ghdUploaded byaniltejas61
- undertheseacodeUploaded byapi-301886724
- Genetic Algorithm Maze Solving ProgramUploaded byRareş Ariciu
- gradiente reducidoUploaded byAlex Valdivia Prieto
- cw1Uploaded byPeter Holc
- 16 SchedulingUploaded byNya Nya'
- List and Linked ListUploaded byFathul Amin
- aifinUploaded bypiyush
- study guide keyUploaded byapi-310503032
- Computer ScienceUploaded byAditya Bhuyan
- Datastructure Course Plan for EceUploaded byRaja Sekar
- song2018 2Uploaded byvainateyagoldar
- LR0Uploaded byTanmoy Mondal
- lect24Uploaded byKESHAV SINGHAL

- Chapter 02Uploaded byBurcu Saygın
- Exs 2-6-50v1 Hl Sum Prod Roots Poly EqnUploaded byDileep Naraharasetty
- iyou9Uploaded byInês Guimarães
- Countable and Uncountable SetsUploaded byWai Lim
- Pages From Engineering Mathematics John BIRDUploaded byShakil Mahmud
- MATRIX_OPERATION (JAVA PROGRAM PDF)Uploaded byAMRENDRA SINGH
- M1 problomsUploaded bySushmitha Reddy
- 07 - Sequences and SeriesUploaded byAlok Singh
- On the Perturbation of the PseudoinverseUploaded byAlejandra Orellana
- HW_1 Solving Systems of EquationsUploaded bySolorio45
- Sequences and SeriesUploaded byMona Hamzeh
- Homework 1 SolutionsUploaded byManik Singhal
- A new genetic representation for quadratic assignment problemUploaded byJozef Kratica
- Explaining Homogeneous Coordinates & Projective Geometry.pdfUploaded by.adtmmal
- UT Dallas Syllabus for cs6363.001 05f taught by Balaji Raghavachari (rbk)Uploaded byUT Dallas Provost's Technology Group
- Super VIP Cheat Sheet : Artiﬁcial IntelligenceUploaded byAthaurRahman
- Unsteady and Transient Flow OfUploaded bywen liu
- Prime Numbers ChartUploaded byRENGANATHAN P
- Pre Solving in Linear ProgrammingUploaded byRenato Almeida Jr
- Econometric Estimation CESUploaded byValentin Burca
- Machine LearningUploaded byLDaggerson
- Higher Revision 1, 2 & 3Uploaded byknoxmaths
- 1301.1028v2Uploaded byEnzio Picanzio
- ME761A_Lecture-3 Curve RepresentationUploaded byanishsukumar000gmailcom
- Permutations HandoutUploaded byghoshmriganka
- rental harmony sperner lemma.pdfUploaded bybhaskarsg
- Polynomial Interpolation and neville's algorithmUploaded byStephen Rickman
- ECE1647_NonlinearControlUploaded byfyahyaie
- Assignment 4Uploaded bynehal
- Fortran 2018 iUploaded byWiwiData

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.