简体   繁体   中英

igraph's Shortest Path find wrong on R

1 2
1 3
1 4
1 5
2 3
2 4
2 5
5 6

This is my graph information text file.

library("igraph")
g<-read.table("/home/emir/RRR/graph.txt",)
g<- graph.data.frame(g,directed=0)
get.shortest.paths(g, 6,4,  weights = NULL ,output=c("vpath", "epath", "both"))
plot(g)

http://imgur.com/vLSTkcK

When i plot the graph it all correct. But When i tried to find shortest path, its not always giving me the right path. Output is =6 3 1 4. It should be 6 5 2 4. As you can see there is no edge between 6 and 3. How can i overcome this issue.

The problem is that your graph has vertex names, and these are different than the ids you have. Drop the name vertex attribute, and everything should be fine. It is true that graph.data.frame() could be smarter about this....

You can also try adding the vertex ids in the correct order:

g <- graph.data.frame(g, directed=FALSE,
     vertices=data.frame(name=as.character(seq_len(max(g))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM