Function rs_graph::algorithms::inverse

source ·
pub fn inverse<'g, 'h, G, H>(g: &'g G) -> Hwhere
    G: IndexDigraph<'g>,
    H: Digraph<'h> + Buildable,
Expand description

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, IndexGraph, Graph, Digraph, Builder};
use rs_graph::algorithms::inverse;

let mut g = LinkedListGraph::<usize>::new();

g.add_nodes(18);
for u in g.nodes() {
    for v in g.nodes() {
        if g.node_id(v) > 0 && g.node_id(u) % g.node_id(v) == 0 {
          g.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, IndexGraph, Graph, Digraph, Builder};
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);
}