-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter1_num6_percolation.py
More file actions
135 lines (109 loc) · 4.3 KB
/
chapter1_num6_percolation.py
File metadata and controls
135 lines (109 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
教程第一周作业: 提交的java版本在percolation.zip里
http://coursera.cs.princeton.edu/algs4/assignments/percolation.html
Write a program to estimate the value of the percolation threshold via Monte Carlo simulation.
"""
import sys
import random
import numpy as np
class WeightedQuickUnionUF(object):
def __init__(self, n):
self.store = list()
self.sz = list()
for i in range(n):
self.store.append(i)
self.sz.append(1)
def find(self, p):
if self.store[p] == p:
return p
return self.find(self.store[p])
def union(self, p, q):
p_root = self.find(p)
q_root = self.find(q)
if p_root == q_root:
return
if self.sz[p_root] < self.sz[q_root]:
self.store[p_root] = q_root
self.sz[q_root] += self.sz[p_root]
else:
self.store[q_root] = p_root
self.sz[p_root] += self.sz[q_root]
def is_connected(self, p, q):
return self.find(p) == self.find(q)
class Percolation(object):
def __init__(self, n):
# 0 代表最上层一个点, n*n +1 代表最下层一个点
# row, col 对应的点序号为n*(row-1) + col
self.union_find = WeightedQuickUnionUF(n*n + 2)
self.site = [0] * (n*n + 2) # 0代表closed, 1 代表opened
self.num = n
self.open_num = 0
for i in range(1, n+1):
self.union_find.union(0, i)
self.union_find.union(n*n+1, n*n+1-i)
def open(self, row, col):
# union 相邻的四个点中为open的site
if self.is_open(row, col):
return
self.open_num += 1
index = self.cal_index(row, col)
self.site[index] = 1
if row - 1 > 0 and self.is_open(row-1, col):
index_up = self.cal_index(row-1, col)
self.union_find.union(index, index_up)
if row + 1 <= self.num and self.is_open(row+1, col):
index_down = self.cal_index(row+1, col)
self.union_find.union(index, index_down)
if col - 1 > 0 and self.is_open(row, col-1):
index_left = self.cal_index(row, col-1)
self.union_find.union(index, index_left)
if col + 1 <= self.num and self.is_open(row, col+1):
index_right = self.cal_index(row, col+1)
self.union_find.union(index, index_right)
def is_open(self, row, col):
index = self.cal_index(row, col)
return self.site[index] == 1
def is_full(self, row, col):
index = self.cal_index(row, col)
return self.site[index] == 1 and self.union_find.is_connected(0, index)
def num_of_open_site(self):
return self.open_num
def is_percolate(self):
return self.union_find.is_connected(0, self.num * self.num + 1)
def cal_index(self, row, col):
if not (0 < row <= self.num):
raise ValueError('row 应该大于零且小于等于n')
if not (0 < col <= self.num):
raise ValueError('col 应该大于零且小于等于n')
return self.num * (row - 1) + col
def random_open_until_percolate(self):
while not self.is_percolate():
row = random.randint(1, self.num)
col = random.randint(1, self.num)
self.open(row, col)
class PercolationStats(object):
def __init__(self, n, t):
self.num = n
self.test_time = t
self.probability = []
for i in range(t):
percolation = Percolation(n)
percolation.random_open_until_percolate()
self.probability.append(percolation.open_num/(n*n))
self.np = np.array(self.probability)
def mean(self):
return self.np.mean()
def stddev(self):
return self.np.var()
def confidence_lo(self):
return self.mean() - 1.96 * self.stddev() / np.sqrt(self.test_time)
def confidence_hi(self):
return self.mean() + 1.96 * self.stddev() / np.sqrt(self.test_time)
if __name__ == '__main__':
n = int(sys.argv[1])
t = int(sys.argv[2])
percolation_stats = PercolationStats(n, t)
print('mean: {}'.format(percolation_stats.mean()))
print('stddev: {}'.format(percolation_stats.stddev()))
print('95% confidence interval: [{}, {}]'.format(
percolation_stats.confidence_lo(), percolation_stats.confidence_hi()))