Graph representation 
Author Message
 Graph representation

Hi,

    Does someone know how to transform this graph :

node([s,t,u,v,w]).
edge(s,t,3).
edge(t,v,1).
edge(t,u,5).
edge(u,t,2).
edge(v,u,2).
edge(v,w,2).
edge(w,u,4).
edge(w,s,6).

    To this graph :

digraph([s,t,u,v,w],[a(s,t,3),a(t,v,1),a(t,u,5),a(u,t,2),a(v,u,2),a(v,w,2),a
(w,u,4),a(w,s,6)]).

    I try somelike that that :

digraph(N,[a(X,Y,Z)] :- node(N), edge(X,Y,Z).

but is does not transforme the a(X,Y,Z) to a real list like
[a(s,t,3),a(t,v,1),...]

Thanks



Thu, 08 May 2003 10:48:45 GMT  
 Graph representation

Quote:
>     Does someone know how to transform this graph :
> node([s,t,u,v,w]).
> edge(s,t,3).
> edge(t,v,1).
> edge(t,u,5).
> edge(u,t,2).
> edge(v,u,2).
> edge(v,w,2).
> edge(w,u,4).
> edge(w,s,6).
>     To this graph :
> digraph([s,t,u,v,w],[a(s,t,3),a(t,v,1),a(t,u,5),a(u,t,2),a(v,u,2),a(v,w,2),a
> (w,u,4),a(w,s,6)]).

transform_graph(digraph(Nodes,Edges)):-
   node(Nodes),
   findall(a(S,T,L),edge(S,T,L),Edges).

--
Gertjan van Noord Alfa-informatica, RUG,  Postbus 716, 9700 AS Groningen
vannoord at let dot rug dot nl            http://www.let.rug.nl/~vannoord



Thu, 08 May 2003 03:00:00 GMT  
 Graph representation

Quote:

> Hi,
>     Does someone know how to transform this graph :
> node([s,t,u,v,w]).
> edge(s,t,3).
> edge(t,v,1).
> edge(t,u,5).
> edge(u,t,2).
> edge(v,u,2).
> edge(v,w,2).
> edge(w,u,4).
> edge(w,s,6).
>     To this graph :
> digraph([s,t,u,v,w],[a(s,t,3),a(t,v,1),a(t,u,5),a(u,t,2),a(v,u,2),a(v,w,2),a
> (w,u,4),a(w,s,6)]).
>     I try somelike that that :
> digraph(N,[a(X,Y,Z)] :- node(N), edge(X,Y,Z).
> but is does not transforme the a(X,Y,Z) to a real list like
> [a(s,t,3),a(t,v,1),...]

This is a "save information over backtracking" problem. What you want to do
is backtrack over your calls to the predicate 'edge' and combine the
information you get from each backtrack into one thing.

It's a common problem for newbies to Prolog to think this sort of thing
can be done if only they fiddle around with the code for long enough. It's
so common that I think all teachers of Prolog should point out, early on,
that it can't be done without employing some sort of trick.

The state of Prolog variables returns to exactly where they were before
when a backtrack occurs. That is why you can't save information over
backtracking. The only way you can do it is to use the findall/setof/bagof
built-in predicates, or manage your own saving of information in the
Prolog database using assert/retract.

Matthew Huntbach



Mon, 12 May 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Sources for graph representation

2. graphical graph representation algorithms

3. graph representation languages

4. Response to Guido's essay on graph representation

5. Nested graph representation

6. graph representations in lisp

7. Directed Acyclic Graph Representation

8. tree (graph) representation in Tk4.0 Canvas?

9. Graphical representation of trees and graphs

10. Graphical representation of trees and graphs

11. Infinite data structures (was representation of graphs ?)

12. Graphs and statistical representations ?

 

 
Powered by phpBB® Forum Software