Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding routes in a graph

Status
Not open for further replies.

techproof

Technical User
Nov 6, 2007
1
0
0
GB
Hello everyone,

I am very new to prolog and i am realy stuck with something, could anyone please take a look and help me out or give me any pointers as to how i have to proceed?

Basically, i have to find all possible routes starting from any given node in a randomly connected undirected graph of 18 nodes, back to the same node. Travelling periods are involved between nodes of the graph i.e the graph edges are weighted so one can only cover certain distances each day. The conditions are that
-the route must pass through nodes 1, 12, 8, 17 (or any 4 given nodes)
-while traversing, one has to cover a minimum of 180 metres(distance in terms of total= edge 1 +...) in a day but not more than 200 metres in a day
-one node can be visited more than once
-as many nodes as possible are to be visited
-at the end of the day, i.e >180 <200, the current node should be returned so we can calculate whcih node was last visited at the end of each day
-nodes can be traversed either way

I know that the following code could be used to start and end at different nodes, but i dont know how to change it so it would include the mandatory nodes to be visited and also so that the start and the end node could be the same

path(First, Last, Path, Length):-
path_1(First, Last, [], 0, Path, Length).

path_1(Last, Last, Path, Length, [Last|Path], Length).
path_1(First, Last, Path0, Length0, Path, Length):-
e(NextToLast, Last, Length1),
\+member(NextToLast, Path0),
Length2 is Length0 + Length1,
path_1(First, NextToLast, [Last|Path0], Length2, Path, Length).

/* Sample data */
/* e.g. path(a0, a1, Path, Length). */
e(a0,a1,5).
e(a0,a2,3).
e(a0,a4,2).
e(a1,a2,2).

If anyone can tell me how or where i could make changes, it would be very helpful

Thanks a lot

Techy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top