Skip to contents

This function finds the shortest path between nodes in a graph using Dijkstra's algorithm. It can be applied to both gGraph and gData objects.

Usage

dijkstraBetween(x, ...)

# S4 method for class 'gGraph'
dijkstraBetween(x, from, to)

# S4 method for class 'gData'
dijkstraBetween(x)

Arguments

x

A gGraph or gData object.

...

Additional arguments passed to other methods (currently not used).

from

A character vector of starting node IDs.

to

A character vector of ending node IDs. If missing, or identical to from, paths are computed between all pairs of nodes in from.

Value

A gPath object:

  • if to is missing or identical to from (and has more than one node), one path for each pair of nodes from[i], from[j] with i < j, in the order used by dist objects; this is always the case for the gData method, which uses all locations of x;

  • otherwise, one path from each node of from to each node of to (length(from) * length(to) paths, from varying slowest).

gPath2dist turns these into a dist object or a length(from) x length(to) matrix, respectively.

Details

The function uses the RBGL package to compute the shortest paths. It checks for the connectivity of the graph and handles cases where there are duplicated paths.

Functions

  • dijkstraBetween(gGraph): Method for gGraph

  • dijkstraBetween(gData): Method for gData

See also

Other dijkstra_methods: dijkstraBuffer(), dijkstraFrom(), gPath2dist()

Examples

## select a few populations from the HGDP dataset
hgdp.sub <- hgdp[getData(hgdp)$Population %in%
  c("French", "Balochi", "BantuKenya", "Papuan", "Pima")]
hgdp.path <- dijkstraBetween(hgdp.sub) # compute shortest path