MyNixOS website logo
Description

Common graph search algorithms.

Library containing common graph search algorithms, including depth-first and breadth-first searches, Dijkstra's algorithm, and A*

search-algorithms

Haskell library containing common graph search algorithms

Build Status

Lots of problems can be modeled as graphs, but oftentimes one doesn't want to use an explicit graph structure to represent the problem. Maybe the graph would be too big (or is infinite), maybe making an explicit graph is unwieldy for the problem at hand, or maybe one just wants to generalize over graph implementations. That's where this library comes in: this is a collection of generalized search algorithms, so that one doesn't have to make the graphs explicit. In general, this means that one provides each search function with a function to generate neighboring states, possibly some functions to generate additional information for the search, a predicate which tells when the search is complete, and an initial state to start from. The result is a path from the initial state to a "solved" state, or Nothing if no such path is possible.

Documentation

Documentation is hosted on Hackage.

Acknowledgements

This library shares a similar functionality with the astar library (which I was unaware of when I released the first version of this library). astar's interface has since influenced the development of this library's interface, and this library owes a debt of gratitude to astar for that reason.

Examples

Change-making problem

import Algorithm.Search (bfs)

countChange target = bfs (add_one_coin `pruning` (> target)) (== target) 0
  where
    add_one_coin amt = map (+ amt) coins
    coins = [1, 5, 10, 25]

-- countChange gives the subtotals along the way to the end:
-- >>> countChange 67
-- Just [1, 2, 7, 17, 42, 67]

Simple directed acyclic graph:

import Algorithm.Search (dfs)
import qualified Data.Map as Map

graph = Map.fromList [
  (1, [2, 3]),
  (2, [4]),
  (3, [4]),
  (4, [])
  ]

-- Run dfs on the graph:
-- >>> dfs (graph Map.!) (== 4) 1
-- Just [3,4]

Using A* to find a path in an area with a wall:

import Algorithm.Search (aStar)

taxicabNeighbors :: (Int, Int) -> [(Int, Int)]
taxicabNeighbors (x, y) = [(x, y + 1), (x - 1, y), (x + 1, y), (x, y - 1)]

isWall :: (Int, Int) -> Bool
isWall (x, y) = x == 1 && (-2) <= y && y <= 1

taxicabDistance :: (Int, Int) -> (Int, Int) -> Int
taxicabDistance (x1, y1) (x2, y2) = abs (x2 - x1) + abs (y2 - y1)

findPath :: (Int, Int) -> (Int, Int) -> Maybe (Int, [(Int, (Int, Int))])
findPath start end =
  let next = taxicabNeighbors
      cost = taxicabDistance
      remaining = (taxicabDistance end)
  in aStar (next `pruning` isWall) cost remaining (== end) start

-- findPath p1 p2 finds a path between p1 and p2, avoiding the wall
-- >>> findPath (0, 0) (2, 0)
-- Just (6,[(0,1),(0,2),(1,2),(2,2),(2,1),(2,0)])
--
-- This correctly goes up and around the wall
Metadata

Version

0.3.2

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows