3

I have a problem that looks like this: partial solution found

enter image description here

I want to calculate the minimum number of roads required to connect all sources (yellow) to the destination (green). However, I also want to ensure that there is the minimum path length from each source to destination. Diagonals are same move cost as up/down/left/right.

Greedy doesn't work, it won't guarantee min total roads, got any other suggestions? Shown in the image is the possible min paths from each source to the destination, with overlap shown as larger circles.

Edit: greedy example failing:

enter image description here

Davaned
  • 31
  • 3
  • Can there be multiple destinations? Do you need the optimal solution or is a "good" one okay? (Is this screeps?) – c2huc2hu Sep 07 '17 at 15:59
  • I'm aiming for optimal. If its a difference of vast amount of computation between optimal and near-optimal then near is ok. Yes, its screeps! :D good eye Planning this to only have 1 destination. That said, if multiple destinations comes with a solution such that every source needs to reach at least 1 destination, thats even better. – Davaned Sep 07 '17 at 16:04
  • I will look at the problem differently. A simple approach is to perform a unidirectional best-first search (Dijkstra or A*) from the goal to a set of destination nodes. Every time the shortest path is found for a node (original start node) you will pop it out from the set. Once the set is empty you have found the shortest path to each of the nodes (here we assume the cost of the path from each node n->n' is the same than the cost of the path from n' to n). – FrankS101 Sep 08 '17 at 11:36
  • Isn't that an expensive way to generate what you get with a breadth first search to the nodes that generates all the paths? It seems it would give you a min path to each source (which is given in my above images, red is only along possible min paths) I can't see how it will achieve the min number of roads overall though – Davaned Sep 08 '17 at 14:54
  • What are the limits on the size of the board? Do you have memory/ time constraints? A variant on depth first search might work, but it's memory intensive – c2huc2hu Sep 08 '17 at 15:27
  • I have cpu constraints, anything over 0.4 sec execution time will be extremely difficult. Memory isn't that bad, less than 5mb should be fine. Board is at max 50x50, with some walls reducing the usable area. – Davaned Sep 08 '17 at 17:32
  • i think following would work..you can treat the destination as the source and sources as destination and then run a djikstra to find the shorteast paths – Debabrata Sep 09 '17 at 16:02
  • So thats going to find shortest path to each source, which is what I have. IF you run every single path permutation that exists you can bruteforce an answer by comparing every path's total number of roads, but its.... well see for yourself here's a reasonably doable computation I just did: ```[12:32:36 AM]path sets: 21 861 135 [12:32:36 AM]total paths: 1017 [12:32:36 AM]cartesian product: 2440935 ``` and here is one tile further ```[12:33:13 AM]path sets: 89 3381 381 [12:33:13 AM]total paths: 3851 [12:33:13 AM]cartesian product: 114646329``` 2440935 vs 114646329 for +1 tile – Davaned Sep 09 '17 at 18:23

0 Answers0