Giraffe.Algorithms.BellmanFord (giraffe v0.2.0)

Implementation of the Bellman-Ford algorithm for finding shortest paths in graphs.

Summary

Functions

Finds the shortest paths from a source vertex to all other vertices in the graph.

Types

adjacency_list()

@type adjacency_list() :: %{required(vertex()) => [{vertex(), vertex(), number()}]}

edge()

@type edge() :: {vertex(), vertex(), number()}

graph_struct()

@type graph_struct() :: %{edges: map(), vertices: MapSet.t()}

vertex()

@type vertex() :: any()

Functions

shortest_paths(graph, source)

@spec shortest_paths(adjacency_list() | graph_struct(), vertex()) ::
  {:ok, %{required(vertex()) => number()}} | {:error, :negative_cycle}

Finds the shortest paths from a source vertex to all other vertices in the graph.

Returns a map of vertices to their shortest distance from the source. If a negative cycle is detected, returns {:error, :negative_cycle}.