Prolog Programming
1. Lists
1.1. Write a predicate endsWith(X,L)
, which checks if the list L
ends with element X
.
1.2. Write a predicate sameFirstLast(L)
which checks if the list L
begins and ends with the same element.
1.3. Write a predicate inner(L1,L2)
which checks if the elements from L1
appear in L2
in exactly the same order. Example ?- inner([3,4],[1,2,3,4]).
1.4. Write a predicate find(L,K,L1)
which binds K
to the number of occurrences of L
in L1
. Example, for L=[1,2,1]
and L1=[1,2,1,2,1,2,1]
the result will be K=3
.
2. Graphs
We encode undirected graphs as lists [V,E]
, where V
is a list of nodes and E
is a list of undirected edges of the form [X,Y]
.
2.1. Write a predicate isolated(X,G)
which is satisfied if X
is a node with no edges.
2.2. Write a predicate neighbors(X,L,G)
which computes the list of direct neighbours L
of a given node X
in G
.
2.3. Write a predicate accessible(X,L,G)
which computes all nodes L
accessible from X
in graph G
.
2.4. Write a predicate which checks if a graph is complete.
2.5. Write a predicate which computes the complement of a graph.
3. NP problems
3.1. Write a solution in PROLOG for the Partition Problem (There exists a partition $ P$ of naturals $ x_1, \ldots, x_n$ , such that $ \sum_{x_i\in P}x_i = \sum_{x_i \not\in P}x_i$ ).
3.2. Write a solution in PROLOG for the k-Color problem.