Here is a simpler solution that does not need reshape()
. We just create an igraph graph directly from the data frame you have. If you really need the adjacency matrix, you can still get it via get.adjacency()
:
library(igraph)
## load data
df <- read.table(header=T, stringsAsFactors=F, text=
" V1 V2 V3
164885 431072 3
164885 164885 24
431072 431072 5")
## create graph
colnames(df) <- c("from", "to", "weight")
g <- graph.data.frame(df)
g
# IGRAPH DNW- 2 3 --
# + attr: name (v/c), weight (e/n)
## get shortest path lengths
shortest.paths(g, mode="out")
# 164885 431072
# 164885 0 3
# 431072 Inf 0
## get the actual shortest path
get.shortest.paths(g, from="164885", to="431072")
# [[1]]
# [1] 1 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…