:- seqex([cost/3, member/2, remove/3]).

cost(a,b,1). cost(b,a,1).  cost(f,g,1). cost(g,f,1).
cost(a,d,2). cost(d,a,2).  cost(b,h,2). cost(h,b,2).
cost(a,f,7). cost(f,a,7).  cost(h,i,7). cost(i,h,7).
cost(b,c,2). cost(c,b,2).  cost(i,j,2). cost(j,i,2).
cost(b,d,3). cost(d,b,3).  cost(k,l,3). cost(l,k,3).
cost(c,d,2). cost(d,c,2).  cost(i,l,2). cost(l,i,2).
cost(c,e,3). cost(e,c,3).  cost(b,j,3). cost(j,b,3).
cost(c,f,2). cost(f,c,2).  cost(g,h,2). cost(h,g,2). 
cost(d,e,5). cost(e,d,5).  cost(g,k,2). cost(k,g,2).  
cost(e,f,4). cost(f,e,4).  cost(e,k,2). cost(k,e,2).

member(X,[X|Y]).
member(X,[Y|Z]) :- X \= Y -> member(X,Z).

remove(X,[X|Y], Y).
remove(X,[Y|Z], [Y|W]) :- X \= Y -> remove(X,Z,W).

paths(X,Cost,Route) :- remove(X,[a,b,c,d,e,f,h,i,k,l], Restlist) -> paths2(X, 0, 9, 0, Cost, Route, Restlist,X).

paths2(X, Maxlen, Maxlen, CurCost, Cost, [First], [], First) :- 
	   cost(X,First,C1) -> 
	   Cost is CurCost + C1.

paths2(X, Clen, Maxlen, CurCost, Cost, Route, Unused, First) :- 
           Clen < Maxlen ->
           cost(X, Y, C) ->
	   member(Y,Unused) ->  
           remove(Y,Unused,NewUnused) ->
           NCost is CurCost+C -> Clen1 is Clen+1 -> Route = [Y|RestRoute] ->
           paths2(Y,Clen1,Maxlen,NCost,Cost,RestRoute,NewUnused,First).

                                            
?- paths(a,Cost,Route).

