Advertisement
Guest User

Untitled

a guest
Oct 14th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.08 KB | None | 0 0
  1. %4 uzduotis
  2.  
  3. %Apibrėžkite predikatą kurio vienas ar daugiau argumentų - įėjimo argumentai, nusakantys pradinius užduoties duomenis ir vienas argumentas atitinka sprendinį.
  4. %Sprendinys - Prologo struktūra (sąrašas ar pan.) - turi būti apibrėžtas konstruktyviai. Didžiausias dėmesys turi būti skiriamas ne sprendimo efektyvumui,
  5. %o deskriptyviajam (Prologo prasme) apibrėžimui.
  6. %Jokiu būdu nereikia ieškoti deterministinių uždavinio sprendimo algoritmų, nors tokie ir būtų.
  7. %Taip pat apibrėžkite predikatą, kuris išveda sprendinį į ekraną skaitomu pavidalu (pvz., pseudo-grafiku)
  8.  
  9. %Grafo brėžimas.
  10. %8. Duotas plokštuminis grafas. Raskite kelią, einantį per visas grafo briaunas, kai:
  11. %1. per tą pačią briauną antrą kartą eiti draudžiama;
  12.  
  13. /*edge(a,c).
  14. edge(a,d).
  15. edge(b,d).
  16. edge(b,e).
  17. edge(c,e).
  18.  
  19. edge(a,b).
  20. edge(a,c).
  21. edge(a,d).
  22. edge(b,c).
  23. edge(b,d).*/
  24.  
  25. edge(a,b).
  26. edge(b,c).
  27. edge(c,d).
  28. edge(d,a).
  29. edge(a,c).
  30. edge(b,d).
  31.  
  32.  
  33. /*edge(a,c).
  34. edge(a,d).
  35. edge(b,c).
  36. edge(d,c).*/
  37.  
  38. addedge(A,B):-
  39.     assert(edge(A,B)).
  40.  
  41. connected(A,B) :-
  42.     edge(A,B);
  43.     edge(B,A).
  44.  
  45. used([A, B], Path) :-                  
  46.         member([A, B], Path);
  47.         member([B, A], Path).
  48.  
  49. allused([], _) :- true.
  50.  
  51. allused([Edge | Edges], Path) :-        % have all the edges been visited?
  52.     used(Edge, Path),                  
  53.     allused(Edges, Path).
  54.  
  55. follows([[_, A]|_], [A, B]) :-      % follows the rule of [ A-B, B-C, C-... ]
  56.     connected(A, B).
  57.  
  58. eulerpath(X) :-
  59.     eulerpath(_,X).
  60.    
  61. eulerpath([], Result) :-               
  62.     connected(A, B),                   
  63.     eulerpath([[A, B]], Result).        % filling our result as a list of lists ( 2 connected nodes )  
  64.    
  65. eulerpath(Path, Result) :-             
  66.     follows(Path, Edge),               
  67.     not(used(Edge, Path)),             
  68.     eulerpath([Edge | Path], Result).    
  69.  
  70. eulerpath(Path, Result) :-
  71.     is_list(Path),                      % the path cannot be empty, it has to have a list of edges
  72.     findall([A, B], edge(A, B), Edges), % finds all connected edges from our database
  73.     allused(Edges, Path),               % check whether we've already went through all of our edges
  74.     reverse(Path, Result),
  75.     !.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement