Skip to content
FutureBlitz

    Install

    Import std.algo.search

    Dijkstra

    std.algo.search

    function

    @PanicsAtCaller Public Function Dijkstra<N, C>(start: N, successors: Func(N): (N, C)[], goal: Func(N): Bool): Path<N, C>?

    The cheapest path from start to the first node that goal says is one, by Dijkstra's algorithm, with each step's weight from successors, or Null when none it reaches is. A negative or NaN weight panics.

    Examples

    // Roads between towns, each with how long it is.
    Local roads = {"a": [("b", 7), ("c", 2)], "b": [("d", 1)], "c": [("b", 3), ("d", 8)], "d": []}
    Local next = Function(town: String) => roads[town]
    Local path = Dijkstra("a", next, Function(town: String) => town == "d")
    Expect(path?.Nodes == ["a", "c", "b", "d"])
    Expect(path?.Cost == 6)

    std/algo/search.fb, line 349

    Report an issue with this page or with FutureBlitz on GitHub.