LasVegas algorithm solves 3SAT problem (c + + implementation code)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhoubin1992/article/details/46469557

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.c++ Implementation Code
//Lasvegas3SAT.cpp: Defines the entry point of the console application. //#include "stdafx.h"#include <stdlib.h>#include <string.h>#include <time.h>#include <iostream>Const intn= -;intm[n][3];intsign[3*n+1];intx[101],y[101];intok[2];BOOLPlace (intK) {//memset (y,1,101);    intT/*for (int j = 1; J <= K-1; j + +) {Y[j]=~x[j]; }*/     for(inti =0; I < n; i++) {t=0; for(intj =0; J <3; J + +) {if(m[i][j]>k) T = t+1;Elset = t+! (sign[i*3+J]^X[M[I][J]]); }if(t<1)return false; }return true; }BOOLSat_true (intX[]) {intK =1;intCount =0;intI while(k <= -) {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++; }return true; }int_tmain (intARGC, _tchar* argv[]) {//Repeat 20 times    //int ncase =;    DoubleRun_time =0.0;//Execution Time    DoubleRun_num =0.0;//number of executionstime_t start,end; Srand (Time (0));//while (ncase--)    //{start = Clock (); for(intI=0; i<n;i++) for(intj=0;j<3; j + +) M[i][j] = rand ()% -+1; for(intI=1; i<=3*n;i++) Sign[i] = rand ()%2;memset(X,0,101*sizeof(int));intk=1; while(! Sat_true (x)) {k++;if(k >100000)            {printf("failed!\n"); Break;        }} end = Clock ();        Run_num = k; Run_time + = (end-start)/clocks_per_sec;if(k <=100000)STD::cout<<"executed."<< Run_num <<"Times"<<STD:: Endl;//}    printf("The Running time is:%f\n", run_time); System"Pause");return 0;}
4. Experimental results and Analysis

To test the results of Las Vegas calculations, we use the randomly generated 3-SAT model (the length of each clause l= 3, and the variable 22 in the clause) as an example. Each value is executed 20 times, considering that there may not be a solution, and when the number of searches is more than 100,000 times, the sample is considered unsatisfied. The results are as follows:

Reference Documents
[1] Zhang. Algorithm design and Analysis (advanced tutorial) [M]. National Defense Industry Press, 2007.

LasVegas algorithm solves 3SAT problem (c + + implementation code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.