Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Constraint processing


1. Constraint processing

2. Constraint logic

3. Constraint logic
A constraint logic system is a system of constraints whose solution needs to satisfy those constraints.

A unifier unifies the constraints without contradictions.

A MGU (Most General Unifier) unifies the constraints without contradictions and has a minimal frontier in the lattice of constraints, a LFP (Least Fixed Point) .

If there are multiple solutions, a constraint logic system will attempt to find all MGU constraints.

4. Constraint processing
Constraint processing is a field involved in determining solutions from a large number of possibilities. These possibilities involve combinations, permutations, etc. The general name for this field is combinatorial optimization.

5. Permutations

6. Permutations
A permutation is an arrangement of distinct objects where the order of the objects is important.


How many ways are there to arrange the letters a, b, and c?

7. Reasoning
Here is the reasoning.

8. Enumeration
Here are the permutations.
a b c a c b b a c b c a c a b c b a


9. Letter arrangements
For 3 letters, there are
3*2*1

ways to arrange them.

10. Generalization
In general, if there are n objects, then there are Using the product rule, we multiply all of the ways together to get total number of permutations.

11. Factorial function
The factorial function (!), sometimes called the permutation function since it provides the number of permutations of n objects, is defined as follows.
0! = 1 (by definition) n! = n * (n-1)!    = n * (n-1) * (n-2)!    = n * (n-1) * (n-2) * (n-3)!    = n * (n-1) * (n-2) * (n-3) * ... * 1


12. Identity permutation
What is 0!?

13. Convention
By convention, 0! is 1, since there is only one way to arrange zero objects, by doing nothing.

Technically, taking 0! as 1 (i.e., the multiplicative identity element) makes the math work out easier.

14. Tires
How many ways are there to arrange 4 tires on an automobile?

Assume that there is only one way to put a tire on an automobile (e.g., the whitewall facing out).

15. Tires
There are 4! = 4*3*2*1 = 24 ways to arrange 4 tires on an automobile.

16. Factorial function
The factorial function can be used to calculate the number of permutations of n objects. The mathematical definition is as follows, using "*" for multiplication.
fact(0) = 1 fact(n) = n * fact(n-1) , n > 0

Do you see a way to program the factorial function (top-down) using this definition.

Note that this definition is recursive in that it refers to itself. The factorial function grows quickly.
n! = n * (n-1) * (n-2) * ... * 1 0! = 1 1! = 1 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 5! = 5 * 4 * 3 * 2 * 1 = 120 6! = 6 * 5 * 4 * 3 * 2 * 1 = 720 7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5,040 ... 10! is more than 3,600,000

Do you see a way to program the factorial function bottom-up using the above pattern?

7 factorial
The factorial function grows rapidly. Here is a graphical depiction of 7! = 5,040.
59! is about 1080

1080is the number of baryons (electrons, protons, neutrons, etc.) in the known universe.

17. Main program
The main program code is the same for both versions of the code.


18. Iterative function
Here is an iterative factorial function (bottom up version).

It is called "iterative" because it has a loop. Pure recursive functions use recursion instead of loops, using recursion to do iteration.

19. Program
Here is the C code.

Here is the output of the C code.


20. Recursive function
Here is the recursive factorial function.

Here is the math definition, using "*" for multiplication.
fact(0) = 1 fact(n) = n * fact(n-1) , n ≠ 0


21. Recursive function
Here is the same recursive factorial function written slightly differently.

The return is done at the end of the function. Here is the same program using a recursive function (the first one).

Here is the C code.

Here is the output of the C code.


22. Tail recursion
Technical note: Tail recursion in the recursive version makes it easy to convert to iteration.

This improvement can be used in functional language, logic languages, and imperative languages.

This saves time and space since it avoids extra push and pops of the stack.

23. Redundant calculations
Problem: Redundant calculations are done.

Solution: Use memoization (behind the scenes) by trading more memory space for less computation time. Memoization is from the Latin word "memorandum" which comes from the Greek «μνεμονικόυς» from the PIE (Proto-Indo European) for "men" or "think" (comment, amnesia, mental, memento, etc.).

Memoization is often done for compiled regular expressions, SQL queries, etc. Space can be traded for time by using the hash of the text and not the text itself.

The browser cache is a form of memoization.

24. Brute force enumeration

25. Brute force
The process of enumeration is sometimes called "brute force".

The process of listing every possible alternative, object, thing, etc., is called enumeration.

26. Combination lock
Brute force enumeration requires looking at all possibilities.

In the case of a combination lock, this is all the possible combinations (permutations).

27. Combination lock
Combination LockHow many combinations are there on a typical combination lock?

28. Combination lock
Combination Lock

29. Combination lock
Combination LockThus, the number of combinations for a typical combination lock is 40*40*40 = 64,000.

How long would a brute force attack take? What happens if the last number is known? Note: Order matters, so a combination lock is really a permutation lock. But the generation of the "combinations" does not follow exactly the permutation algorithm (since there is replacement).

30. Finite state modeling

31. Sorting: Slowsort

32. Sorting
To sort something is to arrange that something into order.

To sort a list is to arrange that list so it is in order.

33. Slowsort
Slowsort is a simple but slow exchange sorting algorithm based on the definition of sorting. That is, take all permutations of the items in the set or multiset to be sorted. At least one such permutation will be in sorted order. Pick one of those permutations to complete the sort.

Obviously, slowsort is not a very machine efficient sorting algorithm.

34. End of page

35. Multiple choice questions for this page

36. Acronyms and/or initialisms for this page