Reprint Please specify source: http://blog.csdn.net/zhoubin1992/article/details/46507919
1.SAT Problem Description
The satisfying problem of the combined paradigm (CNF) in propositional logic (SAT) is the core problem of contemporary theoretical computer science, and it is a typical NP-complete problem. Before you define a satisfying problem SAT, first introduce some logic symbols.
A SAT question is: Is there a set of truth assignments for a given CNF that is true for a propositional variable. Obviously, if a is true, there must be a proposition variable of 1 (TRUE) in each clause of CNF.
2.Las Vegas Algorithm
The Las Vegas algorithm is a probabilistic algorithm that uses random values to make random selections, and does not produce incorrect answers. Random choices made during the calculation may make the algorithm get the required solution faster than other algorithms.
The Las Vegas algorithm does not 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 with the Las Vegas algorithm. Similar to the Monte Carlo algorithm, the probability that the Las Vegas algorithm finds the correct solution increases with the computational time it uses. For any instance of the solved problem, the same Las Vegas algorithm is used to solve the case repeatedly, so that the probability of solving the failure is small.
The Las Vegas algorithm is used to search for the solution space that contains the target node. It moves with some random selection, without having to compute 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 high. The Las Vegas algorithm increases the efficiency of the calculation when the next node is difficult to calculate or if there is no need for a systematic search. Of course, the random selection of the next node may lead to the finding of a successful node, but we can repeat the operation multiple times to improve the efficiency of the target node. One notable feature of the Las Vegas algorithm is that its random decision may lead to the algorithm not finding the desired solution, but it can be overcome by repeated operation, and it will often get miraculous when solving NP-hard problems.
3. Las Vegas + backtracking
Just as we can combine quick sort and insert sort, you can also combine backtracking with random algorithms. When the number of uncertain arguments is high, then the probability of the random determination of the truth is more feasible, with the uncertainty of the variable less, the probability of the smaller, then you can use the deterministic algorithm (backtracking). This hybrid algorithm is described below:
The true value of the first K-element is determined randomly, and then the true value of the n-k is determined by backtracking method. the algorithm is described as follows:
BackTrack (int t) int i;if(T > M)return true;Else fori =0 to 1Do x[t] = i;if(Place (t))if(BackTrack (t+1))return true;if(i = =2)return false; Place (k) fori =0 ton Do t =0 forj =0 to 3Doif(m[i][j]>k) T = t+1;Elset = 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) dosCount=0; fori =0 to 1Do x[k] = i;if(Place (k)) ok[Count] = i;Count++;if(Count==0)return false; i = Ok[rand ()%Count]; X[k] = i; k++;returnBackTrack (STOPST);
Stopst plays a key role, when the number of randomly determined value of the variable is more, the probability of success is smaller, it needs to be executed multiple times. when the value of a randomly determined variable is less, most of the time is spent on the backtracking algorithm, STOPST should choose what kind of values, need to undergo many tests.
Code:
//LasvegasBT3SAT.cpp: Defines the entry point of the console application. ///*********************************-----------------------------------lasvegas+ backtracking algorithm to solve 3SAT problems (C + + Implementation code)-----------------------------------Author: Pastoral, date:2014 email:[email protected] **************************** ******/ #include "stdafx.h"#include <stdlib.h>#include <string.h>#include <time.h>#include <iostream>Const intn= -;Const intm= -;intStopst;intm[n][3];intsign[3*n+1];intx[101],y[101];intok[2];BOOLPlace (intK) {//memset (y,1,101); intTemp/*for (int j = 1; J <= K-1; j + +) {Y[j]=~x[j]; }*/ for(inti =0; I < n; i++) {temp=0; for(intj =0; J <3; J + +) {if(m[i][j]>k) temp = temp+1;///non-generated variables appear in the child as true Elsetemp = temp+! (sign[i*3+J]^X[M[I][J]]);//Variables in the sub-symbol and the variable itself value xor or inverse}if(temp<1)return false; }return true; }BOOLBackTrack (intT) {intIif(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; }}BOOLSat_true (intX[]) {intK =1;intCount =0;intI while(K < Stopst) {count =0; for(i =0; I <=1; i++) {X[k] = i;if(Place (k)) {Ok[count] = i; Count + +; } }if(Count = =0)return false; i = Ok[rand ()% count]; X[k] = i; k++; }returnBackTrack (STOPST); }int_tmain (intARGC, _tchar* argv[]) {Srand (0)); for(intI=0; i<n;i++) for(intj=0;j<3; j + +) M[i][j] = rand ()%m+1; for(intI=1; i<=3*n;i++) Sign[i] = rand ()%2;memset(X,0, (m+1)*sizeof(int));intKDoubleRun_time =0.0;//Execution Time for(inti = -; I <= -; 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. Experimental results
The number of executions and execution times of the tested stopst=60 are ideal.
Here I give the results of m= 50,n=200:
lasvegas+ Backtracking algorithm solves 3SAT problem (c + + implementation code)