Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
AT: AA QUEUE: AC BB AB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
5
AT: AC QUEUE: BB AB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
6
AT: BB QUEUE: AB AB BA CB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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.
15
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
16
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
17
AT: AC QUEUE: BB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
18
AT: BB QUEUE: BA CB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
19
AT: BA QUEUE: CB CA KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
20
AT: CB QUEUE: CA DB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
21
AT: CA QUEUE: DB DA KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
22
AT: DB QUEUE: DA DA DC EB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
23
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
24
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
25
AT: DC QUEUE: EB DB CC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
26
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
27
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
28
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
29
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
30
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
31
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
32
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
33
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
34
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
35
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
36
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
37
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
38
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
39
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
40
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
41
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
42
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
43
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
44
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
45
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
46
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
47
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
48
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
49
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
50
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
51
Wow, that was still really inefficient.
What would happen if we kept track of the nodes we visited?
52
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
53
AT: AB QUEUE: AC BB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
54
AT: AC QUEUE: BB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
55
AT: BB QUEUE: BA CB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
56
AT: BA QUEUE: CB CA KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
57
AT: CB QUEUE: CA DB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
58
AT: CA QUEUE: DB DA KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
59
AT: DB QUEUE: DA DA DC EB KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
60
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
61
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
62
AT: DC QUEUE: EB CC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
63
AT: EB QUEUE: CC EA EC KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
64
AT: CC QUEUE: EA EC BC CD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
65
AT: EA QUEUE: EC BC CD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
66
AT: EC QUEUE: BC CD ED KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
67
AT: BC QUEUE: CD ED BD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
68
AT: CD QUEUE: ED BD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
69
AT: ED QUEUE: BD DD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
70
AT: BD QUEUE: DD AD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
71
AT: DD QUEUE: AD KEY: GREEN – Start RED – Exit YELLOW – Visited Nodes are taken in the order: UP, LEFT, DOWN, RIGHT
72
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.