pub struct Graph<K, N, E>where
K: Clone + Hash + Display + PartialEq + Eq,
N: Clone,
E: Clone,{ /* private fields */ }
Expand description
A directed graph containing nodes and edges. The graph is represented as a
map of nodes, where each node is identified by a unique key. Each node
contains a value and a map of edges. An edge may hold a value as well.
Generic types K
, N
, and E
represent the key, node value, and edge
value, respectively.
Implementations§
source§impl<K, N, E> Graph<K, N, E>where
K: Clone + Hash + Display + PartialEq + Eq,
N: Clone,
E: Clone,
impl<K, N, E> Graph<K, N, E>where K: Clone + Hash + Display + PartialEq + Eq, N: Clone, E: Clone,
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new Graph with a given capacity
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::with_capacity(42);
sourcepub fn contains(&self, key: &K) -> bool
pub fn contains(&self, key: &K) -> bool
Check if a node with the given key exists in the Graph
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
g.insert(Node::new("A", 0));
assert!(g.contains(&"A"));
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the length of the Graph (amount of nodes)
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
g.insert(Node::new("A", 0));
g.insert(Node::new("B", 0));
let len = g.len();
assert!(len == 2);
sourcepub fn get(&self, key: &K) -> Option<Node<K, N, E>>
pub fn get(&self, key: &K) -> Option<Node<K, N, E>>
Get a node by key
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
g.insert(Node::new("A", 0));
g.insert(Node::new("B", 0));
g.insert(Node::new("C", 0));
let node = g.get(&"A").unwrap();
assert!(node.key() == &"A");
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if Graph is empty
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
assert!(g.is_empty());
sourcepub fn insert(&mut self, node: Node<K, N, E>) -> bool
pub fn insert(&mut self, node: Node<K, N, E>) -> bool
Insert a node into the Graph
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
g.insert(Node::new("A", 0));
assert!(g.contains(&"A"));
assert!(g.insert(Node::new("A", 0)) == false);
sourcepub fn remove(&mut self, node: &K) -> Option<Node<K, N, E>>
pub fn remove(&mut self, node: &K) -> Option<Node<K, N, E>>
Remove a node from the Graph
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
g.insert(Node::new("A", 0));
g.insert(Node::new("B", 0));
assert!(g.contains(&"A"));
g.remove(&"A");
assert!(g.contains(&"A") == false);
sourcepub fn to_vec(&self) -> Vec<Node<K, N, E>>
pub fn to_vec(&self) -> Vec<Node<K, N, E>>
Collect nodes into a vector
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
g.insert(Node::new("A", 0));
g.insert(Node::new("B", 0));
g.insert(Node::new("C", 0));
let nodes = g.to_vec();
assert!(nodes.len() == 3);
sourcepub fn roots(&self) -> Vec<Node<K, N, E>>
pub fn roots(&self) -> Vec<Node<K, N, E>>
Collect roots into a vector
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
g.insert(Node::new("A", 0));
g.insert(Node::new("B", 0));
g.insert(Node::new("C", 0));
g["A"].connect(&g["B"], 0x1);
g["A"].connect(&g["C"], 0x1);
g["B"].connect(&g["C"], 0x1);
let roots = g.roots();
assert!(roots.len() == 1);
sourcepub fn leaves(&self) -> Vec<Node<K, N, E>>
pub fn leaves(&self) -> Vec<Node<K, N, E>>
Collect leaves into a vector
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
g.insert(Node::new("A", 0));
g.insert(Node::new("B", 0));
g.insert(Node::new("C", 0));
g["A"].connect(&g["B"], 0x1);
g["A"].connect(&g["C"], 0x1);
let leaves = g.leaves();
assert!(leaves.len() == 2);
sourcepub fn orphans(&self) -> Vec<Node<K, N, E>>
pub fn orphans(&self) -> Vec<Node<K, N, E>>
Collect orpahn nodes into a vector
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
g.insert(Node::new("A", 0));
g.insert(Node::new("B", 0));
g.insert(Node::new("C", 0));
g.insert(Node::new("D", 0));
g["A"].connect(&g["B"], 0x1);
let orphans = g.orphans();
assert!(orphans.len() == 2);
sourcepub fn iter(&self) -> Iter<'_, K, Node<K, N, E>>
pub fn iter(&self) -> Iter<'_, K, Node<K, N, E>>
Iterate over nodes in the graph in random order
Examples
use gdsl::digraph::*;
let mut g = Graph::<&str, u64, u64>::new();
g.insert(Node::new("A", 0));
g.insert(Node::new("B", 0));
g.insert(Node::new("C", 0));
for (key, _) in g.iter() {
println!("{}", key);
}
sourcepub fn scc(&self) -> Vec<Vec<Node<K, N, E>>>
pub fn scc(&self) -> Vec<Vec<Node<K, N, E>>>
Find the strongly connected components of the graph. Can be used to find cycles in the graph and for topological sorting.
Examples
use gdsl::digraph::*;
let mut g: Graph<usize, (), ()> = Graph::new();
g.insert(Node::new(0, ()));
g.insert(Node::new(1, ()));
g.insert(Node::new(2, ()));
g.insert(Node::new(3, ()));
g.insert(Node::new(4, ()));
g.insert(Node::new(5, ()));
g.insert(Node::new(6, ()));
g.insert(Node::new(7, ()));
g.insert(Node::new(8, ()));
g.insert(Node::new(9, ()));
g[0].connect(&g[1], ()); // ---- C1
g[1].connect(&g[2], ()); //
g[2].connect(&g[0], ()); //
g[3].connect(&g[4], ()); // ---- C2
g[4].connect(&g[5], ()); //
g[5].connect(&g[3], ()); //
g[6].connect(&g[7], ()); // ---- C3
g[7].connect(&g[8], ()); //
g[8].connect(&g[6], ()); //
g[9].connect(&g[9], ()); // ---- C4
let mut scc = g.scc();
// Since the graph container is a hash map, the order of the SCCs is
// not deterministic. We sort the SCCs by their size to make the test
// deterministic.
scc.sort_by(|a, b| a.len().cmp(&b.len()));
assert!(scc.len() == 4);
assert!(scc[0].len() == 1);
assert!(scc[1].len() == 3);
assert!(scc[2].len() == 3);
assert!(scc[3].len() == 3);