pub struct Graph<T: Hash + Eq> { /* private fields */ }
Expand description
Data structure that represent generic vertices and undirected connections between them - edges.
Implementations§
Source§impl<T: Hash + Eq + Clone> Graph<T>
impl<T: Hash + Eq + Clone> Graph<T>
Sourcepub fn with_capacity(
vertices_capacity: usize,
edge_per_vertex_capacity: usize,
) -> Self
pub fn with_capacity( vertices_capacity: usize, edge_per_vertex_capacity: usize, ) -> Self
Creates empty graph with preallocated memory for vertices and edges.
§Arguments:
vertices_capacity
- capacity for collection of vertices.
edge_per_vertex_capacity
- capacity for connections collections for each vertex.
Default value const DEFAULT_CONNECTIONS_PER_VERTEX: usize = 4
Sourcepub fn from_data(
vertices: impl Iterator<Item = T>,
edges: impl Iterator<Item = (T, T)>,
) -> Self
pub fn from_data( vertices: impl Iterator<Item = T>, edges: impl Iterator<Item = (T, T)>, ) -> Self
Creates graph filled by vertices
with edges
.
§Arguments:
vertices
- iterator of vertices.
edges
- iterator of pairs of vertices indices, which must be connected.
Sourcepub fn add_vertex(&mut self, v: T) -> bool
pub fn add_vertex(&mut self, v: T) -> bool
Sourcepub fn remove_vertex(&mut self, v: &T) -> Option<HashSet<T>>
pub fn remove_vertex(&mut self, v: &T) -> Option<HashSet<T>>
Sourcepub fn remove_edge(&mut self, v1: &T, v2: &T) -> bool
pub fn remove_edge(&mut self, v1: &T, v2: &T) -> bool
Sourcepub fn is_connected(&self, v1: &T, v2: &T) -> bool
pub fn is_connected(&self, v1: &T, v2: &T) -> bool
Checks if edge, that connects specified vertices, is present.
Connections are undirectional, thats why always
is_connected(v1, v2) == is_connected(v2, v1)
§Arguments:
v1
- first vertex to check.
v2
- second vertex to check.
Sourcepub fn connects_of(&self, v: &T) -> Option<&HashSet<T>>
pub fn connects_of(&self, v: &T) -> Option<&HashSet<T>>
Sourcepub fn remove_weak_connected(&mut self, weak_level: usize) -> HashSet<T>
pub fn remove_weak_connected(&mut self, weak_level: usize) -> HashSet<T>
Removes all points, that have less connections than weak_level
.
In other words, only vertices with more or equal connections than weak_level
remains. Complexity: O(n)
§Returns:
Set of removed vertices
§Example:
use easy_graph::Graph;
let verts = vec![0,1,2,3];
let conns = vec![(0, 1), (1, 2), (2, 0), (2, 3)];
let mut graph = Graph::from_data(verts.into_iter(), conns.into_iter());
graph.remove_weak_connected(2);
assert_eq!(graph.len(), 3);
assert!(graph.contains(&0));
assert!(graph.contains(&1));
assert!(graph.contains(&2));
assert!(!graph.contains(&3));
assert!(!graph.connects_of(&2).unwrap().contains(&3));
Sourcepub fn verts_with_path_to(&self, vertex: &T) -> HashSet<T>
pub fn verts_with_path_to(&self, vertex: &T) -> HashSet<T>
Returns set of vertices, for which exists path to vertex
.
Sourcepub fn take_connected_graph(&mut self, vertex: &T) -> Self
pub fn take_connected_graph(&mut self, vertex: &T) -> Self
Takes connected graph that contains vertex
.
Sourcepub fn into_connected_graphs(self) -> Vec<Self>
pub fn into_connected_graphs(self) -> Vec<Self>
Split self
into connected graphs.