shortest_path

Function shortest_path 

Source
pub fn shortest_path<T, S>(
    graph: &S,
    from_vertex: Option<usize>,
    to_vertex: Option<usize>,
    method: &str,
    directed: bool,
    returnpredecessors: bool,
) -> SparseResult<(Array2<T>, Option<Array2<isize>>)>
where T: Float + Debug + Copy + 'static, S: SparseArray<T>,
Expand description

Compute shortest paths in a graph

§Arguments

  • graph - The graph as a sparse matrix
  • from_vertex - Source vertex (None for all pairs)
  • to_vertex - Target vertex (None for all destinations)
  • method - Algorithm to use
  • directed - Whether the graph is directed
  • returnpredecessors - Whether to return predecessor information

§Returns

A tuple containing:

  • Distance matrix/array
  • Optional predecessor matrix/array

§Examples

use scirs2_sparse::csgraph::shortest_path;
use scirs2_sparse::csr_array::CsrArray;

// Create a simple graph
let rows = vec![0, 0, 1, 2];
let cols = vec![1, 2, 2, 0];
let data = vec![1.0, 4.0, 2.0, 3.0];
let graph = CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).unwrap();

// Find shortest paths from vertex 0
let (distances_) = shortest_path(
    &graph, Some(0), None, "dijkstra", true, false
).unwrap();