Yog.Multi.Graph (YogEx v0.97.1)

Copy Markdown View Source

Core multigraph data structure.

A multigraph allows multiple (parallel) edges between the same pair of nodes. Both directed and undirected variants are supported.

The internal representation keeps three indexes:

  • edges: EdgeId → {from, to, data} — canonical edge store
  • out_edge_ids: NodeId → [EdgeId] — outgoing edges per node
  • in_edge_ids: NodeId → [EdgeId] — incoming edges per node

Fields

  • kind - Either :directed or :undirected
  • nodes - Map from node ID to node data
  • edges - Map from edge ID to {from, to, data}
  • out_edge_ids - Map from node ID to MapSet of edge IDs
  • in_edge_ids - Map from node ID to MapSet of edge IDs
  • next_edge_id - Counter for generating unique edge IDs

Examples

iex> multi = Yog.Multi.Graph.new(:directed)
iex> multi.kind
:directed
iex> multi.next_edge_id
0

Summary

Functions

Creates a new empty directed multigraph.

Returns the total number of edges (including parallel edges).

Creates a new empty multigraph of the given type.

Creates a new empty undirected multigraph.

Types

edge_id()

@type edge_id() :: non_neg_integer()

t()

@type t() :: %Yog.Multi.Graph{
  edges: %{
    required(edge_id()) => {Yog.Model.node_id(), Yog.Model.node_id(), term()}
  },
  in_edge_ids: %{required(Yog.Model.node_id()) => MapSet.t(edge_id())},
  kind: :directed | :undirected,
  next_edge_id: non_neg_integer(),
  nodes: %{required(Yog.Model.node_id()) => term()},
  out_edge_ids: %{required(Yog.Model.node_id()) => MapSet.t(edge_id())}
}

Functions

directed()

@spec directed() :: t()

Creates a new empty directed multigraph.

edge_count(graph)

@spec edge_count(t()) :: non_neg_integer()

Returns the total number of edges (including parallel edges).

new(kind)

@spec new(:directed | :undirected) :: t()

Creates a new empty multigraph of the given type.

Examples

iex> multi = Yog.Multi.Graph.new(:directed)
iex> multi.kind
:directed
iex> Enum.count(multi.nodes)
0

iex> multi = Yog.Multi.Graph.new(:undirected)
iex> multi.kind
:undirected

undirected()

@spec undirected() :: t()

Creates a new empty undirected multigraph.