Advertisement
mgradowski2

Untitled

Jan 23rd, 2021
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.31 KB | None | 0 0
  1. % Author: Mikołaj Gradowski
  2. % E-mail: s180190@student.pg.edu.pl
  3.  
  4. % Info: made with SWI-Prolog version 8.2.3 for x86_64-darwin
  5.  
  6.  
  7. % ___ ____ ____ _  _    _  
  8. %  |  |__| [__  |_/     |  
  9. %  |  |  | ___] | \_    | .
  10. %
  11.  
  12.  
  13. % Insert into an ordered list, preserving the order
  14. insert(X, [], [X]).
  15. insert(X, [H | T], [H | YS]) :-
  16.     X > H,
  17.     insert(X, T, YS).
  18. insert(X, XS, [X | XS]).
  19.  
  20. % Sort a list (insertion sort)
  21. sort_([], []).
  22. sort_([H | T], Y) :-
  23.     sort_(T, Y0),
  24.     insert(H, Y0, Y).
  25.  
  26.  
  27. % ___ ____ ____ _  _    _ _  
  28. %  |  |__| [__  |_/     | |  
  29. %  |  |  | ___] | \_    | | .
  30. %
  31.  
  32.  
  33. % Return smaller of the two
  34. min(X1, X2, X1) :- X1 =< X2.
  35. min(X1, X2, X2) :- X1  > X2.
  36.  
  37. % Return smallest element of a list
  38. minList([X], X).
  39. minList([X | XS], Y) :-
  40.     minList(XS, Y0),
  41.     min(X, Y0, Y).
  42.  
  43. % Skip first N elements of a list
  44. skip(0, X, X).
  45. skip(_, [], []).
  46. skip(N, [_ | XS], Y) :-
  47.     N0 is N-1,
  48.     skip(N0, XS, Y).
  49.  
  50. % Take first N elements of a list
  51. take(0, _, []).
  52. take(N, [X | XS], [Y | YS]) :-
  53.     Y = X,
  54.     N0 is N-1,
  55.     take(N0, XS, YS).
  56.  
  57. % Creates a list of integers 1, 2, ..., N
  58. % Note: It's O(n^2) because it was easier.
  59. range(0, []).
  60. range(N, Y) :-
  61.     N0 is N-1,
  62.     range(N0, Y0),
  63.    
  64.     insert(N, Y0, Y).
  65.  
  66. % Replicates El N times
  67. replicate(_, 0, []).
  68. replicate(El, N, [El | Y0]) :-
  69.     N0 is N-1,
  70.     replicate(El, N0, Y0).
  71.  
  72. % Sum the elements in a list
  73. sum([], 0).
  74. sum([X | XS], Y) :-
  75.     sum(XS, Y0),
  76.     Y is X + Y0.
  77.  
  78. % Checks inequality for a single r
  79. isGraphicAux_(Seq, R) :-
  80.     length(Seq, N),
  81.     take(R, Seq, LHSSeq),
  82.     sum(LHSSeq, LHS),
  83.     NR is N-R,
  84.     replicate(R, NR, RS),
  85.     skip(R, Seq, RHSSeq1),
  86.     maplist(min, RS, RHSSeq1, RHSSeq2),
  87.     sum(RHSSeq2, RHSSum),
  88.     RHS is R * (R - 1) + RHSSum,
  89.     !, LHS =< RHS.
  90.  
  91. % Checks if Seq is a graphic sequence
  92. isGraphic(Seq) :-
  93.     sum(Seq, Sum),
  94.     0 is mod(Sum, 2),
  95.     length(Seq, N),
  96.     N0 is N-1,
  97.     replicate(Seq, N0, Seqs),
  98.     range(N0, RS),
  99.     !, maplist(isGraphicAux_, Seqs, RS).
  100.  
  101. % ___ ____ ____ _  _    _ _ _  
  102. %  |  |__| [__  |_/     | | |  
  103. %  |  |  | ___] | \_    | | | .
  104. %
  105.  
  106. isPotentiallyConnected(Seq) :-
  107.     length(Seq, N),
  108.     minList(Seq, MinDegree),
  109.     MinDegree > 0,
  110.     isGraphic(Seq),
  111.     RHS is 2*(N-1),
  112.     sum(Seq, SeqSum),
  113.     !, SeqSum >= RHS.
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement