Skip to content

Commit

Permalink
Improved time complexity of make_connected.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Engström committed May 11, 2024
1 parent 9fa13bb commit 2ce9277
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/graph_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ def make_connected(self, cost):
number_of_components = len(components)
connected_components = 0

print(f"Components to connect: {len(components)}")
for component in components:
component = list(component)
if component is main_comp:
Expand All @@ -335,11 +336,12 @@ def make_connected(self, cost):
city2 = random.randint(0, len(component) - 1)
self.connect(component[city2], main_comp[city1], cost=cost)
else:
city2 = random.randint(0, len(component) - 1)
closest_big_city = sorted(list(main_comp), key=lambda city1: distance(city1, component[city2]))[0]
self.connect(component[city2], closest_big_city, cost=cost)
city1 = random.randint(0, len(component) - 1)
city2 = find_closest(component[city1], main_comp)
self.connect(component[city1], city2, cost=cost)

connected_components += 1
print(f"Connected components: {connected_components}/{number_of_components}")

self.log(f"Created {connected_components} new edges to make the graph connected")

Expand Down

0 comments on commit 2ce9277

Please sign in to comment.