Monte Carlo algorithm'll return an answer that's not necessarily
Correct within a reasonable amount of time.
Las Vegas algorithm may take infinite time to compute a answer, but
The answer is guaranteed to be correct.
Randomization algorithm (4)-Las Vegas (Las Vegas) algorithm
already out of serial :
1. randomization algorithm (1)-Random number
2. randomization algorithm (2)-Numerical probability algorithm
3. The randomization Algorithm (3)-Sherwood (Sherwood) algorithm
Body:
Savvy enough, this chapter looks at the code for a quick morning before understanding.
The previous chapter has talked about the probability algorithm (3)-Sherwood (Sherwood) algorithm, but his bit is that computational time complexity is relatively uniform for all instances, and the average time complexity does not change. The effectiveness of the algorithm is improved significantly by the Las Vegas algorithm.
A notable feature of the Las Vegas algorithm is that it makes random decisions that can cause the algorithm to find the desired solution. therefore, a bool-type function is usually used to represent the Las Vegas algorithm.
void obstinate (InputType x, OutputType &y)
{
Repeatedly call the Las Vegas algorithm LV (x, y) until a solution to the problem is found
BOOL success= false;
while (!success)
Success = LV (x, y);
}
Consider solving the n queen problem with the Las Vegas algorithm:
For any solution to the problem of N, the position of each queen on the chessboard is free of any regularity, not systematic, and more like random placement. This makes it easy to think of the following Las Vegas algorithm.
Place the Queen randomly on the successive lines of the board, and notice that the newly placed Queen does not attack each other with the placed Queen until the N queen has been placed in a compatible place, or there is no place for the next queen to be placed. Note that the solution here is to find one of the methods, not finding out all the solutions of the N queen.
Here's a little early explanation, otherwise it's hard to understand.
The next one uses the Las Vegas algorithm to solve the N queen problem, we use the random placement strategy and backtracking method combined, specifically, such as the eight Queen, the first few lines to choose the random method to place the Queen, the rest of the choice of backtracking method to solve.
This procedure is not very good understanding, some places I specifically explained that is the key to understand the procedure , we must be serious when looking at, in addition, Wang Xiaodong's book first with a simple random method to solve, you can first understand the book this example. And then I'll analyze this program. But the book on this piece of error more, we look at the place to pay attention to what he wrote wrong.
Las Vegas algorithm solves n Queens code:
Still use the RandomNumber.h head file, we can go to see the first chapter, I will not post it out.
The remainder of the code:
Note that the QUEENSLV () function is the essence of this program.
Queen.h header File
#ifndef Queen_h
#define Queen_h
Class Queen
{
friend bool Nqueen (int);
Private
BOOL Place (int k); Test the legality of Queen K in column X[k]
BOOL Backtrack (int t); Backtracking method for solving the problem after n
BOOL QUEENSLV (int stopvegas); Random placement of N Queen Las Vegas algorithm
int n, *x, *y;
};
#endif
Queen.cpp file
#include <iostream>
#include "Queen.h"
#include "RandomNumber.h"
using namespace Std;
BOOL Queen::P lace (int k)
{
Test the legality of Queen K in column X[k]
for (int j = 1; j < K; + + j)
if (ABS (k-j) = = = ABS (x[j]-x[k)) | | (X[j]==x[k]))
return false;
return true;
}
BOOL Queen::backtrack (int t)
{
Backtracking method for solving the problem after n
if (T > N)
{
for (int i=1; i<=n; ++i)
Y[i] = X[i];
return true;
}
Else
for (int i=1; i<=n; ++i)
{
X[t] = i;
if (place (t) && Backtrack (t+1))
return true;
}
return false;
}
/*
* QUEENSLV is the essence of the entire Las Vegas solution to the N Queens. And this function is not easy to understand, I am here to analyze the specific.
* k is the K-line, X[k] represents the queen of the K-line in the X[k] column
* Below these two points analysis please look carefully, I personally is stuck here for half a day, but understanding is very simple.
* Marking ①: Here is the number of all the columns that can be placed on the K line, save it with Y, and count the number of positions that can be placed
* Marking ②: Here we use the saved columns, and then randomly take one of the columns to place the queen of the K-line. This is the essence of Las Vegas!
*/
Author:tanky Woo
Blog:www.WuTianQi.com
BOOL QUEEN::QUEENSLV (int stopvegas)
{
A Las Vegas algorithm for randomly placing n queens
Randomnumber Rnd; Random number generator
int k = 1; Next queen number to be placed
int count = 1;
1 <= stopvegas <= n means the number of Queens allowed to be randomly placed
while ((k <= Stopvegas) && (Count > 0))
{
Count = 0;
for (int i = 1; i<=n; ++i)//-----------①
{
X[k] = i;
if (place (k))
y[count++] = i;
}
if (Count > 0)//-------------②
x[k++] = y[rnd. Random (count)]; Random position
}
Return (Count > 0); Count > 0 indicates a successful drop location
}
Main.cpp Main file:
/*
* Author:tanky Woo
* Blog:www.WuTianQi.com
* date:2010.12.9
* Las Vegas (Las Vegas) algorithm solves the N queen problem
* Code to Wang Xiaodong "Computer Algorithm design and analysis"
*/
#include "Queen.h"
#include "RandomNumber.h"
#include <iostream>
using namespace Std;
BOOL Nqueen (int n)
{
A Las Vegas algorithm for solving n problems with backtracking method
Queen X;
Initialize X
X.N = n;
int *p = new Int[n+1];
int *q = new Int[n+1];
for (int i=0; i<=n; ++i)
{
P[i] = 0;
Q[i] = 0;
}
x.y = q;
x.x = p;
Set the number of randomly placed Queens
int stop = 8;
if (n > 15)
stop = n-15;
BOOL found = false;
while (! X.QUEENSLV (stop));
The backtracking search part of the algorithm
if (X.backtrack (stop+1))
{
for (int i=1; i<=n; ++i)
cout << P[i] << "";
Found = true;
}
cout << Endl;
delete [] p;
delete [] q;
return found;
}
int main ()
{
Nqueen (8);
}
In the 8 Queen's question:
1.stopvegas=0 says it solves the problem entirely by backtracking. Therefore, a set of solutions must be obtained.
2.stopvegas=8 represents a fully practical stochastic approach to solving problems. Therefore, a set of solutions must be obtained.
Note that the book says to solve the 8 queen problem, when a different Stopvegas value is listed, the corresponding algorithm success rate, when stopvegas=8, he wrote is 0.1293, I personally think is wrong . And then I say why I understand that:
First of all, why does this program cause a failure, that is because at random to find the former Stopvegas line is established, does not mean that the back N-stopvegas line with backtracking can be set up, because this is not a thorough backtracking. It is calculated from the stopvegas+1 line, and the backtracking is not formed when the last return, also only stay in Stopvegas.
When we are completely random, it is the Las Vegas algorithm that repeatedly calls the random position to place n Queens until it is successfully placed. So when stopvegas=8, his success rate should be 100%.
In addition, when stopvegas=3, the success rate is equal to 0.49, approaching 0.5, we can try, basically each run two times will come back results.
If I understand wrong above, please point out that my blog (www.WuTianQi.com).
In the next article I will write the randomization algorithm (5)-Monte Carlo (Monte Carlo) algorithm.
Tanky Woo Original, welcome reprint, reprint please attach link, please do not delete the article in any about this blog link.
Tanky Woo Tags: random algorithm, Las Vegas algorithm, Las Vegas
Las Vegas algorithm (go to explain the introduction of the algorithm topic!!!) )