Lasvegas + backtracking algorithm solves the 3SAT problem (C ++ implementation code), lasvegas3sat
Reprinted please indicate the source: http://blog.csdn.net/zhoubin1992/article/details/46507919
1. SAT Problem Description
In Proposition Logic, the theory of conformity (CNF) is the core issue of modern theoretical computer science. It is a typical NP-complete issue. before defining the satisfied question SAT, introduce some logical symbols.
A sat question is: whether the given CNF has A set of True Value assignment about the proposition variable to make A true. obviously, if A is true, each clause of CNF must have A proposition variable of 1 (true ).
2. Las Vegas Algorithm
The Las Vegas algorithm is a probability algorithm that uses random values for random selection without generating incorrect answers. The random selection made in the calculation process may make the algorithm obtain the required solution faster than other algorithms.
The Las Vegas algorithm won't get an incorrect solution. Once a solution is found using the Las Vegas algorithm, the solution must be correct. But sometimes the solution is not found using the Las Vegas algorithm. Like the Monte Carlo algorithm, the probability that the Las Vegas algorithm finds the correct solution increases with the increase in the computing time it takes. For any instance that solves the problem, the same Las Vegas algorithm can be used repeatedly to solve the instance multiple times, so that the probability of failure can be minimized.
The Las Vegas algorithm is used to search for the solution space containing the target node. It uses some random options to move, instead of having to calculate a new node at each node. If the ratio of successful nodes is quite high in the solution space, the probability of finding the target node may be very high. When the calculation of the next node is difficult or systematic, the Las Vegas algorithm is used to improve the computing efficiency. Of course, the random selection of the next node may lead to failure to find a successful node, but we can run it multiple times to improve the efficiency of the target node. A notable feature of the Las Vegas algorithm is that its random decision-making may lead to the algorithm failing to find the desired solution, but it is overcome by repeated operations. When solving the NP-hard problem, it is often used to receive miraculous results.
3. Las Vegas + backtracking
Just as we can combine fast sorting with insert sorting, we can also combine backtracking with random algorithms. When the number of uncertain variable elements is large, the probability of a random value being determined is higher. as uncertain variable elements become less, the probability is lower, in this case, you can use a deterministic algorithm (backtracking ). This hybrid algorithm is introduced below:
First, we randomly determine the true values of the first k variables, and then use the Backtracking Method to Determine the true values of the next n-k variables.The algorithm is described as follows:
BackTrack(int t) int i; if (t > m) return true; else for i = 0 to 1 do x[t] = i; if (Place(t)) if (BackTrack(t+1)) return true; if(i == 2) return false;Place(k) for i = 0 to n do t = 0 for j = 0 to 3 do if(M[i][j]>k) t = t+1; else t = t+!(sign[i*3+j]^x[M[i][j]] ); if(t<1) return false; return true; SAT_True(x[],success) k = 0; count = 0; while( k < stopST ) dos count = 0; for i = 0 to 1 do x[k] = i; if( Place(k)) ok[count] = i; count ++; if( count == 0 ) return false; i = ok[rand() % count]; x[k] = i; k++; return BackTrack(stopST);
StopST plays a key role. When a random variable has more true values, the smaller the probability of success is, the more executions are required.When the number of variable truth values randomly determined is smaller, most of the time is spent on the backtracking algorithm. The stopST must be tested several times.
Code:
// LasvegasBT3SAT. cpp: defines the entry point of the console application. /// ******************************** ------------------------------- Lasvegas + backtracking algorithm solves 3SAT problems (C ++ implementation code) ----------------------------------- Author: Muzhi, Date: 2014 Email: bzhou84@163.com *********************************/# include "stdafx. h "# include <stdlib. h> # include <string. h> # include <time. h ># include <iostream> const int n = 250; const int m = 100; int stopST; int M [n] [3]; int sign [3 * n + 1]; int x [101], y [101]; Int OK [2]; bool Place (int k) {// memset (y, 1,101); int temp;/* for (int j = 1; j <= k-1; j ++) {y [j] = ~ X [j];} */for (int I = 0; I <n; I ++) {temp = 0; for (int j = 0; j <3; j ++) {if (M [I] [j]> k) temp = temp + 1; // The ungenerated variables appear in the subexpression as real else temp = temp +! (Sign [I * 3 + j] ^ x [M [I] [j]); // The symbol of the variable in the sub-expression is different from the value of the variable itself or the inverse value} if (temp <1) return false;} return true;} bool BackTrack (int t) {int I; if (t> m) return true; else {for (I = 0; I <2; I ++) {x [t] = I; if (Place (t) {if (BackTrack (t + 1) return true ;}} if (I = 2) return false ;}} bool SAT_True (int x []) {int k = 1; int count = 0; int I; while (k <stopST) {count = 0; for (I = 0; I <= 1; I ++) {x [k] = I; I F (Place (k) {OK [count] = I; count ++ ;}} if (count = 0) return false; I = OK [rand () % count]; x [k] = I; k ++;} return BackTrack (stopST);} int _ tmain (int argc, _ TCHAR * argv []) {srand (time (0); for (int I = 0; I <n; I ++) for (int j = 0; j <3; j ++) M [I] [j] = rand () % m + 1; for (int I = 1; I <= 3 * n; I ++) sign [I] = rand () % 2; memset (x, 0, (m + 1) * sizeof (int); int k; double run_time = 0.0; // execution time for (int I = 60; I <= 60; I ++) {Time_t start = clock (); k = 1; stopST = I; while (! SAT_True (x) {k ++; if (k> 100000) {printf ("failed! \ N "); break ;}} if (k <= 10000) std: cout <" executed "<k <" times "<std :: endl; time_t end = clock (); run_time + = (end-start)/CLOCKS_PER_SEC;} printf ("the running time is: % f \ n", run_time ); system ("pause"); return 0 ;}
4. Experiment results
The number of executions and execution time of stopST = 60 are satisfactory.
Here is the result of m = 50, n = 200: