Expand description
A generic directed graph implementation with core graph operations and cycle detection capabilities.
This crate provides a type-safe DirectedGraph structure that supports generic node types
(with basic constraints for hashing and ordering) and common graph operations like node/edge
addition, removal, adjacency query, and node/edge count statistics. It also includes an
analyze module for cycle detection in directed graphs.
§Key Features
- Generic node support (works with all types that implement
NodeConstraints) - Efficient adjacency list storage with
IndexMapandIndexSet - Core graph operations: add/remove nodes/edges, query adjacent nodes
- Node/edge count statistics
- Cycle detection and graph analysis (in the
analyzemodule)
§Basic Usage
use directed_graph::DirectedGraph;
// Create a new directed graph with integer nodes
let mut graph = DirectedGraph::new();
// Add nodes and edges
graph.add_node(1, vec![2, 3]);
graph.add_node(2, vec![3]);
graph.add_node(3, vec![]);
// Query adjacent nodes
assert!(graph.get_adjacent_nodes(&1).unwrap().contains(&2));
// Get node/edge counts
assert_eq!(graph.node_count(), 3);
assert_eq!(graph.edge_count(), 3);
// Remove an edge
graph.remove_edge(&1, &3);
assert_eq!(graph.edge_count(), 2);Modules§
- analyze
- Graph analysis module (cycle detection core logic)
Structs§
- Directed
Graph - A generic directed graph data structure implemented with an adjacency list.
- NodeMap
- A hash table where the iteration order of the key-value pairs is independent of the hash values of the keys.
- NodeSet
- A hash set where the iteration order of the values is independent of their hash values.
Traits§
- Node
Constraints - Trait defining the core constraints for node types in a
DirectedGraph.