Applications of queues and stacks - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Applications of queues and stacks

Description:

This is the simplest approach to maze solving, but unfortunately it only works ... Oliver Penrose, Harriot-Watt University, Edinburgh ... – PowerPoint PPT presentation

Number of Views:288
Avg rating:3.0/5.0
Slides: 37
Provided by: prem6
Category:

less

Transcript and Presenter's Notes

Title: Applications of queues and stacks


1
Applications of queues and stacks
Lecture 23
2
Norbert Wieners method
  • This is the simplest approach to maze solving,
    but unfortunately it only works for a restricted
    class of mazes. You do not need to be able to see
    where the walls are, you simply move so that
    there is always a wall on one side of you.

3
Nobert WienerBorn 26 Nov 1894 in Columbia,
Missouri, USADied 18 March 1964 in Stockholm,
Sweden
  • Norbert Wiener received his Ph.D. from Harvard at
    the age of 18 with a dissertation on mathematical
    logic. From Harvard to went to Cambridge, England
    to study under Russell, then he went to Göttingen
    to study under Hilbert. He was influence by both
    Hilbert and Russell but also, perhaps to an even
    greater degree, by Hardy.
  • After various occupations (journalist, university
    teacher, engineer, writer) in which he was very
    unhappy, he began a long association with MIT in
    1919.

4
The Norbert Wiener Centenary Congress 11/27/94
- 12/3/94) Michigan State University, East
Lansing, MI 48824 Sponsored by AMS, WOSC, MSU
Wiener's concept of the stochastic universe
The Wiener-Kolmogorov conception of the
stochastic organization of Nature. S.A
Molchanov, University of North Carolina,
Charlotte The Wiener program in statistical
physics. Is it feasible in the light of recent
developments? Oliver Penrose, Harriot-Watt
University, Edinburgh The mathematical
ramifications of Wiener's program in statistical
physics L. Gross, Cornell University
5
Generalized harmonic analysis and its
ramifications Wiener and the uncertainly
principle in harmonic analysis D.H. Phong,
Columbia University Generalized harmonic
analysis and Gabor and wavelet systems J.
Benedetto, University of Maryland, College Park
The Wiener-Hopf integral equation and linear
systems I.C. Gohberg, Tel Aviv University
Quantum mechanical ramifications of Wiener's
ideas Quantum field theory and functional
integration I.E. Segal, MIT Optical
coherence before and after Wiener J.R.
Klauder, University of Florida Wiener and
Feynman the role of path integration in science
S. Albeverio, Ruhr-University, Bochum
6
Leibniz, Haldane and Wiener on mind The
role of Leibniz and Haldane in Wiener's
cybernetics G. Gale, University of Missouri
Quantum mechanical coherence, resonance and
mind H.P. Stapp, Lawrence Laboratory,
Berkeley Evidence from brain research
regarding conscious processes K.H. Prinbram,
Radford University Shannon-Wiener information
and stochastic complexity J. Rissanen, IBM
Research Division, San Jose, CA Nonlinear
stochastic analysis Non-linearity and
Martingales--- D.L. Burkholder. Nonlinear
prediction and filtering--- G. Kallianpur,
Stochastic analysis on Wiener space---- S.
Watanabe. Uncertainty, feedback and Wiener's
vision of cybernetics-- S.K. Mitter.
7
For example, if you kept your hand on the wall in
the maze shown in figure M.1 , you would follow
the illustrated path to the center. Unlike, the
maze in figure M.2, as can been seen from the
path, cannot be solved in this way
8
For a human who can see the entire maze, the
answer is easy!! For arbitrary Maze Routing, we
see that maze have "walls" where one cannot go
through But if you were a robot who could only
see just 1 square directly ahead, how could you
get HOME?
9
Representation of a Maze
  • Maze with m p 9 Enter 0 0 0 0 0 0 0 0 0
  • 1 1 1 1 1 1 1 1 0
  • 1 0 0 0 0 0 0 0 1
  • 0 1 1 1 1 1 1 1 1
  • 1 0 0 0 0 0 0 0 1
  • 1 1 1 1 1 1 1 1 0
  • 1 0 0 0 0 0 0 0 1
  • 0 1 1 1 1 1 1 1 1
  • 1 0 0 0 0 0 0 0 0 exit
  • A maze of size mp
  • two dimensional array mazeij where m ? i
    ? 1 and p ? j ? 1 .
  • Matrix is filled with 1s and 0s with
    1 implying a blocked path and 0 implying that one
    can go through it.
  • Start maze11. End at mazemp

