36
Applications of queues and stacks

Applications of queues and stacks

  • Upload
    iain

  • View
    68

  • Download
    0

Embed Size (px)

DESCRIPTION

Lecture 23. Applications of queues and stacks. Norbert Wiener’s method. - PowerPoint PPT Presentation

Citation preview

Page 1: Applications of queues and stacks

Applications of queues and stacks

Page 2: 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.

Page 3: Applications of queues and stacks

Nobert WienerBorn: 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.

Page 4: Applications of queues and stacks

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

Page 5: Applications of queues and stacks

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

Page 6: Applications of queues and stacks

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.

Page 7: Applications of queues and stacks

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

Page 8: Applications of queues and stacks

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?

Page 9: Applications of queues and stacks

Representation of a Maze

• A maze of size mp• two dimensional array

maze[i][j] where m i 1 and 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

Page 10: Applications of queues and stacks

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

Page 11: Applications of queues and stacks

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

NW[i-j][j-1]

N [i-1][j]

NE[i-1][j+1]

W[i][j-1]X [i][j]

[i][j+1] E

[i+1][j-1] SW

[I+1][j] S

[i+1][j+1] SE

Page 12: Applications of queues and stacks

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]

Page 13: Applications of queues and stacks

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.

Page 14: Applications of queues and stacks

MOVE

• 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];

Page 15: Applications of queues and stacks

More on 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

Page 16: Applications of queues and stacks

• 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.

Page 17: Applications of queues and stacks

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[][]

• 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!!!!!

Page 18: Applications of queues and stacks

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, sincethere is at least one path from Start Pointto End Point.

Page 19: Applications of queues and stacks

First algorithm

• 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;• }

Page 20: Applications of queues and stacks

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 eastwhile(stack is not empty){(I,j,dir) =coordinates and direction from top of stackcode from previous slides

}no path found;

Page 21: Applications of queues and stacks

What comes next???

Now we need to represent the list ofnew triples {x,y, and direction).

Why do we chose a stack?? Because if the path fails, we need toremove the most recent entered triple and then check again..meaning we needLIFO which is represented by the stack.

Page 22: Applications of queues and stacks

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;• };

Page 23: Applications of queues and stacks

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

Page 24: Applications of queues and stacks

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.

Page 25: Applications of queues and stacks

(inside while (d<8) loop

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

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

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

Page 26: Applications of queues and stacks

//if the position has a 0and if the position has not been visitedIf((!maze[g][h]) && (!mark[g][h]{mark[g][h]=1; //mark it to visitedtemp.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)

Page 27: Applications of queues and stacks

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

Page 28: Applications of queues and stacks

BUT what if the staement:

If((!maze[g][h]) && (!mark[g][h]))comes out false??

Then we simply need to check theincrement d to check the next direction.

This is done with d++;

Page 29: Applications of queues and stacks

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

Page 30: Applications of queues and stacks

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 anew move.

This will keep on happening till the correct path is found.

Page 31: Applications of queues and stacks

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)

Page 32: Applications of queues and stacks

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.

Page 33: Applications of queues and stacks

Backtracking

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

Page 34: Applications of queues and stacks

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.

Page 35: Applications of queues and stacks

• 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.

Page 36: Applications of queues and stacks

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