×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help me program in prolog!

Help me program in prolog!

Help me program in prolog!

(OP)
I am not an expert in logic programming and Prolog and I'm struggling with this project to make the university "taking pleasure in a domain, write two programs that make, one depth-first search and another in breadth" ... I have chosen as domain a journey through the various Italian cities, place the following code:

CODE

[dazed]
arc_weighed(roma,firenze,3).
arc_weighed(firenze,genova,3).
arc_weighed(genova,torino,2).
arc_weighed(torino,milano,2).
arc_weighed(torino,aosta,1).
arc_weighed(firenze,bologna,2).
arc_weighed(bologna,venezia,2).
arc_weighed(venezia,trento,2).
arc_weighed(venezia,trieste,2).
arc_weighed(napoli,potenza,2).
arc_weighed(palermo,cagliari,9).
arc_weighed(potenza,bari,2).
arc_weighed(potenza,catanzaro,4).
arc_weighed(catanzaro,palermo,5).
arc_weighed(roma,laquila,1).
arc_weighed(laquila,campobasso,3).
arc_weighed(laquila,ancona,2).
arc_weighed(campobasso,napoli,2).

calculated_route:- write('Hi, welcome we give in to our travel agency virtuale.Le propose, starting from Rome an interesting tourist route.'), write('Enter capital of arrival:'),nl, read(X), search_loc('roma',X).

search_loc(X,X):-  write('Already here, good sightseeing!'),nl.

search_loc(X,Y):-search(X,Y,L,[X],Costo), reverse_append(L,_),write('. To take this route:'), write(Costo), write(' hours.') .

search(X,X,L,L,0).
search(X,Y,L,P,O):- arc_weighed(X,W,C),ricerca(W,Y,L,[W|P],K),weighted_path(C,K,O).

reverse_append([],[]).
reverse_append([A|C],Q):-reverse_append(C,K),
append(K,[A],Q),write(A), write(' ').

weighted_path(A,C,Costo):- Costo is C+A.

not(X):- X,!,fail.
not(_).
 :uhh: 

with this code I do I make a journey .. how can I make the two different research?
Can you help me in writing algorithms? thank you! :)

RE: Help me program in prolog!

You should seach the route without writing anything.
You should write the towns after the search is finished.
For DFS you can start with that :

CODE

calculated_route:-
	write('Hi, welcome we give in to our travel agency virtuale.Le propose, starting from Rome an interesting tourist route.'),
	write('Enter capital of arrival:'),nl,
	read(X),
	search_loc(X, [[roma], 0], P),
	maplist(writeln,P).

search_loc(X,[[X | T], Cost], [Path, Cost]) :-
	reverse([X | T], Path).

search_loc(X,[[Y | T], C1] , FP):-
	arc_weighed(Y ,Z,Costo),
	C2 is C1 + Costo,
	search_loc(X,[[Z, Y | T], C2],FP). 
You get :

CODE

?- calculated_route.
Hi, welcome we give in to our travel agency virtuale.Le propose, starting from Rome an interesting tourist route.Enter capital of arrival:
|: napoli.
[roma,laquila,campobasso,napoli]
6
true 
Be aware that the code IS NOT correct, you must improve it !

RE: Help me program in prolog!

(OP)
And for BFS?! Thank you for your help! :)

RE: Help me program in prolog!

(OP)
Thaks! :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close