10
Points to be addressed
  • Maze Solution
  • Enter 0 0 0 0 0 0 0 0 0
  • 1 1 1 1 1 1 1 1 0
  • 1 0 0 0 0 0 0 0 1
  • 0 1 1 1 1 1 1 1 1
  • 1 0 0 0 0 0 0 0 1
  • 1 1 1 1 1 1 1 1 0
  • 1 0 0 0 0 0 0 0 1
  • 0 1 1 1 1 1 1 1 1
  • 1 0 0 0 0 0 0 0 0 exit
  • When in position mazei,j, what moves can be
    taken?
  • How do we avoid duplicating positions?
  • What do we do if we find a 0, a 1?
  • How do we keep track of where we were?

11
Possible Moves
  • There are 8 possible directions for most i,j
    N,NE, E,SE,S,SW,W and NW
  • The position that we are at is marked by an X
  • Not every position has eight neighbors.
  • Note the exceptions

12
Exceptions
  • If i,j 1 or im or jp then they do not have
    eight neighbors. The corners only have three.
  • In order to avoid problems, and simplify it, the
    program is surrounded by a border of ones.
  • mazem2p2

13
Recursive backtracker
  • A general Maze solving algorithm It focuses on
    you, is fast for all types of Mazes, and uses
    stack space up to the size of the Maze. It won't
    necessarily find the shortest solution. Very
    simple If you're at a wall (or an area you've
    already plotted), return failure, else if you're
    at the finish, return success, else recursively
    try moving in the 8 directions. Plot a line when
    you try a new direction, and erase a line when
    you return failure, and a single solution will be
    marked out when you hit success. In Computer
    Science terms this is basically a depth first
    search.

14
MOVE
  • We create a struct with x,y offsets and an array
    of the 8 possible directions
  • struct offsets
    int a,b
  • enum directions N, NE, E, SE, S, SW, W, NW
  • offsets move 8
  • MOVE
  • This predefines the possible directions to move
  • If we are in position 34 and we want to move
    NW then we 3-14-1 to get to 23
  • g, h is the new position

15
More on MOVE
  • Current position
  • We may have to come back to the current position
    if the pat of 0s we take lead us to a block
  • We have to save our current position and the
    direction that we last moved to do that we create
    a stack to keep track of our moves
  • So from position ij to position gh
    we set
  • g i move x.a
  • h j movex.b
  • where x is one of the positions from 1 to 8

16
  • The algorithm tests all the paths from the
    current position. It tests in a clockwise manner
    from North (simply set dir to 0 and increment for
    each direction). If there are no possible routes,
    then backtrack down its original path by reading
    the Stacks history. If a path is found, then it
    moves forward and applies the same rules to its
    new location.

17
To prevent retracing
  • We do not want to retrace the same paths again
    and again.
  • We need to mark which paths have been taken.
  • To do this we create an array mark
  • MARK
  • Markm2p2
  • This is initially set to 0 since none of the
    paths have been taken
  • once we arrive at a position i,j markij is
    set to 1.
  • Mazemp must be 0!!!!!

18
The upper-left corner is the Start Point
(Yellow one). The lower-right corner is the
End Point (Red one) The thick line
represent The Path (Black) This Maze is a
Complete Maze, since there is at least one path
from Start Point to End Point.
19
First algorithm
  • if((!mazehh (!markgh))
  • markgh1
  • dir next direction
  • add)i,j.dir) to stack
  • i g
  • jh
  • dir north
  • Inner loop checks all 8 possible positions
  • while (there are more moves)
  • (g,h) //coordinates of next move
  • if((gm) (hp)))
  • success

