yog/operation
Graph operations - Set-theoretic operations, composition, and structural comparison.
This module implements binary operations that treat graphs as sets of nodes and edges, following NetworkX’s “Graph as a Set” philosophy. These operations allow you to combine, compare, and analyze structural differences between graphs.
Set-Theoretic Operations
| Function | Description | Use Case |
|---|---|---|
union/2 | All nodes and edges from both graphs | Combine graph data |
intersection/2 | Only nodes and edges common to both | Find common structure |
difference/2 | Nodes/edges in first but not second | Find unique structure |
symmetric_difference/2 | Edges in exactly one graph | Find differing structure |
Composition & Joins
| Function | Description | Use Case |
|---|---|---|
disjoint_union/2 | Combine with automatic ID re-indexing | Safe graph combination |
cartesian_product/2 | Multiply graphs (grids, hypercubes) | Generate complex structures |
compose/2 | Merge overlapping graphs with combined edges | Layered systems |
power/2 | k-th power (connect nodes within distance k) | Reachability analysis |
Structural Comparison
| Function | Description | Use Case |
|---|---|---|
is_subgraph/2 | Check if first is subset of second | Validation, pattern matching |
is_isomorphic/2 | Check if graphs are structurally identical | Graph comparison |
Example: Combining Graphs Safely
import yog
import yog/operation
// Two triangle graphs with overlapping IDs
let triangle1 = yog.from_simple_edges(Directed, [#(0, 1), #(1, 2), #(2, 0)])
let triangle2 = yog.from_simple_edges(Directed, [#(0, 1), #(1, 2), #(2, 0)])
// disjoint_union re-indexes the second graph automatically
let combined = operations.disjoint_union(triangle1, triangle2)
// Result: 6 nodes (0-5), two separate triangles
Example: Finding Common Structure
let common = operations.intersection(graph_a, graph_b)
// Returns only nodes and edges present in both graphs
Values
pub fn cartesian_product(
first: model.Graph(n, e),
second: model.Graph(m, f),
with_first default_first: f,
with_second default_second: e,
) -> model.Graph(#(n, m), #(e, f))
Returns the Cartesian product of two graphs.
pub fn compose(
first: model.Graph(n, e),
second: model.Graph(n, e),
) -> model.Graph(n, e)
Composes two graphs by merging overlapping nodes and combining their edges.
pub fn difference(
first: model.Graph(n, e),
second: model.Graph(n, e),
) -> model.Graph(n, e)
Returns a graph containing nodes and edges that exist in the first graph but not in the second.
pub fn disjoint_union(
base: model.Graph(n, e),
other: model.Graph(n, e),
) -> model.Graph(n, e)
Combines two graphs assuming they are separate entities with automatic re-indexing.
pub fn intersection(
first: model.Graph(n, e),
second: model.Graph(n, e),
) -> model.Graph(n, e)
Returns a graph containing only nodes and edges that exist in both input graphs.
pub fn is_isomorphic(
first: model.Graph(n, e),
second: model.Graph(n, e),
) -> Bool
Checks if two graphs are isomorphic (structurally identical).
pub fn is_subgraph(
potential: model.Graph(n, e),
container: model.Graph(n, e),
) -> Bool
Checks if the first graph is a subgraph of the second graph.
pub fn power(
graph: model.Graph(n, e),
k: Int,
default_weight: e,
) -> model.Graph(n, e)
Returns the k-th power of a graph.
The k-th power of a graph G, denoted G^k, is a graph where two nodes are adjacent if and only if their distance in G is at most k.
New edges are created with the provided default_weight.
pub fn symmetric_difference(
first: model.Graph(n, e),
second: model.Graph(n, e),
) -> model.Graph(n, e)
Returns a graph containing edges that exist in exactly one of the input graphs.
pub fn union(
base: model.Graph(n, e),
other: model.Graph(n, e),
) -> model.Graph(n, e)
Returns a graph containing all nodes and edges from both input graphs.