Chapter 20 Queues and Priority Queues CS165 Colorado State - - PowerPoint PPT Presentation

chapter 20 queues and priority queues
SMART_READER_LITE
LIVE PREVIEW

Chapter 20 Queues and Priority Queues CS165 Colorado State - - PowerPoint PPT Presentation

Chapter 20 Queues and Priority Queues CS165 Colorado State University Original slides by Daniel Liang Modified slides by Wim Bohm, Sudipto Ghosh Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 1


slide-1
SLIDE 1

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

1

Chapter 20 Queues and Priority

Queues

CS165 Colorado State University

Original slides by Daniel Liang Modified slides by Wim Bohm, Sudipto Ghosh

slide-2
SLIDE 2

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

2

Queues and Priority Queues

Queue is a first-in/first-out data structure.

! Elements are added to the end of the queue. ! Elements are removed from the beginning of the

queue. Priority queues assign priorities to elements.

! The element with the highest priority is removed

first.

slide-3
SLIDE 3

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

3

The Queue Interface

slide-4
SLIDE 4

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

4

Using LinkedList for Queue

slide-5
SLIDE 5

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Reference-Based Implementation 1

A linked list with two external references

– A reference to the front – A reference to the back

At which end do we enqueue / dequeue?

5

. 70 . 55 . Back Front . 60 Item Next

add / offer at BACK remove at FRONT What if we did it the other way?

slide-6
SLIDE 6

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Reference-Based Implementation 2

A circular linked list with one external reference

– lastNode references the back of the queue – lastNode.getNext() references the front

6 70 . 55 60 . Last Node: node reference .

slide-7
SLIDE 7

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

.

Adding an item into a nonempty queue

7

70 . 55 60 .

Last Node

1.

newNode.next = lastNode.next;

2.

lastNode.next = newNode;

3.

lastNode = newNode;

85 .

New node

slide-8
SLIDE 8

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Adding an item to an empty queue

8

.

! Insert a new item into the empty queue Last Node

60 .

slide-9
SLIDE 9

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Add item to queue

public void add (Object newItem){ Node newNode = new Node(newItem); if (isEmpty()){ newNode.next = newNode; } else { newNode.next = lastNode.next; lastNode.next = newNode; } lastNode = newNode; }

9

  • A. Empty queue
  • B. items in queue
slide-10
SLIDE 10

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Removing an item from queue

public Object remove() throws QueueException{ if (!isEmpty()){ Node firstNode = lastNode.next; if (firstNode == lastNode) { lastNode = null; } else{ lastNode.next = firstNode.next; } return firstNode.item; } else { exception handling.. } }

10

Why?

slide-11
SLIDE 11

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Removing an Item

11

70 . 60 . 80 .

Last Node

.

Node firstNode = lastNode.next; if (firstNode == lastnode) { lastNode = null;} else{lastNode.next = firstNode.next;} return firstNode.item;

First Node

.

What happens to this node?

CS200 -Queues

slide-12
SLIDE 12

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Naïve Array-Based Implementation

Drift wastes space How do we initialize front and back? (Hint: what does a queue with a single element look like? what does an empty queue look like? ) Problem: Drift 12

slide-13
SLIDE 13

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Circular implementation of a queue solves drift

13

1 6 5 4 3 2 MAX_QUEUE-1 i a e

  • FRONT

BACK

slide-14
SLIDE 14

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Solving Drift:

! First Delete

14

1 6 5 4 3 2 MAX_QUEUE-1 i a e

  • FRONT

BACK

CS200 -Queues

slide-15
SLIDE 15

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Solving Drift:

! Second Delete

1 6 5 4 3 2 MAX_QUEUE-1 i e

  • BACK

FRONT

slide-16
SLIDE 16

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Solving Drift

! add u

16

1 6 5 4 3 2 MAX_QUEUE-1 i

  • BACK

FRONT

u

BACK

When either front or back advances past MAX_QUEUE-1, it wraps around (to 0: using % MAX_QUEUE)

slide-17
SLIDE 17

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Queue with Single Item

! back and front are pointing at the same slot. 17

1 6 5 4 3 2 MAX_QUEUE-1 u

BACK FRONT

CS200 -Queues

slide-18
SLIDE 18

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Empty Queue: remove Single Item

Remove last item.

– front passed back.

18

1 6 5 4 3 2 MAX_QUEUE-1 u

BACK F R O N T F R O N T

When the queue is EMPTY, front is one slot ahead of back.

slide-19
SLIDE 19

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Insert the last item

back catches up to front when the queue becomes full.

19

1 6 5 4 3 2 MAX_QUEUE-1 u

BACK FRONT

  • i

e a b f n

BACK

When the queue is FULL, front is one slot ahead of back. Problem? Maintain size: 0:empty max_queue: full Solution?

slide-20
SLIDE 20

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Wrapping the values for front and back

! Initializing

front = 0 back = MAX_QUEUE-1 count = 0

! Adding

back = (back+1) % MAX_QUEUE; items[back] = newItem; ++count;

! Deleting / dequeueing

removeItem = items[front]; front = (front +1) % MAX_QUEUE;

  • -count;

20

slide-21
SLIDE 21

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

21

The PriorityQueue Class

Run

PriorityQueueDemo

slide-22
SLIDE 22

Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Implementation of Priority Queue

! Naïve: ArrayList or Linked List

– Keeping the elements ordered – This will make add costly

! Better implementation: Heap (later)

22