-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_basic.pl
53 lines (40 loc) · 1.39 KB
/
1_basic.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
%% 10.1.1 Basic union-find
%% :- module(unionfind,[make/1,union/2,find/2,root/1,(~>)/2]).
:- use_module(library(chr)).
:- op(700, xfx, '~>').
:- chr_constraint
make/1,
find/2,
union/2,
(~>)/2,
link/2,
root/1.
%% The following operations are supported:
%% *make(A)*: create a new set with single element A
%% *find(A,B)*: B is representative of the set containing A
%% *union(A,B)*: join the two sets containing A and B
%% *link(A,B)*: join the two sets represented by A and B (internal)
%% Data is represented as: #
%% *root(A)*: A is the representative of a set (root of tree) #
%% *A ~> B*: A and B are in the same set (edge indirectly pointing to root)
make @ make(A) <=> root(A).
union @ union(A,B) <=> find(A,X), find(B,Y), link(X,Y).
findNode @ A ~> B \ find(A,X) <=> find(B,X).
findRoot @ root(A) \ find(A,X) <=> X=A.
linkEq @ link(A,A) <=> true.
link @ link(A,B), root(A), root(B) <=> B ~> A, root(A).
%% Tests
%?- make(a), make(b), make(c), make(d), make(e), union(a,b), union(c,d), union(e,c).
%@ c~>e,
%@ d~>c,
%@ b~>a,
%@ root(e),
%@ root(a).
%?- make(a), make(b), make(c), make(d), make(e), union(a,b), union(c,d), union(e,c), find(b,X), find(d,Y).
%@ X = a,
%@ Y = e,
%@ c~>e,
%@ d~>c,
%@ b~>a,
%@ root(e),
%@ root(a).