@@ -62,7 +62,7 @@ def set_component(self, u_node: int) -> None:
62
62
63
63
def union (self , component_size : list , u_node : int , v_node : int ) -> None :
64
64
"""Union finds the roots of components for two nodes, compares the components in terms of size,
65
- and attaches the smaller one to the larger one to form single component """
65
+ and attaches the smaller one to the larger one to form single component """
66
66
67
67
if component_size [u_node ] <= component_size [v_node ]:
68
68
self .m_component [u_node ] = v_node
@@ -103,17 +103,23 @@ def boruvka(self) -> None:
103
103
v_component = self .m_component [v ]
104
104
105
105
if u_component != v_component :
106
- """If the current minimum weight edge of component u doesn't exist (is -1), or if
107
- it's greater than the edge we're observing right now, we will assign the value
108
- of the edge we're observing to it.
109
-
110
- If the current minimum weight edge of component v doesn't exist (is -1), or if
111
- it's greater than the edge we're observing right now, we will assign the value
112
- of the edge we're observing to it"""
113
-
114
- if minimum_weight_edge [u_component ] == - 1 or minimum_weight_edge [u_component ][2 ] > w :
106
+ """If the current minimum weight edge of component u doesn't exist (is -1), or if
107
+ it's greater than the edge we're observing right now, we will assign the value
108
+ of the edge we're observing to it.
109
+
110
+ If the current minimum weight edge of component v doesn't exist (is -1), or if
111
+ it's greater than the edge we're observing right now, we will assign the value
112
+ of the edge we're observing to it"""
113
+
114
+ if (
115
+ minimum_weight_edge [u_component ] == - 1
116
+ or minimum_weight_edge [u_component ][2 ] > w
117
+ ):
115
118
minimum_weight_edge [u_component ] = [u , v , w ]
116
- if minimum_weight_edge [v_component ] == - 1 or minimum_weight_edge [v_component ][2 ] > w :
119
+ if (
120
+ minimum_weight_edge [v_component ] == - 1
121
+ or minimum_weight_edge [v_component ][2 ] > w
122
+ ):
117
123
minimum_weight_edge [v_component ] = [u , v , w ]
118
124
119
125
for node in range (self .m_v ):
@@ -128,9 +134,16 @@ def boruvka(self) -> None:
128
134
if u_component != v_component :
129
135
mst_weight += w
130
136
self .union (component_size , u_component , v_component )
131
- print ("Added edge [" + str (u ) + " - "
132
- + str (v ) + "]\n "
133
- + "Added weight: " + str (w ) + "\n " )
137
+ print (
138
+ "Added edge ["
139
+ + str (u )
140
+ + " - "
141
+ + str (v )
142
+ + "]\n "
143
+ + "Added weight: "
144
+ + str (w )
145
+ + "\n "
146
+ )
134
147
num_of_components -= 1
135
148
136
149
minimum_weight_edge = [- 1 ] * self .m_v
0 commit comments