Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conected cities

Status
Not open for further replies.

vbx

Programmer
Dec 12, 2010
18
RO
If i have this links:
Code:
link(detroit,toronto).
link(chicago,washington).
link(denver,sacramento).
link(sacramento,chicago).
link(toronto,chicago).
....................

How can i find witch pair of distinct cities are connected using 2 links ? Any ideea ? Thanks
 
You should somehow express that A is connected to B by 2 links if there is a C such that A is connected to C directly and C is connected to B directly. So you need to create an additional rule called 'link2' or something appropriate to express 'connected by 2 direct links'

Also, take care of cycles. You'll see that a naive approach to this problem would report each city as being connected to itself by 2 links, because A - B and B - A also fulfill the rule above.
 
hi

I need one sample of the output that you want
ie,one virtual result of the program.

thanks
 
link(X,C),link(C,Y),X \= Y.
 

link(detroit,toronto).
link(chicago,washington).
link(denver,sacramento).
link(sacramento,chicago).
link(toronto,chicago).


connected(X,Z):-link(X,Y),link(Y,Z).
%connected(X,Z):-link(Z,Y),link(Y,X).

cities(X,Y,C):-findall([X,Y],connected(X,Y),C).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top