17
Breadth-First Search David Johnson

Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

Embed Size (px)

Citation preview

Page 1: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

Breadth-First Search

David Johnson

Page 2: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

Today

• Look at one version of Breadth-first search on a grid

• Develop Matlab version of BFS

Page 3: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

Wavefront planner

• Use BFS on a grid• Label cells with values

– Zero means unchecked– 1 is obstacle– Set start cell to 2

• Expand from start • Add +1 to neighbors of current wavefront• Use gradient descent to search from goal

to start

Page 4: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

Representations: A Grid

• Distance is reduced to discrete steps– For simplicity, we’ll assume distance is uniform

• Direction is now limited from one adjacent cell to another

Page 5: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

Representations: Connectivity

• 8-Point Connectivity– (chessboard metric)

• 4-Point Connectivity– (Manhattan metric)

Page 6: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

The Wavefront Planner: Setup

Page 7: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

The Wavefront in Action (Part 1)

• Starting with the goal, set all adjacent cells with “0” to the current cell + 1– 4-Point Connectivity or 8-Point Connectivity?– Your Choice. We’ll use 8-Point Connectivity in our example

Page 8: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

The Wavefront in Action (Part 2)

• Now repeat with the modified cells– This will be repeated until goal is reached

• 0’s will only remain when regions are unreachable

Page 9: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

The Wavefront in Action (Part 3)

• Repeat again...

Page 10: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

The Wavefront in Action (Part 4)

• And again...

Page 11: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

The Wavefront in Action (Part 5)

• And again until...

Page 12: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

The Wavefront in Action (Done)

• You’re done

Page 13: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

The Wavefront

• To find the shortest path simply always move toward a cell with a lower number– The numbers generated by the Wavefront planner are roughly

proportional to their distance from the goal

Twopossibleshortestpathsshown

Page 14: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

Developing the Breadth-First Search Algorithm

• Store the list of graph vertices under consideration (the wavefront).– Store indices rather than the whole vertex

• Look for successors of the wavefront• Put the successors back on the wavefront

– Where should they go?

1st gen 2nd gen 3rd gen

Page 15: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

Queue

• A queue is a datastructure where you can– Add elements to the back– Take an element off the front

• First in the queue is first out– FIFO

Page 16: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

Queue

• Matlab can easily make a queue– Pop element from front

element = wavefront(1)

wavefront = wavefront(2:end)

– Add to endwavefront = [wavefront newelement]

Page 17: Breadth-First Search David Johnson. Today Look at one version of Breadth-first search on a grid Develop Matlab version of BFS

Matlab

• Develop BFS in Matlab