Guest User

Untitled

a guest
Nov 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. So, essentially, for a NP-complete program, you want to iterate over the entire solution space.
  2. To do this, you want to define a bijection between a range of the natural numbers and the solution space.
  3. The most elegant way to do this is to define functions on the solutions that mirror the natural numbers (i.e., a homomorphism).
  4. The natural numbers can be defined as `type nat = Z | S of nat`, which gives you an initial (zero) element and a successor function.
  5. To define these for the solutions, I would have functions `zero_soln` and `succ_soln` which mirror `Z` and `S`:
  6.  
  7. ```ocaml
  8. Z : nat
  9. S : nat -> nat
  10. zero_soln : soln
  11. succ_soln : soln -> soln
  12. ```
  13.  
  14. However, this definition describes an infinite set of solutions homomorphic to the infinite set of natural numbers.
  15. This is incorrect, since the solution space is finite in size.
  16.  
  17. Instead, we'd want to create a structure which has both initial and final elements.
  18. On the natural numbers, this could be represented by keeping the initial element, and adding a final element.
  19. OCaml's type system can't represent this in a strong way, but it can be implemented by changing the type of `S` to `nat -> nat option`, and stating that for any value equal to or above the maximum, `S` returns `None`.
  20.  
  21. By the homomorphism, the types of the functions on `soln` then are:
  22.  
  23. ```ocaml
  24. zero_soln : soln
  25. succ_soln : soln -> soln option
  26. ```
  27.  
  28. The actual solutions are of type `(int * string) list`, while `soln` also needs to include the set of colors and vertices in order to define `succ_soln`, so I would define `soln` as:
  29.  
  30. ```ocaml
  31. type vertex = int;;
  32. type color = string;;
  33.  
  34. type vertices = vertex list;;
  35. type colors = color list;;
  36.  
  37. type color_mapping = (vertex * color) list;;
  38.  
  39. type soln = vertices * colors * graph * color_mapping;;
  40. ```
  41.  
  42. Since each `color_mapping` must contain every vertex, one can then treat `color_mapping` as an *m*-digit number in base *n*, where *m* is the number of vertices and *n* is the number of colors.
  43. The `succ_soln` function can then be treated as a simple successor.
  44. Iterating over the solution space as one iterates over the natural numbers (i.e. with relatively standard recursion or iteration) is then trivial.
Add Comment
Please, Sign In to add comment