Yog.Multi.Model (YogEx v0.97.0)

Copy Markdown View Source

Core multigraph type and basic operations.

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

The internal representation keeps three indices:

  • 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

Summary

Functions

Adds an edge from from to to with the given data.

Adds a node with the given ID and data. If the node already exists, its data is replaced (edges are unaffected).

Returns all edge IDs in the graph.

Returns all node IDs in the multigraph.

Creates a new, empty directed multigraph.

Returns all parallel edges between from and to as [{edge_id, edge_data}].

Returns true if an edge with this ID exists in the graph.

Returns the in-degree of a node (number of incoming edges).

Creates a new, empty multigraph of the given type.

Returns the number of nodes (graph order).

Returns the out-degree of a node (number of outgoing edges). For undirected graphs, this equals the total degree.

Returns all incoming edges to id as [{from_node, edge_id, edge_data}].

Removes a single edge by its EdgeId. For undirected graphs, both direction-index entries are removed.

Removes a node and all edges connected to it.

Returns the total number of edges (graph size). For undirected graphs, each physical edge is counted once.

Returns all outgoing edges from id as [{to_node, edge_id, edge_data}].

Collapses the multigraph into a simple Yog.graph() by combining parallel edges with combine_fn(existing, new).

Collapses parallel edges, keeping the minimum weight.

Collapses parallel edges, summing weights.

Creates a new, empty undirected multigraph.

Types

edge_id()

@type edge_id() :: integer()

t()

@type t() :: Yog.Multi.Model.Graph.t()

Functions

add_edge(graph, from, to, data)

@spec add_edge(t(), Yog.node_id(), Yog.node_id(), any()) :: {t(), edge_id()}

Adds an edge from from to to with the given data.

Returns {updated_graph, new_edge_id}.

For undirected graphs, a single EdgeId is issued and the reverse direction is indexed automatically.

add_node(graph, id, data)

@spec add_node(t(), Yog.node_id(), any()) :: t()

Adds a node with the given ID and data. If the node already exists, its data is replaced (edges are unaffected).

all_edge_ids(graph)

@spec all_edge_ids(t()) :: [edge_id()]

Returns all edge IDs in the graph.

all_nodes(graph)

@spec all_nodes(t()) :: [Yog.node_id()]

Returns all node IDs in the multigraph.

directed()

@spec directed() :: t()

Creates a new, empty directed multigraph.

edges_between(graph, from, to)

@spec edges_between(t(), Yog.node_id(), Yog.node_id()) :: [{edge_id(), any()}]

Returns all parallel edges between from and to as [{edge_id, edge_data}].

has_edge(graph, edge_id)

@spec has_edge(t(), edge_id()) :: boolean()

Returns true if an edge with this ID exists in the graph.

in_degree(graph, id)

@spec in_degree(t(), Yog.node_id()) :: integer()

Returns the in-degree of a node (number of incoming edges).

new(graph_type)

@spec new(Yog.graph_type()) :: t()

Creates a new, empty multigraph of the given type.

order(graph)

@spec order(t()) :: integer()

Returns the number of nodes (graph order).

out_degree(graph, id)

@spec out_degree(t(), Yog.node_id()) :: integer()

Returns the out-degree of a node (number of outgoing edges). For undirected graphs, this equals the total degree.

predecessors(graph, id)

@spec predecessors(t(), Yog.node_id()) :: [{Yog.node_id(), edge_id(), any()}]

Returns all incoming edges to id as [{from_node, edge_id, edge_data}].

remove_edge(graph, edge_id)

@spec remove_edge(t(), edge_id()) :: t()

Removes a single edge by its EdgeId. For undirected graphs, both direction-index entries are removed.

remove_node(graph, id)

@spec remove_node(t(), Yog.node_id()) :: t()

Removes a node and all edges connected to it.

size(graph)

@spec size(t()) :: integer()

Returns the total number of edges (graph size). For undirected graphs, each physical edge is counted once.

successors(graph, id)

@spec successors(t(), Yog.node_id()) :: [{Yog.node_id(), edge_id(), any()}]

Returns all outgoing edges from id as [{to_node, edge_id, edge_data}].

to_simple_graph(graph, combine_fn)

@spec to_simple_graph(t(), (any(), any() -> any())) :: Yog.graph()

Collapses the multigraph into a simple Yog.graph() by combining parallel edges with combine_fn(existing, new).

Example

Keep minimum weight among parallel edges:

multi.to_simple_graph(mg, fn a, b -> min(a, b) end)

to_simple_graph_min_edges(graph)

@spec to_simple_graph_min_edges(t()) :: Yog.graph()

Collapses parallel edges, keeping the minimum weight.

to_simple_graph_sum_edges(graph, add)

@spec to_simple_graph_sum_edges(t(), (any(), any() -> any())) :: Yog.graph()

Collapses parallel edges, summing weights.

undirected()

@spec undirected() :: t()

Creates a new, empty undirected multigraph.