Skip to contents

Computes up to k loopless shortest paths from from to to using Yen's algorithm in the Rust core. Useful for route alternatives.

Usage

ox_k_shortest_paths(g, from, to, k = 3, weight = "length")

Arguments

g

An osm_graph.

from, to

Node osmids.

k

Number of paths to return. Default 3.

weight

Edge column used as weight. Default "length".

Value

A tibble with one row per path: rank, cost and a list-column path of node osmids, ordered by increasing cost. Fewer than k rows are returned when fewer distinct paths exist.

Examples

g <- example_osm_graph()
from <- ox_nearest_nodes(g, 0, 0)
to <- ox_nearest_nodes(g, 200, 200)
ox_k_shortest_paths(g, from, to, k = 3)
#> # A tibble: 3 × 3
#>    rank  cost path     
#>   <int> <dbl> <list>   
#> 1     1   400 <int [5]>
#> 2     2   400 <int [5]>
#> 3     3   400 <int [5]>