Skip to content

Commit 55061f5

Browse files
committed
added Time Complexity calculations
1 parent 6540239 commit 55061f5

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

src/lrsa_optimized.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def add_edge(self, node1, node2, weight):
1919
self.graph.add_edge(node1, node2, weight=weight)
2020

2121
def form_clusters(self):
22+
start_time = time.time()
2223
num_clusters = 5
2324
nodes = list(self.graph.nodes())
2425
random.shuffle(nodes)
@@ -27,20 +28,32 @@ def form_clusters(self):
2728
self.clusters.append(nodes[i * cluster_size:(i + 1) * cluster_size])
2829
if len(nodes) % num_clusters != 0:
2930
self.clusters[-1].extend(nodes[num_clusters * cluster_size:])
31+
end_time = time.time()
32+
print(f"Clustering Time: {end_time - start_time:.6f} seconds")
33+
# Time Complexity: O(n)
3034

3135
def assign_gateway_nodes(self):
36+
start_time = time.time()
3237
for cluster in self.clusters:
3338
gateway_node = cluster[0]
3439
self.gateway_nodes[gateway_node] = cluster
40+
end_time = time.time()
41+
print(f"Gateway Assignment Time: {end_time - start_time:.6f} seconds")
42+
# Time Complexity: O(n)
3543

3644
def precompute_paths(self):
45+
start_time = time.time()
3746
for node in self.graph.nodes():
3847
self.precomputed_paths[node] = nx.single_source_dijkstra_path(self.graph, source=node, weight='weight')
48+
end_time = time.time()
49+
print(f"Precomputation Time: {end_time - start_time:.6f} seconds")
50+
# Time Complexity: O(n * (n log n + e)) for precomputation
3951

4052
def intra_cluster_shortest_path(self, source, target):
4153
cluster = self.find_cluster(source)
4254
subgraph = self.graph.subgraph(cluster)
4355
return nx.shortest_path(subgraph, source=source, target=target, weight='weight')
56+
# Time Complexity: O(n log n + e) per query within a cluster
4457

4558
def inter_cluster_shortest_path(self, source, target):
4659
source_gateway = self.find_gateway(source)
@@ -52,6 +65,7 @@ def inter_cluster_shortest_path(self, source, target):
5265

5366
full_path = path1 + path2[1:] + path3[1:]
5467
return full_path
68+
# Time Complexity: O(n log n + e) per query involving multiple clusters
5569

5670
def shortest_path(self, source, target):
5771
if source == target:
@@ -87,8 +101,8 @@ def find_gateway(self, node):
87101
return gateway
88102
return None
89103

90-
91104
def generate_connected_graph(num_nodes, edge_probability):
105+
start_time = time.time()
92106
graph = nx.Graph()
93107
for i in range(num_nodes):
94108
graph.add_node(str(i))
@@ -97,24 +111,60 @@ def generate_connected_graph(num_nodes, edge_probability):
97111
for j in range(i + 1, num_nodes):
98112
if random.random() < edge_probability:
99113
graph.add_edge(str(i), str(j), weight=random.randint(1, 100))
114+
end_time = time.time()
115+
print(f"Graph Generation Time: {end_time - start_time:.6f} seconds")
100116
return graph
101117

102118
def ensure_dense_connectivity(graph, num_nodes, edge_probability):
119+
start_time = time.time()
103120
for i in range(num_nodes):
104121
for j in range(i + 1, num_nodes):
105122
if not graph.has_edge(str(i), str(j)):
106123
if random.random() < edge_probability:
107124
graph.add_edge(str(i), str(j), weight=random.randint(1, 100))
125+
end_time = time.time()
126+
print(f"Dense Connectivity Time: {end_time - start_time:.6f} seconds")
108127
return graph
109128

110129
def connect_clusters(graph, clusters, edge_probability):
130+
start_time = time.time()
111131
for cluster in clusters:
112132
for other_cluster in clusters:
113133
if cluster != other_cluster:
114134
for node1 in cluster:
115135
for node2 in other_cluster:
116136
if random.random() < edge_probability and not graph.has_edge(node1, node2):
117137
graph.add_edge(node1, node2, weight=random.randint(1, 100))
138+
end_time = time.time()
139+
print(f"Cluster Connection Time: {end_time - start_time:.6f} seconds")
140+
141+
def calculate_time_complexity(num_nodes, num_edges):
142+
# Graph Generation Complexity
143+
graph_generation = f"O(n^2 * p)"
144+
145+
# Clustering Complexity
146+
clustering = f"O(n)"
147+
148+
# Gateway Assignment Complexity
149+
gateway_assignment = f"O(n)"
150+
151+
# Precomputation Complexity
152+
precomputation = f"O(n * (n log n + e)) where e = {num_edges}"
153+
154+
# Shortest Path Calculation Complexity
155+
shortest_path_calc = f"O(n log n + e) where e = {num_edges}"
156+
157+
# Overall Time Complexity
158+
overall_time_complexity = f"O(n * (n log n + e))"
159+
160+
return {
161+
"Graph Generation": graph_generation,
162+
"Clustering": clustering,
163+
"Gateway Assignment": gateway_assignment,
164+
"Precomputation": precomputation,
165+
"Shortest Path Calculation": shortest_path_calc,
166+
"Overall Time Complexity": overall_time_complexity
167+
}
118168

119169
if __name__ == "__main__":
120170
num_nodes = 50
@@ -154,5 +204,7 @@ def connect_clusters(graph, clusters, edge_probability):
154204
num_nodes = len(graph.nodes())
155205
num_edges = len(graph.edges())
156206

157-
time_complexity_category = "O(n log n) Time Complexity"
158-
print(f"Estimated time complexity category for LSRA: {time_complexity_category}")
207+
time_complexity = calculate_time_complexity(num_nodes, num_edges)
208+
print("\nTime Complexity Analysis:")
209+
for step, complexity in time_complexity.items():
210+
print(f"{step}: {complexity}")

0 commit comments

Comments
 (0)