Yog 🌳

Package VersionHex Docs

A graph algorithm library for Gleam, providing implementations of classic graph algorithms with a functional API.

Why Yog?

In many Indic languages, Yog (pronounced like "yoke") translates to "Union," "Addition," or "Connection." It stems from the ancient root yuj, meaning to join or to fasten together.

In the world of computer science, this is the literal definition of Graph Theory. A graph is nothing more than the union of independent points through purposeful connections.

Features

Installation

Add Yog to your Gleam project:

gleam add yog

Quick Start

import gleam/int
import gleam/io
import gleam/option.{None, Some}
import yog
import yog/pathfinding

pub fn main() {
  // Create a directed graph
  let graph =
    yog.directed()
    |> yog.add_node(1, "Start")
    |> yog.add_node(2, "Middle")
    |> yog.add_node(3, "End")
    |> yog.add_edge(from: 1, to: 2, with: 5)
    |> yog.add_edge(from: 2, to: 3, with: 3)
    |> yog.add_edge(from: 1, to: 3, with: 10)

  // Find shortest path
  case pathfinding.shortest_path(
    in: graph,
    from: 1,
    to: 3,
    with_zero: 0,
    with_add: int.add,
    with_compare: int.compare
  ) {
    Some(path) -> {
      io.println("Found path with weight: " <> int.to_string(path.total_weight))
    }
    None -> io.println("No path found")
  }
}

Examples

We have some real-world projects that use Yog for graph algorithms:

Detailed examples are located in the examples/ directory:

Running Examples Locally

The examples live in the examples/ directory. To run them with gleam run, create a one-time symlink that makes Gleam's module system aware of them:

ln -sf "$(pwd)/examples" src/yog/internal/examples

Then run any example by its module name:

gleam run -m yog/internal/examples/gps_navigation
gleam run -m yog/internal/examples/network_bandwidth
# etc.

The symlink is listed in .gitignore and is not committed to the repository, so it won't affect CI or other contributors' environments.

Algorithm Selection Guide

Detailed documentation for each algorithm can be found on HexDocs.

Algorithm Use When Time Complexity
Dijkstra Non-negative weights, single shortest path O((V+E) log V)
A* Non-negative weights + good heuristic O((V+E) log V)
Bellman-Ford Negative weights OR cycle detection needed O(VE)
Floyd-Warshall All-pairs shortest paths, distance matrices O(V³)
Edmonds-Karp Maximum flow, bipartite matching, network optimization O(VE²)
BFS/DFS Unweighted graphs, exploring reachability O(V+E)
Kruskal's MST Finding minimum spanning tree O(E log E)
Stoer-Wagner Global minimum cut, graph partitioning O(V³)
Tarjan's SCC Finding strongly connected components O(V+E)
Tarjan's Connectivity Finding bridges and articulation points O(V+E)
Hierholzer Eulerian paths/circuits, route planning O(V+E)
Topological Sort Ordering tasks with dependencies O(V+E)
Gale-Shapley Stable matching, college admissions, medical residency O(n²)

Performance Characteristics

AI Assistance

Parts of this project were developed with the assistance of AI coding tools. All AI-generated code has been reviewed, tested, and validated by the maintainer.


Yog - Graph algorithms for Gleam 🌳