Robert Farley
Robert Farley

Reputation: 45

highlight shortest path on plot of graph

I am trying to find a way to highlight some shortest paths on my graph. I am trying to automate the use the output of get.shortest.paths into the path= function of E() but the data structure seems wrong. See below:

###################################################################
g <- graph.ring(10,directed=TRUE)               
plot(g)
ShortPth <- get.shortest.paths(g, 8, 2)    # List of path 8->2
ShortPth
E(g)$color <- "SkyBlue2"
E(g)$width <- 1
E(g, path=ShortPth)$color <- "red" 

### setting edges by path= is failing !!!!!!!!!!
plot(g)
#############################################################

any help would be greatly appreciated....

Upvotes: 4

Views: 2720

Answers (2)

Jota
Jota

Reputation: 17621

I think you just missed a few brackets. The path argument wants a numeric vector, but ShortPth is a list. So, you can supply a vector by typing ShortPth[[1]] Try the following:

E(g, path=ShortPth[[1]])$color <- "red"
plot(g)

Update:

As pointed out in the comment by jcarlos, the above solution throws an error with igraph_1.0.1:

Error in as.igraph.vs(graph, path) : (list) object cannot be coerced to type 'double'

The documentation says the path argument should be A list of vertices, to select edges along a path. Passing in a list throws an error, though. Any of the following are working at the moment with igraph_1.0.1:

E(g, path=ShortPth$vpath[[1]])$color <- "red"
E(g, path=unlist(ShortPth$vpath))$color <- "red"
E(g, path=unlist(ShortPth[[1]]))$color <- "red"

ShortPth$vpath[[1]]  # class "igraph.vs"
# + 5/10 vertices:
# [1]  8  9 10  1  2
unlist(ShortPth$vpath)
# [1]  8  9 10  1  2

Upvotes: 9

Workhorse
Workhorse

Reputation: 1560

You can also easily do this using mathematica using the function ShortestPathFunction[].

Upvotes: 0

Related Questions