How to limit the number of shortest path in igraph/R

Using the shortest.paths function we get the shortest path from a graph. Now, I want to limit the length of the shortest path.

For example, when I am running the below code, I am getting all the shortest paths from a vertex to any vertex.

df <- read.csv("~/data.csv")
g1 <- df
graph1 <- graph_from_data_frame(g1, directed = FALSE)
plot(graph1, vertex.label = V(graph1)$name)
mat <- shortest.paths(graph1)

The output I am getting

       ID_1 ID_2 ID_3 ID_4 ID_8 ID_5 ID_7 ID_100
ID_1      0    1    1    1  Inf    2    2    Inf
ID_2      1    0    2    1  Inf    1    2    Inf
ID_3      1    2    0    2  Inf    3    1    Inf
ID_4      1    1    2    0  Inf    2    1    Inf
ID_8    Inf  Inf  Inf  Inf    0  Inf  Inf      1
ID_5      2    1    3    2  Inf    0    3    Inf
ID_7      2    2    1    1  Inf    3    0    Inf
ID_100  Inf  Inf  Inf  Inf    1  Inf  Inf      0

But, I want to keep only (say) the path length is 3 and the other will be 0 or Inf. Actually, I do not need other except (path length =3).

Moreover, I want the sum of the path weight not only the number of the path. I thought I Can do this just by changing only one line

mat <- shortest.paths(graph1, weights=E(graph1)$weight)

But, how can limit the path length?

Reproducible Data

structure(list(nodeA = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 4L, 
5L), .Label = c("ID_1", "ID_2", "ID_3", "ID_4", "ID_8"), class = "factor"), 
    nodeB = structure(c(2L, 3L, 4L, 5L, 4L, 6L, 6L, 1L), .Label = c("ID_100", 
    "ID_2", "ID_3", "ID_4", "ID_5", "ID_7"), class = "factor"), 
    weight = c(0.5, 0.77, 0.5, 0.9, 0.44, 0.32, 0.45, 0.543)), class = "data.frame", row.names = c(NA, 
-8L))

Hi Akib,

I believe that you've provided not the correct dataset, as I get different results in the shortest.paths matrix (see below).

But looking at your output, do you want to have a matrix containing only the combinations ID_3 + ID_5 and ID_5 + ID_7?

library(igraph)

df <- structure(list(nodeA = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 4L, 5L), 
                                       .Label = c("ID_1", "ID_2", "ID_3", "ID_4", "ID_8"), 
                                       class = "factor"
                                       ), 
                     nodeB = structure(c(2L, 3L, 4L, 5L, 4L, 6L, 6L, 1L), 
                                       .Label = c("ID_100", "ID_2", "ID_3", "ID_4", "ID_5", "ID_7"), 
                                       class = "factor"
                                       ), 
                     weight = c(0.5, 0.77, 0.5, 0.9, 0.44, 0.32, 0.45, 0.543)
                     ), 
                class = "data.frame", 
                row.names = c(NA, -8L)
                )

df
#>   nodeA  nodeB weight
#> 1  ID_1   ID_2  0.500
#> 2  ID_1   ID_3  0.770
#> 3  ID_1   ID_4  0.500
#> 4  ID_2   ID_5  0.900
#> 5  ID_2   ID_4  0.440
#> 6  ID_3   ID_7  0.320
#> 7  ID_4   ID_7  0.450
#> 8  ID_8 ID_100  0.543

graph1 <- graph_from_data_frame(df, directed = FALSE)

plot(graph1, vertex.label = V(graph1)$name)

mat <- shortest.paths(graph1)
mat
#>        ID_1 ID_2 ID_3 ID_4  ID_8 ID_5 ID_7 ID_100
#> ID_1   0.00 0.50 0.77 0.50   Inf 1.40 0.95    Inf
#> ID_2   0.50 0.00 1.21 0.44   Inf 0.90 0.89    Inf
#> ID_3   0.77 1.21 0.00 0.77   Inf 2.11 0.32    Inf
#> ID_4   0.50 0.44 0.77 0.00   Inf 1.34 0.45    Inf
#> ID_8    Inf  Inf  Inf  Inf 0.000  Inf  Inf  0.543
#> ID_5   1.40 0.90 2.11 1.34   Inf 0.00 1.79    Inf
#> ID_7   0.95 0.89 0.32 0.45   Inf 1.79 0.00    Inf
#> ID_100  Inf  Inf  Inf  Inf 0.543  Inf  Inf  0.000

Created on 2021-03-06 by the reprex package (v1.0.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.