I'm working on an interesting problem in R (maybe using igraph and/or tidygraph libraries), where I need to find ALL POSSIBLE paths on a graph, which satisfy certain criteria. The problem can be simplified to the following:
I have 16 distinct nodes that can be separated into 4 sets, and where each node has a single characteristic, call it a color. *Note: Below might not be the best way to represent the data, but hopefully it communicates the situation.
nodes_set_1 <- c("red", "blue", "orange")
nodes_set_2 <- c("green", "blue", "red", "yellow", "purple")
nodes_set_3 <- c("blue", "green", "red", "orange", "purple")
nodes_set_4 <- c("orange", "blue", "green")
I now need to find ALL POSSIBLE paths between these nodes that satisfy the following three conditions:
(1) Each path must contain exactly one node from each set.
(2) The graph is directed from nodes_set_1 to nodes_set_2 to nodes_set_3 to nodes_set_4
(3) No color can be repeated within a single path.
So for example the following path would be valid:
path_1 <- c(nodes_set_1[1], nodes_set_2[1], nodes_set_3[1], nodes_set_4[1])
And the path below would be invalid because the color "blue" is repeated:
path_2 <- c(nodes_set_1[2], nodes_set_2[2], nodes_set_3[2], nodes_set_4[2])
I would love some advice on setting up this problem and solving. It would also be amazing to find a way to efficiently determine if no valid solution exists.
Thank you!
Base R options
I don't think you really need
igraphortidygraphto find all possible paths, and base R should be sufficient to make it. Below are two options:-
expand.grid+FilterUsing
expand.gridto generate all combinations and then subset it based on criteriaand you will see
- Recursion (More Efficient)
Probably a more efficient way is using recursion, by defining a custom function, such that all possible duplicates are skipped during the process of generating the paths
and you can simply run
f()and will obtainBenchmark
shows