Core graph data structure.
A graph is represented as a struct with four fields:
kind: Either:directedor:undirectednodes: Map of node_id => node_dataout_edges: Map of node_id => %{neighbor_id => weight}in_edges: Map of node_id => %{neighbor_id => weight}
The dual-map representation (storing both out_edges and in_edges) enables:
- O(1) graph transpose (just swap out_edges ↔ in_edges)
- Efficient predecessor queries without traversing the entire graph
- Fast bidirectional edge lookups
Examples
iex> %Yog.Graph{
...> kind: :directed,
...> nodes: %{1 => "A", 2 => "B"},
...> out_edges: %{1 => %{2 => 10}},
...> in_edges: %{2 => %{1 => 10}}
...> }Protocols
Yog.Graph implements the Enumerable and Inspect protocols:
- Enumerable: Iterates over nodes as
{id, data}tuples - Inspect: Compact representation showing graph type and statistics
Summary
Functions
Returns the total number of edges in the graph.
Creates a new empty graph of the given type.
Returns the number of nodes in the graph.
Types
Functions
@spec edge_count(t()) :: non_neg_integer()
Returns the total number of edges in the graph.
For undirected graphs, this counts each edge once (not twice).
Creates a new empty graph of the given type.
@spec node_count(t()) :: non_neg_integer()
Returns the number of nodes in the graph.