pub struct DiGraph {
pub n: usize,
pub adj: Vec<Vec<(usize, i64)>>,
}Expand description
A simple directed graph with Rust-level algorithms.
Fields§
§n: usizeNumber of vertices (0..n).
adj: Vec<Vec<(usize, i64)>>Adjacency list: adj[u] = list of (v, weight) neighbors.
Implementations§
Source§impl DiGraph
impl DiGraph
Sourcepub fn add_edge(&mut self, u: usize, v: usize, w: i64)
pub fn add_edge(&mut self, u: usize, v: usize, w: i64)
Add a directed edge u → v with weight w.
Sourcepub fn bfs(&self, s: usize) -> Vec<usize>
pub fn bfs(&self, s: usize) -> Vec<usize>
BFS from source s; returns distances (usize::MAX if unreachable).
Sourcepub fn topo_sort(&self) -> Option<Vec<usize>>
pub fn topo_sort(&self) -> Option<Vec<usize>>
Topological sort (Kahn’s algorithm). Returns None if cycle exists.
Sourcepub fn dijkstra(&self, s: usize) -> (Vec<i64>, Vec<Option<usize>>)
pub fn dijkstra(&self, s: usize) -> (Vec<i64>, Vec<Option<usize>>)
Dijkstra’s shortest paths from source s (non-negative weights only). Returns (dist, parent) where dist[v] = shortest distance, parent[v] = predecessor.
Sourcepub fn bellman_ford(&self, s: usize) -> Option<Vec<i64>>
pub fn bellman_ford(&self, s: usize) -> Option<Vec<i64>>
Bellman-Ford shortest paths from source s (allows negative weights). Returns None if negative cycle is reachable.
Sourcepub fn floyd_warshall(&self) -> Vec<Vec<i64>>
pub fn floyd_warshall(&self) -> Vec<Vec<i64>>
Floyd-Warshall all-pairs shortest paths. Returns distance matrix (i64::MAX/2 = unreachable).
Sourcepub fn reachable_count(&self, s: usize) -> usize
pub fn reachable_count(&self, s: usize) -> usize
Count vertices reachable from s.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for DiGraph
impl RefUnwindSafe for DiGraph
impl Send for DiGraph
impl Sync for DiGraph
impl Unpin for DiGraph
impl UnsafeUnpin for DiGraph
impl UnwindSafe for DiGraph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more