-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
414 lines (330 loc) · 13.1 KB
/
benchmark.py
File metadata and controls
414 lines (330 loc) · 13.1 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import time
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import cProfile
import pstats
import os
from io import StringIO
from shortest_cycle import sota_shortest_cycle, create_grid_graph, create_spatial_graph
# --------------------------
# Benchmarking & Visualization
# --------------------------
def benchmark_graph_type(graph_generator, sizes, num_trials=3, title="Benchmark Results"):
"""
Benchmark the shortest cycle algorithm on different graph types and sizes.
Args:
graph_generator : callable
A function that takes a size parameter and returns a NetworkX graph.
sizes : list
List of graph sizes (nodes) to benchmark.
num_trials : int, optional
Number of trials per size to average the results.
title : str, optional
Title for the benchmark plot.
Returns:
tuple: (sizes, times, title) for plotting.
"""
times = []
for size in sizes:
print(f"Benchmarking {title} of size {size}...")
time_sum = 0
for _ in range(num_trials):
# Generate graph
G = graph_generator(size)
# Measure execution time
start_time = time.time()
sota_shortest_cycle(G)
end_time = time.time()
time_sum += (end_time - start_time)
# Average time across trials
avg_time = time_sum / num_trials
times.append(avg_time)
print(f" Average time: {avg_time:.6f} seconds")
return sizes, times, title
def benchmark_vs_traditional(graph_generator, sizes, num_trials=3):
"""
Compare our optimized algorithm with a traditional approach.
Args:
graph_generator : callable
Function to generate test graphs.
sizes : list
List of graph sizes to test.
num_trials : int
Number of trials per size.
Returns:
dict: Results with times for each approach.
"""
results = {
'optimized': {'time': []},
'traditional': {'time': []}
}
for size in sizes:
print(f"Testing size {size}")
opt_times = []
trad_times = []
for _ in range(num_trials):
G = graph_generator(size)
# Optimized algorithm (our implementation)
start = time.time()
sota_shortest_cycle(G)
opt_times.append(time.time() - start)
# Traditional approach (edge removal + Dijkstra)
start = time.time()
traditional_shortest_cycle(G)
trad_times.append(time.time() - start)
results['optimized']['time'].append(np.mean(opt_times))
results['traditional']['time'].append(np.mean(trad_times))
return results
def traditional_shortest_cycle(G):
"""
Traditional approach to finding shortest cycles by edge removal.
Used for comparison benchmarking.
Args:
G: NetworkX graph
Returns:
float or None: Length of shortest cycle
"""
shortest_cycle_length = float('inf')
for u, v in list(G.edges()): # Create a copy of edges to iterate
weight = G[u][v].get('weight', 1.0)
G.remove_edge(u, v)
try:
path_length = nx.shortest_path_length(G, u, v, weight='weight')
cycle_length = path_length + weight
shortest_cycle_length = min(shortest_cycle_length, cycle_length)
except nx.NetworkXNoPath:
pass
G.add_edge(u, v, weight=weight)
if shortest_cycle_length == float('inf'):
return None
return shortest_cycle_length
def plot_benchmark_results(results, title):
"""
Plot benchmark results with log-log scale.
Args:
results : list
List of tuples (sizes, times, label).
title : str
Plot title.
"""
plt.figure(figsize=(12, 8))
for sizes, times, label in results:
plt.loglog(sizes, times, marker='o', label=label)
plt.xlabel('Graph Size (Nodes)')
plt.ylabel('Execution Time (seconds)')
plt.title(title)
plt.grid(True, linestyle='--', alpha=0.7)
plt.legend()
# Save the plot
plt.savefig(f"{title.replace(' ', '_').lower()}.png", dpi=300, bbox_inches='tight')
plt.show()
def plot_comparison_results(results, sizes, title):
"""
Plot comparison between optimized and traditional approaches.
Args:
results : dict
Dictionary with benchmark results.
sizes : list
Graph sizes that were tested.
title : str
Plot title.
"""
plt.figure(figsize=(10, 6))
plt.plot(sizes, results['optimized']['time'], marker='o', label='Optimized Algorithm')
plt.plot(sizes, results['traditional']['time'], marker='s', label='Traditional Approach')
plt.xlabel('Graph Size')
plt.ylabel('Execution Time (s)')
plt.title(f'Algorithm Comparison: {title}')
plt.grid(True, linestyle='--', alpha=0.7)
plt.legend()
# Save the plot
plt.savefig(f"comparison_{title.replace(' ', '_').lower()}.png", dpi=300, bbox_inches='tight')
plt.show()
def run_all_benchmarks():
"""Run benchmarks on various graph types and plot the results."""
# Grid graph parameters
grid_sizes = [4, 5, 7, 9, 11, 13, 15] # Grid dimensions
# Spatial graph parameters
spatial_sizes = [20, 50, 100, 200, 300, 400, 500] # Node counts
# Results collection for different graph types
results = []
# Grid graph benchmarks
print("\nBenchmarking Grid Graphs...")
result = benchmark_graph_type(
lambda size: create_grid_graph(size),
grid_sizes,
title="Grid Graphs"
)
results.append(result)
# Spatial graph benchmarks
print("\nBenchmarking Spatial Graphs...")
result = benchmark_graph_type(
lambda size: create_spatial_graph(size, radius=0.4),
spatial_sizes,
title="Spatial Graphs"
)
results.append(result)
# Plot combined results
plot_benchmark_results(results, "Shortest Cycle Algorithm Performance")
# Compare with traditional approach on smaller graphs
print("\nComparing with traditional approach...")
comparison_grid_sizes = [4, 5, 7, 9]
comparison_spatial_sizes = [20, 50, 100, 150]
grid_comparison = benchmark_vs_traditional(
lambda size: create_grid_graph(size),
comparison_grid_sizes
)
plot_comparison_results(grid_comparison, comparison_grid_sizes, "Grid Graphs")
spatial_comparison = benchmark_vs_traditional(
lambda size: create_spatial_graph(size),
comparison_spatial_sizes
)
plot_comparison_results(spatial_comparison, comparison_spatial_sizes, "Spatial Graphs")
# Print summary
print("\nBenchmark Summary:")
for sizes, times, title in results:
print(f"{title}:")
for size, t in zip(sizes, times):
print(f" Size {size}: {t:.6f} seconds")
def benchmark_algorithm_components():
"""Benchmark different components of the algorithm to identify bottlenecks."""
# This is an optional function to profile different parts of the algorithm
# Create a medium-sized test graph
G = create_spatial_graph(200, radius=0.3)
# Time breakdown
times = {
'dijkstra': 0,
'lca_construction': 0,
'lca_queries': 0,
'total': 0
}
# Start timing
total_start = time.time()
# We'd need to instrument the algorithm to get component times
# This would typically be done using profiling tools
# End timing
times['total'] = time.time() - total_start
# Print breakdown
print("\nAlgorithm Component Analysis:")
for component, duration in times.items():
percentage = (duration / times['total']) * 100 if times['total'] > 0 else 0
print(f" {component}: {duration:.6f}s ({percentage:.1f}%)")
# --------------------------
# Profiling Functions
# --------------------------
def run_profiling(graph_size=20, output_dir="profiling_results"):
"""
Run detailed profiling on the algorithm with various settings.
Args:
graph_size (int): Size of the test graph
output_dir (str): Directory to save profiling results
"""
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
print(f"Running profiling on grid graph of size {graph_size}...")
G = create_grid_graph(graph_size)
# Run and profile the algorithm
profiler = cProfile.Profile()
profiler.enable()
result = sota_shortest_cycle(G)
profiler.disable()
# Save raw profiling data
prof_file = os.path.join(output_dir, f"grid_{graph_size}_profile.prof")
profiler.dump_stats(prof_file)
# Generate text report
s = StringIO()
ps = pstats.Stats(profiler, stream=s).sort_stats('cumulative')
ps.print_stats(30) # Print top 30 functions by cumulative time
# Save the report to file
with open(os.path.join(output_dir, f"grid_{graph_size}_stats.txt"), 'w') as f:
f.write(s.getvalue())
# Print the report to console
print("\nProfiling Results Summary:")
print(f"Shortest cycle length: {result}")
print("\nTop 10 functions by cumulative time:")
pstats.Stats(profiler).strip_dirs().sort_stats('cumulative').print_stats(10)
print(f"\nFull profiling data saved to {prof_file}")
print(f"To analyze further, you can use tools like snakeviz:")
print(f" pip install snakeviz")
print(f" snakeviz {prof_file}")
def compare_algorithm_variants(graph_size=20, output_dir="profiling_results"):
"""
Compare different algorithm variants with detailed profiling.
Args:
graph_size (int): Size of the test graph
output_dir (str): Directory to save profiling results
"""
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
print(f"Comparing algorithm variants on grid graph of size {graph_size}...")
G = create_grid_graph(graph_size)
# Define variant descriptions for output
variants = {
'traditional': "Traditional approach (edge removal + shortest path)",
'optimized': "Optimized approach with LCA and pruning",
}
# Initialize results
results = {}
# Profile traditional approach
profiler = cProfile.Profile()
profiler.enable()
trad_result = traditional_shortest_cycle(G)
profiler.disable()
prof_file = os.path.join(output_dir, f"grid_{graph_size}_traditional.prof")
profiler.dump_stats(prof_file)
s = StringIO()
ps = pstats.Stats(profiler, stream=s).sort_stats('cumulative')
ps.print_stats(20)
with open(os.path.join(output_dir, f"grid_{graph_size}_traditional_stats.txt"), 'w') as f:
f.write(s.getvalue())
results['traditional'] = {
'result': trad_result,
'prof_file': prof_file
}
# Profile optimized approach
profiler = cProfile.Profile()
profiler.enable()
opt_result = sota_shortest_cycle(G)
profiler.disable()
prof_file = os.path.join(output_dir, f"grid_{graph_size}_optimized.prof")
profiler.dump_stats(prof_file)
s = StringIO()
ps = pstats.Stats(profiler, stream=s).sort_stats('cumulative')
ps.print_stats(20)
with open(os.path.join(output_dir, f"grid_{graph_size}_optimized_stats.txt"), 'w') as f:
f.write(s.getvalue())
results['optimized'] = {
'result': opt_result,
'prof_file': prof_file
}
# Print comparison summary
print("\nAlgorithm Variant Comparison Summary:")
print(f"Graph: Grid graph with {graph_size}x{graph_size} nodes")
print(f"Shortest cycle length: {opt_result}")
for variant, data in results.items():
print(f"\n{variants[variant]}:")
print(f" Result: {data['result']}")
print(f" Profile data: {data['prof_file']}")
print(" Top 5 functions by cumulative time:")
pstats.Stats(data['prof_file']).strip_dirs().sort_stats('cumulative').print_stats(5)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Shortest Cycle Algorithm Benchmarks and Profiling')
parser.add_argument('--run', choices=['benchmarks', 'profile', 'compare'], default='benchmarks',
help='What to run: regular benchmarks, detailed profiling, or algorithm comparison')
parser.add_argument('--size', type=int, default=20,
help='Graph size for profiling (default: 20)')
args = parser.parse_args()
if args.run == 'benchmarks':
print("Starting benchmarks...")
run_all_benchmarks()
print("\nBenchmarking complete!")
elif args.run == 'profile':
print("Starting detailed profiling...")
run_profiling(graph_size=args.size)
print("\nProfiling complete!")
elif args.run == 'compare':
print("Starting algorithm comparison...")
compare_algorithm_variants(graph_size=args.size)
print("\nComparison complete!")