Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Breadth-first Search (BFS) Algorithm in Ruby

Breadth-first Search (BFS) Algorithm in Ruby

Breadth-first search algorithm in Ruby implemented as a maze solution finder.

Avatar for brianledsworth

brianledsworth

July 19, 2012
Tweet

Other Decks in Programming

Transcript

  1. Background • Graph Traversal Algorithms • http://en.wikipedia.org/wiki/Graph_search_algorithm - 24 graph

    algorithms of 7/17/2012 • Many flavors, each crafted for a specific use, some more complicated than others. • Learn basic BFS and DFS first as a foundation, before diving into the more complicated algorithms (e.g. A* or Dijkstra's) • BFS usage: Finding shortest path Thursday, July 19, 2012
  2. Application • Shortest path through a maze • Ruby •

    Mazes hydrated from text files Thursday, July 19, 2012
  3. How do we do this? • First, classify problem •

    Second, understand the algorithm • Third, code Thursday, July 19, 2012
  4. Data Structures / Approach • Queue • Linked List •

    Simple loop Thursday, July 19, 2012
  5. Let’s do this manually 1) Take three friends and have

    each walk one of the three paths 2) Walk at the same pace 3) First out = shortest path! Thursday, July 19, 2012
  6. How does a Queue Help “...at same pace”? • Place

    entrance on a queue • Loop until exit found: • Pop queue • Is exit? • Place all possible directions available for the next step on the queue Thursday, July 19, 2012
  7. How does a Queue Help? • Ensure you “walk” (traverse)

    each path (nodes/ vertices) at the same pace • As new child pathways appear, they’re “walked” at the same pace as their siblings Thursday, July 19, 2012
  8. What about the Linked List? • Each time a new

    square/step is “walked”, it’s linked to its parent • When the exit is found, the path is simply reconstructed by traversing back up the list • All square/steps not in the solution are ignored Thursday, July 19, 2012