Yog.Operation (YogEx v1.0.0)

Copy Markdown View Source

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

FunctionComplexityDescriptionUse Case
union/2$\mathcal{O}(V_1 + V_2 + E_1 + E_2)$All nodes and edges from both graphsCombine graph data
intersection/2$\mathcal{O}(V + E)$Only nodes and edges common to bothFind common structure
difference/2$\mathcal{O}(V + E)$Nodes/edges in first but not secondFind unique structure
symmetric_difference/2$\mathcal{O}(V_1 + V_2 + E_1 + E_2)$Edges in exactly one graphFind differing structure

Composition & Joins

FunctionComplexityDescriptionUse Case
disjoint_union/2$\mathcal{O}(V_1 + V_2 + E_1 + E_2)$Combine with automatic ID re-indexingSafe graph combination
cartesian_product/4$\mathcal{O}(V_1 V_2 + E_1 V_2 + E_2 V_1)$Multiply graphs (grids, hypercubes)Generate complex structures
tensor_product/2$\mathcal{O}(V_1 V_2 + E_1 E_2)$Kronecker direct productProduct graphs
strong_product/4$\mathcal{O}(V_1 V_2 + E_1 V_2 + E_2 V_1 + E_1 E_2)$Strong product (grid + diagonals)Spatial topologies
lexicographic_product/4$\mathcal{O}(V_1 V_2 + E_1 V_2^2 + V_1 E_2)$Graph compositionHierarchical substitution
compose/2$\mathcal{O}(V_1 + V_2 + E_1 + E_2)$Merge overlapping graphsLayered systems
line_graph/2$\mathcal{O}(E^2)$Convert edges to nodesEdge-centric analysis
power/3$\mathcal{O}(V \cdot (V + E))$$k$-th power (distance $\le k$)Reachability analysis

Structural Comparison

FunctionComplexityDescriptionUse Case
subgraph?/2$\mathcal{O}(V_p + E_p)$Check if first is subset of secondValidation, pattern matching
isomorphic?/2Exponential worst-caseCheck if graphs are structurally identicalStructural equivalence

Summary

Functions

Returns the Cartesian product of two graphs.

Composes two graphs by merging overlapping nodes and combining their edges.

Returns a graph containing nodes and edges that exist in the first graph but not in the second.

Computes the disjoint union of two graphs.

Returns a graph containing only nodes and edges that exist in both input graphs.

Checks if two graphs are isomorphic (structurally identical).

Returns the Lexicographic product (composition) of two graphs.

Returns the line graph of a graph.

Returns the $k$-th power of a graph (connecting nodes at distance $\le k$).

Returns the Strong product of two graphs.

Checks if the first graph is a subgraph of the second graph.

Returns a graph containing edges that exist in exactly one of the input graphs.

Returns the Tensor product (Kronecker product) of two graphs.

Returns a graph containing all nodes and edges from both input graphs.

Functions

cartesian_product(first, second, default_first, default_second)

@spec cartesian_product(Yog.Graph.t(), Yog.Graph.t(), any(), any()) :: Yog.Graph.t()

Returns the Cartesian product of two graphs.

Creates a new graph where each node represents a pair of nodes from the input graphs.

Time Complexity: $\mathcal{O}(V_1 V_2 + E_1 V_2 + E_2 V_1)$

compose(first, second)

@spec compose(Yog.Graph.t(), Yog.Graph.t()) :: Yog.Graph.t()

Composes two graphs by merging overlapping nodes and combining their edges.

Time Complexity: $\mathcal{O}(V_1 + V_2 + E_1 + E_2)$

difference(first, second)

@spec difference(Yog.Graph.t(), Yog.Graph.t()) :: Yog.Graph.t()

Returns a graph containing nodes and edges that exist in the first graph but not in the second.

Both graphs must have the same kind (:directed or :undirected).

Time Complexity: $\mathcal{O}(V + E)$

Errors

disjoint_union(graph_a, graph_b)

@spec disjoint_union(Yog.Graph.t(), Yog.Graph.t()) :: Yog.Graph.t()

Computes the disjoint union of two graphs.

Guarantees that nodes from Graph A and Graph B remain distinct by tagging their IDs as {0, id} and {1, id}.

Time Complexity: $\mathcal{O}(V_1 + V_2 + E_1 + E_2)$

intersection(first, second)

@spec intersection(Yog.Graph.t(), Yog.Graph.t()) :: Yog.Graph.t()

Returns a graph containing only nodes and edges that exist in both input graphs.

Both graphs must have the same kind (:directed or :undirected).

Time Complexity: $\mathcal{O}(V + E)$

Errors

isomorphic?(first, second)

@spec isomorphic?(Yog.Graph.t(), Yog.Graph.t()) :: boolean()

Checks if two graphs are isomorphic (structurally identical).

Both graphs must have the same kind (:directed or :undirected).

Time Complexity: Exponential in worst case due to backtracking.

Errors

lexicographic_product(first, second, default_first, default_second)

@spec lexicographic_product(Yog.Graph.t(), Yog.Graph.t(), any(), any()) ::
  Yog.Graph.t()

Returns the Lexicographic product (composition) of two graphs.

Time Complexity: $\mathcal{O}(V_1 V_2 + E_1 V_2^2 + V_1 E_2)$

line_graph(graph, default_weight \\ 1)

@spec line_graph(Yog.Graph.t(), term()) :: Yog.Graph.t()

Returns the line graph of a graph.

Time Complexity: $\mathcal{O}(E^2)$

power(graph, k, default_weight)

@spec power(Yog.Graph.t(), integer(), any()) :: Yog.Graph.t()

Returns the $k$-th power of a graph (connecting nodes at distance $\le k$).

Time Complexity: $\mathcal{O}(V \cdot (V + E))$

Errors

strong_product(first, second, default_first, default_second)

@spec strong_product(Yog.Graph.t(), Yog.Graph.t(), any(), any()) :: Yog.Graph.t()

Returns the Strong product of two graphs.

Time Complexity: $\mathcal{O}(V_1 V_2 + E_1 V_2 + E_2 V_1 + E_1 E_2)$

subgraph?(potential, container)

@spec subgraph?(Yog.Graph.t(), Yog.Graph.t()) :: boolean()

Checks if the first graph is a subgraph of the second graph.

Both graphs must have the same kind (:directed or :undirected).

Time Complexity: $\mathcal{O}(V_p + E_p)$

Errors

symmetric_difference(first, second)

@spec symmetric_difference(Yog.Graph.t(), Yog.Graph.t()) :: Yog.Graph.t()

Returns a graph containing edges that exist in exactly one of the input graphs.

Both graphs must have the same kind (:directed or :undirected).

Time Complexity: $\mathcal{O}(V_1 + V_2 + E_1 + E_2)$

Errors

tensor_product(first, second)

@spec tensor_product(Yog.Graph.t(), Yog.Graph.t()) :: Yog.Graph.t()

Returns the Tensor product (Kronecker product) of two graphs.

Time Complexity: $\mathcal{O}(V_1 V_2 + E_1 E_2)$

union(base, other)

@spec union(Yog.Graph.t(), Yog.Graph.t()) :: Yog.Graph.t()

Returns a graph containing all nodes and edges from both input graphs.

Node data and edge weights from other take precedence on conflicts. Both graphs must be %Yog.Graph{} structs.

Time Complexity: $\mathcal{O}(V_1 + V_2 + E_1 + E_2)$