01_basic

command
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 9 Imported by: 0

README

Example 01 — Basic shortest paths

What it demonstrates

The minimal end-to-end GoGraph flow: build a weighted directed graph with the mutable adjlist builder, freeze it into an immutable CSR snapshot, and run a single-source shortest-paths query with search.Dijkstra. It reads back both the shortest distance and the reconstructed route to each destination.

Domain / scenario

A tiny road network between five European cities. Each directed edge is a one-way leg with a distance in kilometres:

Lisbon -> Madrid (624)
Lisbon -> Paris  (1737)
Madrid -> Paris  (1274)
Madrid -> Rome   (1969)
Paris  -> Rome   (1422)

The query is anchored at Lisbon. The interesting case is Rome: the direct-looking Lisbon -> Paris -> Rome path costs 3159 km, while Lisbon -> Madrid -> Rome costs only 2593 km — so the route, not just the distance, is worth printing.

How to run

go run ./examples/01_basic

Expected output

Lisbon -> Madrid  :  624 km   route: Lisbon -> Madrid
Lisbon -> Paris   : 1737 km   route: Lisbon -> Paris
Lisbon -> Rome    : 2593 km   route: Lisbon -> Madrid -> Rome

Key APIs

  • graph/adjlist.New / AdjList.AddEdge — build the mutable weighted directed graph.
  • graph/adjlist.AdjList.Mapper — translate between city names and compact NodeIDs (Lookup, Resolve).
  • graph/csr.BuildFromAdjList — freeze the builder into an immutable CSR snapshot for analytics.
  • search.Dijkstra — single-source shortest paths over non-negative weights.
  • search.Distances.Distance / search.Distances.Path — read back the cost and reconstruct the route to each node.

Further reading

Documentation

Overview

Example 01_basic — build a small weighted directed graph, snapshot it to a CSR view, and run a single-source shortest-paths query.

For each reachable city it prints both the shortest distance from Lisbon and the route taken (the parent chain reconstructed from the Dijkstra result, resolved back to city names through the adjacency list's mapper).

Sample output: run `go run ./examples/01_basic` and capture the stdout — the output is deterministic for the inputs hard-coded above and serves as the regression baseline a future change should preserve.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL