yog/generators/classic
Classic graph patterns generator.
This module provides functions to generate well-known graph structures:
- Complete graphs: K_n where every node connects to every other
- Cycles: C_n nodes forming a ring
- Paths: P_n linear chains
- Stars: Central hub with spokes
- Wheels: Cycle with central hub
- Bipartite: Complete bipartite graphs K_{m,n}
- Trees: Binary trees, hierarchical structures
- Grids: 2D lattices
- Famous graphs: Petersen graph
These generators are useful for:
- Testing: Create graphs with known properties
- Benchmarking: Generate graphs of various sizes
- Education: Demonstrate algorithms on well-known structures
- Prototyping: Quickly create graph structures
Example
import yog/generators/classic
pub fn main() {
// Generate a cycle graph with 5 nodes
let cycle = classic.cycle(5)
// Generate a complete graph with 4 nodes
let complete = classic.complete(4)
// Generate a binary tree of depth 3
let tree = classic.binary_tree(3)
}
Values
pub fn binary_tree(depth: Int) -> model.Graph(Nil, Int)
Generates a complete binary tree of given depth.
A complete binary tree where:
- Node 0 is the root
- For node i: left child is 2i+1, right child is 2i+2
- Total nodes: 2^(depth+1) - 1
All edges have unit weight (1).
Time Complexity: O(2^depth)
Example
let tree = generate.binary_tree(3)
// Creates a binary tree with 15 nodes (depth 3)
// 0
// / \
// 1 2
// / \ / \
// 3 4 5 6
// /|\ /|\ ...
pub fn binary_tree_with_type(
depth: Int,
graph_type: model.GraphType,
) -> model.Graph(Nil, Int)
Generates a complete binary tree with specified graph type.
pub fn complete(n: Int) -> model.Graph(Nil, Int)
Generates a complete graph K_n where every node is connected to every other node.
In a complete graph with n nodes, there are n(n-1)/2 edges for undirected graphs and n(n-1) edges for directed graphs.
All edges have unit weight (1).
Time Complexity: O(n²)
Example
let k5 = generate.complete(5)
// Creates a complete graph with 5 nodes
// Each node connected to all other 4 nodes
pub fn complete_bipartite(
m: Int,
n: Int,
) -> model.Graph(Nil, Int)
Generates a complete bipartite graph K_{m,n}.
A complete bipartite graph has two partitions of nodes where every node in the first partition is connected to every node in the second partition.
Nodes 0 to m-1 form the left partition. Nodes m to m+n-1 form the right partition.
All edges have unit weight (1).
Time Complexity: O(mn)
Example
let k33 = generate.complete_bipartite(3, 3)
// Nodes 0,1,2 on left, nodes 3,4,5 on right
// All left nodes connected to all right nodes
pub fn complete_bipartite_with_type(
m: Int,
n: Int,
graph_type: model.GraphType,
) -> model.Graph(Nil, Int)
Generates a complete bipartite graph with specified graph type.
pub fn complete_with_type(
n: Int,
graph_type: model.GraphType,
) -> model.Graph(Nil, Int)
Generates a complete graph with specified graph type.
Example
let directed_k4 = generate.complete_with_type(4, model.Directed)
pub fn cycle(n: Int) -> model.Graph(Nil, Int)
Generates a cycle graph C_n where nodes form a ring.
A cycle graph connects n nodes in a circular pattern: 0 -> 1 -> 2 -> … -> (n-1) -> 0
All edges have unit weight (1).
Time Complexity: O(n)
Example
let c6 = generate.cycle(6)
// Creates a cycle: 0-1-2-3-4-5-0
pub fn cycle_with_type(
n: Int,
graph_type: model.GraphType,
) -> model.Graph(Nil, Int)
Generates a cycle graph with specified graph type.
pub fn empty(n: Int) -> model.Graph(Nil, Int)
Generates an empty graph with n nodes and no edges.
Useful as a starting point for custom graph construction.
Time Complexity: O(n)
Example
let empty = generate.empty(10)
// 10 isolated nodes, no edges
pub fn empty_with_type(
n: Int,
graph_type: model.GraphType,
) -> model.Graph(Nil, Int)
Generates an empty graph with specified graph type.
pub fn grid_2d(rows: Int, cols: Int) -> model.Graph(Nil, Int)
Generates a 2D grid graph (lattice).
Creates a grid of rows × cols nodes arranged in a rectangular lattice. Each internal node has 4 neighbors (up, down, left, right). Edge nodes have fewer neighbors.
Node IDs are assigned row-major: node_id = row * cols + col
All edges have unit weight (1).
Time Complexity: O(rows * cols)
Example
let grid = generate.grid_2d(3, 4)
// Creates a 3×4 grid:
// 0 - 1 - 2 - 3
// | | | |
// 4 - 5 - 6 - 7
// | | | |
// 8 - 9 -10 -11
pub fn grid_2d_with_type(
rows: Int,
cols: Int,
graph_type: model.GraphType,
) -> model.Graph(Nil, Int)
Generates a 2D grid graph with specified graph type.
pub fn path(n: Int) -> model.Graph(Nil, Int)
Generates a path graph P_n where nodes form a linear chain.
A path graph connects n nodes in a line: 0 - 1 - 2 - … - (n-1)
All edges have unit weight (1).
Time Complexity: O(n)
Example
let p5 = generate.path(5)
// Creates a path: 0-1-2-3-4
pub fn path_with_type(
n: Int,
graph_type: model.GraphType,
) -> model.Graph(Nil, Int)
Generates a path graph with specified graph type.
pub fn petersen() -> model.Graph(Nil, Int)
Generates a Petersen graph.
The Petersen graph is a famous graph in graph theory, often used as a counterexample. It has 10 nodes and 15 edges, and is non-planar.
All edges have unit weight (1).
Time Complexity: O(1) - fixed size
Example
let petersen = generate.petersen()
// Creates the classic Petersen graph with 10 nodes
pub fn petersen_with_type(
graph_type: model.GraphType,
) -> model.Graph(Nil, Int)
Generates a Petersen graph with specified graph type.
pub fn star(n: Int) -> model.Graph(Nil, Int)
Generates a star graph where one central node is connected to all others.
Node 0 is the center, connected to nodes 1 through n-1. A star with n nodes has n-1 edges.
All edges have unit weight (1).
Time Complexity: O(n)
Example
let s6 = generate.star(6)
// Center node 0 connected to nodes 1, 2, 3, 4, 5
pub fn star_with_type(
n: Int,
graph_type: model.GraphType,
) -> model.Graph(Nil, Int)
Generates a star graph with specified graph type.
pub fn wheel(n: Int) -> model.Graph(Nil, Int)
Generates a wheel graph: a cycle with a central hub.
A wheel graph is a cycle of n-1 nodes with an additional central node connected to all nodes in the cycle.
Node 0 is the center, nodes 1 through n-1 form the cycle.
All edges have unit weight (1).
Time Complexity: O(n)
Example
let w6 = generate.wheel(6)
// Center node 0, cycle 1-2-3-4-5-1, center connected to all
pub fn wheel_with_type(
n: Int,
graph_type: model.GraphType,
) -> model.Graph(Nil, Int)
Generates a wheel graph with specified graph type.