Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
snowleopard committed Nov 2, 2017
1 parent b8dd0b9 commit c12297d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# path-finding
# Path finding

Experimenting with path-finding algorithms in Haskell

Build with `stack build` and run the example with `stack exec bfs`.
22 changes: 22 additions & 0 deletions examples/bfs.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Data.Array
import Tuura.BFS

box :: Graph
box = array (1, 4) [ (1, [2, 3])
, (2, [1, 4])
, (3, [1, 4])
, (4, [2, 3]) ]

tree :: Graph
tree = array (1, 7) [ (1, [2, 3])
, (2, [4, 5])
, (3, [6, 7])
, (4, [])
, (5, [])
, (6, [])
, (7, []) ]

main :: IO ()
main = do
print (bfs box 1)
print (bfs tree 1)
35 changes: 35 additions & 0 deletions path-finding.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: path-finding
version: 0.0.1
synopsis: Path-finding algorithms in Haskell
license: MIT
license-file: LICENSE
author: Andrey Mokhov <[email protected]>, github: @snowleopard
maintainer: Andrey Mokhov <[email protected]>, github: @snowleopard
copyright: 2017 Tuura, https://github.com/tuura
homepage: https://github.com/tuura/path-finding.git
category: Algorithms
build-type: Simple
cabal-version: >=1.10

source-repository head
type: git
location: https://github.com/tuura/path-finding.git

library
hs-source-dirs: src
exposed-modules: Tuura.BFS
build-depends: array == 0.5.*,
base >= 4.7 && < 5,
containers == 0.5.*
default-language: Haskell2010
GHC-options: -Wall -fno-warn-name-shadowing -fwarn-tabs -O2

executable bfs
hs-source-dirs: examples
main-is: bfs.hs
build-depends: array == 0.5.*,
base >= 4.7 && < 5,
containers == 0.5.*,
path-finding
default-language: Haskell2010
GHC-options: -Wall -fno-warn-name-shadowing -fwarn-tabs -O2
25 changes: 25 additions & 0 deletions src/Tuura/BFS.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Tuura.BFS (Graph, Vertex, bfs) where

import Data.Array
import Data.Sequence (Seq, ViewL (..), (><))
import Data.IntSet (IntSet)

import qualified Data.Sequence as Seq
import qualified Data.IntSet as Set

type Vertex = Int
type Graph = Array Vertex [Vertex]

bfs :: Graph -> Vertex -> [Vertex]
bfs graph vertex = visit Set.empty (Seq.singleton vertex)
where
visit :: IntSet -> Seq Vertex -> [Vertex]
visit visited queue = case Seq.viewl queue of
EmptyL -> []
v :< vs -> if Set.member v visited
then visit visited vs
else v : (visit newVisited newQueue)
where
toVisit = filter (\u -> Set.notMember u visited) (graph ! v)
newVisited = Set.insert v visited
newQueue = vs >< Seq.fromList toVisit
6 changes: 6 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resolver: lts-9.11
packages:
- .
extra-deps: []
flags: {}
extra-package-dbs: []

0 comments on commit c12297d

Please sign in to comment.