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

FunctionDescriptionUse Case
union/2All nodes and edges from both graphsCombine graph data
intersection/2Only nodes and edges common to bothFind common structure
difference/2Nodes/edges in first but not secondFind unique structure
symmetric_difference/2Edges in exactly one graphFind differing structure

Composition & Joins

FunctionDescriptionUse Case
disjoint_union/2Combine with automatic ID re-indexingSafe graph combination
cartesian_product/2Multiply graphs (grids, hypercubes)Generate complex structures
compose/2Merge overlapping graphs with combined edgesLayered systems
power/2k-th power (connect nodes within distance k)Reachability analysis

Structural Comparison

FunctionDescriptionUse Case
is_subgraph/2Check if first is subset of secondValidation, pattern matching
is_isomorphic/2Check if graphs are structurally identicalGraph 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.

Search Document