[][src]Struct gchemol_graph::NxGraph

pub struct NxGraph<N, E> where
    N: Default,
    E: Default
{ /* fields omitted */ }

networkx-like API wrapper around petgraph

Methods

impl<N, E> NxGraph<N, E> where
    N: Default,
    E: Default
[src]

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);
}

pub fn new() -> Self[src]

Build a default Graph

pub fn neighbors(&self, n: NodeIndex) -> impl Iterator<Item = NodeIndex> + '_[src]

Returns an iterator over all neighbors of node n.

Reference

  • https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.neighbors.html

pub fn node_indices(&self) -> impl Iterator<Item = NodeIndex> + '_[src]

Return an iterator over the node indices of the graph

pub fn has_node(&self, n: NodeIndex) -> bool[src]

Returns true if the graph contains the node n.

pub fn has_edge(&self, u: NodeIndex, v: NodeIndex) -> bool[src]

Returns true if the edge (u, v) is in the graph.

pub fn number_of_nodes(&self) -> usize[src]

Returns the number of nodes in the graph.

pub fn number_of_edges(&self) -> usize[src]

Returns the number of edges in the graph.

pub fn add_node(&mut self, data: N) -> NodeIndex[src]

Add a node with associated data into graph.

pub fn add_nodes_from<M: IntoIterator<Item = N>>(
    &mut self,
    nodes: M
) -> Vec<NodeIndex>
[src]

Add multiple nodes.

pub fn add_edge(&mut self, u: NodeIndex, v: NodeIndex, data: E)[src]

Add an edge with data between u and v (no parallel edge).

pub fn add_edges_from<M: IntoIterator<Item = (NodeIndex, NodeIndex, E)>>(
    &mut self,
    edges: M
)
[src]

Add multiple edges from edges.

pub fn remove_edge(&mut self, node1: NodeIndex, node2: NodeIndex) -> Option<E>[src]

Remove an edge between node1 and node2. Return None if trying to remove a non-existent edge.

pub fn remove_node(&mut self, n: NodeIndex) -> Option<N>[src]

Removes the node n and all adjacent edges. Return None if trying to remove a non-existent node.

pub fn clear(&mut self)[src]

Remove all nodes and edges

pub fn clear_edges(&mut self)[src]

Remove all edges

impl NxGraph<usize, usize>[src]

pub fn path_graph(n: usize) -> Self[src]

Returns the Path graph P_n of linearly connected nodes. Node data and edge data are usize type, mainly for test purpose.

impl<N, E> NxGraph<N, E> where
    N: Default,
    E: Default
[src]

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);

Important traits for Nodes<'a, N, E>
pub fn nodes(&self) -> Nodes<N, E>[src]

A Node view of the Graph.

Reference

  • https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.nodes.html

Important traits for Edges<'a, N, E>
pub fn edges(&self) -> Edges<N, E>[src]

An Edge view of the Graph.

Reference

  • https://networkx.github.io/documentation/stable/reference/classes/generated/networkx.Graph.edges.html

Trait Implementations

impl<N: Clone, E: Clone> Clone for NxGraph<N, E> where
    N: Default,
    E: Default
[src]

impl<N: Debug, E: Debug> Debug for NxGraph<N, E> where
    N: Default,
    E: Default
[src]

impl<N: Default, E: Default> Default for NxGraph<N, E> where
    N: Default,
    E: Default
[src]

impl<'de, N, E> Deserialize<'de> for NxGraph<N, E> where
    N: Default,
    E: Default,
    N: Deserialize<'de>,
    E: Deserialize<'de>, 
[src]

impl<N, E> Index<(NodeIndex<u32>, NodeIndex<u32>)> for NxGraph<N, E> where
    N: Default,
    E: Default
[src]

type Output = E

The returned type after indexing.

impl<N, E> Index<NodeIndex<u32>> for NxGraph<N, E> where
    N: Default,
    E: Default
[src]

type Output = N

The returned type after indexing.

impl<N, E> IndexMut<(NodeIndex<u32>, NodeIndex<u32>)> for NxGraph<N, E> where
    N: Default,
    E: Default
[src]

impl<N, E> IndexMut<NodeIndex<u32>> for NxGraph<N, E> where
    N: Default,
    E: Default
[src]

impl<N, E> Serialize for NxGraph<N, E> where
    N: Default,
    E: Default,
    N: Serialize,
    E: Serialize
[src]

Auto Trait Implementations

impl<N, E> RefUnwindSafe for NxGraph<N, E> where
    E: RefUnwindSafe,
    N: RefUnwindSafe

impl<N, E> Send for NxGraph<N, E> where
    E: Send,
    N: Send

impl<N, E> Sync for NxGraph<N, E> where
    E: Sync,
    N: Sync

impl<N, E> Unpin for NxGraph<N, E> where
    E: Unpin,
    N: Unpin

impl<N, E> UnwindSafe for NxGraph<N, E> where
    E: UnwindSafe,
    N: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.