9

I came across a puzzle a few days ago. It's solvable easily by hand. But I was trying to build an algorithm for solving it. But i dont know how I should proceed. picture 1

Here you can see that i have to connect all pairs of colored dots. For example i need to connect yellow dot to another yellow dot, green to other green, blue to blue and so on.

Here is an example of how it should be solved. if the description was not clear. enter image description here

So you can see that i connected yellow dot with another yellow dot. And blue with another blue. But this causes a problem. I've blocked the path of aqua color as you can see. I hope you get the idea.

So i want to solve it. Brute force approach would work but it will take a long time and i'm not interested in that. I tried about implementing Breadth First Search, Depth First Search, and Dijkstra algorithm. But i think they won't be good in this case. Correct me please if I'm wrong. A* Search may work, but what will be the heuristic?

Can anyone give me some intuition on how to solve the problem?

Shubhashis
  • 10,411
  • 11
  • 33
  • 48
  • As I can see, any solutions of your example will contain blocked pairs. What should do the algorithms in this case? – Mark Shevchenko Sep 08 '15 at 14:09
  • 2
    This type of puzzle is called Numberlink or Arukone. According to [Wikipedia](https://en.wikipedia.org/wiki/Numberlink), the problem is NP-complete, so in the general case you won't be able to do much better than brute force. Though some algorithms may be able to do better on specific arrangements. You can find some solvers if you [search](https://www.google.com/search?q=arukone&ie=utf-8&oe=utf-8#q=numberlink+solver). – interjay Sep 08 '15 at 14:14
  • A* would work alot quicker than BFS. You need to figure out a heuristic. A way of scoring a valid solution. This way the A* can move toward the best score. You need to use a state object which keeps track of a current board state. Each move would be a new state that is scored by the heuristic – element11 Sep 08 '15 at 14:19
  • This is also the Flow Free game on Android. It's played with relatively small board sizes, around `12x12`, so brute force should run plenty fast with a few optimization. – IVlad Sep 08 '15 at 14:19
  • Actually what interjay said in the comments is true. This is an NP complete problem. I dont know if heuristic would work. And A* will be a lot faster. But computing the heuristic will be tough. Can you give me some intuition? – Shubhashis Sep 08 '15 at 14:21
  • @IVlad, yes it is. But what about if it was 50*50 ? You are correct. While i was solving the free flow levels i thought about solving it. – Shubhashis Sep 08 '15 at 14:23
  • I think you could do some backtracking, each time playing the most constrained color. In your example, the upper green dot has to "snake" to the east, because for any other color that would be a dead end, and then twice south, thus restricting the top blue and brown to only one possible move each, and so on. – tobias_k Sep 08 '15 at 14:24
  • Formulating the multicommodity flow problem as an integer program might be viable. – David Eisenstat Sep 08 '15 at 14:44
  • I think an alternate representation that captures the number of paths that can pass in the gap between two nodes would be useful. – biziclop Sep 08 '15 at 14:47
  • Apparently they like Numberlink at the University of Oxford. There's a link there to a solver written in Go: https://spivey.oriel.ox.ac.uk/corner/Programming_competition_results (https://github.com/thomasahle/numberlink) – גלעד ברקן Sep 09 '15 at 03:44

1 Answers1

1

I genetic algorithm might be an appropriate means to get a solution. The fitness function and the cross-over would have to be tailored specifically for the problem.

Chromosome:

  • 9x9 2d array of ints (genes)
  • Your 18 unique colored peices will be statically set as 512|1, 512|2, 512|4, 512|8, 512|16, 512|32, 512|64, 512|128, 512|256 representing the 9 unique colors; 512 (2^9) will be the flag denoting them as static/unchangable genes.
  • Colored connecting squares will have 2^0-2^8 values, these genes can change and be composed, a value of 3 (011b) would mean 2 colors (1 and 2) are sharing the same square.

Fitness function off the top of my head

  • Spaces with 1 unique color are +8, 2 colors +6, 3 colors +4, 4 colors +2, 5 colors +0, 6 colors -2, 7 colors -4, 8 colors -6, all 9 colors -8
  • Spaces connected (left,right,up,down) with a matching colored static space are +9
  • Spaces connected with a matching colored space in 1 direction +4, 2 directions +3, 3 directions +2, 4 directions +1

Mutation

  • Logical XOR a random color bit (1 << Floor(Random() * 9)) with a random non-static gene

Cross-Over / Breeding Off the top of my head

  • Copy your 2 candidate chromosomes
  • Clear out genes within the copies that contain 5 or more overlapping colors
  • Logical OR the two chromosomes together to get your result
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62