20
Outer loop
Now the outer loop is the one that keeps track of
current moves and continues as long as the stack
is not empty. The stack is initialized to the
entrance and direction east while(stack is not
empty) (I,j,dir) coordinates and direction from
top of stack code from previous slides no path
found
21
What comes next???
Now we need to represent the list of new triples
x,y, and direction). Why do we chose a stack??
Because if the path fails, we need to remove the
most recent entered triple and then check
again..meaning we need LIFO which is represented
by the stack.
22
STACK
  • Each position in the maze can only be visited
    once in a round so mp is the bound of the stack
    .
  • When we are faced with a block we need to retrace
    our steps and see where we went wrong.
  • And Recheck for other possible moves.
  • Stack will be a stack of items containing
  • x,y and direction
  • struct items
  • int x,y,dir

23
The finished algorithm
Void path(int m, int p) //start at
(1,1) mark11 1 Stackltitemsgtstack(mp) item
s temp temp.x1 temp.y1 temp.dir
E stack.Add(temp) //this declares the first
point going on the stack
24
While (!stack.IsEmpty()) temp
stack.Delete(temp)// take off first pt. Int
Itemp.x int jtemp.y int d temp.dir while(dlt
8) int g Imoved.a int h j move d.b
//now check if reached exit.
25
(inside while (dlt8) loop If((Gm)
(hp)//reached exit coutltltStack //last two
squares on path coutltltIltltltltjltltendl cout ltltmltlt
ltltpltltendl return //now what if its not the
exit??
26
//if the position has a 0 and if the position has
not been visited If((!mazegh)
(!markgh markgh1 //mark it to
visited temp.xitemp.yjtemp.dird1 //put
present point on stack stack.Add(temp) igjhd
N //(new point)
27
Now this previous code takes care of the
problems we had to address. 1)checking for 0s
and 1s (!maze gh) 2) checking if the
position has been visited
(!markgh) 3)and keeping track of current
positions stack.Add(temp)
28
BUT what if the staement If((!mazegh)
(!markgh)) comes out false??Then we simply
need to check the increment d to check the next
direction. This is done with d
29
The rest of the code is simply to close off
everything else d// try next
direction //close while (dlt8) //close
(while (!stack.IsEmpty())) coutltlt no path in
maze ltltendl close void path
30
The program will exit the inner loop when a
path is found but a block occurs. Then the outer
loop will delete that move from the stack and
check those values for a new move. This will
keep on happening till the correct path is found.
31
More info
  • The number of iterations of the outer while loop
    depends on each maze
  • in the inner while loop, 8 is the maximum
    iterations for each position 0(1)
  • say z is the number of 0s
  • z lt mp
  • so computing time is O(mp)

32
Using a Stack
  • Lecture 7 introduces the stack data type.
  • Several example applications of stacks are given
    in Lecture 8.
  • another use called backtracking to solve the
    N-Queens problem.

33
Backtracking
  • The method for exhaustive search in solution
    space which uses systematic enumeration of all
    potential solutions

34
Eight Queens Problem (Classic Combinatorial
Problem)
  • To place 8 queen chess pieces on an 8 x 8 board
    so that no two queens lie in the same row, same
    column, or same 45 degree diagonal.

35
  • The solution space of a problem often forms a
    tree structure
  • Finding a solution to the problem is regarded as
    search on the tree.
  • A strategy to choose the next vertex to be
    visited is important in order to find a
    solution quickly.

36
Applications of queues and stacks
  • Two Major Strategies
  • o DFS Search LIFO (Last In First Out) Search
    Backtracking which uses a stack
  • o BFS Search FIFO (First In First Out) Search
    which uses a queue
Write a Comment
User Comments (0)
About PowerShow.com