Data Structures: Priority Queues and Binary Heaps, Slides of Data Structures and Algorithms

An in-depth analysis of priority queues, their applications, and binary heap implementations. It covers topics such as motivations, model, simple implementations, and various types of heaps. The document also discusses the heap order property, basic heap operations, and other heap operations. Use cases of priority queues, such as the selection problem, are explained with two different algorithms.

Typology: Slides

2011/2012

Uploaded on 07/15/2012

sahill
sahill 🇮🇳

4.8

(4)

46 documents

1 / 38

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
7/7/2012 Priorty Queues 6-1
Chapter 6
Priority Queues
EE 232 Data Structures
Session-05 , Spring-07
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Data Structures: Priority Queues and Binary Heaps and more Slides Data Structures and Algorithms in PDF only on Docsity!

7/7/2012 Priorty Queues 6-

Chapter 6

Priority Queues

EE 232 Data Structures Session-05 , Spring-

7/7/2012 Priorty Queues 6-

Motivations

 Printer Management

 Three jobs are sent to a printer

in the order A, B and C

  • Job A --- print a pdf file
  • Job B --- print a word document
  • Job C --- print the data structure ppt slides

 One job might be particularly

important

 Desirable to allow that job to run

as soon as the printer is

available

front rear

A B^ C

7/7/2012 Priorty Queues 6-

Motivations…

 Multitasking Operating System

 The scheduler of an operating system decides

which of the several processes to run

 One algorithm of the scheduler uses FCFS queue

  • Jobs are initially placed at end of the queue
  • The scheduler will repeatedly take the first job on the queue, run it until either it finishes or its time limit is up and place it at the end of the queue if it does not finish

 Interactive programs?

 Processes such as Internet explorer would work?

7/7/2012 Priorty Queues 6-

Chapter 6: Priority Queues

6.1 Model

6.2 Simple Implementations

6.3 Binary Heap

6.4 Applications of Priority Queues

6.5 d - Heaps

6.6 Leftist Heaps

6.7 Skew Heaps

6.8 Binomial Queues

7/7/2012 Priorty Queues 6-

Chapter 6: Priority Queues

6.1 Model

6.2 Simple Implementations

6.3 Binary Heap

6.4 Applications of Priority Queues

6.5 d - Heaps

6.6 Leftist Heaps

6.7 Skew Heaps

6.8 Binomial Queues

7/7/2012 Priorty Queues 6-

Model

 Priority queue is a data structure which allows at

least two operations

 Insert

 DeleteMin

  • finds, returns and removes the minimum element in the priority queue

DeleteMin Priority Queue Insert

Basic model of a priority queue

7/7/2012 Priorty Queues 6-

Simple Implementations

 Simple Linked List

 Insert

  • at the front in O (1)

 DeleteMin

  • traverse list in O ( N ) time

 Sorted Linked List

 Insert

• O ( N )

 DeleteMin

• O (1)

7/7/2012 Priorty Queues 6-

Simple Implementations…

 Binary Search Tree

 Insert

  • O (log N)

 DeleteMin

  • O (log N)

 overkill to use BST

  • no need for all the BST operations, pointers

7/7/2012 Priorty Queues 6-

More tree terminology

 Degree of a node

 The degree of a

node n is the number

of its children

 Degree of a tree

 The degree of a tree

is the maximum

degree of any of its

nodes

7/7/2012 Priorty Queues 6-

More tree terminology…

 A node n in a binary

tree is full if it has

degree two

 A full binary tree of

height h has leaves only

on level h and each of

its internal nodes is full

7/7/2012 Priorty Queues 6-

Binary Heap

 Like binary search trees, heaps have two properties

 Structure property  Heap order property

 Structure Property

 A binary heap (or a heap) is a complete binary tree

 A complete binary tree of height h has between 2 h

and 2 h +1^ -1 nodes

 height of a complete binary tree is log N  which is O (log N )

7/7/2012 Priorty Queues 6-

Binary Heap…

 *We are able to store a complete tree as an array

 Simply traverse the array in breadth-first order,

placing the entries into the array

*http://www.ece.uwaterloo.ca/~ece250/

For each depth, starting at zero: visit the nodes in the order from left to right

7/7/2012 Priorty Queues 6-

Binary Heap…

 *To remove a node while keeping the structure, we

simply remove the last element in the array

*http://www.ece.uwaterloo.ca/~ece250/ docsity.com

7/7/2012 Priorty Queues 6-

Binary Heap…

 For any element in array

position i

 The left child is in position 2i  The right child is in position 2i+  The parent is in position i/2

 No pointer storage

 Fast computation of 2i and

i/2 by bit shifting

i << 1 = 2i i >> 1 = i/2

A B C D E F G H I J

A

B C

D E F G

H I J