Graph¶
| Spice | |
|---|---|
Vertex<T> struct¶
Implements: IHashable
A vertex (or node) of a graph, wrapping a single value of arbitrary type
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a vertex wrapping the given value
Parameters
| Name | Type | Description |
|---|---|---|
value |
const T& |
Value to store in the vertex |
Methods¶
getValue¶
| Spice | |
|---|---|
Retrieve the value stored in the vertex
Returns: T& — Reference to the stored value
hash¶
| Spice | |
|---|---|
Compute the hash of the vertex, derived from the hash of its wrapped value
Returns: Hash — Hash of the vertex
Graph<T> struct¶
Implements: IIterable<T>
A graph in Spice is a data structure consisting of a set of vertices and a set of edges connecting them. It is stored as an adjacency list, mapping each vertex to the list of its neighbors. The graph can be either directed or undirected, which is decided at construction time.
Time complexity (V = number of vertices, E = number of edges):
Add vertex: O(1) (average case)
Add edge: O(1) (average case)
Traversal (DFS/BFS): O(V + E)
The average case for adding vertices and edges assumes a good hash distribution in the underlying unordered map. In the worst case, hash collisions degrade these operations to O(V).
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct an empty graph
Parameters
| Name | Type | Description |
|---|---|---|
directed |
bool |
Whether the graph is directed (true) or undirected (false) (default: true) |
Methods¶
addVertex¶
| Spice | |
|---|---|
Add a new vertex with the given value to the graph
Parameters
| Name | Type | Description |
|---|---|---|
value |
const T& |
The value to store in the new vertex |
Returns: Vertex<T>& — Reference to the newly added vertex
addEdge¶
| Spice | |
|---|---|
Add an edge between two vertices that are already part of the graph. For undirected graphs the caller is responsible for adding the reverse edge as well.
Parameters
| Name | Type | Description |
|---|---|---|
from |
const Vertex<T>& |
The source vertex of the edge |
to |
const Vertex<T>& |
The destination vertex of the edge |
hasCycles¶
| Spice | |
|---|---|
Check whether the graph contains at least one cycle
Returns: bool — true if the graph contains a cycle, false otherwise
isDirected¶
| Spice | |
|---|---|
Check whether the graph is directed
Returns: bool — true if the graph is directed, false if it is undirected
isDAG¶
| Spice | |
|---|---|
Check whether the graph is a directed acyclic graph (DAG)
Returns: bool — true if the graph is directed and contains no cycles, false otherwise
toGraphviz¶
| Spice | |
|---|---|
Serialize the graph to the Graphviz DOT format and write it into the given string stream
Parameters
| Name | Type | Description |
|---|---|---|
ss |
StringStream& |
The string stream to write the DOT representation to |
dfs¶
| Spice | |
|---|---|
Retrieve a depth-first iterator for the graph
Parameters
| Name | Type | Description |
|---|---|---|
startVertex |
Vertex<T>* |
(default: nil<Vertex<T>*>) |
Returns: GraphDFSIterator<T>
bfs¶
| Spice | |
|---|---|
Retrieve a breadth-first iterator for the graph
Parameters
| Name | Type | Description |
|---|---|---|
startVertex |
Vertex<T>* |
(default: nil<Vertex<T>*>) |
Returns: GraphBFSIterator<T>
getIterator¶
| Spice | |
|---|---|
Retrieve a depth-first iterator for the graph
Returns: GraphDFSIterator<T>
GraphDFSIterator<T> struct¶
Implements: IIterator<Vertex<T>>
Iterator to iterate over a graph data structure in a depth-first manner.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a depth-first iterator over the given graph
Parameters
| Name | Type | Description |
|---|---|---|
graph |
Graph<T>& |
Graph to iterate over |
startVertex |
Vertex<T>* |
Vertex to start the traversal from, or nil to start at the first vertex (default: nil<Vertex<T>*>) |
Methods¶
get¶
| Spice | |
|---|---|
Returns the current vertex of the graph
Returns: Vertex<T>& — Reference to the current vertex
getIdx¶
| Spice | |
|---|---|
Returns the current index and the current vertex of the graph
Returns: Pair<unsigned long, Vertex<T>&> — Pair of current index and reference to current vertex
isValid¶
| Spice | |
|---|---|
Check if the iterator is valid
Returns: bool — true or false
next¶
| Spice | |
|---|---|
Returns the current vertex of the graph iterator and moves the cursor to the next vertex
GraphBFSIterator<T> struct¶
Implements: IIterator<Vertex<T>>
Iterator to iterate over a graph data structure in a breadth-first manner.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a breadth-first iterator over the given graph
Parameters
| Name | Type | Description |
|---|---|---|
graph |
Graph<T>& |
Graph to iterate over |
startVertex |
Vertex<T>* |
Vertex to start the traversal from, or nil to start at the first vertex (default: nil<Vertex<T>*>) |
Methods¶
get¶
| Spice | |
|---|---|
Returns the current vertex of the graph
Returns: Vertex<T>& — Reference to the current vertex
getIdx¶
| Spice | |
|---|---|
Returns the current index and the current vertex of the graph
Returns: Pair<unsigned long, Vertex<T>&> — Pair of current index and reference to current vertex
isValid¶
| Spice | |
|---|---|
Check if the iterator is valid
Returns: bool — true or false
next¶
| Spice | |
|---|---|
Returns the current vertex of the graph iterator and moves the cursor to the next vertex
Operators¶
operator==¶
| Spice | |
|---|---|
Check if two vertices are equal, i.e. they wrap equal values
Parameters
| Name | Type | Description |
|---|---|---|
v1 |
const Vertex<T>& |
|
v2 |
const Vertex<T>& |
Returns: bool — Equal or not equal