Spatial Point Patterns: Methodology and Applications with R
by Adrian Baddeley, Ege Rubak and Rolf Turner

Auxiliary script shortestpath.R

The auxiliary script shortestpath.R below is loaded in the R code for chapter 17. The chapter R code assumes that the script is placed in a subdirectory ‘R’ of the current working directory. You can download the script here.

## shortestpath.R
## Code to find and display the shortest path between vertices on a network.
## For making illustrations only!
## Copyright (C) 2015 Adrian Baddeley, Ege Rubak and Rolf Turner

shortestpath <- function(L, i, j) {
  L <- as.linnet(L, sparse=FALSE)
  d <- L$dpath
  m <- L$m
  to <- L$to
  from <- L$from
  path <- i
  leader <- i
  repeat {
    k <- setdiff(which(m[leader,]), path)
    leader <- k[which.min(d[i,k] + d[k, j])]
    path <- c(path, leader)
    if(leader == j) break

joinpoints <- function(X, i, ...) {
  co <- coords(X[i])
  lines(co[,1], co[,2], ...)