1 chapter 6 queues. 2 topics fifo (first0in-first0out) structure implementations: formula-based and...

48
1 Chapter 6 Queues

Upload: brandon-kelley

Post on 08-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

3 Queues

TRANSCRIPT

Page 1: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

1

Chapter 6

Queues

Page 2: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

2

Topics• FIFO (first0in-first0out) structure• Implementations: formula-based and linked

classes• Applications:

– railroad-switching problem– shortest path for a wire that is to connect two points– pixels labeling– machine shop simulation

Page 3: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

3

Queues

Page 4: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

4

Abstract data type

Page 5: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

5

Queues 1• location(i) = i - 1• add - O(1)• delete - Θ(n) (need to slide items to front)

Page 6: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

6

Queues 2• location(i) = location(1) + i - 1• add - worst-case Θ(n) (when buffer is full)• delete - O(1)

Page 7: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

7

Shifting when rear=MaxSize-1 and front > 0

Page 8: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

8

Queues 3• location(i) = (location(1) + i - 1) % MaxSize• add - Θ(1)• delete - Θ(1)

Page 9: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

9

Empty and full queue

• Empty queue: front=rear

• full queue: can only hold up to MaxSize-1 elements

Page 10: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

10

Formula-based class Queue

Page 11: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

11

Constructor - Θ(1) when T is internal; O(MaxStackSize) when T is

user-defined

First - Θ(1)

Page 12: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

12

Last - Θ(1)

Page 13: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

13

Add and Delete - Θ(1)

Page 14: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

14

Linked presentation

Page 15: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

15

Addition

Page 16: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

16

Deletion

Page 17: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

17

Class definition

Page 18: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

18

Destructor - Θ(n)

Page 19: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

19

IsFull - Θ(1)

Page 20: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

20

First and Last - Θ(1)

Page 21: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

21

Add - Θ(1)

Page 22: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

22

Delete - Θ(1)

Page 23: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

23

Wire-Routing Problem• Need to route wires from A to B using the

shortest-path.• Routing region can be represented with a

grid: an nxm matrix of squares• wire runs midpoint of one square to midpoint

of another making only right-angles.• Grid squares that already have wires in them

are blocked.

Page 24: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

24

Wire-Routing Problem

Page 25: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

25

Wire-Routing Problem• Begin at source a• label its reachable neighbors with 1• next the reachable neighbors of distance 1

squares are labeled 2.• Continue labeling processing until either

reach destination b or have no more reachable neighbors.

• If b is reached, label it with its distance.

Page 26: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

26

Wire-Routing ProblemTo construct shortest path, • Start from b and move to anyone of its

neighbors labeled 1 less than b's label.• From the neighbor found in previous step, move

to one of its neighbors whose label is 1 less• repeat until square a is reachedThe sequence of neighbors collected forms the

shortest path.

Page 27: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

27

Wire-Routing Problem

Page 28: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

28

Page 29: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

29

Page 30: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

30

Railroad Car Rearrangement

Page 31: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

31

Rules

• Reserve Hk for moving cars directly from the input track to the output track

• Move car c to a holding track that contains only cars with a smaller label; if there are several such tracks, select one with largest label at its left end; otherwise, select an empty track (if one remains)

Page 32: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

32

Output

Page 33: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

33

Hold

Page 34: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

34

Hold (continue)

Page 35: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

35

Hold (continue)

Page 36: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

36

Output without using queue

Page 37: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

37

Output without using queue (continue)

Page 38: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

38

Output without using queue (continue)

Page 39: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

39

Railroad without using queue - O(nlogk)

Page 40: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

40

Railroad without using queue (continue)

Page 41: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

41

Railroad without using queue (continue)

Page 42: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

42

Image-Component labeling

Page 43: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

43

Offset

Page 44: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

44

Wall of blank pixels (0)Note: change 1 to 0

Page 45: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

45

Component labeling

Page 46: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

46

Component labeling (continue)

Page 47: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

47

Evaluation• Initialize the wall - Θ(m)• Initialize offsets - Θ(1)• identifying and labeling pixels of one

component - Θ(# of pixels in component)• identifying and labeling nonseed component -

Θ(# of pixels in component pixels in image) = O(m2)

• Overall complexity - O(m2)

Page 48: 1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem

48

End of Chapter 6