Floyd warshall
  Home FAQ Contact Sign in
comp.lang.prolog only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.prolog Profile…
 Up
Floyd warshall         


Author: andrea
Date: Jul 11, 2007 01:44

I need to find shortest paths from from every nodes to every nodes of
a graph.

I think Floyd warshall could be alright but I've got no idea of how
could be the implementation in prolog...

Any idea?? Google didn't helped unfortunately...

Any other ways to solve this problem?
25 Comments
Re: Floyd warshall         


Author: Cesar Rabak
Date: Jul 11, 2007 11:52

andrea escreveu:
> I need to find shortest paths from from every nodes to every nodes of
> a graph.

Since this questions is suspiciously similar to someone asking for a
homework:
>
> I think Floyd warshall could be alright but I've got no idea of how
> could be the implementation in prolog...

Do you know it works so you recognize how to describe it?

Do you know enough Prolog so you can represent the graph in Prolog terms?
>
> Any idea?? Google didn't helped unfortunately...

If you can do what I ask above, you could build a set of predicates to
perform the algorithm.
>
>
> Any other ways to solve this problem?
>

Yes, a lot. What is exactly your purpose?
no comments
Re: Floyd warshall         


Author: andrea
Date: Jul 12, 2007 00:02

On 11 Lug, 20:52, Cesar Rabak yahoo.com.br> wrote:
> andrea escreveu:
>
>> I need to find shortest paths from from every nodes to every nodes of
>> a graph.
>
> Since this questions is suspiciously similar to someone asking for a
> homework:
>
>
>
>> I think Floyd warshall could be alright but I've got no idea of how
>> could be the implementation in prolog...
>
> Do you know it works so you recognize how to describe it?
>
> Do you know enough Prolog so you can represent the graph in Prolog terms?
>
>
> ...
Show full article (2.05Kb)
no comments
Re: Floyd warshall         


Author: Cesar Rabak
Date: Jul 12, 2007 14:31

andrea escreveu:
> On 11 Lug, 20:52, Cesar Rabak yahoo.com.br> wrote:
>> andrea escreveu:
>>
>>> I need to find shortest paths from from every nodes to every nodes of
>>> a graph.
[snipped]
>>> Any other ways to solve this problem?
>> Yes, a lot. What is exactly your purpose?
>
> The purpose is a kind of navigator.
> Given an input file with cities and a distance table and a desired
> path I have to give as output the shortest path between them (if
> exists).
>
> I ended up with something like this
> %%%% X = src, Y = dst, K = infra nodes, D = final distance

From the comment above and the implementation below, it seems your
nodes are being represented by integers, right?
Show full article (2.25Kb)
no comments
Re: Floyd warshall         


Author: andrea
Date: Jul 13, 2007 08:34

On 12 Lug, 23:31, Cesar Rabak yahoo.com.br> wrote:
> andrea escreveu:
>
>
>
>> On 11 Lug, 20:52, Cesar Rabak yahoo.com.br> wrote:
>>> andrea escreveu:
>
>>>> I need to find shortest paths from from every nodes to every nodes of
>>>> a graph.
> [snipped]
>>>> Any other ways to solve this problem?
>>> Yes, a lot. What is exactly your purpose?
>
>> The purpose is a kind of navigator.
>> Given an input file with cities and a distance table and a desired
>> path I have to give as output the shortest path between them (if
>> exists).
>
>> I ended up with something like this ...
Show full article (2.92Kb)
no comments
Re: Floyd warshall         


Author: Cesar Rabak
Date: Jul 13, 2007 21:44

andrea escreveu:
> On 12 Lug, 23:31, Cesar Rabak yahoo.com.br> wrote:
>> andrea escreveu:
>>
>>
>>
>>> On 11 Lug, 20:52, Cesar Rabak yahoo.com.br> wrote:
>>>> andrea escreveu:
>>>>> I need to find shortest paths from from every nodes to every nodes of
>>>>> a graph.
>> [snipped]
>>>>> Any other ways to solve this problem?
>>>> Yes, a lot. What is exactly your purpose?
>>> The purpose is a kind of navigator.
>>> Given an input file with cities and a distance table and a desired
>>> path I have to give as output the shortest path between them (if
>>> exists).
>>> I ended up with something like this
>>> %%%% X = src, Y = dst, K = infra nodes, D = final distance
>> From the comment above and the implementation below, it seems your ...
Show full article (3.47Kb)
no comments
Re: Floyd warshall         


