maze problem
------------------
|1 | | |
---- - -------- --
| | |
-- ---------- ----
| | |
--- -- ----- -----
| | |
-------- ---------
| 0|
------------------
- how can we pose this problem as a search problem?
- for a search problem, we need some notion of a node and neighbors
- node, is the position in the maze
- neighbors are then non-wall adjacent cells
- what would happen if we run BFS on this maze?
------------------
|1***| | |
----*- -------- --
| | |
-- ---------- ----
| | |
--- -- ----- -----
| | |
- ------ ---------
| 0|
------------------
------------------
|1***| | |
----*-* -------- --
|******| |
--*---------- ----
| | |
--- -- ----- -----
| | |
- ------ ---------
| 0|
------------------
------------------
|1***|***** | |
----*-*-------- --
|******| |
--*---------- ----
|****| |
---*-- ----- -----
| *** | |
- ------ ---------
| 0|
------------------
- slowly branches out comparing all positions at distance X away from the starting point before moving on
- what about DFS?
------------------
|1***| | |
----*- -------- --
| | |
-- ---------- ----
| | |
--- -- ----- -----
| | |
- ------ ---------
| 0|
------------------
------------------
|1***| | |
----*- -------- --
|**** | |
--*---------- ----
|*** | |
---*-- ----- -----
| | |
- ------ ---------
| 0|
------------------
------------------
|1***| | |
----*- -------- --
|**** | |
--*---------- ----
|*** | |
---*-- ----- -----
|*** | |
-*------ ---------
|*** 0|
------------------
- always going to try and go deeper. Similar to following one wall (in the example above, the right wall)
- which is better?
- DFS will probably find the exit faster
- when is DFS slower?
- BFS will find THE shortest path from the start to the finish
- at each step it examines all paths that are one step further, so when it finds the exit, we know there are no shorter paths
- proof
- starting at node s, prove that when we get to t, that is a shortest path from s to t
- assume a shorter path exists that was not found by BFS from s to t
- Consider some u that the algorithm “misses” along the shortest path
- Choose any path from s to u and look at the last vertex on that path that we
actually visited
- Call this node z
- let w be the node immediately after it on the same path. z would be visited
and w was not, which is a contradiction
above.