Yog.Pathfinding (YogEx v0.70.0)

Copy Markdown View Source

Unified facade for pathfinding algorithms.

This module provides a single entry point for all pathfinding and distance computation algorithms. Each function delegates to a specialized submodule.

Submodules

Algorithm Selection Guide

AlgorithmUse WhenTime Complexity
DijkstraNon-negative weights, single pairO((V+E) log V)
A*Non-negative weights + good heuristicO((V+E) log V)
Bellman-FordNegative weights OR cycle detectionO(VE)
BidirectionalLarge graphs, unweighted or uniform weightsO((V+E) log V)
Floyd-WarshallAll-pairs, dense graphsO(V³)
Johnson'sAll-pairs, sparse graphs, negative weightsO(V² log V + VE)

Summary

Functions

Finds the shortest path using A* search with a heuristic.

Alias for a_star/1.

Finds the shortest path using Bellman-Ford (supports negative weights).

Finds the shortest path using bidirectional Dijkstra.

Finds the shortest path using bidirectional BFS (unweighted graphs).

Detects negative cycles in the graph via Floyd-Warshall.

Computes all-pairs shortest paths using Floyd-Warshall.

Finds the shortest path between two nodes using Dijkstra's algorithm.

Single-source distances from a node to all reachable nodes (Dijkstra).

Functions

a_star(opts)

Finds the shortest path using A* search with a heuristic.

Options

  • :in - The graph to search
  • :from - Starting node ID
  • :to - Target node ID
  • :zero - Zero value for weights (default: 0)
  • :add - Addition function for weights (default: &Kernel.+/2)
  • :compare - Comparison function for weights (default: &Yog.Utils.compare/2)
  • :heuristic - Heuristic function (node, goal) -> weight (Mandatory)

astar(opts)

Alias for a_star/1.

bellman_ford(opts)

Finds the shortest path using Bellman-Ford (supports negative weights).

Returns {:error, :negative_cycle} if a negative cycle is reachable.

Options

  • :in - The graph to search
  • :from - Starting node ID
  • :to - Target node ID
  • :zero - Identity value for weights (default: 0)
  • :add - Addition function (default: &Kernel.+/2)
  • :compare - Comparison function (default: &Yog.Utils.compare/2)

bidirectional(opts)

Finds the shortest path using bidirectional Dijkstra.

Options

  • :in - The graph to search
  • :from - Starting node ID
  • :to - Target node ID
  • :zero - Zero value for weights (default: 0)
  • :add - Addition function (default: &Kernel.+/2)
  • :compare - Comparison function (default: &Yog.Utils.compare/2)

Example

iex> {:ok, graph} = Yog.directed()
...>   |> Yog.add_node(1, "A")
...>   |> Yog.add_node(2, "B")
...>   |> Yog.add_node(3, "C")
...>   |> Yog.add_edges([{1, 2, 5}, {2, 3, 3}, {1, 3, 10}])
iex> {:ok, path} = Yog.Pathfinding.bidirectional(
...>   in: graph, from: 1, to: 3
...> )
iex> path.weight
8

bidirectional_unweighted(opts)

Finds the shortest path using bidirectional BFS (unweighted graphs).

Options

  • :in - The graph to search
  • :from - Starting node ID
  • :to - Target node ID

detect_negative_cycle?(graph, zero, add, compare)

Detects negative cycles in the graph via Floyd-Warshall.

distance_matrix(graph, points, zero \\ 0, add \\ &Kernel.+/2, compare \\ &Yog.Utils.compare/2, subtract \\ nil)

Computes a distance matrix between specified points of interest.

Uses Dijkstra from each point to compute pairwise distances.

floyd_warshall(opts)

Computes all-pairs shortest paths using Floyd-Warshall.

Options

  • :in - The graph
  • :zero - Identity element (default: 0)
  • :add - Addition function (default: &Kernel.+/2)
  • :compare - Comparison function (default: &Yog.Utils.compare/2)

johnson(graph, zero \\ 0, add \\ &Kernel.+/2, subtract \\ &Kernel.-/2, compare \\ &Yog.Utils.compare/2)

Computes all-pairs shortest paths using Johnson's algorithm.

More efficient than Floyd-Warshall for sparse graphs. Supports negative edge weights (but not negative cycles).

shortest_path(opts)

Finds the shortest path between two nodes using Dijkstra's algorithm.

Options

  • :in - The graph to search
  • :from - Starting node ID
  • :to - Target node ID
  • :zero - Zero value for weights (default: 0)
  • :add - Addition function for weights (default: &Kernel.+/2)
  • :compare - Comparison function for weights (:lt, :eq, :gt) (default: &Yog.Utils.compare/2)

Example

iex> {:ok, graph} = Yog.directed()
...>   |> Yog.add_node(1, "A")
...>   |> Yog.add_node(2, "B")
...>   |> Yog.add_node(3, "C")
...>   |> Yog.add_edges([{1, 2, 5}, {2, 3, 3}, {1, 3, 10}])
iex> {:ok, path} = Yog.Pathfinding.shortest_path(
...>   in: graph, from: 1, to: 3
...> )
iex> path.weight
8

single_source_distances(opts)

Single-source distances from a node to all reachable nodes (Dijkstra).

Options

  • :in - The graph
  • :from - Source node
  • :zero - Zero value (default: 0)
  • :add - Addition function (default: &Kernel.+/2)
  • :compare - Comparison function (default: &Yog.Utils.compare/2)