-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8dd0b9
commit c12297d
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |