Expand description
A simple library for manipulating graphs.
At this moment, the library is barebones, but there is more to come. If you actually want to get things done for now, use petgraph.
See Graph
for a quick start on some of the methods to manipulate graphs.
§Example
let mut g = Graph::<i32>::with_vertices_and_edges(0..3, [(0,1), (1,2)])?;
assert_eq!(g.vertex_count(), 3);
assert_eq!(g.edge_count(), 2);
g.add_vertices([3, 4]);
g.add_edges([(1, 3),(2, 4)]);
assert_eq!(g.vertex_count(), 5);
assert_eq!(g.edge_count(), 4);
// There are only five vertices, so edge is invalid.
assert_eq!(g.add_edge((4, 5)), Err(GraphError::InvalidEdge));
g.remove_edge((2, 4))?;
assert_eq!(g.edge_count(), 3);
g.add_vertices([6, 7]);
// There is no vertex at index 7.
assert_eq!(g.add_edge((6, 7)), Err(GraphError::InvalidEdge));
// But we can add the edge by value instead.
g.add_edge_by_vertices((6, 7))?;
assert_eq!(g.edge_count(), 4);
g.remove_edges([(0, 1), (1, 2)]);
assert_eq!(g.edges().collect::<Vec<&(usize, usize)>>(), vec![&(1, 3), &(5, 6)]);
g.remove_vertices([0, 1, 2, 3, 4]);
assert_eq!(g.vertices().collect::<Vec<&i32>>(), vec![&6, &7]);
// Removing the vertices above also removed their edges, and shifted the indices down
assert_eq!(g.edges().collect::<Vec<&(usize, usize)>>(), vec![&(0, 1)]);
Structs§
- Graph
- A barebones representation of a graph.
Enums§
- Graph
Error - Represents errors that can occur during various graph operations, such as removing a vertex that does not exist.
Traits§
- Edge
Deleter - A subtrait for when it is possible to delete vertices without having to use
GraphBehaviour::EdgeId
. - Edge
Pair Adder - A subtrait for when it is possible to add edges between a pair of existing vertices directly by their values.
- Edge
Searcher - A subtrait for when it is possible to search for edges.
- Graph
Behaviour - A trait defining basic graph behaviour.
- Vertex
Deleter - A subtrait of for when it is possible to delete vertices without having to use
GraphBehaviour::VertexId
. - Vertex
Searcher - A subtrait for when it is possible to search for vertices.