390 likes | 575 Views
Lecture 23. Applications of queues and stacks. Norbert Wiener’s method.
E N D
Lecture 23 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 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.
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?
A maze of size mp two dimensional array maze[i][j] where m i 1and p j 1 . Matrix is filled with 1’s and 0’s with 1 implying a blocked path and 0 implying that one can go through it. Start maze[1][1]. End at maze[m][p] 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 Representation of a Maze
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 0exit When in position maze[i,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? Points to be addressed
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 Possible Moves
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[] This predefines the possible directions to move If we are in position [3][4] and we want to move NW then we [3-1][4-1] to get to [2][3] g, h is the new position 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
So from position [i][j] to position [g][h] we set : g = i + move [x].a h = j+ move[x].b where x is one of the positions from 1 to 8 Current position We may have to come back to the current position if the pat of 0’s 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 More on MOVE
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.
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[][] Mark[m+2][p+2] This is initially set to 0 since none of the paths have been taken once we arrive at a position i,j mark[i][j] is set to 1. Maze[m][p] must be 0!!!!! To prevent retracing
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.
Inner loop checks all 8 possible positions while (there are more moves) { (g,h) //coordinates of next move if((g==m) && (h==p))) success; if((!maze[h][h] && (!mark[g][h])) { mark[g][h]=1; dir = next direction; add)i,j.dir) to stack; i = g; j=h; dir = north; } First algorithm
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.
Each position in the maze can only be visited once in a round so m*p 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; }; STACK
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 stackdata 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