# All-pairs distances
adj <- matrix(c(
0, 1, 0, 0,
1, 0, 1, 0,
0, 1, 0, 1,
0, 0, 1, 0
), 4, 4)
rownames(adj) <- colnames(adj) <- LETTERS[1:4]
cograph::shortest_paths(adj)
# Single source to all
cograph::shortest_paths(adj, from = "A")
# Point-to-point
cograph::shortest_paths(adj, from = "A", to = "D")
Run the code above in your browser using DataLab