| shortestpath | R Documentation |
Find the shortest path between two given points on a linear network.
shortestpath(X, i=1, j=2)
X |
Point pattern on a linear network (object of class |
i |
Integer index of the start point |
j |
Integer index of the end point |
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)).
Line segment pattern (object of class "psp") with an
attribute "steps" which is an integer vector.
.
The length of the shortest path is computed by
pairdist.lpp.
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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.