Yog 🌳

Package Version Hex Docs

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

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

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.

AlgorithmUse WhenTime Complexity
DijkstraNon-negative weights, single shortest pathO((V+E) log V)
A*Non-negative weights + good heuristicO((V+E) log V)
Bellman-FordNegative weights OR cycle detection neededO(VE)
Floyd-WarshallAll-pairs shortest paths, distance matricesO(V³)
Edmonds-KarpMaximum flow, bipartite matching, network optimizationO(VE²)
BFS/DFSUnweighted graphs, exploring reachabilityO(V+E)
Kruskal’s MSTFinding minimum spanning treeO(E log E)
Stoer-WagnerGlobal minimum cut, graph partitioningO(V³)
Tarjan’s SCCFinding strongly connected componentsO(V+E)
Tarjan’s ConnectivityFinding bridges and articulation pointsO(V+E)
HierholzerEulerian paths/circuits, route planningO(V+E)
Topological SortOrdering tasks with dependenciesO(V+E)
Gale-ShapleyStable matching, college admissions, medical residencyO(n²)

Performance Characteristics


Yog - Graph algorithms for Gleam 🌳

Search Document