Author: andrea
Date: Aug 22, 2007 02:00

On 14 Lug, 06:44, Cesar Rabak yahoo.com.br> wrote:
> andrea escreveu:
>
>
>
>
>
>> On 12 Lug, 23:31, Cesar Rabak yahoo.com.br> wrote:
>>> andrea escreveu:
>
>>>> On 11 Lug, 20:52, Cesar Rabak yahoo.com.br> wrote:
>>>>> andrea escreveu:
>>>>>> I need to find shortest paths from from every nodes to every nodes of
>>>>>> a graph.
>>> [snipped]
>>>>>> Any other ways to solve this problem?
>>>>> Yes, a lot. What is exactly your purpose?
>>>> The purpose is a kind of navigator.
>>>> Given an input file with cities and a distance table and a desired
>>>> path I have to give as output the shortest path between them (if ...
Show full article (4.18Kb)
no comments
Re: Floyd warshall         


Author: andrea
Date: Aug 22, 2007 05:12

This is the final predicates (for now)

%%%% data la lista delle citta calcolo il percorso piu breve
%%%% 3 casi, 1.lista terminata, 2.piu di due elementi, 3.due elementi
path([],0).
path([H | T],D) :-
T = [H1 | []], shortest_path(H,H1,K,D,T).
path([H | T],D) :-
T = [H1 | T1], shortest_path(H,H1,K,D1,T), path(T,D2), D is D1 + D2.

%%%% TODO necessarie informazioni su dove si passa
%%%% forma finale dei predicati distanza dist(C1,C2,X)
%%%% shortest_path(Src,Dst,K,Dst,Tappe).
shortest_path(X,X,_,0,_). %%%% citta' con se stessa non...
Show full article (1.55Kb)
no comments
Re: Floyd warshall         


Author: andrea
Date: Aug 30, 2007 09:37

I changed my predicate to one closer to the floyd warshall's
algorithm, now it's like that.

min_dist(_,[],_). %%%% caso base
min_dist(K,[C1 | T1], []) :- cities(C),path_to_idx(C,I),
min_dist(K,T1,I). %%%% ho consumato la sottolista

min_dist(0,Cities,[C2 | T2]) :-
Cities=[C1 | T1], not(dist(C1,C2,Dist)), write_min_dist(C1,C2,1000,
[C2]), min_dist(0,Cities,T2).
min_dist(0,Cities, [C2 | T2]) :-
Cities=[C1 | T1], dist(C1,C2,Dist), write_min_dist(C1,C2,Dist,[C2]),
min_dist(0,Cities,T2).

min_dist(K, Cities, [C2 | T2]) :-
Cities=[C1|T1], K > 0, J is K-1,
sh_path(C1,C2,Diretto,InfraD), sh_path(C1,J,Before,InfraB),
sh_path(J,C2,After,InfraA),
S is Before+After, min(Diretto,S,Diretto) ->
write_min_dist(C1,C2,Diretto,InfraD), min_dist(K,Cities,T2);
append(InfraD,InfraB,Total), write_min_dist(C1,C2,Total,S),
min_dist(K,Cities,T2).
Show full article (1.90Kb)
no comments
Re: Floyd warshall         


Author: andrea
Date: Aug 30, 2007 14:49

I tried with a very small example

prova(X,Y) :-
dist(X,Y,Z), Z > 2 ->
write('maggiore'); write('minore'),write(Z).

?- prova(0,1).
minore_L136

So in the "else branch" Z is not unified anymore, why?? I dont' find a
suitable explanation..
How can I translate this little piece of code?
no comments
 
1 2 3