yog/model
Types
A graph data structure that can be directed or undirected.
node_data: The type of data stored at each nodeedge_data: The type of data (usually weight) stored on each edge
pub type Graph(node_data, edge_data) {
Graph(
kind: GraphType,
nodes: dict.Dict(Int, node_data),
out_edges: dict.Dict(Int, dict.Dict(Int, edge_data)),
in_edges: dict.Dict(Int, dict.Dict(Int, edge_data)),
)
}
Constructors
The type of graph: directed or undirected.
pub type GraphType {
Directed
Undirected
}
Constructors
-
DirectedA directed graph where edges have a direction from source to destination.
-
UndirectedAn undirected graph where edges are bidirectional.
Values
pub fn add_edge(
graph: Graph(n, e),
from src: Int,
to dst: Int,
with weight: e,
) -> Graph(n, e)
Adds an edge to the graph with the given weight.
For directed graphs, adds a single edge from src to dst.
For undirected graphs, adds edges in both directions.
Example
graph
|> model.add_edge(from: 1, to: 2, with: 10)
pub fn add_node(
graph: Graph(n, e),
id: Int,
data: n,
) -> Graph(n, e)
Adds a node to the graph with the given ID and data. If a node with this ID already exists, its data will be replaced.
Example
graph
|> model.add_node(1, "Node A")
|> model.add_node(2, "Node B")
pub fn all_nodes(graph: Graph(n, e)) -> List(Int)
Returns all unique node IDs that have edges in the graph. This includes all nodes that appear in either out_edges or in_edges.
pub fn neighbors(graph: Graph(n, e), id: Int) -> List(#(Int, e))
Gets everyone connected to the node, regardless of direction. Useful for algorithms like finding “connected components.”
pub fn new(graph_type: GraphType) -> Graph(n, e)
Creates a new empty graph of the specified type.
Example
let graph = model.new(Directed)
pub fn predecessors(
graph: Graph(n, e),
id: Int,
) -> List(#(Int, e))
Gets nodes you came FROM (Predecessors).
pub fn successor_ids(graph: Graph(n, e), id: Int) -> List(Int)
Returns just the NodeIds of successors (without edge weights). Convenient for traversal algorithms that only need the IDs.
pub fn successors(graph: Graph(n, e), id: Int) -> List(#(Int, e))
Gets nodes you can travel TO (Successors).