Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.85 KB | None | 0 0
  1.  
  2. :- dynamic(node/1).
  3. :- dynamic(edge/2).
  4. :- initialization(goal).
  5.  
  6. process(node(X)) :- assertz(node(X)). %îê
  7. process(edge(X, Y)) :- assertz(edge(X, Y)). %îê
  8. process(_) :- write('Syntax Error!'), fail, !. %îê
  9.  
  10. goal :- open('graph2.txt', read, F), load_graph(F), close(F), !, main.
  11. load_graph(F) :- at_end_of_stream(F) -> true; read(F, X), process(X), load_graph(F).
  12.  
  13. writeBagOf :- bagof(Path, get_ham_path(Path), Ls), write(Ls).
  14. lenBagOf :- bagof(Path, get_ham_path(Path), Ls), length(Ls, T), write(T).
  15.  
  16. main :- get_ham_path(Path), write(Path).
  17. get_ham_path(Path) :- path(A,_,Path), node(A), is_hamilt(Path).
  18.  
  19. subpath(A, [A | P], [A | P]).
  20. subpath(A, [Y | P1], P) :- adjacent(X, Y), \+ member(X, P1), subpath(A, [X, Y | P1], P).
  21. path(A, Z, Path) :- subpath(A, [Z], Path).
  22.  
  23. adjacent(X, Y) :- (edge(X, Y);edge(Y, X)).
  24. is_hamilt(P) :- \+ (node(A), \+ member(A,P)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement