Function grid_graph

Source
pub fn grid_graph<G, T, F, H, M>(
    rows: Option<usize>,
    cols: Option<usize>,
    weights: Option<Vec<T>>,
    default_node_weight: F,
    default_edge_weight: H,
    bidirectional: bool,
) -> Result<G, InvalidInputError>
where G: Build + Create + Data<NodeWeight = T, EdgeWeight = M> + NodeIndexable, F: FnMut() -> T, H: FnMut() -> M,
Expand description

Generate a grid graph

Arguments:

  • rows - The number of rows to generate the graph with. If specified, cols also need to be specified.
  • cols: The number of columns to generate the graph with. If specified, rows also need to be specified. rows*cols defines the number of nodes in the graph.
  • weights: A Vec of node weights. Nodes are filled row wise. If rows and cols are not specified, then a linear graph containing all the values in weights list is created. If number of nodes(rowscols) is less than length of weights list, the trailing weights are ignored. If number of nodes(rowscols) is greater than length of weights list, extra nodes with None weight are appended.
  • default_node_weight - A callable that will return the weight to use for newly created nodes. This is ignored if weights is specified.
  • default_edge_weight - A callable that will return the weight object to use for newly created edges.
  • bidirectional - Whether edges are added bidirectionally. If set to true then for any edge (u, v) an edge (v, u) will also be added. If the graph is undirected this will result in a parallel edge.

ยงExample

use rustworkx_core::petgraph;
use rustworkx_core::generators::grid_graph;
use rustworkx_core::petgraph::visit::EdgeRef;

let g: petgraph::graph::UnGraph<(), ()> = grid_graph(
    Some(3),
    Some(3),
    None,
    || {()},
    || {()},
    false
).unwrap();
assert_eq!(
    vec![(0, 3), (0, 1), (1, 4), (1, 2), (2, 5),
         (3, 6), (3, 4), (4, 7), (4, 5), (5, 8), (6, 7), (7, 8)],
    g.edge_references()
        .map(|edge| (edge.source().index(), edge.target().index()))
        .collect::<Vec<(usize, usize)>>(),
)