拉斯维加斯算法&蒙特卡罗算法_杨劲松

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

• the probability of Majority method for return ture: p>1/2 • the probability of Majority2 method for return ture: p+(1-p)p = 1-(1-p)² > 3/4
2013-6-21 21/23
2013-6-21 16/23
Las Vegas Does N-Queens : � Solution4 Solution4:
• random permutation vectors
�a
random permutation vectors
3 7 2 8 1 4 6
two vector flags for diagonal attacks

2013-6-21
17/23
Las Vegas Does N-Queens : � Solution5 Solution5:
• GridSearch
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
� Retain information about cell
2013-6-21
failtimes =2
15/23
Las Vegas Does N-Queens
� The efficiency with different step
solution2
solution3
• step: the number of randomly placed queen • p: success probability • s: the average number of nodes search access for a successful
� � � � do {LV(x,y,stopVegas); Backtracking(n-stopVegas) { ...
if(fail) { failtimes++; trace-back(failtimes) }
� } � }while(LV(x,y)is success)
2013-6-21
failtimes =1
2013-6-21 11/23
Las Vegas Does N-Queens : � Solution3 Solution3:
• backtrack n steps according failtimes
� � � � do {LV(x,y,stopVegas); Backtracking(n-stopVegas) { ...
(1,1,1……,1,1),(1,1,1……,1,2) ,(1,1,1……,2,2),……,(n,n,n…… ,n,n)。
2013-6-21 7/23
N-Queens : � The solution space tree tree:
root a[1][1] a[2][1] 2 3 3 1 2 2 2 a[1][2] 1 2 2 2 a[1][3] 1 2 2
2013-6-21 9/23
Las Vegas Does N-Queens : � Solution2 Solution2:
• combine the trace-back and Las Vegas algorithm � do � { //Placeing the part of
queens with random raw
• backtrack n steps according failtime
� � � � do {LV(x,y,stopVegas); Backtracking(n-stopVegas) { ...
if(fail) { failtimes++; trace-back(failtimes) }
� } � }while(LV(x,y)is success)
1 1 1 1 1 1 1
1 1
2013-6-21
18/23
Monte Carlo Algorithm � Characteristic
• running time is deterministic. • output may be incorrect with a certain (typically small) probability.
if(fail) { failtimes++; trace-back(failtimes) }
� } � }while(LV(x,y)is success)
2013-6-21 12/23
Las Vegas Does N-Queens : � Solution3 Solution3:
• backtrack n steps according failtimes
3
......
8/23
2013-6-21
Las Vegas Does N-Queens : Poor efficiency � Solution1 Solution1:
� do { � LV(x,y){ � try to place the queen in random
column
�} � }while(LV(x,y)is success)
2013-6-21
23/23
华南师范大学计算机学院 – 计算机算法
拉斯维加斯算法 &蒙特卡罗算法
作者:杨劲松
2013-6-21
Outline
� Probabilistic Algorithm � Las Vegas Algorithm � Probabilistic Algorithm
2013-6-21
2/23
Probabilistic Algorithm
INPUT
ALGORITHM
OUTPUT
RANDOM NUMBERS
� Algorithm takes a source of random numbers and makes random choices during execution; � Behavior can vary even on a fixed input;
13/23
Las Vegas Does N-Queens : � Solution3 Solution3:
• backtrack n steps according failtimes
� � � � do {LV(x,y,stopVegas); Backtracking(n-stopVegas) { ...
� public static boolean majority(int[]t, int n) � { rnd = new Random(); � int i=rnd.random(n)+1; � int x=t[i]; int k=0; � for (int j=1;j<=n;j++) if (t[j]==x) k++; � return (k>n/2); � � }
2013-6-21
19/23
Monte Carlo Algorithm � The main element of array
• In an array whose size is N, the main element is a occurs more than N / 2 times . • eg: [ 1,2,3,1,3,1,1] , the main element is 1.
百度文库
Monte Carlo Algorithm
table1.Monte Carlo versus Las Vegas
2013-6-21
22/23
REFERENCES
[1]Rolfe T J. Las Vegas does n-queens[J]. ACM SIGCSE Bulletin, 2006, 38(2): 37-38. [2]何月梅 ,刘翠霞 ,张青 . 拉斯维加斯方法解 n 后问题的研究 [J]. 邯郸学院 学报 ,2008,03:35-37.
if(fail) { failtimes++; trace-back(failtimes) }
� } � }while(LV(x,y)is success)
2013-6-21
failtimes =2
14/23
Las Vegas Does N-Queens : � Solution3 Solution3:
2013-6-21
3/23
Probabilistic Algorithm
2013-6-21
4/23
Las Vegas Algorithm
� Randomized algorithm � Correct results � void obstinate(Object x, Object y) �{ � bool success= false; � while (!success) success=LV(x,y); �}
2013-6-21 20/23
Monte Carlo Algorithm � The main element of array
• Repeated calls to improve the correct probability
� public static boolean Majority2(int []t, int n) � {// call Majority method 2 times � if (Majority(t,n)) return true; � else return majority(t,n); � }
2013-6-21
5/23
N-Queens � Placing n queens on an n×n chessboard.
� No two queens attack each other.
2013-6-21
6/23
N-Queens � Solution vector : X[i] represents the positions of the i-th queen in the x[i]-th column ,so,the solution vector is(X[1],X[2],X[3],……, X[n]) :( nn) � Solution space space:( :(n
� LV(x,y,stopVegas); � Backtracking(nstopVegas) � }while(LV(x,y)is success)
2013-6-21 10/23
Las Vegas Does N-Queens : � Solution2 Solution2:
• combine the trace-back and Las Vegas algorithm
the efficiency with different step
• step: the number of randomly placed queen • p: success probability the average number of nodes search access for • s: a successful
相关文档
最新文档