How to find shortest path in prolog with weighted graph
up vote
0
down vote
favorite
I have this code :
link(a,b,4).
link(a,c,2).
link(b,g,5).
link(c,g,6).
link(c,d,5).
link(d,g,3).
path(S,D,TDist):-
link(S,D,TDist).
path(S,D,TDist):-
link(S,X,TD1), path(X,D,TD2), TDist=TD1+TD2.
This will follow a depth first search strategy, but the result is that it will give me all the paths, and it won't show which is the shortest. Is it possible to still use that strategy and find the shortest path? if not, what search strategy to use? and how can I implement it.
prolog depth-first-search shortest-path uniform-cost-search
add a comment |
up vote
0
down vote
favorite
I have this code :
link(a,b,4).
link(a,c,2).
link(b,g,5).
link(c,g,6).
link(c,d,5).
link(d,g,3).
path(S,D,TDist):-
link(S,D,TDist).
path(S,D,TDist):-
link(S,X,TD1), path(X,D,TD2), TDist=TD1+TD2.
This will follow a depth first search strategy, but the result is that it will give me all the paths, and it won't show which is the shortest. Is it possible to still use that strategy and find the shortest path? if not, what search strategy to use? and how can I implement it.
prolog depth-first-search shortest-path uniform-cost-search
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this code :
link(a,b,4).
link(a,c,2).
link(b,g,5).
link(c,g,6).
link(c,d,5).
link(d,g,3).
path(S,D,TDist):-
link(S,D,TDist).
path(S,D,TDist):-
link(S,X,TD1), path(X,D,TD2), TDist=TD1+TD2.
This will follow a depth first search strategy, but the result is that it will give me all the paths, and it won't show which is the shortest. Is it possible to still use that strategy and find the shortest path? if not, what search strategy to use? and how can I implement it.
prolog depth-first-search shortest-path uniform-cost-search
I have this code :
link(a,b,4).
link(a,c,2).
link(b,g,5).
link(c,g,6).
link(c,d,5).
link(d,g,3).
path(S,D,TDist):-
link(S,D,TDist).
path(S,D,TDist):-
link(S,X,TD1), path(X,D,TD2), TDist=TD1+TD2.
This will follow a depth first search strategy, but the result is that it will give me all the paths, and it won't show which is the shortest. Is it possible to still use that strategy and find the shortest path? if not, what search strategy to use? and how can I implement it.
prolog depth-first-search shortest-path uniform-cost-search
prolog depth-first-search shortest-path uniform-cost-search
edited Nov 13 at 0:22
false
10.9k769141
10.9k769141
asked Nov 10 at 19:08
sosscs
363
363
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I think there are problems with your code:
TDist=TD1+TD2
doesn't compute the sum, use is/2 instead, at least when a path is returned.It will loop if the graph contains cycles, but assuming the data actually is a DAG, we can ignore by now.
We can't say what the actual path will be, just its value.
Anyway, library(aggregate) can be used to find the shortest path. For instance
?- aggregate(min(D), path(a,g,D), D).
D = 8.
Or, since gnu-prolog doesn't have library(aggregate), take the first element computed by setof/3:
?- setof(D, path(a,g,D), [Min|Rest]).
Min = 8,
Rest = [9, 10].
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I think there are problems with your code:
TDist=TD1+TD2
doesn't compute the sum, use is/2 instead, at least when a path is returned.It will loop if the graph contains cycles, but assuming the data actually is a DAG, we can ignore by now.
We can't say what the actual path will be, just its value.
Anyway, library(aggregate) can be used to find the shortest path. For instance
?- aggregate(min(D), path(a,g,D), D).
D = 8.
Or, since gnu-prolog doesn't have library(aggregate), take the first element computed by setof/3:
?- setof(D, path(a,g,D), [Min|Rest]).
Min = 8,
Rest = [9, 10].
add a comment |
up vote
0
down vote
I think there are problems with your code:
TDist=TD1+TD2
doesn't compute the sum, use is/2 instead, at least when a path is returned.It will loop if the graph contains cycles, but assuming the data actually is a DAG, we can ignore by now.
We can't say what the actual path will be, just its value.
Anyway, library(aggregate) can be used to find the shortest path. For instance
?- aggregate(min(D), path(a,g,D), D).
D = 8.
Or, since gnu-prolog doesn't have library(aggregate), take the first element computed by setof/3:
?- setof(D, path(a,g,D), [Min|Rest]).
Min = 8,
Rest = [9, 10].
add a comment |
up vote
0
down vote
up vote
0
down vote
I think there are problems with your code:
TDist=TD1+TD2
doesn't compute the sum, use is/2 instead, at least when a path is returned.It will loop if the graph contains cycles, but assuming the data actually is a DAG, we can ignore by now.
We can't say what the actual path will be, just its value.
Anyway, library(aggregate) can be used to find the shortest path. For instance
?- aggregate(min(D), path(a,g,D), D).
D = 8.
Or, since gnu-prolog doesn't have library(aggregate), take the first element computed by setof/3:
?- setof(D, path(a,g,D), [Min|Rest]).
Min = 8,
Rest = [9, 10].
I think there are problems with your code:
TDist=TD1+TD2
doesn't compute the sum, use is/2 instead, at least when a path is returned.It will loop if the graph contains cycles, but assuming the data actually is a DAG, we can ignore by now.
We can't say what the actual path will be, just its value.
Anyway, library(aggregate) can be used to find the shortest path. For instance
?- aggregate(min(D), path(a,g,D), D).
D = 8.
Or, since gnu-prolog doesn't have library(aggregate), take the first element computed by setof/3:
?- setof(D, path(a,g,D), [Min|Rest]).
Min = 8,
Rest = [9, 10].
answered Nov 10 at 19:35
CapelliC
50.8k43262
50.8k43262
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242473%2fhow-to-find-shortest-path-in-prolog-with-weighted-graph%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown