Queue
Hsuan-Tien Lin
Dept. of CSIE, NTU
April 7, 2020
What We Have Done
algorithm data structure
sequential search array (or linked list) selection sort array (or linked list) insertion sort linked list (or array) binary search ordered array polynomial merge sparse array on linked list parenthesis matching stack
postfix evaluation stack
infix to postifix stack
next: another algorithm with stack (and more)
The Maze Problem
http://commons.wikimedia.org/wiki/File:Maze01-01.png given a (2D) maze, is there a way out?
Recursive Algorithm
GET-OUT-RECURSIVE(m, (0, 0))
Getting Out of Maze Recursively
GET-OUT-RECURSIVE(Maze m, Postion (i, j)) mark (i, j) as visited
for each unmarked (k , `) reachable from (i, j) do if (k , `) is an exit
return TRUE end if
if GET-OUT-RECURSIVE(m, (k , `)) return TRUE
end if end for
return FALSE
Recursion (Reading Assignment: Section 3.5, Remember?)
• a function call to itself
• be ware ofterminating conditions
• can represent programming intentions clearly
• at the expense of“space”(why?)
From Recursion to Stack
Getting Out of Maze by Stack
GET-OUT-STACK(Maze m, Postion (i, j)) while stack not empty do
(i, j) ← pop from stack mark (i, j) asvisited
for each unmarked (k , `) reachable from (i, j) do if (k , `) is an exit
return TRUE end if
push (k , `) to stack [and mark (k , `) astodo]
end for end while return FALSE
• similar result to recursive version, but conceptually different
• recursive: one path on the system stack
• stack: many positions-to-be-explored on the user stack
A General Maze Algorithm
Getting Out of Maze byContainer
GET-OUT-CONTAINER(Maze m, Postion (i, j)) whilecontainernot emptydo
(i, j) ← remove fromcontainer mark (i, j) as visited
for each unmarked (k , `) reachable from (i, j) do if (k , `) is an exit
return TRUE end if
insert (k , `) tocontainer[and mark (k , `) as todo]
end for end while return FALSE
• if “random” remove fromcontainer: “random walk” to exit
Queues
Queue
• object: a container that holds some elements
• action: [constant-time] enqueue (to the rear), dequeue (from the front)
• first-in-first-out (FIFO): 買票 , 印表機
• also very restricted data structure, but also important for computers
Queues Implemented on Circular Array (5.2.4)
Reading Assignment
be sure to go ask the TAs or me if you are still confused
Queues Implemented on Circular List (5.2.5)
Reading Assignment
be sure to go ask the TAs or me if you are still confused
Maze From Stack to Queue
Getting Out of Maze byQueue
GET-OUT-QUEUE(Maze m, Postion (i, j)) whilequeuenot emptydo
(i, j) ←dequeuefromqueue mark (i, j) as visited
for each unmarked (k , `) reachable from (i, j) do if (k , `) is an exit
return TRUE end if
enqueue(k , `) toqueue[and mark (k , `) as todo]
end for end while return FALSE
• use of stack/queue: store the yet-to-be-explored positions
• stack version : first (lexicographically) way out (explore deeply)
—depth-first search
• queue version : shortest way out (explore broadly) —breadth-first
Deques
Deque = Stack + Queue + push_front
• object: a container that holds some elements
• action: [constant-time] push_back (like push and enqueue), pop_back (like pop), pop_front (like dequeue), push_front
• application: job scheduling
Deques Implemented on Doubly-linked List (5.3.2)
Reading Assignment
be sure to go ask the TAs or me if you are still confused