Model Module
Core graph data structures and basic operations for the yog library.
This module defines the fundamental Graph type and provides all basic operations
for creating and manipulating graphs. The graph uses an adjacency list representation
with dual indexing (both outgoing and incoming edges) for efficient traversal in both
directions.
Types
| Type | Description |
|
A simple graph data structure that can be directed or undirected.
|
|
|
The type of graph: directed or undirected. |
|
|
Unique identifier for a node in the graph. |
Functions and values
| Function or value |
Description
|
||||
|
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
|
||||
|
Like If
Example
|
||||
|
Adds an edge, but if an edge already exists between The combine function receives Time Complexity: O(1)
Example
Use Cases
val graph: obj
|
||||
|
|||||
|
|||||
|
Returns the number of edges in the graph. For undirected graphs, each edge is counted once (the pair {u, v}). For directed graphs, each directed edge (u -> v) is counted once. Time Complexity: O(V)
|
||||
|
|||||
|
|||||
Returns the number of nodes in the graph.
Equivalent to Time Complexity: O(1)
|
|||||
|
Returns the number of nodes in the graph (graph order). Time Complexity: O(1)
|
||||
|
|||||
|
|||||
|
Removes an edge from src to dst. For undirected graphs, removes edges in both directions. Time Complexity: O(1)
Example
val graph: obj
|
||||
|
Removes a node and all its connected edges (incoming and outgoing). Time Complexity: O(deg(v)) - proportional to the number of edges connected to the node, not the whole graph.
Example
val graph: obj
|
||||
|
|||||
|