Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Chapter 6 Queues. 2 Topics FIFO (first0in-first0out) structure Implementations: formula-based and linked classes Applications: –railroad-switching problem."— Presentation transcript:

1 1 Chapter 6 Queues

2 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

3 3 Queues

4 4 Abstract data type

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

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

7 7 Shifting when rear=MaxSize-1 and front > 0

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

9 9 Empty and full queue Empty queue: front=rear full queue: can only hold up to MaxSize-1 elements

10 10 Formula-based class Queue

11 11 Constructor - Θ(1) when T is internal; O(MaxStackSize) when T is user-defined First - Θ(1)

12 12 Last - Θ(1)

13 13 Add and Delete - Θ(1)

14 14 Linked presentation

15 15 Addition

16 16 Deletion

17 17 Class definition

18 18 Destructor - Θ(n)

19 19 IsFull - Θ(1)

20 20 First and Last - Θ(1)

21 21 Add - Θ(1)

22 22 Delete - Θ(1)

23 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.

24 24 Wire-Routing Problem

25 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.

26 26 Wire-Routing Problem To 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 reached The sequence of neighbors collected forms the shortest path.

27 27 Wire-Routing Problem

28 28

29 29

30 30 Railroad Car Rearrangement

31 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)

32 32 Output

33 33 Hold

34 34 Hold (continue)

35 35 Hold (continue)

36 36 Output without using queue

37 37 Output without using queue (continue)

38 38 Output without using queue (continue)

39 39 Railroad without using queue - O(nlogk)

40 40 Railroad without using queue (continue)

41 41 Railroad without using queue (continue)

42 42 Image-Component labeling

43 43 Offset

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

45 45 Component labeling

46 46 Component labeling (continue)

47 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(m 2 ) Overall complexity - O(m 2 )

48 48 End of Chapter 6


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

Similar presentations


Ads by Google