Closed mkirzon closed 11 months ago
@mkirzon I believe supplying more than one node to nodes
argument of node_distance_to()
does not do what you think. These "target" nodes get recycled, so the first entry in the result is the distance to "a", the second to "d", the third to "a" and so on. You can compare to igraph results and see that this is the case. You probably need:
gt %>%
activate(nodes) %>%
mutate(
distToA = node_distance_to(nodes = 1, mode = "all", algorithm = "automatic"),
distToD = node_distance_to(nodes = 4, mode = "all", algorithm = "automatic")
) %>%
as_tibble()
@mbojan Wow, thank you. I entirely missed the middle column for igraph
. Is there a more efficient tidygraph
way then to generate the distance matrix that's returned by igraph::distances
? I'm building out a set of tools in which we would often expect 100's or 1000's of target nodes.
I was using node_distance_*
to help filter the graph to the subset of all paths that contain at least one of the N target nodes. Open to other ideas :) Though sorry if this should be taken to stackoverflow
What's wrong with using igraph::distances()
directly? No wrapper, however tidy, will be more efficient. If you need a dataframe output you may e.g.:
gt %>% # or `gi`, does not matter
distances(to = c("a", "d")) %>%
as.data.frame(stringsAsFactors=FALSE) %>%
rownames_to_column(var = "from")
Sorry, missed this:
I was using
node_distance_*
to help filter the graph to the subset of all paths that contain at least one of the N target nodes. Open to other ideas :) Though sorry if this should be taken to stackoverflow
Does that mean that you basically want a union of two minimal spanning trees rooted at "a" and "d" respectively?
+1 Invalid vertex name(s)
I'm finding that the tidygraph
node_distance_to
is returning incorrect results, even though it should be just a wrapper aroundigraph::distances
. Here's an example:We want to calculate the distance from all nodes to 'a' and 'd' (which will be ids 1 and 4, respectively)
Here's the result from igraph
Here's the result from tidygraph
A clear example of the issue is in looking at the distance from nodes 'b' and 'c'. These should really be identical but tidygraph gives distances 4 and 1, respectively.