# Introduction to Programming Languages/Exhaustive Searches

Even though Prolog is a general purpose programming language, it really shines when we have to deal with problems involving exhaustive search. Many problems are like this, i.e., the only way to find an exact solution is through a brute force search. Two techniques are key to implement simple and elegant searches. The first is a way to produce subsets from a set, which we implement as follows:

```
subSet([], []).
subSet([H|T], [H|R]) :- subSet(T, R).
subSet([_|T], R) :- subSet(T, R).
```

The second technique is a way to produce permutations of a sequence. There are several ways to implement this predicate. Below we find a simple implementation:

```
perm([], []).
perm(List, [H|Perm]) :- select(H, List, Rest), perm(Rest, Perm).
```

So, which kind of problems are nicely solved via exhaustive search? NP-complete problems, for one, are a good start. Perhaps the simplest NP-complete problem is the Subset Sum Problem, which we state as follows: given a set S, and an integer N, is there a subset L within S whose sum adds up to N? A solution to this problem, in Prolog, is given below:

```
intSum(L, N, S) :- subSet(L, S), sumList(S, N).
```

We are using two predicates that we had seen earlier: `subSet` and `sumList`.
The beauty of this solution is its simplicity.
The Prolog solution, indeed, is a statement of the problem, almost in plain English.
We say that a programming language is high-level it it reduces the semantic gap between a problem and an implementation of the solution to that problem.
For this particular example, a description of the solution of the subset sum problem is also the solution itself.
This problem illustrate the elegant and expressiveness of logic programming languages, when it comes to solving problems that involve exhaustive searches.

Notice that even problems that admit efficient exact solution can be solved through exhaustive search.
Such brute force solutions are usually not efficient; however, they are elegant and illustrate well the basic structure of the problem that we want to solve.
As an example, consider this sorting algorithm below, implemented through exhaustive search, which uses the `perm` predicate seen above.
A sorted version of a list is a permutation of its elements, which meets the following property:
given any two contiguous elements in the sorted list, H1 and H2, we have that H1 is less than, or equal to H2:

```
isSorted([]).
isSorted([_]).
isSorted([H1,H2|T]) :- H1 =< H2, isSorted([H2|T]).
mysort(L, S) :- perm(L, S), isSorted(S).
```

Of course, we can write more useful predicates using exhaustive search.
For instance, what if we want to discover if a certain string represents an anagram of any English
word?
Assume that we have a dictionary `dic.txt`, with the known words, separated by a dot.
The implementation of this predicate, in Prolog, can be seen below:

```
file_to_list(FILE, LIST) :- see(FILE), inquire([], LIST), seen.
inquire(IN, OUT):-
read(Data),
(Data == end_of_file -> OUT = IN; atom_codes(Data, LData), inquire([LData|IN], OUT)).
find_anagram(Dic, A, S) :- file_to_list(Dic, L), perm(A, X), member(X, L), name(S, X).
```

Several of the terms in the program above are part of the swipl standard library, e.g., `see`, `seen`, `read`, `atom_codes` and `name`.
These predicates are used either to handle input and output, or to transform atoms into lists (`atom_codes`), and atoms into strings (`name`).
Notice how `perm` is used to find all the permutations of the input string.
Some of these permutations might have an equivalent in the list that was built out of the input file.
If that is the case, then we report that as a solution to our problem.
To execute this predicate, assuming that we have a file called 'dic.txt' in the local directory, we can do:

```
?- find_anagram('dic.txt', "topa", S).
S = pato ;
S = atop ;
```

And, if we want to join all the solutions into a single list, Prolog gives us the predicate `findall`, which works as follows:

```
?- findall(S, find_anagram('dic.txt', "topa", S), Solutions).
Solutions = [pato, atop].
```

This predicate receives three arguments.
The term `findall(S, Q, L)` will build a list `L`, using the patterns `S` that are produced upon solving the query `Q`.
Every pattern `S` that is a possible solution of `Q` shall be inserted in the output list.
As an example, let us consider the three queries below:

```
?- findall(S, intSum([2, 3, 5, 7, 9, 11], 25, S), Answers).
Answers = [[2, 3, 9, 11], [2, 5, 7, 11], [5, 9, 11]].
?- findall(S, perm([a, b, c], S), Answers).
Answers = [[a, b, c], [a, c, b], [b, a, c], [b, c, a], [c, a, b], [c, b, a]].
?- findall(S, subSet([0, 1, 2], S), Answers).
Answers = [[0, 1, 2], [0, 1], [0, 2], [0], [1, 2], [1], [2], []].
```

As a last example of exhaustive search, we use Prolog to find cliques in graphs. The problem of finding a clique of size N in a graph is NP-complete. Therefore, it is very unlikely that we can have an efficient solution to this problem. Yet, we can have a simple one, in Prolog. To represent a graph, we can use a set of predicates representing edges. For instance:

```
edge(a, b).
edge(a, c).
edge(b, c).
edge(c, d).
edge(c, e).
edge(b, d).
edge(b, e).
edge(e, f).
edge(b, f).
```

These predicates determine a directed graph.
In case we do not need directions, then we can have a predicate that checks if either `edge(X, Y)` or `edge(Y, X)` is in our database of predicates:

```
linked(X, Y) :- edge(X, Y).
linked(X, Y) :- edge(Y, X).
```

From these definitions, it is easy to produce a predicate that determines if a list of nodes form a clique:

```
clique([]).
clique([_]).
clique([X1,X2|R]) :- linked(X1, X2), clique([X1|R]), clique([X2|R]).
```

Finally, what we really want is to know if we have a clique of size N in our graph. Given the predicates that we already have, producing this query is almost like writing it in straightforward English:

```
cliqueN(V, C, N) :- sublist(V, C), clique(C), length(C, N).
```