Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions graphs/minimum_spanning_tree_boruvka.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ def boruvka_mst(graph) -> "Graph":
union_find = Graph.UnionFind()
mst_edges = []
while num_components > 1:
cheap_edge = {}
for vertex in graph.get_vertices():
cheap_edge[vertex] = -1
cheap_edge: dict[int, list[int] | None] = dict.fromkeys(
graph.get_vertices()
)

edges = graph.get_edges()
for edge in edges:
Expand All @@ -180,13 +180,12 @@ def boruvka_mst(graph) -> "Graph":
set1 = union_find.find(head)
set2 = union_find.find(tail)
if set1 != set2:
if cheap_edge[set1] == -1 or cheap_edge[set1][2] > weight:
cheap_edge[set1] = [head, tail, weight]

if cheap_edge[set2] == -1 or cheap_edge[set2][2] > weight:
cheap_edge[set2] = [head, tail, weight]
for component in (set1, set2):
current = cheap_edge[component]
if current is None or current[2] > weight:
cheap_edge[component] = [head, tail, weight]
for head_tail_weight in cheap_edge.values():
if head_tail_weight != -1:
if head_tail_weight is not None:
head, tail, weight = head_tail_weight
if union_find.find(head) != union_find.find(tail):
union_find.union(head, tail)
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ rules.deprecated = "ignore"
rules.invalid-argument-type = "ignore"
rules.invalid-return-type = "ignore"
rules.no-matching-overload = "ignore"
rules.not-iterable = "ignore"
rules.not-subscriptable = "ignore"
rules.parameter-already-assigned = "ignore"
rules.unresolved-attribute = "ignore"
Expand Down
Loading