-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproposed_algorithm.py
More file actions
397 lines (340 loc) · 14.4 KB
/
proposed_algorithm.py
File metadata and controls
397 lines (340 loc) · 14.4 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
import heapq
import time
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
# Optional Fibonacci heap - will be imported only when requested
EXTERNAL_FIB_HEAP_AVAILABLE = False
EXTERNAL_FIB_HEAP_IMPORTED = False
# --------------------------
# Graph Generation Functions
# --------------------------
def create_grid_graph(size, default_weight=10, cycle_weight=1):
"""Create a grid graph with a hidden low-weight cycle."""
G = nx.grid_2d_graph(size, size)
G = nx.convert_node_labels_to_integers(G)
# Set default weights for all edges
for u, v in G.edges():
G[u][v]['weight'] = default_weight
# Embed a 4-node cycle with lower weight
hidden_nodes = [size*size - size - 2, size*size - size -1, size*size -1, size*size -2]
for i in range(4):
u, v = hidden_nodes[i], hidden_nodes[(i+1)%4]
G[u][v]['weight'] = cycle_weight
return G
def create_spatial_graph(n_nodes, radius=0.3, default_weight=10, cycle_weight=1):
"""Create a random geometric graph with a hidden low-weight cycle."""
G = nx.random_geometric_graph(n_nodes, radius)
G = nx.convert_node_labels_to_integers(G)
# Set default weights for all edges
for u, v in G.edges():
G[u][v]['weight'] = default_weight
# Add a 4-node cycle with lower weight
hidden_nodes = np.random.choice(n_nodes, 4, replace=False)
for i in range(4):
u, v = hidden_nodes[i], hidden_nodes[(i+1)%4]
if G.has_edge(u, v):
G[u][v]['weight'] = cycle_weight
else:
G.add_edge(u, v, weight=cycle_weight)
return G
# --------------------------
# Algorithm Implementations
# --------------------------
class LCATree:
"""Efficient LCA structure using binary lifting for ancestor jumps."""
def __init__(self, parents, depth, nodes):
"""
Initialize LCA tree with parent pointers and depths.
Args:
parents: Dict of parent nodes from Dijkstra's tree
depth: Dict of depths from Dijkstra's tree
nodes: List of all nodes in the graph
"""
self.nodes = nodes
self.log_max_depth = 20 # Supports graphs up to 2^20 nodes
self.up = np.full((len(nodes), self.log_max_depth), -1, dtype=int) # Ancestor table
self.depth = np.array([depth[n] for n in nodes], dtype=int)
self.node_to_index = {node: idx for idx, node in enumerate(nodes)} # O(1) lookups
# Initialize ancestor table
for idx, node in enumerate(nodes):
if parents.get(node) is not None:
self.up[idx][0] = self.node_to_index[parents[node]]
# Precompute ancestors using dynamic programming
for j in range(1, self.log_max_depth):
for idx in range(len(nodes)):
if self.up[idx][j-1] != -1:
self.up[idx][j] = self.up[self.up[idx][j-1]][j-1]
def lca(self, u, v):
"""Find lowest common ancestor using binary lifting."""
u_idx = self.node_to_index[u]
v_idx = self.node_to_index[v]
# Equalize depths using binary jumps
if self.depth[u_idx] != self.depth[v_idx]:
if self.depth[u_idx] < self.depth[v_idx]:
u_idx, v_idx = v_idx, u_idx
# Lift u to v's depth
for i in reversed(range(self.log_max_depth)):
if self.depth[u_idx] - (1 << i) >= self.depth[v_idx]:
u_idx = self.up[u_idx][i]
if u_idx == v_idx:
return self.nodes[u_idx]
# Find LCA by lifting both nodes
for i in reversed(range(self.log_max_depth)):
if self.up[u_idx][i] != -1 and self.up[u_idx][i] != self.up[v_idx][i]:
u_idx = self.up[u_idx][i]
v_idx = self.up[v_idx][i]
return self.nodes[self.up[u_idx][0]] if self.up[u_idx][0] != -1 else None
# Internal Fibonacci heap implementation
class FibonacciHeap:
"""A simple Fibonacci heap implementation for Dijkstra's algorithm."""
def __init__(self):
self.heap = []
self.entry_finder = {}
self.counter = 0
self.REMOVED = '<removed>'
self.operations = 0
def push(self, item, priority):
"""Add a new item or update the priority of an existing item."""
self.operations += 1
if item in self.entry_finder:
self.remove_item(item)
entry = [priority, self.counter, item]
self.entry_finder[item] = entry
heapq.heappush(self.heap, entry)
self.counter += 1
def remove_item(self, item):
"""Mark an existing item as REMOVED. Raise KeyError if not found."""
self.operations += 1
entry = self.entry_finder.pop(item)
entry[-1] = self.REMOVED
def pop(self):
"""Remove and return the lowest priority item. Raise KeyError if empty."""
self.operations += 1
while self.heap:
priority, _, item = heapq.heappop(self.heap)
if item is not self.REMOVED:
del self.entry_finder[item]
return priority, item
raise KeyError('pop from an empty priority queue')
def empty(self):
"""Return True if the queue is empty."""
return not any(item[-1] is not self.REMOVED for item in self.heap)
def dijkstra_base(G, start, gamma, use_fib_heap=False):
"""
Dijkstra's algorithm with early termination and optional Fibonacci heap.
Returns:
distances: Shortest paths from start node
preds: Predecessor pointers for path reconstruction
depth: Depth of each node in the shortest path tree
ops: Number of priority queue operations
"""
global EXTERNAL_FIB_HEAP_AVAILABLE, EXTERNAL_FIB_HEAP_IMPORTED
# Check if we need to import the Fibonacci heap
if use_fib_heap and not EXTERNAL_FIB_HEAP_IMPORTED:
try:
from fibonacci_heap_mod import Fibonacci_heap
EXTERNAL_FIB_HEAP_AVAILABLE = True
except ImportError:
EXTERNAL_FIB_HEAP_AVAILABLE = False
print("External Fibonacci heap not available. Using internal implementation.")
finally:
EXTERNAL_FIB_HEAP_IMPORTED = True
distances = {n: float('inf') for n in G.nodes()}
depth = {n: 0 for n in G.nodes()}
distances[start] = 0
preds = {}
ops = 0
if use_fib_heap:
if EXTERNAL_FIB_HEAP_AVAILABLE:
# Use the external Fibonacci heap implementation
from fibonacci_heap_mod import Fibonacci_heap
heap = Fibonacci_heap()
nodes_heap = {start: heap.enqueue(start, 0)}
visited = set()
while nodes_heap:
try:
min_node = heap.dequeue_min()
u = min_node.get_value()
ops += 1
# Remove from tracking dict
nodes_heap.pop(u, None)
if u in visited or distances[u] > gamma / 2:
continue
visited.add(u)
for v in G.neighbors(u):
new_dist = distances[u] + G[u][v]['weight']
if new_dist < distances[v]:
distances[v] = new_dist
preds[v] = u
depth[v] = depth[u] + 1
if v in nodes_heap:
heap.decrease_key(nodes_heap[v], new_dist)
else:
nodes_heap[v] = heap.enqueue(v, new_dist)
ops += 1
except Exception as e:
print(f"Warning: Error with external Fibonacci heap: {e}")
print("Falling back to standard implementation")
break
else:
# Use our internal Fibonacci heap implementation
heap = FibonacciHeap()
heap.push(start, 0)
visited = set()
while not heap.empty():
try:
current_dist, u = heap.pop()
ops += 1
if u in visited or current_dist > gamma / 2:
continue
visited.add(u)
for v in G.neighbors(u):
new_dist = current_dist + G[u][v]['weight']
if new_dist < distances[v]:
distances[v] = new_dist
preds[v] = u
depth[v] = depth[u] + 1
heap.push(v, new_dist)
ops += 1
except KeyError:
break
else:
# Use standard heapq
heap = [(0, start)]
visited = set()
while heap:
current_dist, u = heapq.heappop(heap)
ops += 1
if u in visited or current_dist > gamma / 2:
continue
visited.add(u)
for v in G.neighbors(u):
new_dist = current_dist + G[u][v]['weight']
if new_dist < distances[v]:
distances[v] = new_dist
preds[v] = u
depth[v] = depth[u] + 1
heapq.heappush(heap, (new_dist, v))
ops += 1
return distances, preds, depth, ops
def proposed_algorithm(G, use_fib_heap=False, use_lca=False):
"""Find shortest cycle using optimized Dijkstra with pruning."""
gamma = float('inf') # Current shortest cycle length
total_ops = 0 # Total priority queue operations
active_nodes = set(G.nodes()) # Nodes not yet pruned
min_edge_weight = min(e['weight'] for _, _, e in G.edges(data=True)) # For pruning
nodes = list(G.nodes()) # Stable node order for LCA
for node in nodes:
if node not in active_nodes:
continue
# Run Dijkstra and get shortest paths
distances, preds, depth, ops = dijkstra_base(G, node, gamma, use_fib_heap)
total_ops += ops
# Build LCA tree once per iteration
lca_tree = LCATree(preds, depth, nodes) if use_lca else None
# Track best cycle found in this iteration
local_cycle_length = float('inf')
# Check all edges for potential cycles
for u in G.nodes():
for v in G.neighbors(u):
if distances[u] == float('inf') or distances[v] == float('inf') or u == v:
continue
# Calculate cycle length using LCA if available
if use_lca and lca_tree:
p = lca_tree.lca(u, v)
if p and p != u and p != v:
cycle_length = distances[u] + distances[v] + G[u][v]['weight'] - 2 * distances[p]
else:
cycle_length = float('inf') # Invalid cycle
else:
# Fallback to simple cycle check
if preds.get(v) != u and preds.get(u) != v:
cycle_length = distances[u] + distances[v] + G[u][v]['weight']
else:
cycle_length = float('inf')
# Update global and local cycle information
if cycle_length < gamma:
gamma = cycle_length
if cycle_length < local_cycle_length:
local_cycle_length = cycle_length
# Aggressive pruning: Remove nodes that can't improve the cycle
for v in G.nodes():
if distances[v] + 2 * min_edge_weight >= gamma:
active_nodes.discard(v)
return gamma, total_ops
# --------------------------
# Benchmarking & Visualization
# --------------------------
def benchmark(graph_type, sizes, n_trials=3):
"""Compare algorithm variants across graph sizes."""
results = {
'heapq': {'ops': [], 'time': []},
'fib_heap': {'ops': [], 'time': []},
'fib_heap_lca': {'ops': [], 'time': []}
}
for size in sizes:
print(f"Testing {graph_type} size {size}")
ops = {k: [] for k in results}
times = {k: [] for k in results}
for _ in range(n_trials):
# Generate test graph
if graph_type == 'grid':
G = create_grid_graph(size)
elif graph_type == 'spatial':
G = create_spatial_graph(size)
# Test heapq variant
start = time.time()
_, o = proposed_algorithm(G, use_fib_heap=False)
times['heapq'].append(time.time() - start)
ops['heapq'].append(o)
# Test Fibonacci heap variant
start = time.time()
_, o = proposed_algorithm(G, use_fib_heap=True)
times['fib_heap'].append(time.time() - start)
ops['fib_heap'].append(o)
# Test Fibonacci+LCA variant
start = time.time()
_, o = proposed_algorithm(G, use_fib_heap=True, use_lca=True)
times['fib_heap_lca'].append(time.time() - start)
ops['fib_heap_lca'].append(o)
# Aggregate results
for k in results:
results[k]['ops'].append(np.mean(ops[k]))
results[k]['time'].append(np.mean(times[k]))
return results
def plot_results(results, sizes, title):
"""Visualize benchmark results."""
plt.figure(figsize=(12, 5))
# Operations plot
plt.subplot(1, 2, 1)
for label, data in results.items():
plt.plot(sizes, data['ops'], marker='o', label=label)
plt.xlabel('Graph Size')
plt.ylabel('Priority Queue Operations')
plt.title(f'{title} - Operations')
plt.legend()
# Runtime plot
plt.subplot(1, 2, 2)
for label, data in results.items():
plt.plot(sizes, data['time'], marker='o', label=label)
plt.xlabel('Graph Size')
plt.ylabel('Execution Time (s)')
plt.title(f'{title} - Runtime')
plt.legend()
plt.tight_layout()
plt.show()
# --------------------------
# Main Execution
# --------------------------
if __name__ == '__main__':
# Configure benchmark parameters
graph_classes = ['grid', 'spatial']
sizes = {
'grid': [5, 7, 9], # Grid dimensions
'spatial': [20, 30, 40] # Node counts
}
# Run benchmarks and plot results
for graph_type in graph_classes:
results = benchmark(graph_type, sizes[graph_type])
plot_results(results, sizes[graph_type], f"{graph_type.capitalize()} Graphs")