Struct petgraph::graphmap::GraphMap
[−]
[src]
pub struct GraphMap<N, E> { /* fields omitted */ }
GraphMap<N, E>
is an undirected graph, with generic node values N
and edge weights E
.
It uses an combined adjacency list and sparse adjacency matrix representation, using **O(|V|
- |E|)** space, and allows testing for edge existance in constant time.
The node type N
must implement Copy
and will be used as node identifier, duplicated
into several places in the data structure.
It must be suitable as a hash table key (implementing Eq + Hash
).
The node type must also implement Ord
so that the implementation can
order the pair (a
, b
) for an edge connecting any two nodes a
and b
.
GraphMap
does not allow parallel edges, but self loops are allowed.
Methods
impl<N, E> GraphMap<N, E> where
N: NodeTrait,
[src]
N: NodeTrait,
pub fn new() -> Self
[src]
Create a new GraphMap
.
pub fn with_capacity(nodes: usize, edges: usize) -> Self
[src]
Create a new GraphMap
with estimated capacity.
pub fn capacity(&self) -> (usize, usize)
[src]
Return the current node and edge capacity of the graph.
pub fn from_edges<I>(iterable: I) -> Self where
I: IntoIterator,
I::Item: IntoWeightedEdge<E, NodeId = N>,
[src]
I: IntoIterator,
I::Item: IntoWeightedEdge<E, NodeId = N>,
Create a new GraphMap
from an iterable of edges.
Node values are taken directly from the list.
Edge weights E
may either be specified in the list,
or they are filled with default values.
Nodes are inserted automatically to match the edges.
use petgraph::GraphMap; let gr = GraphMap::<_, ()>::from_edges(&[ (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3), ]);
pub fn node_count(&self) -> usize
[src]
Return the number of nodes in the graph.
pub fn edge_count(&self) -> usize
[src]
Return the number of edges in the graph.
pub fn clear(&mut self)
[src]
Remove all nodes and edges
pub fn add_node(&mut self, n: N) -> N
[src]
Add node n
to the graph.
pub fn remove_node(&mut self, n: N) -> bool
[src]
Return true
if node n
was removed.
pub fn contains_node(&self, n: N) -> bool
[src]
Return true
if the node is contained in the graph.
pub fn add_edge(&mut self, a: N, b: N, weight: E) -> Option<E>
[src]
Add an edge connecting a
and b
to the graph, with associated
data weight
.
Inserts nodes a
and/or b
if they aren't already part of the graph.
Return None
if the edge did not previously exist, otherwise,
the associated data is updated and the old value is returned
as Some(old_weight)
.
use petgraph::GraphMap; let mut g = GraphMap::new(); g.add_edge(1, 2, -1); assert_eq!(g.node_count(), 2); assert_eq!(g.edge_count(), 1);
pub fn remove_edge(&mut self, a: N, b: N) -> Option<E>
[src]
Remove edge from a
to b
from the graph and return the edge weight.
Return None
if the edge didn't exist.
use petgraph::GraphMap; let mut g = GraphMap::new(); g.add_edge(1, 2, -1); let edge_data = g.remove_edge(2, 1); assert_eq!(edge_data, Some(-1)); assert_eq!(g.edge_count(), 0);
pub fn contains_edge(&self, a: N, b: N) -> bool
[src]
Return true
if the edge connecting a
with b
is contained in the graph.
ⓘImportant traits for Nodes<'a, N>pub fn nodes(&self) -> Nodes<N>
[src]
Return an iterator over the nodes of the graph.
Iterator element type is N
.
ⓘImportant traits for Neighbors<'a, N>pub fn neighbors(&self, from: N) -> Neighbors<N>
[src]
Return an iterator over the nodes that are connected with from
by edges.
If the node from
does not exist in the graph, return an empty iterator.
Iterator element type is N
.
ⓘImportant traits for Edges<'a, N, E>pub fn edges(&self, from: N) -> Edges<N, E>
[src]
Return an iterator over the nodes that are connected with from
by edges,
paired with the edge weight.
If the node from
does not exist in the graph, return an empty iterator.
Iterator element type is (N, &E)
.
pub fn edge_weight(&self, a: N, b: N) -> Option<&E>
[src]
Return a reference to the edge weight connecting a
with b
, or
None
if the edge does not exist in the graph.
pub fn edge_weight_mut(&mut self, a: N, b: N) -> Option<&mut E>
[src]
Return a mutable reference to the edge weight connecting a
with b
, or
None
if the edge does not exist in the graph.
ⓘImportant traits for AllEdges<'a, N, E>pub fn all_edges(&self) -> AllEdges<N, E>
[src]
Return an iterator over all edges of the graph with their weight in arbitrary order.
Iterator element type is (N, N, &E)
Trait Implementations
impl<N: Clone, E: Clone> Clone for GraphMap<N, E>
[src]
fn clone(&self) -> GraphMap<N, E>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<N: Eq + Hash + Debug, E: Debug> Debug for GraphMap<N, E>
[src]
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl<N, E, Item> FromIterator<Item> for GraphMap<N, E> where
Item: IntoWeightedEdge<E, NodeId = N>,
N: NodeTrait,
[src]
Item: IntoWeightedEdge<E, NodeId = N>,
N: NodeTrait,
Create a new GraphMap
from an iterable of edges.
fn from_iter<I>(iterable: I) -> Self where
I: IntoIterator<Item = Item>,
[src]
I: IntoIterator<Item = Item>,
Creates a value from an iterator. Read more
impl<N, E, Item> Extend<Item> for GraphMap<N, E> where
Item: IntoWeightedEdge<E, NodeId = N>,
N: NodeTrait,
[src]
Item: IntoWeightedEdge<E, NodeId = N>,
N: NodeTrait,
Extend the graph from an iterable of edges.
Nodes are inserted automatically to match the edges.
fn extend<I>(&mut self, iterable: I) where
I: IntoIterator<Item = Item>,
[src]
I: IntoIterator<Item = Item>,
Extends a collection with the contents of an iterator. Read more
impl<N, E> Index<(N, N)> for GraphMap<N, E> where
N: NodeTrait,
[src]
N: NodeTrait,
Index GraphMap
by node pairs to access edge weights.
type Output = E
The returned type after indexing.
fn index(&self, index: (N, N)) -> &E
[src]
Performs the indexing (container[index]
) operation.
impl<N, E> IndexMut<(N, N)> for GraphMap<N, E> where
N: NodeTrait,
[src]
N: NodeTrait,
Index GraphMap
by node pairs to access edge weights.
fn index_mut(&mut self, index: (N, N)) -> &mut E
[src]
Performs the mutable indexing (container[index]
) operation.
impl<N, E> Default for GraphMap<N, E> where
N: NodeTrait,
[src]
N: NodeTrait,
Create a new empty GraphMap
.
impl<'a, N: 'a, E> NeighborIter<'a> for GraphMap<N, E> where
N: Copy + Ord + Hash,
[src]
N: Copy + Ord + Hash,
type Iter = Neighbors<'a, N>
ⓘImportant traits for Neighbors<'a, N>fn neighbors(&'a self, n: N) -> Neighbors<'a, N>
[src]
Return an iterator that visits all neighbors of the node n.
impl<N: Clone, E> Graphlike for GraphMap<N, E>
[src]
type NodeId = N
impl<N, E> Visitable for GraphMap<N, E> where
N: Copy + Ord + Hash,
[src]
N: Copy + Ord + Hash,
impl<N, E> Revisitable for GraphMap<N, E> where
N: Copy + Ord + Hash,
[src]
N: Copy + Ord + Hash,
impl<N, E> GetAdjacencyMatrix for GraphMap<N, E> where
N: Copy + Ord + Hash,
[src]
N: Copy + Ord + Hash,
The GraphMap
keeps an adjacency matrix internally.