Struct gchemol_graph::NxGraph
source · pub struct NxGraph<N, E>{ /* private fields */ }
Expand description
networkx-like API wrapper around petgraph
Implementations§
source§impl<N, E> NxGraph<N, E>
impl<N, E> NxGraph<N, E>
Build/Read/Edit Graph
§Example
use gchemol_graph::NxGraph;
let mut g = NxGraph::path_graph(2);
let u = g.add_node(2);
let v = g.add_node(3);
g.add_edge(u, v, 5);
assert!(g.has_node(u));
assert!(g.has_edge(u, v));
// loop over neighbors of node u
for x in g.neighbors(u) {
dbg!(x);
}
sourcepub fn neighbors(&self, n: NodeIndex) -> impl Iterator<Item = NodeIndex> + '_
pub fn neighbors(&self, n: NodeIndex) -> impl Iterator<Item = NodeIndex> + '_
Returns an iterator over all neighbors of node n
.
§Reference
- https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.neighbors.html
sourcepub fn node_indices(&self) -> impl Iterator<Item = NodeIndex> + '_
pub fn node_indices(&self) -> impl Iterator<Item = NodeIndex> + '_
Return an iterator over the node indices of the graph
sourcepub fn has_edge(&self, u: NodeIndex, v: NodeIndex) -> bool
pub fn has_edge(&self, u: NodeIndex, v: NodeIndex) -> bool
Returns true if the edge (u, v) is in the graph.
sourcepub fn number_of_nodes(&self) -> usize
pub fn number_of_nodes(&self) -> usize
Returns the number of nodes in the graph.
sourcepub fn number_of_edges(&self) -> usize
pub fn number_of_edges(&self) -> usize
Returns the number of edges in the graph.
sourcepub fn add_nodes_from<M: IntoIterator<Item = N>>(
&mut self,
nodes: M
) -> Vec<NodeIndex>
pub fn add_nodes_from<M: IntoIterator<Item = N>>( &mut self, nodes: M ) -> Vec<NodeIndex>
Add multiple nodes.
sourcepub fn add_edge(&mut self, u: NodeIndex, v: NodeIndex, data: E)
pub fn add_edge(&mut self, u: NodeIndex, v: NodeIndex, data: E)
Add an edge with data
between u
and v
(no parallel edge). If edge
u–v already exists, the associated data will be updated.
§Panics
- To avoid self-loop, this method will panic if node
u
andv
are the same.
sourcepub fn add_edges_from<M: IntoIterator<Item = (NodeIndex, NodeIndex, E)>>(
&mut self,
edges: M
)
pub fn add_edges_from<M: IntoIterator<Item = (NodeIndex, NodeIndex, E)>>( &mut self, edges: M )
Add multiple edges from edges
.
sourcepub fn remove_edge(&mut self, node1: NodeIndex, node2: NodeIndex) -> Option<E>
pub fn remove_edge(&mut self, node1: NodeIndex, node2: NodeIndex) -> Option<E>
Remove an edge between node1
and node2
. Return None if trying to
remove a non-existent edge.
sourcepub fn remove_node(&mut self, n: NodeIndex) -> Option<N>
pub fn remove_node(&mut self, n: NodeIndex) -> Option<N>
Removes the node n
and all adjacent edges. Return None if trying to
remove a non-existent node.
sourcepub fn clear_edges(&mut self)
pub fn clear_edges(&mut self)
Remove all edges
source§impl<N, E> NxGraph<N, E>
impl<N, E> NxGraph<N, E>
sourcepub fn raw_graph(&self) -> &StableUnGraph<N, E>
pub fn raw_graph(&self) -> &StableUnGraph<N, E>
Provides read access to raw Graph struct.
sourcepub fn raw_graph_mut(&mut self) -> &mut StableUnGraph<N, E>
pub fn raw_graph_mut(&mut self) -> &mut StableUnGraph<N, E>
Provides mut access to raw Graph struct.
source§impl<N, E> NxGraph<N, E>
impl<N, E> NxGraph<N, E>
Methods for creating NxGraph
struct
sourcepub fn from_raw_graph(graph: StableUnGraph<N, E>) -> Self
pub fn from_raw_graph(graph: StableUnGraph<N, E>) -> Self
Return NxGraph
from raw petgraph struct.
source§impl NxGraph<usize, usize>
impl NxGraph<usize, usize>
sourcepub fn path_graph(n: usize) -> Self
pub fn path_graph(n: usize) -> Self
Returns the Path graph P_n
of linearly connected nodes. Node data and
edge data are usize type, mainly for test purpose.
source§impl<N, E> NxGraph<N, E>
impl<N, E> NxGraph<N, E>
Node view and Edge view for NxGraph
.
§Example
use gchemol_graph::NxGraph;
let mut g = NxGraph::path_graph(3);
let u = g.add_node(5);
let v = g.add_node(2);
let w = g.add_node(1);
g.add_edge(u, v, 7);
g.add_edge(u, w, 6);
// loop over nodes
for (node_index, node_data) in g.nodes() {
// do something
}
// get node data of node `u`
let nodes = g.nodes();
let node_u = nodes[u];
assert_eq!(node_u, 5);
// Collect nodes into HashMap
let nodes: std::collections::HashMap<_, _> = g.nodes().collect();
assert_eq!(nodes.len(), 6);
// loop over edges
for (u, v, edge_data) in g.edges() {
// dbg!(u, v, edge_data)
}
// get edge data
let edges = g.edges();
let edge_uv = edges[(u, v)];
assert_eq!(edge_uv, 7);