transitive-closure

Create a list of unique numbers by applying transitive closure

若如初见. 提交于 2020-01-04 06:45:33
问题 I have a list of tuples (each tuple consists of 2 numbers) like: array = [(1, 2), (1, 3), (2, 4), (5, 8), (8, 10)] Lets say, these numbers are ids of some db objects (records) and inside a tuple, there are ids of duplicate objects. Which means 1 and 2 are duplicate. 1 and 3 are duplicate which means 2 and 3 are also duplicate. if a == b and b == c then a == c Now I want to merge all these duplicate objects ids into a single tuple like this: output = [(1, 2, 3, 4), (5, 8, 10)] I know I can do

Prolog : eliminating cycles from indirect relation

戏子无情 提交于 2020-01-04 06:21:06
问题 I have a list of user facts defined as: user(@michael). user(@ana). user(@bob). user(@george). user(@john). and so on. Furthermore, I have a set of facts as: follows(@michael,@ana). follows(@ana,@bob). follows(@bob,@michael). I am trying to write a relation indirect(user1,user1) which will tell me if user1 indirectly follows user2. However, I am not able to do away with cyclic relations. Like in the given example, michael -> ana -> bob -> michael will cause a cycle. What is the best way to

Prolog, Determine if graph is acyclic

倖福魔咒の 提交于 2020-01-04 05:20:15
问题 I need to define a predicate acyclic/1 that takes a graph in as input and determine if that graph is acyclic. So from my understanding graph1(a,b). graph1(b,c). graph1(c,a). Will return no and graph2(a,b). graph2(b,c). will return yes I made a predicate to determine if 2 nodes in a graph are connected and if so they will return yes. isConnected(X,Y) :- a(X,Z), isConnected(Z,Y). is there a way that I can use this to determine if a graph is acyclic? I do not want to use any predefined

Can't understand why is prolog looping infinitly

只谈情不闲聊 提交于 2019-12-23 18:53:28
问题 From Bratko's book, Prolog Programming for Artificial Intelligence (4th Edition) We have the following code which doesn't work - anc4(X,Z):- anc4(X,Y), parent(Y,Z). anc4(X,Z):- parent(X,Z). In the book, on page 55, figure 2.15, is shown that parent(Y,Z) is kept calling until stack is out of memory. What I don't understand is that prolog does a recursiv call to anc4(X,Y), and not to parent (Y,Z) first. Why doesn't prolog goes over and over to the first line , anc4(X,Y) , and rather goes to the

ERROR: Out of local stack in my Prolog code

╄→尐↘猪︶ㄣ 提交于 2019-12-22 12:27:07
问题 I cannot figure out why the following query from the given Prolog code generates the error Out of local stack . Prolog code: likes(g,c). likes(c,a). likes(c,b). likes(b,a). likes(b,d). likes(X,Z) :- likes(X,Y), likes(Y,Z). the query ?- likes(g,X). results in X = c ; X = a ; X = b ; ERROR: Out of local stack Edit 1 This is the way I think that Prolog should deal with this query, likes(g,c) is a fact, so X={c} likes(g,b) <= likes(g,c) and likes(c,b), so X={c,b} likes(g,a) <= likes(g,b) and

Define graph in Prolog: edge and path, finding if there is a path between two vertices

本秂侑毒 提交于 2019-12-17 20:16:55
问题 I'm very new to Prolog. I defined in graph.pl the following graph: And here's my Prolog code: edge(a,e). edge(e,d). edge(d,c). edge(c,b). edge(b,a). edge(d,a). edge(e,c). edge(f,b). path(X,X). path(X,Y):- edge(X,Z) ; path(Z,Y). I understand it like this: there is a path between vertex X and vertex Y only if there is an edge between vertex X and vertex Z AND there is a path between vertex Z and vertex Y (some kind of recursion). Is that right for the presented graph? When I ask Prolog about

Prolog compiler return error

此生再无相见时 提交于 2019-12-12 20:46:40
问题 I have this hypothetical program to check if a path exists from point A to B. /*city rules*/ edge(phx, tuc). edge(tuc, ccg). edge(ccg, sf). connected(C1, C2) :- edge(C1, C2). connected(C1, C2) :- edge(C1, X), connected(X, C2). the problem is it returns true, then false. Where is the false coming from? 回答1: Let's look at the exact reply you got from Prolog! First you got a single true and on pressing SPACE or ; you eventually got: ?- connected(phx,sf). true ; false. So you got true ; false. as

An infinite success tree, or not?

最后都变了- 提交于 2019-12-10 20:33:20
问题 I'm given the following program: edge(a,b). edge(b,c). edge(a,d). path(N,M):- path(N,New),edge(New,M). path(N,M):- edge(N,M). And asked if when applying a proof tree algorithm to the following query: ?- path(a,X). the proof tree is an infinite success tree, or an infinite failure tree? Now as I see it, during the building of the tree, you get stuck in applying rule 1 of path over and over again, creating an infinite tree and never getting to rule 2 of path.. thus creating an infinite failure

Stop condition for recursion in prolog

别来无恙 提交于 2019-12-10 19:22:28
问题 Here are the facts that I have in my knowledge base (http://www.doc.gold.ac.uk/~mas02gw/prolog_tutorial/prologpages/recursion.html (Recursion Exercise 2)): taller(bob,mike). % bob is taller than mike taller(mike,jim). % mike is taller than jim taller(jim,george). % jim is taller than george Now I want to use recursion to deduce something that is obvious "bob" is taller than "george". I tried to add this rule to solve this: taller(X,Y) :- taller(X,Z),taller(Z,Y). I need your help to make a

Prolog: check transitivity for simple facts

£可爱£侵袭症+ 提交于 2019-12-08 07:26:43
问题 My intention was to implement a simple example (just for myself) of transitivity in Prolog. These are my facts: trust_direct(p1, p2). trust_direct(p1, p3). trust_direct(p2, p4). trust_direct(p2, p5). trust_direct(p5, p6). trust_direct(p6, p7). trust_direct(p7, p8). trust_direct(p100, p200). I've written this predicate to check whether A trusts C , which is true whenever there is a B that trusts C and A trusts this B : trusts(A, B) :- trust_direct(A, B). trusts(A, C) :- trusts(A, B), trusts(B,