Skip to content

shortest path

3 messages · Barry Rowlingson, Karl Ove Hufthammer, Mathieu Rajerison

#
On Fri, Aug 24, 2012 at 9:11 PM, Francis P. Boscoe
<fpb01 at health.state.ny.us> wrote:
using igraph, it couldn't be much simpler:

#1 make a graph, giving a two-column matrix of FROM and TO node names:
 > ABC = graph.edgelist(cbind(c("A","B","A"),c("B","C","C")))

#2 attach distances as weights:
 > E(ABC)$weight = c(3,4,8)

#3 see what we got:
 > ABC
Vertices: 3
Edges: 3
Directed: TRUE
Edges:

[0] 'A' -> 'B'
[1] 'B' -> 'C'
[2] 'A' -> 'C'

 Note this is a Directed graph. There's no way back from B to A!

#4 shortest path from A to C:
[[1]]
[1] 0 1 2

And since there might be more than one shortest path, the return is a
list. Here it has one element, and the numbers are the indices of the
nodes.  Starting from zero[1]. This is computer science, after all. To
get the nodes, index the V (vertices) function:

 > V(ABC)[get.shortest.paths(ABC,from="A",to="C")[[1]]]
Vertex sequence:
[1] "A" "B" "C"

#5 just to check, lets set the distances to all 1 and see if the
algorithm figures out the direct route is quicker:

 > E(ABC)$weight = c(1,1,1)
 > V(ABC)[get.shortest.paths(ABC,from="A",to="C")[[1]]]
Vertex sequence:
[1] "A" "C"

 Happy graphing!

Barry

[1] I think the zero indexing might be changing soon, or has
changed... Recent igraphs might be different, but indexing the V
function should still work.
#
Barry Rowlingson skreiv:
Indeed. From the release notes of igraph 0.6:

,----[ http://igraph.sourceforge.net/relnotes-0.6.html ]
| The biggest change in the R interface is that starting from this version
| vertices and edges are numbered from one. This change might be painful
| for many people, because it makes already existing code incompatible with
| igraph 0.6. To make the switch easier, there is now an igraph0 package
| on CRAN; igraph0 uses 0-based vertex and edge ids, and it can be used
| to run old code. Note, however, that igraph0 will not be developed in
| the future. Please use the igraph package for current and future work.
|
| (Also note that in Python and C vertices and edges are still numbered
| from zero, as these languages traditionally use zero-based indexing.)
`----
5 days later
#
Hi,


I've got a question which is graph-related.

Let's suppose I have a linear network.

I cut the lines at the intersections and get some vertexes and edges
connecting them

How can I turn this into a graph that I could treat in igraph?

I've put a line sample as attached file if any geohacker is interested in
facing the challenge ;)


Mat

2012/8/25 Karl Ove Hufthammer <karl at huftis.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20120831/8f398f95/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: LINE.zip
Type: application/zip
Size: 1728 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20120831/8f398f95/attachment.zip>