pub struct DirectedGraph<N: NodeConstraints> { /* private fields */ }Expand description
A generic directed graph data structure implemented with an adjacency list.
The graph uses a IndexMap to map each node to an IndexSet of its adjacent nodes (outgoing edges),
ensuring efficient:
- Node/edge lookups (O(1) average for hash map)
- Duplicate edge prevention (via
IndexSet) - Node/edge addition/removal
The node type N must implement the NodeConstraints trait for core operations.
Implementations§
Source§impl<N: NodeConstraints> DirectedGraph<N>
impl<N: NodeConstraints> DirectedGraph<N>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty directed graph.
§Examples
use directed_graph::DirectedGraph;
let graph: DirectedGraph<u32> = DirectedGraph::new();
assert_eq!(graph.node_count(), 0);Sourcepub fn contains(&self, n: &N) -> bool
pub fn contains(&self, n: &N) -> bool
Check if the graph contains a specific node.
§Arguments
n- Reference to the node to check for existence
§Returns
true if the node exists in the graph, false otherwise.
§Examples
use directed_graph::DirectedGraph;
let mut graph = DirectedGraph::new();
graph.add_node("Alice", vec![]);
assert!(graph.contains(&"Alice"));
assert!(!graph.contains(&"Bob"));Sourcepub fn add_node(&mut self, start: N, ends_vec: Vec<N>)
pub fn add_node(&mut self, start: N, ends_vec: Vec<N>)
Add a node to the graph with a list of outgoing edges (adjacent nodes).
If the node already exists in the graph, the new edges are appended (duplicate edges are ignored). If the node does not exist, it is created with the provided outgoing edges.
§Arguments
start- The node to add to the graph (source node for the outgoing edges)ends_vec- Vector of adjacent nodes (target nodes for the outgoing edges fromstart)
§Notes
- The target nodes in
ends_vecdo not need to exist in the graph (orphaned edges are allowed) - Duplicate edges in
ends_vecare automatically removed (viaIndexSet)
§Examples
use directed_graph::DirectedGraph;
let mut graph = DirectedGraph::new();
// Add a new node with two outgoing edges
graph.add_node(1, vec![2, 3]);
// Append a new edge to an existing node (duplicate 3 is ignored)
graph.add_node(1, vec![3, 4]);
assert!(graph.get_adjacent_nodes(&1).unwrap().contains(&4));Sourcepub fn get_adjacent_nodes(&self, n: &N) -> Option<&NodeSet<N>>
pub fn get_adjacent_nodes(&self, n: &N) -> Option<&NodeSet<N>>
Get the set of adjacent nodes (outgoing edges) for a specific node.
§Arguments
n- Reference to the node to query for adjacent nodes
§Returns
Some(&NodeSet<N>) if the node exists (containing all adjacent nodes), None otherwise.
§Examples
use directed_graph::DirectedGraph;
let mut graph = DirectedGraph::new();
graph.add_node(1, vec![2, 3]);
let adj = graph.get_adjacent_nodes(&1).unwrap();
assert!(adj.contains(&2) && adj.contains(&3));Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Get the total number of unique edges in the graph.
Counts all outgoing edges across all nodes (duplicate edges are not counted, via IndexSet).
§Returns
The total number of unique outgoing edges in the graph.
§Examples
use directed_graph::DirectedGraph;
let mut graph = DirectedGraph::new();
graph.add_node(1, vec![2, 3]);
graph.add_node(2, vec![3]);
assert_eq!(graph.edge_count(), 3);Sourcepub fn remove_node(&mut self, n: N) -> Option<NodeSet<N>>
pub fn remove_node(&mut self, n: N) -> Option<NodeSet<N>>
Remove a node from the graph and return its outgoing edges (if the node existed).
Removes the node and all its outgoing edges from the adjacency list. Incoming edges to this node are not removed (they remain as orphaned edges from other nodes).
§Arguments
n- The node to remove from the graph
§Returns
Some(NodeSet<N>) if the node existed (containing its outgoing edges), None otherwise.
§Examples
use directed_graph::DirectedGraph;
let mut graph = DirectedGraph::new();
graph.add_node(1, vec![2, 3]);
let edges = graph.remove_node(1).unwrap();
assert_eq!(edges.len(), 2);
assert!(!graph.contains(&1));Sourcepub fn remove_edge(&mut self, start: &N, end: &N) -> bool
pub fn remove_edge(&mut self, start: &N, end: &N) -> bool
Remove a single outgoing edge from a source node to a target node.
§Arguments
start- The source node of the edge to removeend- The target node of the edge to remove
§Returns
true if the edge existed and was removed, false otherwise (either the source node
does not exist, or the edge does not exist).
§Examples
use directed_graph::DirectedGraph;
let mut graph = DirectedGraph::new();
graph.add_node(1, vec![2, 3]);
assert!(graph.remove_edge(&1, &3));
assert!(!graph.remove_edge(&1, &4));Trait Implementations§
Source§impl<N: Clone + NodeConstraints> Clone for DirectedGraph<N>
impl<N: Clone + NodeConstraints> Clone for DirectedGraph<N>
Source§fn clone(&self) -> DirectedGraph<N>
fn clone(&self) -> DirectedGraph<N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more