Given a grid how can I determine if elements of a grid are all in a single region. In the below case is true because each element in the matrix has a neighbor.
Example1:
gridneighbours([[1,1],[1,2],[1,3],[2,1],[2,2],[2,3],[3,1],[4,1],[4,2]]).
true.
However in my second example, Example2:
gridneighbours([[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[3,1],[4,1],[4,2]]).
false.
This is false because [3,1],[4,1],[4,2] are disjoint to the previous elements. Initially I tried using subset from Prolog to check for an existing element next to another by simply adding or subtracting from X or Y, however this doesn't work because elements of a split region would be next to each other. Also diagonals don't count as being next to each other.
Updated, added code: Here is what I got:
%Check right
neighbourcheck([X,Y|_],S) :- X1 is X + 1, subset([[X1,Y]],S).
%Check left
neighbourcheck([X,Y|_],S) :- X1 is X - 1, subset([[X1,Y]],S).
%Check Up
neighbourcheck([X,Y|_],S) :- Y1 is Y + 1, subset([[X,Y1]],S).
%Check Down
neighbourcheck([X,Y|_],S) :- Y1 is Y - 1, subset([[X,Y1]],S).
% Iterate through all sublists and check for neighbours
gridneighbour(S) :- forall(member(X,S), neighbourcheck(X,S)).
The reason why this doesn't work is because subset doesn't care if we have a match up with another element that is disjointed. i.e. [3,1] matches up with [4,1]. Running this code and using the examples above give:
- Example1: True
- Example2: True (clearly this should be false because [3,1],[4,1]and [4,2] are seperated).
This problem can be viewed as an instance of union find algorithms. Such algorithms often make use of a special data structure, which basically serves the following two purposes:
Here is a Prolog implementation that uses a thread local fact linked/2 as the union find data structure. Here is an example run for the second grid problem:
Technical note: Prolog unification has also a built-in union find component, if you mention a variable X, it will be dereferenced, which is step 1). If you do the unification X=Y and X and Y are already dereferenced, one variable will link to another, which is step 2).
A naive approach that works can be outlined as follows:
Region
and the rest,Rest
. At the beginning, you can pick any single point to belong toRegion
, and whatever remains is inRest
.Rest
for a point that is a neighbor to any point inRegion
.Rest
toRegion
and repeatRest
at the end, then this is not a region.Here is a simpler way to define
neighbors/2
:You can look for a point in one list that is a neighbor of a point in another list by simply trying out every possible combination:
The call to member/2 picks each point in Region to A, by backtracking. The call to select/3 picks each point in Rest0 to B, with rest of the points in Rest. If the two points are neighbors, B is added to front of Region.
This will fail if there is no more neighbors to the current region in
Rest
, and succeed at least once if there are. You might want to call this withonce(add_to_region(Region0, Rest0, Region, Rest))
so that you don't have unnecessary choice points. Using your examples:See how
[2,2]
was picked fromRest
and added toRegion
.This fails because none of the points in
Rest
is a neighbor to any of the points inRegion
.Edit
As explained above is definitely doable, but with a slight modification, we can have an algorithm that is much easier to implement in Prolog. It goes like this:
set_region_rest(+Set, -Region, -Rest)
ordset
.To do the splitting, we will maintain one extra list. We will call it a list of Open nodes: nodes that we haven't explored yet. At the beginning, the first element of our input list is the only open node. The rest of the elements are passed as they are. The last two arguments, the Region, and the Rest, are the output arguments.
open_set_closed_rest(Open, Set, Closed, Rest)
To do this in Prolog, I will first clean up the coordinate representation. It is a bit annoying that they come in lists of two: it is far less writing if we used for example a pair instead:
[X,Y] ---> X-Y
. To do this, I add this predicate:(I also put 4 additional test sets!)
So with this, I get:
Here is how one could try to write the above algorithms in code:
This just sorted the input Set and split off the first element from it. The first element is the first coordinate pair in the Open set, the rest is the Set, then the output arguments.
Now, to split the Set into a Region and a Rest, we need to keep on growing the Region as long as we have coordinate pairs in the Open set. If the Open set is empty, this means our Region is complete, and the remaining Set is the Rest:
To find out which neighbors of a coordinate are in the Set, we use
ord_intersection/4
, which gives us the neighbors in Set and the rest of Set at the same time.NB: The 4 neighbor coordinates are listed sorted!
This is it. With this, I get the following 6 solutions:
By the way, using
set_region_rest/3
as a building block, we can easily split a set of coordinates into regions:So now: