Learn R Programming

spatstat.linnet (version 3.4-1)

shortestpath: Shortest Path Between Two Points on a Linear Network

Description

Find the shortest path between two given points on a linear network.

Usage

shortestpath(X, i=1, j=2)

Value

Line segment pattern (object of class "psp") with an attribute "steps" which is an integer vector.

Arguments

X

Point pattern on a linear network (object of class "lpp").

i

Integer index of the start point

j

Integer index of the end point

Author

Adrian Baddeley Adrian.Baddeley@curtin.edu.au.

Details

The shortest path in the network between the two specified points X[i] and X[j] is determined.

The result is a line segment pattern (object of class "psp") consisting of (in order) a line segment joining X[i] to a vertex of the network, then a series of segments joining adjacent vertices of the network, then a line segment joining a vertex to X[j].

The result has an attribute "steps" which is an integer vector giving the sequence of vertices of the network through which the shortest path passes. This vector will have length zero if X[i] and X[j] lie on the same segment of the network. Otherwise it will contain a sequence of integers which index the vertices as given in vertices(domain(X)).

See Also

The length of the shortest path is computed by pairdist.lpp.

Examples

Run this code
  X <- if(interactive()) chicago[c(20, 39)] else runiflpp(2, simplenet)
  P <- shortestpath(X)
  if(interactive()) {
    plot(X, pch=16, main="shortest path")
    ## draw the path
    plot(P, add=TRUE, col=2, lwd=2)
    ## draw the vertices on the path
    V <- vertices(L)
    steps <- attr(P, "steps")
    plot(V[steps], add=TRUE, col=3)
  }

Run the code above in your browser using DataLab