You are on page 1of 36

Applications of queues

and stacks
Norbert Wiener’s 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.
Nobert Wiener
Born: 26 Nov 1894 in Columbia, Missouri, USA
Died: 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.
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
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
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.
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
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?
Representation of a Maze
• Maze with m= p = 9
• A maze of size mp Enter: 0 0 0 0 0 0 0 0 0
• two dimensional array • 111111110
maze[i][j] where m  i  • 100000001
1 and p  j  1 .
• 011111111
• Matrix is filled with 1’s and
• 100000001
0’s with 1 implying a
blocked path and 0 implying • 111111110
that one can go through it. • 100000001
• Start maze[1][1]. End at • 011111111
maze[m][p] • 1 0 0 0 0 0 0 0 0 exit
Points to be addressed
• Maze Solution • When in position
• Enter: 0 0 0 0 0 0 0 0 0 maze[i,j], what moves
• 111111110 can be taken?
• 100000001
• How do we avoid
• 011111111
duplicating positions?
• 100000001
• 111111110 • What do we do if we
• 100000001 find a 0, a 1?
• 011111111 • How do we keep track
• 1 0 0 0 0 0 0 0 0 exit of where we were?
Possible Moves
• There are 8 possible
NW N NE directions for most i,j:
[i-j][j-1] [i-1][j] [i-1][j+1]
N,NE, E,SE,S,SW,W and
NW
W[i][j-1] [i][j+1] E
X [i][j] • The position that we are
at is marked by an X
[i+1][j-1] [I+1][j] [i+1][j+1]
SW S SE • Not every position has
eight neighbors.
• Note the exceptions
Exceptions
• If i,j =1 or i=m or j=p 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.
• maze[m+2][p+2]
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.
MOVE
• We create a struct with
• MOVE[] x,y offsets and an array
• This predefines the of the 8 possible
possible directions to directions
move • struct offsets{
• If we are in position [3]
[4] and we want to int a,b; };
move NW then we [3- • enum directions {N, NE,
1][4-1] to get to [2][3] E, SE, S, SW, W, NW};
• g, h is the new position • offsets move [8];
More on MOVE
• So from position [i][j] • Current position
• We may have to come
to position [g][h]
back to the current position
we set : if the pat of 0’s we take
• g = i + move [x].a lead us to a block
• h = j+ move[x].b • We have to save our
current position and the
• where x is one of the direction that we last
positions from 1 to 8 moved to do that we create
a stack to keep track of our
moves
• 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 Stack’s history.
If a path is found, then it moves forward and
applies the same rules to its new location.
To prevent retracing
• We do not want to • Mark[m+2][p+2]
retrace the same paths • This is initially set to 0
again and again. since none of the paths
• We need to mark have been taken
which paths have been • once we arrive at a
taken. position i,j mark[i][j] is
• To do this we create an set to 1.
array mark[][] • Maze[m][p] must be
• MARK[][] 0!!!!!
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.
First algorithm
• if((!maze[h][h] && (!
• Inner loop checks all 8 mark[g][h]))
possible positions • {
• while (there are more • mark[g][h]=1;
moves) • dir = next direction;
• { • add)i,j.dir) to stack;
• (g,h) //coordinates of • i = g;
next move • j=h;
• if((g==m) && (h==p))) • dir = north;
• success; • }
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;
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.
STACK
• Each position in the maze • Stack will be a stack
can only be visited once of items containing
in a round so m*p is the
bound of the stack . • x,y and direction:
• When we are faced with a
block we need to retrace • struct items{
our steps and see where
we went wrong. • int x,y,dir;
• And Recheck for other • };
possible moves.
The finished algorithm

Void path(int m, int p)


//start at (1,1)

mark[1][1] =1;
Stack<items>stack(m*p);
items temp;
temp.x=1; temp.y=1; temp.dir =E;
stack.Add(temp)
//this declares the first point going on the
stack
While (!stack.IsEmpty())
{
temp =*stack.Delete(temp);// take off first pt.
Int I=temp.x;
int j=temp.y;
int d =temp.dir;

while(d<8)
int g = I+move[d].a; int h =j +move [d].b;
//now check if reached exit.
(inside while (d<8) loop

If((G==m) && (h==p){//reached exit

cout<<Stack;
//last two squares on path
cout<<I<<“”<<j<<endl;
cout <<m<<“ “ <<p<<endl;
return;
}

//now what if its not the exit…??


//if the position has a 0
and if the position has not been visited
If((!maze[g][h]) && (!mark[g][h]
{
mark[g][h]=1; //mark it to visited
temp.x=i;temp.y=j;temp.dir=d+1;
//put present point on stack

stack.Add(temp);
i=g;j=h;d=N; //(new point)
Now this previous code takes care of the
problems we had to address.

1)checking for 0’s and 1’s


(!maze [g][h])

2) checking if the position has been visited


(!mark[g][h])

3)and keeping track of current positions


stack.Add(temp);
BUT what if the staement:

If((!maze[g][h]) && (!mark[g][h]))


comes out false??

Then we simply need to check the


increment d to check the next direction.

This is done with d++;


The rest of the code is simply to close off
everything:

else d++;// try next direction


}//close while (d<8)
}//close (while (!stack.IsEmpty()))
cout<< “no path in maze” <<endl;

}close void path


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.
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 0’s
• z <= mp
• so computing time is O(mp)
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.
Backtracking
The method for exhaustive search in solution
space which uses systematic enumeration of
all potential solutions
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.
• 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.
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

You might also like