prolog - Getting all nodes that have atleast 2 nodes connected to them -


having issues prolog question:

the following clauses represent directed graph, nodes atoms, , edges denoted connected predicate. given following clauses in database, answer 2 questions below.

connected(a,b). connected(b,d). connected(b,e). connected(b,f). connected(a,c). connected(c,f). connected(c,g). connected(h,c). path(x,y) :- connected(x,y). path(x,z) :- connected(x,y), path(y,z). 

show prolog query returns nodes having 2 or more different incoming edges (i.e., @ least 2 different nodes connected it). also, show result of entering query (asking every solution). query may return same node multiple times, , may printout values of other variables in query. variable denoting node in question should called dnode.


so far have:

path(dnode,__) , path(__,dnode). 

but give me b , c
think letters more 1 nodes a, b, c, f.
tried a, b , c:

path(__,dnode),path(dnode,__) ; path(dnode,__) , path(dnode,__). 

but got a, b, c , h.
assuming i'll have letters want:

path(__,dnode),path(dnode,__) ; path(dnode,__) , path(dnode,__) ; path(__,dnode) , path(__,dnode). 

it gives me a, b, c, e, f, g , h though.

any advice how 4 letters want appreciated.

if display graph, perhaps graphviz

enter image description here

?- writeln('digraph g {'), forall(connected(a,b), writeln(a->b)), writeln('}'). 

you can see [c,f] have 2 incoming edges, , don't need path/2. it's sufficient join on second argument of connected/2, test first arguments different (operator (\=)/2).


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -