GithubHelp home page GithubHelp logo

drruisseau / scala-algorithms-bfs-dfs-astar-workshop Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mtumilowicz/scala-algorithms-bfs-dfs-astar-workshop

0.0 1.0 0.0 82 KB

Short introduction into BFS, DFS and A* algorithms with case-studies.

License: GNU General Public License v3.0

Scala 100.00%

scala-algorithms-bfs-dfs-astar-workshop's Introduction

Build Status License: GPL v3

scala-algorithms-bfs-dfs-astar-workshop

preface

  • goal of this workshop:
    • basic understanding of bfs, dfs and astar algorithms
    • the ability to apply theory in practice

dfs

val frontier = stack
frontier push initialNode

val explored = set

while (frontier.nonEmpty) {
  val current = frontier.pop
  if (current is goal) return Some(current)
  successors(current)
    .filter(not in explored)
    .foreach(child => {
      explored add child
      frontier push child
    })
}
None

note also that you have some structure to track path

case class Node[T](state: T, parent: Option[Node[T]] = Option.empty)

bfs

  • just take dfs and use queue instead of stack
  • always finds the shortest solution path where one exists
    • why?
      • try to find path from Cracow to Warsaw with fewest stops (by train)
      • dfs: keep going in the same direction and backtrack when a dead end
        • route could go through Poznan
      • bfs: check all of the stations one stop away from Cracow
        • then, two stops away from Cracow and so on
        • when you do find Warsaw, you will know you have found the route with the fewest stops
          • all of the stations that are fewer stops away from Cracow were checked

astar

  • one important aspect of A* is f = g + h
    • G - distance between the current node and the start node
    • H - heuristic (estimated distance from the current node to the end node)
    • F - total cost of the node
      • we can look at all our nodes and ask: "is this the best node I can pick to move forward?"
    • and slightly modified Node class
      case class AStarNode[T](
                               state: T,
                               parent: AStarNode[T] = null,
                               cost: Double = 0.0,
                               heuristic: Double = 0.0
                             )
      
  • code
    val frontier = priorityQueue // by total cost
    frontier enqueue initial
    val explored = map // state, cost
    explored put (initial, 0.0)
    while (frontier.nonEmpty) {
      val current = frontier.dequeue()
      if (current is goal) return Some(current)
      for (child <- successors(current)) {
        val newCost = currentNode.cost + 1
        if (child is not explored || explored(child) > newCost) {
          explored put (child, newCost)
          frontier enqueue child
        }
      }
    }
    None
    
    • explored(child) > newCost
      • check if path to that square is better, using G cost as the measure
      • a lower G cost means that this is a better path

practice

  • task 1
    • you have a maze (n x m) with start location (startX, startY), goal location (goalX, goalY) and possible walls
    • find path from start to goal
      • note that the path could not exist or there could be many solutions
    • example
      "S, , , ,X,X,X,X,X,X,"
      "X,X,X, ,X,X,X,X,X,X,"
      "X,X,X, ,X,X,X,X,X,X,"
      " , , , ,X,X,X,X,X,X,"
      " ,X,X,X,X,X,X,X,X,X,"
      " ,X,X,X,X,X,X,X,X,X,"
      " ,X,X,X,X,X,X,X,X,X,"
      " , , , , ,X,X,X,X,X,"
      "X,X,X,X, , , , , , ,"
      "X,X,X,X,X,X,G, , , ,"
      
      and the solution
      "S,*,*,*,X,X,X,X,X,X"
      "X,X,X,*,X,X,X,X,X,X"
      "X,X,X,*,X,X,X,X,X,X"
      "*,*,*,*,X,X,X,X,X,X"
      "*,X,X,X,X,X,X,X,X,X"
      "*,X,X,X,X,X,X,X,X,X"
      "*,X,X,X,X,X,X,X,X,X"
      "*,*,*,*,*,X,X,X,X,X"
      "X,X,X,X,*,*,*, , , "
      "X,X,X,X,X,X,G, , , "
      
  • task 2
    • three policemen and three thieves are on the west bank of a river
    • they all must cross to the opposite bank of the river
    • they have a riverboat that can hold two people
    • there may never be more thieves than policemen
    • riverboat must have at least one person on board to cross the river
    • return the sequence of crossings

scala-algorithms-bfs-dfs-astar-workshop's People

Contributors

mtumilowicz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.