Skip to content

Commit e9af60a

Browse files
Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree. Code Changes.
1 parent f8125d7 commit e9af60a

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

graphs/boruvka.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727

2828
class Graph:
29-
"""Class Graph."""
30-
3129
def __init__(self, num_of_nodes: int) -> None:
3230
"""
3331
Arguments:
@@ -58,9 +56,7 @@ def find_component(self, u_node: int) -> int:
5856
def set_component(self, u_node: int) -> None:
5957
"""Finds the component index of a given node"""
6058

61-
if self.m_component[u_node] == u_node:
62-
return
63-
else:
59+
if self.m_component[u_node] != u_node:
6460
for k in self.m_component.keys():
6561
self.m_component[k] = self.find_component(k)
6662

@@ -79,8 +75,6 @@ def union(self, component_size: list, u_node: int, v_node: int) -> None:
7975
component_size[u_node] += component_size[v_node]
8076
self.set_component(v_node)
8177

82-
print(self.m_component)
83-
8478
def boruvka(self) -> None:
8579
"""Performs Borůvka's algorithm to find MST."""
8680

@@ -98,7 +92,8 @@ def boruvka(self) -> None:
9892
num_of_components = self.m_v
9993

10094
while num_of_components > 1:
101-
for i in range(len(self.m_edges)):
95+
l_edges = len(self.m_edges)
96+
for i in range(l_edges):
10297

10398
u = self.m_edges[i][0]
10499
v = self.m_edges[i][1]
@@ -119,13 +114,13 @@ def boruvka(self) -> None:
119114
we're observing to it"""
120115

121116
if (
122-
minimum_weight_edge[u_component] == -1
123-
or minimum_weight_edge[u_component][2] > w
117+
minimum_weight_edge[u_component] == -1
118+
or minimum_weight_edge[u_component][2] > w
124119
):
125120
minimum_weight_edge[u_component] = [u, v, w]
126121
if (
127-
minimum_weight_edge[v_component] == -1
128-
or minimum_weight_edge[v_component][2] > w
122+
minimum_weight_edge[v_component] == -1
123+
or minimum_weight_edge[v_component][2] > w
129124
):
130125
minimum_weight_edge[v_component] = [u, v, w]
131126

@@ -172,31 +167,24 @@ def test_vector() -> None:
172167
>>> g.add_edge(5, 7, 15)
173168
>>> g.add_edge(6, 7, 4)
174169
>>> g.boruvka()
175-
{0: 3, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}
176170
Added edge [0 - 3]
177171
Added weight: 5
178172
<BLANKLINE>
179-
{0: 3, 1: 3, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}
180173
Added edge [0 - 1]
181174
Added weight: 10
182175
<BLANKLINE>
183-
{0: 3, 1: 3, 2: 3, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}
184176
Added edge [2 - 3]
185177
Added weight: 4
186178
<BLANKLINE>
187-
{0: 3, 1: 3, 2: 3, 3: 3, 4: 7, 5: 5, 6: 6, 7: 7}
188179
Added edge [4 - 7]
189180
Added weight: 5
190181
<BLANKLINE>
191-
{0: 3, 1: 3, 2: 3, 3: 3, 4: 7, 5: 7, 6: 6, 7: 7}
192182
Added edge [4 - 5]
193183
Added weight: 10
194184
<BLANKLINE>
195-
{0: 3, 1: 3, 2: 3, 3: 3, 4: 7, 5: 7, 6: 7, 7: 7}
196185
Added edge [6 - 7]
197186
Added weight: 4
198187
<BLANKLINE>
199-
{0: 7, 1: 7, 2: 7, 3: 7, 4: 7, 5: 7, 6: 7, 7: 7}
200188
Added edge [3 - 4]
201189
Added weight: 8
202190
<BLANKLINE>

0 commit comments

Comments
 (0)