Graph
Auto-generated from
src/stdlib/graph.mcrs— do not edit manually.
API
- graph_is_valid_node
- graph_new
- graph_add_edge
- graph_add_undirected
- graph_node_count
- graph_edge_count
- graph_bfs
- graph_dfs
- graph_has_path
- graph_shortest_path
graph_is_valid_node v1.0.0
Check whether a node index is valid for this graph.
fn graph_is_valid_node(g: int[], node: int): intParameters
| Parameter | Description |
|---|---|
g | Graph array (from graph_new) |
node | Node index to validate |
Returns: 1 if node is in [0, node_count), 0 otherwise
graph_new v1.0.0
Create a new empty graph with n nodes and capacity for 256 directed edges.
fn graph_new(n: int): int[]Parameters
| Parameter | Description |
|---|---|
n | Number of nodes (clamped to [0, 64]) |
Returns: Initialized graph int[] with g[0]=0 (edges), g[1]=n (nodes)
Example
let g: int[] = graph_new(5) // graph with 5 nodes, no edgesgraph_add_edge v1.0.0
Add a directed weighted edge to the graph.
fn graph_add_edge(g: int[], src: int, dst: int, weight: int): int[]Parameters
| Parameter | Description |
|---|---|
g | Graph array |
src | Source node index |
dst | Destination node index |
weight | Edge weight (use 1 for unweighted graphs) |
Returns: Updated graph array; silently ignores invalid nodes or full edge list
Example
g = graph_add_edge(g, 0, 1, 5) // edge from 0 to 1 with weight 5graph_add_undirected v1.0.0
Add an undirected weighted edge (adds both a→b and b→a directed edges).
fn graph_add_undirected(g: int[], a: int, b: int, weight: int): int[]Parameters
| Parameter | Description |
|---|---|
g | Graph array |
a | First node index |
b | Second node index |
weight | Edge weight |
Returns: Updated graph array with two directed edges added
Example
g = graph_add_undirected(g, 2, 3, 2) // bidirectional edge between 2 and 3graph_node_count v1.0.0
Return the number of nodes in the graph.
fn graph_node_count(g: int[]): intParameters
| Parameter | Description |
|---|---|
g | Graph array |
Returns: Node count (g[1])
graph_edge_count v1.0.0
Return the number of directed edges in the graph.
fn graph_edge_count(g: int[]): intParameters
| Parameter | Description |
|---|---|
g | Graph array |
Returns: Edge count (g[0])
graph_bfs v1.0.0
Breadth-first search from a start node, returning visit order.
fn graph_bfs(g: int[], start: int, out_visited: int[]): int[]Parameters
| Parameter | Description |
|---|---|
g | Graph array |
start | Starting node index |
out_visited | int[] of length >= node_count; cells set to 1 for visited nodes |
Returns: int[] of node indices in BFS visit order; empty if start is invalid
Example
let vis: int[] = [0, 0, 0, 0, 0]
let order: int[] = graph_bfs(g, 0, vis)graph_dfs v1.0.0
Depth-first search from a start node, returning visit order.
fn graph_dfs(g: int[], start: int, out_visited: int[]): int[]Parameters
| Parameter | Description |
|---|---|
g | Graph array |
start | Starting node index |
out_visited | int[] of length >= node_count; cells set to 1 for visited nodes |
Returns: int[] of node indices in DFS visit order (iterative, stack-based)
Example
let vis: int[] = [0, 0, 0, 0, 0]
let order: int[] = graph_dfs(g, 0, vis)graph_has_path v1.0.0
Check whether a path exists between two nodes using BFS.
fn graph_has_path(g: int[], src: int, dst: int): intParameters
| Parameter | Description |
|---|---|
g | Graph array |
src | Source node index |
dst | Destination node index |
Returns: 1 if a directed path from src to dst exists, 0 otherwise
Example
let reachable: int = graph_has_path(g, 0, 3)graph_shortest_path v1.0.0
Find the shortest weighted path from src to all nodes using Dijkstra's algorithm.
fn graph_shortest_path(g: int[], src: int, dst: int, out_dist: int[]): intParameters
| Parameter | Description |
|---|---|
g | Graph array |
src | Source node index |
dst | Destination node index (for return value) |
out_dist | int[] of length >= node_count; filled with shortest distances (-1 if unreachable) |
Returns: Shortest distance from src to dst, or -1 if unreachable
Example
let dist: int[] = [0, 0, 0, 0, 0]
let d: int = graph_shortest_path(g, 0, 3, dist)