-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercolation_defination.py
More file actions
71 lines (56 loc) · 1.84 KB
/
Percolation_defination.py
File metadata and controls
71 lines (56 loc) · 1.84 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
import networkx as nx
import random
import pickle
import matplotlib.pyplot as plt
import time
# Load one month's data
def load_monthly_network(month_to_load):
file_path = f"monthly_networks/{month_to_load}.pkl"
with open(file_path, 'rb') as file:
loaded_graph = pickle.load(file)
return loaded_graph
def site_percolation(graph, p):
H = graph.copy()
for node in list(H.nodes()):
if random.random() > p:
H.remove_node(node)
return H
def calculate_connectivity(H):
components = list(nx.connected_components(H))
if components:
largest_component = max(components, key=len)
return len(components), len(largest_component)
else:
return len(components), 0
def has_spanning_path(G, p, threshold_ratio=0.5):
H = site_percolation(G, p)
components = list(nx.connected_components(H))
if not components:
return False
largest_component = max(components, key=len)
if len(largest_component) >= threshold_ratio * H.number_of_nodes():
return True
else:
return False
def simulate_percolation(G, p_values, num_simulations,threshold_ratio=0.5):
results = []
for p in p_values:
success_count = 0
for _ in range(num_simulations):
if has_spanning_path(G, p, threshold_ratio):
success_count += 1
success_ratio = success_count / num_simulations
results.append((p, success_ratio))
return results
p_values = [i/10 for i in range(1, 11)]
num_simulations = 100
G = load_monthly_network("2000-05")
tic = time.time()
results = simulate_percolation(G, p_values, num_simulations)
toc = time.time()
print(toc - tic)
plt.plot([result[0] for result in results], [result[1] for result in results])
plt.xlabel("p")
plt.ylabel("Success ratio")
plt.title("Percolation simulation")
plt.show()