pub fn line_graph<K, G, T, F, H, M>(
input_graph: K,
default_node_weight: F,
default_edge_weight: H,
) -> (G, HashMap<K::EdgeId, G::NodeId>)
Expand description
Constructs the line graph of an undirected graph.
The line graph L(G)
of a graph G
represents the adjacencies between edges of G.
L(G)
contains a vertex for every edge in G
, and L(G)
contains an edge between two
vertices if the corresponding edges in G
have a vertex in common.
Arguments:
input_graph
- The input graphG
.default_node_weight
- A callable that will return the weight to use for newly created nodes.default_edge_weight
- A callable that will return the weight object to use for newly created edges.
Returns the constructed line graph L(G)
, and the map from the edges of L(G)
to
the vertices of G
.
ยงExample
use rustworkx_core::line_graph::line_graph;
use rustworkx_core::petgraph::visit::EdgeRef;
use rustworkx_core::petgraph::Graph;
use hashbrown::HashMap;
use petgraph::graph::{EdgeIndex, NodeIndex};
use petgraph::Undirected;
let input_graph =
Graph::<(), (), Undirected>::from_edges(&[(0, 1), (0, 2), (1, 2), (0, 3)]);
let (output_graph, output_edge_map): (
petgraph::graph::UnGraph<(), ()>,
HashMap<petgraph::prelude::EdgeIndex, petgraph::prelude::NodeIndex>,
) = line_graph(&input_graph, || (), || ());
let output_edge_list = output_graph
.edge_references()
.map(|edge| (edge.source().index(), edge.target().index()))
.collect::<Vec<(usize, usize)>>();
let expected_edge_list = vec![(3, 1), (3, 0), (1, 0), (2, 0), (2, 1)];
let expected_edge_map: HashMap<EdgeIndex, NodeIndex> = [
(EdgeIndex::new(0), NodeIndex::new(0)),
(EdgeIndex::new(1), NodeIndex::new(1)),
(EdgeIndex::new(2), NodeIndex::new(2)),
(EdgeIndex::new(3), NodeIndex::new(3)),
]
.into_iter()
.collect();
assert_eq!(output_edge_list, expected_edge_list);
assert_eq!(output_edge_map, expected_edge_map);