-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtiming_individual_function.py
110 lines (96 loc) · 3.17 KB
/
timing_individual_function.py
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
import time
import networkx as nx
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
import nx_parallel as nxp
# Code to create README heatmaps for individual function currFun
heatmapDF = pd.DataFrame()
# for bipartite graphs
# n = [50, 100, 200, 400]
# m = [25, 50, 100, 200]
number_of_nodes_list = [200, 400, 800, 1600]
weighted = False
pList = [1, 0.8, 0.6, 0.4, 0.2]
currFun = nx.tournament.is_reachable
"""
for p in pList:
for num in range(len(number_of_nodes_list)):
# create original and parallel graphs
G = nx.fast_gnp_random_graph(
number_of_nodes_list[num], p, seed=42, directed=True
)
# for bipartite.node_redundancy
G = nx.bipartite.random_graph(n[num], m[num], p, seed=42, directed=True)
for i in G.nodes:
l = list(G.neighbors(i))
if len(l) == 0:
v = random.choice(list(G.nodes) - [i,])
G.add_edge(i, v)
G.add_edge(i, random.choice([node for node in G.nodes if node != i]))
elif len(l) == 1:
G.add_edge(i, random.choice([node for node in G.nodes if node != i and node not in list(G.neighbors(i))]))
# for weighted graphs
if weighted:
random.seed(42)
for u, v in G.edges():
G[u][v]["weight"] = random.random()
H = nxp.ParallelGraph(G)
# time both versions and update heatmapDF
t1 = time.time()
c1 = currFun(H)
if isinstance(c1, types.GeneratorType):
d = dict(c1)
t2 = time.time()
parallelTime = t2 - t1
t1 = time.time()
c2 = currFun(G)
if isinstance(c2, types.GeneratorType):
d = dict(c2)
t2 = time.time()
stdTime = t2 - t1
timesFaster = stdTime / parallelTime
heatmapDF.at[number_of_nodes_list[num], p] = timesFaster
print("Finished " + str(currFun))
"""
# Code to create for row of heatmap specifically for tournaments
for num in number_of_nodes_list:
print(num)
G = nx.tournament.random_tournament(num, seed=42)
H = nxp.ParallelGraph(G)
t1 = time.time()
c = currFun(H, 1, num)
t2 = time.time()
parallelTime = t2 - t1
print(parallelTime)
t1 = time.time()
c = currFun(G, 1, num)
t2 = time.time()
stdTime = t2 - t1
print(stdTime)
timesFaster = stdTime / parallelTime
heatmapDF.at[num, 3] = timesFaster
print("Finished " + str(currFun))
# plotting the heatmap with numbers and a green color scheme
plt.figure(figsize=(20, 4))
hm = sns.heatmap(data=heatmapDF.T, annot=True, cmap="Greens", cbar=True)
# Remove the tick labels on both axes
hm.set_yticklabels(
[
3,
]
)
# Adding x-axis labels
hm.set_xticklabels(number_of_nodes_list)
# Rotating the x-axis labels for better readability (optional)
plt.xticks(rotation=45)
plt.yticks(rotation=20)
plt.title(
"Small Scale Demo: Times Speedups of " + currFun.__name__ + " compared to NetworkX"
)
plt.xlabel("Number of Vertices")
plt.ylabel("Edge Probability")
print(currFun.__name__)
# displaying the plotted heatmap
plt.tight_layout()
plt.savefig("timing/" + "heatmap_" + currFun.__name__ + "_timing.png")