Recursion and how to construct a graph? (What is the boundary condition?)

Ben Engbers <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
I have found lots of examples on how to construct a path through a
graph, but I didn't find any examples on how to construct the graph
itselfand that's exactly what I need to do..

I have a set of edges, all in the form edge( from, to, value) and I want
to construct a graph (list) for which 'from' is member.
Length(Graph) has to be >2.

In pseudocode I wanted to do this:

construct_graph( X, Graph) :-
  If \+member(X, Graph) Then construct_graph(X, [X|Graph]). /* Add X to
the graph, Recursion */

construct_graph( X, Graph) :-
   edge( X, To, Value), construct_graph( To, Graph), /* all the edges
for which X is starting point
   edge( To, X, Value), construct_graph( To, Graph). /* all the edges
for which X is end point

construct_graph( X, Graph) :-
   \+edge( X, _, _). /* X is not starting point for other edges.


In all the examples that I see for recursion, the ending boundary
condition is the first clause.
In my pseudocode, the ending boundary condition is the last clause.

My question is if this approach will work?

Ben Engbers
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.