queues, deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · what we have...

28
Queues, Deques Hsuan-Tien Lin Dept. of CSIE, NTU March 31, 2014 H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 0 / 14

Upload: others

Post on 08-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Queues, Deques

Hsuan-Tien Lin

Dept. of CSIE, NTU

March 31, 2014

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 0 / 14

Page 2: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

What We Have Done

algorithm data structuresequential search array or (linked list)

selection sort array or (linked list)insertion sort array or (linked list)binary search ordered array

polynomial “merge” sparse array on array or (linked list)parenthesis matching stack

postfix evaluation stackinfix to postifix stack

next: another algorithm with stack (and more)

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 1 / 14

Page 3: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

The Maze Problem

http://commons.wikimedia.org/wiki/File:Maze01-01.png

given a (2D) maze, is there a way out?

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 2 / 14

Page 4: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

The Maze Problem

http://commons.wikimedia.org/wiki/File:Maze01-01.png

given a (2D) maze, is there a way out?

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 2 / 14

Page 5: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

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 visitedfor each unmarked (k , `) reachable from (i , j) do

if (k , `) is an exitreturn TRUE

end ifif GET-OUT-RECURSIVE(m, (k , `))

return TRUEend if

end forreturn FALSE

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 3 / 14

Page 6: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Recursion (Reading Assignment: Section 3.5)

a function call to itselfbe ware of terminating conditionscan represent programming intentions clearlyat the expense of “space” (why?)

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 4 / 14

Page 7: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Recursion (Reading Assignment: Section 3.5)

a function call to itselfbe ware of terminating conditionscan represent programming intentions clearlyat the expense of “space” (why?)

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 4 / 14

Page 8: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Recursion (Reading Assignment: Section 3.5)

a function call to itselfbe ware of terminating conditionscan represent programming intentions clearlyat the expense of “space” (why?)

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 4 / 14

Page 9: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Recursion (Reading Assignment: Section 3.5)

a function call to itselfbe ware of terminating conditionscan represent programming intentions clearlyat the expense of “space” (why?)

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 4 / 14

Page 10: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

From Recursion to Stack

Getting Out of Maze by StackGET-OUT-STACK(Maze m, Postion (i , j))

while stack not empty do(i , j)← pop from stackmark (i , j) as visitedfor each unmarked (k , `) reachable from (i , j) do

if (k , `) is an exitreturn TRUE

end ifpush (k , `) to stack [and mark (k , `) as todo]

end forend whilereturn FALSE

similar result to recursive version, but conceptually differentrecursive: one path on the system stackstack: many positions-to-be-explored on the user stack

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 5 / 14

Page 11: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

From Recursion to Stack

Getting Out of Maze by StackGET-OUT-STACK(Maze m, Postion (i , j))

while stack not empty do(i , j)← pop from stackmark (i , j) as visitedfor each unmarked (k , `) reachable from (i , j) do

if (k , `) is an exitreturn TRUE

end ifpush (k , `) to stack [and mark (k , `) as todo]

end forend whilereturn FALSE

similar result to recursive version, but conceptually differentrecursive: one path on the system stackstack: many positions-to-be-explored on the user stack

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 5 / 14

Page 12: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

A General Maze Algorithm

Getting Out of Maze by ContainerGET-OUT-CONTAINER(Maze m, Postion (i , j))

while container not empty do(i , j)← remove from containermark (i , j) as visitedfor each unmarked (k , `) reachable from (i , j) do

if (k , `) is an exitreturn TRUE

end ifinsert (k , `) to container [and mark (k , `) as todo]

end forend whilereturn FALSE

if “random” remove from container: “random walk” to exit

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 6 / 14

Page 13: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

A General Maze Algorithm

Getting Out of Maze by ContainerGET-OUT-CONTAINER(Maze m, Postion (i , j))

while container not empty do(i , j)← remove from containermark (i , j) as visitedfor each unmarked (k , `) reachable from (i , j) do

if (k , `) is an exitreturn TRUE

end ifinsert (k , `) to container [and mark (k , `) as todo]

end forend whilereturn FALSE

if “random” remove from container: “random walk” to exit

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 6 / 14

Page 14: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Queues

Queueobject: a container that holds some elementsaction: [constant-time] enqueue (to the rear), dequeue (from thefront)

first-in-first-out (FIFO): 買票 , 印表機also very restricted data structure, but also important forcomputers

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 7 / 14

Page 15: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Queues

Queueobject: a container that holds some elementsaction: [constant-time] enqueue (to the rear), dequeue (from thefront)

first-in-first-out (FIFO): 買票 , 印表機also very restricted data structure, but also important forcomputers

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 7 / 14

Page 16: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Queues Implemented on Circular Array (5.2.4)

Reading Assignmentbe sure to go ask the TAs or me if you are still confused

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 8 / 14

Page 17: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Queues Implemented on Circular List (5.2.5)

Reading Assignmentbe sure to go ask the TAs or me if you are still confused

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 9 / 14

Page 18: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Maze From Stack to Queue

Getting Out of Maze by QueueGET-OUT-QUEUE(Maze m, Postion (i , j))

while queue not empty do(i , j)← dequeue from queuemark (i , j) as visitedfor each unmarked (k , `) reachable from (i , j) do

if (k , `) is an exitreturn TRUE

end ifenqueue (k , `) to queue [and mark (k , `) as todo]

end forend whilereturn FALSE

use of stack/queue: store the yet-to-be-explored positionsstack version : first (lexicographically) way out (explore deeply)queue version : shortest way out (explore broadly)H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 10 / 14

Page 19: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Maze From Stack to Queue

Getting Out of Maze by QueueGET-OUT-QUEUE(Maze m, Postion (i , j))

while queue not empty do(i , j)← dequeue from queuemark (i , j) as visitedfor each unmarked (k , `) reachable from (i , j) do

if (k , `) is an exitreturn TRUE

end ifenqueue (k , `) to queue [and mark (k , `) as todo]

end forend whilereturn FALSE

use of stack/queue: store the yet-to-be-explored positionsstack version : first (lexicographically) way out (explore deeply)queue version : shortest way out (explore broadly)H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 10 / 14

Page 20: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Maze From Stack to Queue

Getting Out of Maze by QueueGET-OUT-QUEUE(Maze m, Postion (i , j))

while queue not empty do(i , j)← dequeue from queuemark (i , j) as visitedfor each unmarked (k , `) reachable from (i , j) do

if (k , `) is an exitreturn TRUE

end ifenqueue (k , `) to queue [and mark (k , `) as todo]

end forend whilereturn FALSE

use of stack/queue: store the yet-to-be-explored positionsstack version : first (lexicographically) way out (explore deeply)queue version : shortest way out (explore broadly)H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 10 / 14

Page 21: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Maze From Stack to Queue

Getting Out of Maze by QueueGET-OUT-QUEUE(Maze m, Postion (i , j))

while queue not empty do(i , j)← dequeue from queuemark (i , j) as visitedfor each unmarked (k , `) reachable from (i , j) do

if (k , `) is an exitreturn TRUE

end ifenqueue (k , `) to queue [and mark (k , `) as todo]

end forend whilereturn FALSE

use of stack/queue: store the yet-to-be-explored positionsstack version : first (lexicographically) way out (explore deeply)queue version : shortest way out (explore broadly)H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 10 / 14

Page 22: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Deques

Deque = Stack + Queue + push_frontobject: a container that holds some elementsaction: [constant-time] push_back (like push and enqueue),pop_back (like pop), pop_front (like dequeue), push_front

application: job scheduling

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 11 / 14

Page 23: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Deques Implemented on Doubly-linked List (5.3.2)

Reading Assignmentbe sure to go ask the TAs or me if you are still confused

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 12 / 14

Page 24: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Some Useful Implementations in C++

Standard Template Library (STL)

container vector: dynamically growing dense arraycontainer list: doubly-linked listcontainer deque: “chunked” linked-list implementation of dequecontainer adapter stack: turning some container to a stack

1 template <typename T , typename Container = deque<T> >2 c lass stack ;

container adapter queue: turning some container to a queue

1 template <typename T , typename Container = deque<T> >2 c lass queue ;

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 13 / 14

Page 25: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Some Useful Implementations in C++

Standard Template Library (STL)

container vector: dynamically growing dense arraycontainer list: doubly-linked listcontainer deque: “chunked” linked-list implementation of dequecontainer adapter stack: turning some container to a stack

1 template <typename T , typename Container = deque<T> >2 c lass stack ;

container adapter queue: turning some container to a queue

1 template <typename T , typename Container = deque<T> >2 c lass queue ;

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 13 / 14

Page 26: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Some Useful Implementations in C++

Standard Template Library (STL)

container vector: dynamically growing dense arraycontainer list: doubly-linked listcontainer deque: “chunked” linked-list implementation of dequecontainer adapter stack: turning some container to a stack

1 template <typename T , typename Container = deque<T> >2 c lass stack ;

container adapter queue: turning some container to a queue

1 template <typename T , typename Container = deque<T> >2 c lass queue ;

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 13 / 14

Page 27: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Some Useful Implementations in C++

Standard Template Library (STL)

container vector: dynamically growing dense arraycontainer list: doubly-linked listcontainer deque: “chunked” linked-list implementation of dequecontainer adapter stack: turning some container to a stack

1 template <typename T , typename Container = deque<T> >2 c lass stack ;

container adapter queue: turning some container to a queue

1 template <typename T , typename Container = deque<T> >2 c lass queue ;

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 13 / 14

Page 28: Queues, Deques - 國立臺灣大學htlin/course/dsa14spring/doc/0331.handout.pdf · What We Have Done algorithm data structure sequential search array or (linked list) selection sort

Some Useful Implementations in C++

1 # inc lude <vector >2 # inc lude <stack >3 # inc lude <queue>4 using namespace std ;5 vector < i n t > i n t a r r a y ;6 stack <char , vector <char > > chars tackonvector ;7 queue<double > doublequeue ;8 i n t a r r a y . res i ze ( 2 0 ) ; i n t a r r a y [ 3 ] = 5 ;9 chars tack . push_back ( ’ ( ’ ) ;

10 char c = charstack . pop_back ( ) ;11 doublequeue . push_back ( 3 . 1 4 ) ;12 double d = doublequeue . pop_f ront ( ) ;

H.-T. Lin (NTU CSIE) Queues, Deques 03/31/2014 14 / 14