A Series of Slides in 5 Parts Movement 2. BFS Maze Running in C Minor A Series of Slides in 5 Parts Movement 2. BFS
AT: AA QUEUE: AB We will mark the nodes as visited in order to be compliant with how the visualizer code works. No visit information is actually being stored.. KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: AB QUEUE: AA AC BB Note that we added AA to the queue. We could choose not to add the node we came from, but we are doing so anyhow to show that it will only slowdown the search. KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: AA QUEUE: AC BB AB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: AC QUEUE: BB AB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: BB QUEUE: AB AB BA CB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: AB QUEUE: AB BA AB CB AA AC BB Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: AB QUEUE: BA AB CB AA AC BB Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: BA QUEUE: AB CB AA AC BB Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: AB QUEUE: CB AA AC BB CB Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: CB QUEUE: AA AC BB AA CB DB Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: AA QUEUE: AC BB AA AC CB DB AB Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: AC QUEUE: BB AA AC BB CB DB AB BA DA Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
At this point, you should realize how inefficient BFS is without keeping visit information. However, you should also see that it would eventually reach the exit due to the queue data structure. We will now start over.
AT: AA QUEUE: AB For each node, we will keep track of the node that we first came from to reach that node. KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: AB QUEUE: AC BB Note that we did not add AA to the queue. This time, we do not add the node we came from. KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: AC QUEUE: BB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: BB QUEUE: BA CB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: BA QUEUE: CB CA KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: CB QUEUE: CA DB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: CA QUEUE: DB DA KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DB QUEUE: DA DA DC EB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DA QUEUE: DA DC EB DB This is an interesting situation. We add DB and not CA to the code since we know that we reached DB through CA. KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DA QUEUE: DC EB DB This is an interesting situation. We add DB and not CA to the code since we know that we reached DB through CA. KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DC QUEUE: EB DB CC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: EB QUEUE: DB DB CC EA EC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DB QUEUE: DB CC EA EC DA DC EB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DB QUEUE: CC EA EC DA DC EB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: CC QUEUE: EA EC DA DC EB BC CD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: EA QUEUE: EC DA DC EB BC CD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: EC QUEUE: DA DC EB DA BC CD ED KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DA QUEUE: DC EB DA DC BC CD ED DB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DC QUEUE: EB DA DC EB BC CD ED DB CC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: EB QUEUE: DA DC EB BC CD ED DB CC EA EC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DA QUEUE: DC EB BC CD ED DB CC EA EC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DC QUEUE: EB BC CD ED DB CC EA EC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: EB QUEUE: BC CD ED DB CC EA EC Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: BC QUEUE: CD ED DB CC EA EC BD Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: CD QUEUE: ED DB CC EA EC BD Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: ED QUEUE: DB CC EA EC DB BD DD Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: DB QUEUE: CC EA EC DB CC BD DD DA DC EB Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: CC QUEUE: EA EC DB CC EA BD DD DA DC EB BC CD Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: EA QUEUE: EC DB CC EA EC BD DD DA DC EB BC CD Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: EC QUEUE: DB CC EA EC BD DD DA DC EB BC CD ED Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: DB QUEUE: CC EA EC BD DD DA DC EB BC CD ED Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: CC QUEUE: EA EC BD DD DA DC EB BC CD ED Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: EA QUEUE: EC BD DD DA DC EB BC CD ED Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: EC QUEUE: BD DD DA DC EB BC CD ED Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: BD QUEUE: DD DA DC EB BC CD ED AD Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
AT: DD QUEUE: DA DC EB BC CD ED DA AD Nodes are taken in the order: UP, LEFT, DOWN, RIGHT KEY: GREEN – Start RED – Exit YELLOW – Visited
Wow, that was still really inefficient. What would happen if we kept track of the nodes we visited?
AT: AA QUEUE: AB For each node, we will keep track of the node that we first came from to reach that node. KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: AB QUEUE: AC BB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: AC QUEUE: BB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: BB QUEUE: BA CB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: BA QUEUE: CB CA KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: CB QUEUE: CA DB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: CA QUEUE: DB DA KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DB QUEUE: DA DA DC EB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DA QUEUE: DA DC EB We still have the double DA effect occurring, but this time, we add no nodes to the queue since both neighbors of DA have already been visited. KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DA QUEUE: DC EB Ever have a feeling of déjà vu? KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DC QUEUE: EB CC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: EB QUEUE: CC EA EC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: CC QUEUE: EA EC BC CD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: EA QUEUE: EC BC CD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: EC QUEUE: BC CD ED KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: BC QUEUE: CD ED BD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: CD QUEUE: ED BD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: ED QUEUE: BD DD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: BD QUEUE: DD AD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
AT: DD QUEUE: AD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
That was better, wasn’t it That was better, wasn’t it? We still explored the same number of nodes in the maze but with less repetition. However, did we change the principle that BFS always finds the optimal path? The answer is no. Convince yourself as to why this is true.