Skip to content

Commit 4d775f1

Browse files
committed
added count cluster feature
1 parent 38eb58b commit 4d775f1

File tree

6 files changed

+27
-27
lines changed

6 files changed

+27
-27
lines changed
3 Bytes
Binary file not shown.
403 Bytes
Binary file not shown.

src/app.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# @author: Chinmay Paranjape, Kushal Kaparatti, Prathamesh Chitnis
2-
# @version: v1.2.0
3-
# @date: 13-JUN-2024
2+
# @version: v1.3.0
3+
# @date: 15-JUN-2024
44

55
import base64
66
import io
7-
import os
87
import random
98
import time
109

@@ -19,7 +18,6 @@
1918

2019
app = Flask(__name__)
2120

22-
2321
def plot_graph(graph):
2422
pos = nx.spring_layout(graph)
2523
plt.figure(figsize=(10, 7))
@@ -33,7 +31,6 @@ def plot_graph(graph):
3331
plt.close()
3432
return plot_url
3533

36-
3734
@app.route("/", methods=["GET", "POST"])
3835
def index():
3936
if request.method == "POST":
@@ -55,9 +52,7 @@ def index():
5552

5653
# Optimized Algorithm
5754
graph, graph_gen_time = generate_connected_graph(num_nodes, 0.25)
58-
graph, dense_connectivity_time = ensure_dense_connectivity(
59-
graph, num_nodes, 0.25
60-
)
55+
graph, dense_connectivity_time = ensure_dense_connectivity(graph, num_nodes, 0.25)
6156
lsra_optimized = LSRA_Hierarchical(graph)
6257
cluster_connection_time = connect_clusters(graph, lsra_optimized.clusters, 0.25)
6358
optimized_plot = plot_graph(graph)
@@ -75,9 +70,9 @@ def index():
7570
optimized_plot=optimized_plot,
7671
optimized_shortest_paths=optimized_shortest_paths,
7772
optimized_time=optimized_time,
73+
num_clusters=len(lsra_optimized.clusters)
7874
)
7975
return render_template("index.html")
8076

81-
8277
if __name__ == "__main__":
83-
app.run(debug=True)
78+
app.run(debug=True)

src/lsra_baseline.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @author: Chinmay Paranjape, Kushal Kaparatti, Prathamesh Chitnis
2-
# @version: v1.2.0
3-
# @date: 13-JUN-2024
2+
# @version: v1.3.0
3+
# @date: 15-JUN-2024
44

55
import random
66
import time
@@ -18,22 +18,21 @@ def add_edge(self, node1, node2, weight):
1818

1919
def shortest_path(self, source, target):
2020
# Initialize distances to infinity
21-
distances = {node: float('inf') for node in self.graph.nodes()}
21+
distances = {node: float("inf") for node in self.graph.nodes()}
2222
distances[source] = 0
2323

24-
# Introduce redundant loop to slow down the process
25-
for _ in range(len(self.graph.nodes()) * 2): # Redundant extra iterations
26-
for _ in range(len(self.graph.edges())): # Nested redundant loop
24+
for _ in range(len(self.graph.nodes()) * 2):
25+
for _ in range(len(self.graph.edges())): # Nested loop
2726
pass
2827

29-
# Relax edges repeatedly with additional unnecessary calculations
3028
for _ in range(len(self.graph.nodes()) - 1):
3129
for node1, node2 in self.graph.edges():
32-
distances[node2] = min(distances[node2], distances[node1] + self.graph[node1][node2]['weight'])
33-
# Additional redundant computations
30+
distances[node2] = min(
31+
distances[node2],
32+
distances[node1] + self.graph[node1][node2]["weight"],
33+
)
3434
_ = sum(range(100))
3535

36-
# Additional redundant step to further slow down the process
3736
redundant_list = [random.randint(1, 100) for _ in range(1000)]
3837
redundant_sum = sum(redundant_list)
3938

@@ -57,13 +56,13 @@ def shortest_path(self, source, target):
5756
# Visualization
5857
pos = nx.spring_layout(lsra_baseline.graph)
5958
nx.draw(lsra_baseline.graph, pos, with_labels=True, node_size=700)
60-
edge_labels = nx.get_edge_attributes(lsra_baseline.graph, 'weight')
59+
edge_labels = nx.get_edge_attributes(lsra_baseline.graph, "weight")
6160
nx.draw_networkx_edge_labels(lsra_baseline.graph, pos, edge_labels=edge_labels)
62-
plt.title('Network Graph')
61+
plt.title("Network Graph")
6362
plt.show()
6463

6564
# Calculate shortest paths from source node '0'
66-
source_node = '0'
65+
source_node = "0"
6766
start_time = time.time()
6867
shortest_paths = lsra_baseline.shortest_path(source_node, str(num_nodes - 1))
6968
end_time = time.time()

src/lsra_optimized.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# @author: Chinmay Paranjape, Kushal Kaparatti, Prathamesh Chitnis
2-
# @version: v1.2.0
3-
# @date: 13-JUN-2024
2+
# @version: v1.3.0
3+
# @date: 15-JUN-2024
44

55
import random
66
import time
7+
from math import sqrt
78

89
import matplotlib.pyplot as plt
910
import networkx as nx
@@ -24,7 +25,8 @@ def add_edge(self, node1, node2, weight):
2425

2526
def form_clusters(self):
2627
start_time = time.time()
27-
num_clusters = 5 # Number of clusters
28+
num_nodes = len(self.graph.nodes())
29+
num_clusters = int(sqrt(num_nodes)) # Dynamic number of clusters based on the number of nodes
2830
nodes = list(self.graph.nodes())
2931
random.shuffle(nodes)
3032
cluster_size = len(nodes) // num_clusters
@@ -103,6 +105,9 @@ def find_gateway(self, node):
103105
return gateway
104106
return None
105107

108+
def get_number_of_clusters(self):
109+
return len(self.clusters)
110+
106111
def generate_connected_graph(num_nodes, edge_probability):
107112
start_time = time.time()
108113
graph = nx.Graph()
@@ -201,4 +206,4 @@ def calculate_time_complexity(num_nodes, num_edges, graph_gen_time, cluster_time
201206
print(f"{step} - {value}")
202207
else:
203208
complexity, duration = value
204-
print(f"{step} - Complexity: {complexity}, Time: {duration:.6f} seconds")
209+
print(f"{step} - Complexity: {complexity}, Time: {duration:.6f} seconds")

src/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ <h2>Optimized Algorithm</h2>
2626
<h3>Shortest Paths</h3>
2727
<pre>{{ optimized_shortest_paths }}</pre>
2828
<p>Total time taken: {{ optimized_time }} seconds</p>
29+
<p>Number of Clusters Formed:{{ num_clusters }}</p>
2930
{% endif %}
3031
</body>
3132
</html>

0 commit comments

Comments
 (0)