[][src]Function rs_graph::algorithms::inverse

pub fn inverse<'g, 'h, G, H>(g: &'g G) -> H where
    G: IndexDigraph<'g>,
    H: Digraph<'h> + Buildable

Returns the inverse directed graph of g.

For $G=(V,A)$ the returned graph is $G=(V,A')$ with $A' := {(v,u) \colon (u,v) \in A}$.

Example

use rs_graph::{LinkedListGraph, Buildable, Builder};
use rs_graph::algorithms::inverse;
use rs_graph::traits::*;

let g = LinkedListGraph::<usize>::new_with(|b| {
    let nodes = b.add_nodes(18);
    for &u in &nodes {
        for &v in &nodes {
            if b.node2id(v) > 0 && b.node2id(u) % b.node2id(v) == 0 {
              b.add_edge(u, v);
            }
        }
    }
});

let h: LinkedListGraph = inverse(&g);
assert_eq!(g.num_nodes(), h.num_nodes());
assert_eq!(g.num_edges(), h.num_edges());
for e in h.edges() {
    let (u,v) = (h.node_id(h.src(e)), h.node_id(h.snk(e)));
    assert!(u > 0 && v % u == 0);
}

Another example to create a star with all edges directed to the center.

use rs_graph::LinkedListGraph;
use rs_graph::traits::*;
use rs_graph::classes::star;
use rs_graph::algorithms::inverse;

type G = LinkedListGraph<usize>;
let g: G = inverse(&star::<G>(42));
assert_eq!(g.num_nodes(), 43);
for e in g.edges() {
    let (u,v) = (g.node_id(g.src(e)), g.node_id(g.snk(e)));
    assert!(u > 0 && v == 0);
}