Result of pathfinding algorithms (Dijkstra, A*, BFS, etc.).
Represents a path through the graph as a sequence of nodes with a total weight and metadata about how the path was found.
Fields
nodes- Ordered list of node IDs forming the pathweight- Total weight/cost of the pathalgorithm- Name of the algorithm used (optional)metadata- Optional metadata (visited nodes, iterations, time, etc.)
Examples
iex> path = %Yog.Pathfinding.Path{
...> nodes: [1, 2, 3, 4],
...> weight: 15.5,
...> algorithm: :dijkstra
...> }
iex> path.weight
15.5
iex> Yog.Pathfinding.Path.length(path)
3
iex> Yog.Pathfinding.Path.start(path)
1
iex> Yog.Pathfinding.Path.finish(path)
4
Summary
Functions
Returns the node at a specific position in the path (0-indexed).
Checks if a node is part of the path.
Checks if the path is empty (contains no nodes).
Returns the ending node of the path.
Backward compatibility: convert from legacy map format.
Creates a path from the legacy tuple format {:path, nodes, weight}.
Returns the length of the path (number of edges).
Creates a new path result.
Creates a new path result with algorithm name.
Creates a new path result with algorithm and metadata.
Reverses the path (both node order and preserves weight).
Returns the starting node of the path.
Convert to legacy map format.
Converts the path to a legacy tuple format {:path, nodes, weight}.
Types
@type t() :: %Yog.Pathfinding.Path{ algorithm: atom(), metadata: map(), nodes: [Yog.Model.node_id()], weight: number() }
Functions
@spec at(t(), non_neg_integer()) :: Yog.Model.node_id() | nil
Returns the node at a specific position in the path (0-indexed).
Returns nil if the index is out of bounds.
Examples
iex> path = Yog.Pathfinding.Path.new([1, 2, 3], 10)
iex> Yog.Pathfinding.Path.at(path, 0)
1
iex> Yog.Pathfinding.Path.at(path, 2)
3
iex> Yog.Pathfinding.Path.at(path, 5)
nil
@spec contains?(t(), Yog.Model.node_id()) :: boolean()
Checks if a node is part of the path.
Examples
iex> path = Yog.Pathfinding.Path.new([1, 2, 3], 10)
iex> Yog.Pathfinding.Path.contains?(path, 2)
true
iex> Yog.Pathfinding.Path.contains?(path, 5)
false
Checks if the path is empty (contains no nodes).
Examples
iex> path = Yog.Pathfinding.Path.new([], 0)
iex> Yog.Pathfinding.Path.empty?(path)
true
iex> path = Yog.Pathfinding.Path.new([1, 2], 5)
iex> Yog.Pathfinding.Path.empty?(path)
false
@spec finish(t()) :: Yog.Model.node_id() | nil
Returns the ending node of the path.
Returns nil if the path is empty.
Examples
iex> path = Yog.Pathfinding.Path.new([1, 2, 3], 10)
iex> Yog.Pathfinding.Path.finish(path)
3
iex> path = Yog.Pathfinding.Path.new([], 0)
iex> Yog.Pathfinding.Path.finish(path)
nil
Backward compatibility: convert from legacy map format.
@spec from_tuple({:path, [Yog.Model.node_id()], number()}) :: t()
Creates a path from the legacy tuple format {:path, nodes, weight}.
Examples
iex> path = Yog.Pathfinding.Path.from_tuple({:path, [1, 2, 3], 10})
iex> path.nodes
[1, 2, 3]
iex> path.weight
10
@spec length(t()) :: non_neg_integer()
Returns the length of the path (number of edges).
The length is the number of nodes minus 1. An empty path or single-node path has length 0.
Examples
iex> path = Yog.Pathfinding.Path.new([1, 2, 3, 4], 15)
iex> Yog.Pathfinding.Path.length(path)
3
iex> path = Yog.Pathfinding.Path.new([1], 0)
iex> Yog.Pathfinding.Path.length(path)
0
iex> path = Yog.Pathfinding.Path.new([], 0)
iex> Yog.Pathfinding.Path.length(path)
0
@spec new([Yog.Model.node_id()], number()) :: t()
Creates a new path result.
Examples
iex> path = Yog.Pathfinding.Path.new([1, 2, 3], 10)
iex> path.nodes
[1, 2, 3]
iex> path.weight
10
@spec new([Yog.Model.node_id()], number(), atom()) :: t()
Creates a new path result with algorithm name.
Examples
iex> path = Yog.Pathfinding.Path.new([1, 2, 3], 10, :dijkstra)
iex> path.algorithm
:dijkstra
@spec new([Yog.Model.node_id()], number(), atom(), map()) :: t()
Creates a new path result with algorithm and metadata.
Examples
iex> path = Yog.Pathfinding.Path.new([1, 2, 3], 10, :astar, %{visited: 42})
iex> path.metadata
%{visited: 42}
Reverses the path (both node order and preserves weight).
Examples
iex> path = Yog.Pathfinding.Path.new([1, 2, 3], 10, :dijkstra)
iex> reversed = Yog.Pathfinding.Path.reverse(path)
iex> reversed.nodes
[3, 2, 1]
iex> reversed.weight
10
iex> reversed.algorithm
:dijkstra
@spec start(t()) :: Yog.Model.node_id() | nil
Returns the starting node of the path.
Returns nil if the path is empty.
Examples
iex> path = Yog.Pathfinding.Path.new([1, 2, 3], 10)
iex> Yog.Pathfinding.Path.start(path)
1
iex> path = Yog.Pathfinding.Path.new([], 0)
iex> Yog.Pathfinding.Path.start(path)
nil
Convert to legacy map format.
@spec to_tuple(t()) :: {:path, [Yog.Model.node_id()], number()}
Converts the path to a legacy tuple format {:path, nodes, weight}.
Examples
iex> path = Yog.Pathfinding.Path.new([1, 2, 3], 10)
iex> Yog.Pathfinding.Path.to_tuple(path)
{:path, [1, 2, 3], 10}