Solving Mazes Constraint propagation, Graph traversal, and - - PowerPoint PPT Presentation

solving mazes
SMART_READER_LITE
LIVE PREVIEW

Solving Mazes Constraint propagation, Graph traversal, and - - PowerPoint PPT Presentation

Solving Mazes Constraint propagation, Graph traversal, and Backtracking Constraint Satisfaction Given some constraints, can I find a solution? e.g. Given the contents of my fridge, is there a nutritious dinner to be made? e.g. Can we


slide-1
SLIDE 1

Solving Mazes

Constraint propagation, Graph traversal, and Backtracking

slide-2
SLIDE 2

Constraint Satisfaction

  • Given some constraints, can I find a solution?
  • e.g. Given the contents of my fridge, is there a

nutritious dinner to be made?

  • e.g. Can we find some values such that this

equation evaluates to True? A & ~B

slide-3
SLIDE 3

Constraint Propagation

  • Can we reduce the number of constraints to our

problem?

  • e.g. We have hour long classes.
  • 1 at 10, 1 at 10:30, 2 at 11.
  • How many classrooms will we need?
slide-4
SLIDE 4

Graph Traversal

  • One of the most fundamental graph problems is to

traverse every edge and vertex in a graph.

  • For correctness, we must do the traversal in a

systematic way so that we don’t miss anything.

  • For efficiency, we must make sure we visit each edge at

most twice.

  • Since a maze is just a graph, such an algorithm must

be powerful enough to enable us to get out of an arbitrary maze.

slide-5
SLIDE 5

Marking Vertices

  • The key idea is that we must mark each vertex when we first visit it, and keep track
  • f what have not yet completely explored.
  • Each vertex will always be in one of the following three states:
  • 1. Undiscovered: the vertex in its initial, untouched state. 

  • 2. Discovered: the vertex after we have encountered it, but before we have 


checked out all its incident edges. 


  • 3. Processed: the vertex after we have visited all its incident edges. 

  • A vertex cannot be processed before we discover it, so over the course of the

traversal the state of each vertex progresses from undiscovered to discovered to processed.

slide-6
SLIDE 6

To Do List

  • We must also maintain a structure containing all the vertices

we have discovered but not yet completely explored.

  • Initially, only a single start vertex is considered to be

discovered.

  • To completely explore a vertex, we look at each edge going
  • ut of it. For each edge which goes to an undiscovered vertex,

we mark it discovered and add it to the list of work to do.

  • Note that regardless of what order we fetch the next vertex to

explore, each edge is considered exactly twice, when each of its endpoints are explored.

slide-7
SLIDE 7

In what order should we process vertices?

  • Breadth first search: First-in, First-out
  • Depth first search: Last-in, First-out
slide-8
SLIDE 8

Depth-first Search

slide-9
SLIDE 9

Depth-first Search

https://brilliant.org/wiki/depth-first-search-dfs/

slide-10
SLIDE 10

Backtracking

  • A general algorithm for finding all (or some)

solutions to some computational problems, notably constraint satisfaction problems. - Wikipedia

  • Term coined in the 1950’s by D. H. Lehmer
slide-11
SLIDE 11

Eight Queens Problem

  • Give all arrangements of eight queens on an 8x8

chessboard so that no queen attacks another.

  • No two queens are on the same row, column, or

diagonal

  • Problem invented by Max Bezel in 1848
slide-12
SLIDE 12
slide-13
SLIDE 13

Algorithm

Start in the leftmost column If all queens are placed, return true for (every possible choice among the rows in this column): if the queen can be placed safely there, make that choice and then recursively try to place the rest of the queens if recursion successful, return true
 if ! successful, remove queen and try another row in this column if all rows have been tried and nothing worked, return false to trigger backtracking

slide-14
SLIDE 14

Solving Mazes using Backtracking

https://www.cs.bu.edu/teaching/alg/maze/