Trait graph_types::GraphEngine
source · pub trait GraphEngine {
Show 13 methods
// Required methods
fn has_node(&self, node_id: usize) -> bool;
fn remove_node_with_edges(&mut self, node_id: usize);
fn traverse_nodes(&self) -> NodesVisitor<'_, Self> ⓘ;
fn get_edges(&self) -> GetEdgesVisitor<'_, Self>;
fn insert_edge_with_nodes<E: Edge>(&mut self, edge: E) -> EdgeInsertID;
fn remove_edge<E>(&mut self, edge: E)
where E: Into<EdgeQuery>;
fn count_edges(&self) -> usize;
// Provided methods
fn exception(&self, ability: &'static str) -> ! { ... }
fn get_node_degree(&self, node_id: usize) -> Result<usize, GraphError> { ... }
fn count_nodes(&self) -> usize { ... }
fn insert_node(&mut self, node_id: usize) -> usize { ... }
fn remove_node(&mut self, node_id: usize) { ... }
fn insert_edge<E: Edge>(&mut self, edge: E) -> EdgeInsertID { ... }
}Expand description
Required Methods§
sourcefn has_node(&self, node_id: usize) -> bool
fn has_node(&self, node_id: usize) -> bool
Check if the node exists, return the node id if exists.
Examples
use graph_theory::{CompleteGraph, GraphEngine};
assert_eq!(CompleteGraph::new(5).count_nodes(), 5);sourcefn remove_node_with_edges(&mut self, node_id: usize)
fn remove_node_with_edges(&mut self, node_id: usize)
Remove the given node and all edges connected to it.
Examples
use graph_theory::{adjacency_list::UnGraph, GraphEngine};
let mut graph = UnGraph::default();
assert_eq!(graph.count_nodes(), 0);
graph.insert_node(5);
assert_eq!(graph.count_nodes(), 1);sourcefn traverse_nodes(&self) -> NodesVisitor<'_, Self> ⓘ
fn traverse_nodes(&self) -> NodesVisitor<'_, Self> ⓘ
sourcefn get_edges(&self) -> GetEdgesVisitor<'_, Self>
fn get_edges(&self) -> GetEdgesVisitor<'_, Self>
Get the edges of the graph.
sourcefn insert_edge_with_nodes<E: Edge>(&mut self, edge: E) -> EdgeInsertID
fn insert_edge_with_nodes<E: Edge>(&mut self, edge: E) -> EdgeInsertID
sourcefn remove_edge<E>(&mut self, edge: E)where
E: Into<EdgeQuery>,
fn remove_edge<E>(&mut self, edge: E)where E: Into<EdgeQuery>,
sourcefn count_edges(&self) -> usize
fn count_edges(&self) -> usize
Count the number of edges in the graph.
Examples
use graph_theory::{CompleteGraph, GraphEngine};
assert_eq!(CompleteGraph::new(5).count_edges(), 20);Provided Methods§
sourcefn exception(&self, ability: &'static str) -> !
fn exception(&self, ability: &'static str) -> !
Mark the graph engine does not support the ability.
Currently, we can not detect the ability at compile time, so we use this method to mark the ability is not supported.
sourcefn get_node_degree(&self, node_id: usize) -> Result<usize, GraphError>
fn get_node_degree(&self, node_id: usize) -> Result<usize, GraphError>
sourcefn count_nodes(&self) -> usize
fn count_nodes(&self) -> usize
Count the number of nodes in the graph.
Examples
use graph_theory::{CompleteGraph, GraphEngine};
assert_eq!(CompleteGraph::new(5).count_nodes(), 5);sourcefn insert_node(&mut self, node_id: usize) -> usize
fn insert_node(&mut self, node_id: usize) -> usize
Insert a node without any neighbors (edges).
Examples
use graph_theory::{adjacency_list::UnGraph, GraphEngine};
let mut graph = UnGraph::default();
assert_eq!(graph.count_nodes(), 0);
graph.insert_node(5);
assert_eq!(graph.count_nodes(), 1);sourcefn remove_node(&mut self, node_id: usize)
fn remove_node(&mut self, node_id: usize)
Remove the given node.
Undefined Behavior
- If the node has any edges, the behavior is undefined.
It is recommended to remove all edges before removing the node, see GraphEngine::remove_node_with_edges.
Examples
use graph_theory::{adjacency_list::UnGraph, GraphEngine};
let mut graph = UnGraph::default();
assert_eq!(graph.count_nodes(), 0);
graph.insert_node(5);
assert_eq!(graph.count_nodes(), 1);sourcefn insert_edge<E: Edge>(&mut self, edge: E) -> EdgeInsertID
fn insert_edge<E: Edge>(&mut self, edge: E) -> EdgeInsertID
Insert a edge between two nodes.
Undefined Behaviors
- If the nodes does not exist, the behavior is undefined.
It is recommended to check the existence of the nodes before inserting the edge, see GraphEngine::insert_edge_with_nodes.
- Insert undirected edge to directed graph.
Two edges will be inserted, but only return last edge’s id.
Panics
- No such ability
Not all graph engine supports insert edge.
- Insert disconnected edge
Meaningless, don’t do that.
Examples
use graph_theory::GraphEngine;