0
votes

I'm making this topic cause I'm completely lost in something I have to do...

I have the following edges in the next sintaxis:

edge(N1,N2,C1,C2,Color).

Where:

  • N1 means Node 1.
  • N2 means Node 2.
  • C1 is the cost to go from Node 1 to Node 2.
  • C2 is the cost to go from Node 2 to Node 1.
  • and Color is the color of the edge.

Now, because this is a directed multigraph we can have N edges from N1 to N2, and the thing that I have to do is make a rule which calculate the sum of the costs to go from N1 to N2 (the color doesn't matter), but it have to have the sintaxis of "rule(N1,N2,X)" where X will give the sum of costs, so if we have the following facts.

edge(a,b,1,6,red).
edge(c,b,2,1,blue).
edge(d,a,4,7,yellow).
edge(a,b,6,2,green).
edge(c,b,3,4,black).
edge(d,a,2,7,white).
edge(a,b,1,6,green).

I should be able to do this: ?- sumofcosts(a,b,X): X= 8.

I have been seen a lot of ways to have a counter in prolog, to acumulate, etc... but I have a problem that make me don't even know how to start, which is that all those acumulators, counters, etc... were implemented with lists, but in my case there is not a list (or do I have to implement 1 in the list?).

I need Help!!!.

1

1 Answers

0
votes

With findall/3 you can get a list of the costs between two nodes N1 and N2:

sumofcosts(N1,N2,Sum) :-
  findall(C1,edge(N1,N2,C1,_,_),Costs),
  ... .

Now, computing the sum of the list's elements should be easy.