• 沒有找到結果。

April7,2020 Hsuan-TienLin Queue

N/A
N/A
Protected

Academic year: 2022

Share "April7,2020 Hsuan-TienLin Queue"

Copied!
13
0
0

加載中.... (立即查看全文)

全文

(1)

Queue

Hsuan-Tien Lin

Dept. of CSIE, NTU

April 7, 2020

(2)

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)

(3)

The Maze Problem

http://commons.wikimedia.org/wiki/File:Maze01-01.png given a (2D) maze, is there a way out?

(4)

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

(5)

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

(6)

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

(7)

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

(8)

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

(9)

Queues Implemented on Circular Array (5.2.4)

Reading Assignment

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

(10)

Queues Implemented on Circular List (5.2.5)

Reading Assignment

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

(11)

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

(12)

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

(13)

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

參考文獻

相關文件

From a visual comparison, we observe that based on the same demosaicing algorithm, the demosaiced images, which use the zoomed mosaic images created by our proposed zooming algorithm

The cubic eigenvalue problem, then, can be solved by the cubic Jaeobi-Davidson method presented in Figure 3 to compute the smallest positive eigenvalue

共Received 27 May 2005; accepted 20 September 2005; published online 20 October 2005兲 In application of focused-ion-beam lithography and grazing Ar + milling on the U-shape barrier

之後每次從 heap 取 出一個 item, 放入 sorted list.. array of items 想是

mid: 左半部 array 的最大 index high: array 最大的 index.. 股市大亨 之

[r]

The PROM is a combinational programmable logic device (PLD) – an integrated circuit with programmable gates divided into an AND array and an OR array to provide an

請多利用下周二三四的 office