-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpercolation.cpp
70 lines (56 loc) · 1.55 KB
/
percolation.cpp
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
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#include "weighted-quick-union.h"
#include "generate-random.h"
int main()
{
int start = time(nullptr);
unsigned int N;
unsigned long int total_els, iter = 0;
float probability;
cout << "Enter size of matrix: ";
cin >> N;
total_els = N * N;
unsigned int *rand_hash = new unsigned int[total_els];
// Added 2 because last 2 elements will be treated as virtual top and bottom respectively.
WQuickUnionUF U1(total_els + 2);
unsigned long int v_top = total_els;
unsigned long int v_bottom = total_els + 1;
memset(rand_hash, 0, total_els);
while (1)
{
unsigned long int r = getRandomNumber(total_els);
if (!rand_hash[r])
{
rand_hash[r] = 1;
iter++;
// Top connection
if (r < N)
U1.Union(v_top, r);
else if (rand_hash[r - N])
U1.Union(r - N, r);
// Botton connection
if (r + N > total_els - 1)
U1.Union(v_bottom, r);
else if (rand_hash[r + N])
U1.Union(r + N, r);
// Left connection
if (r % N > 0 && rand_hash[r - 1] == 1)
U1.Union(r - 1, r);
// Right connection
if (r % N < N - 1 && rand_hash[r + 1] == 1)
U1.Union(r + 1, r);
probability = (float)iter / (float)total_els;
if (probability > 0.5 && U1.connected(v_top, v_bottom))
break;
}
}
delete[] rand_hash;
probability = (float)iter / (float)total_els;
cout << probability << endl;
int end = time(nullptr);
cout << end - start << endl;
return 0;
}