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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying toconceptualize what these edges and facts define.

Status
Not open for further replies.

jwdavis

Programmer
Feb 8, 2005
2
US
I've never done anything in Prolog, and I'm terribly ignorant of what even the simplest of Prolog programs are doing.

I'm presented with such a Prolog program such as this:
edge(a,b).
edge(b,c).
edge(c,d).
edge(b,e).
edge(a,e).

path(X,Y):- edge(X,Y).
path(X,Z):- edge(X,Y), path(Y,Z).

For the most part, I've been able to discover that these facts represent a directed graph. The following 2 rules, as far as I figure, say something to the effect of (in English), "A path is made up of 2 nodes between X and Y, and the path between X and Y is bidirectional. Given the query to find the path between X and Z, first start with an edge between X and Y, and recursively proceed with Y until Z is reached."

At least, I think that's what it means.

So, when the above code is run against a query like:

path(X,Y)

You would get the following output:

X = a,
Y = b;
X = b,
Y = c;
X = c,
Y = d;
X = b,
Y = e;
X = a,
Y = e;
X = a,
Y = c;
X = a,
Y = e;
X = a,
Y = d;
X = b,
Y = d

Which is where the program would then fail.

Unfortunately for me, I don't even know what the initial query implies. Is running the query of "path(X,Y)" simply trying to find all the available paths between 2 nodes? That couldn't possibly be the case, and beyond that, I haven't a clue as to why it returns false as a path between B and D.

My justification for that last statement, about failing on B and D, is that since there's facts that say the edges {B,C} and {C,D} exist that it stands to reason that a path exists from {B, D} by way of C!

It was then that I drew out this graph, and I still haven't figured out much of anything.

Sorry for the long post. I just know there's something simple happening in here, and I can't figure it out and it's starting to make me a bit crazy.
 
I think perhaps I was viewing this incorrectly. After looking at it a bit more, I believe the program is simply path finding. I also think I was incorrect in saying the nodes were bidirectional as now I don't believe they are.

So, it now appears to me that perhaps all it's doing is attempting to display all of the paths that exists between any two nodes.

Unfortunately, even with that reasoning, I can't figure out why it would return the path between A and E twice.
 
path(X,Y). query Prolog to find all the connexions it can find in his database.
after X = b, Y = d, it gives false because there are no more connexions.
nodes are not bidirectional.
path(X,e) gives all the nodes that can be connected to e (path ending in e), there are 2 paths from a to e so it gives it twice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top