Skip to content

Commit d8b6132

Browse files
Added Borůvka's algorithm, a graph algorithm that finds the minimum spanning tree.
1 parent 89cf0a4 commit d8b6132

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

graphs/boruvka.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Borůvka's algorithm is a greedy algorithm for finding a minimum spanning tree in a graph,
55
or a minimum spanning forest in the case of a graph that is not connected.
66
7-
The time complexity of this algorithm is O(ElogV), where E represents the number of edges,
7+
The time complexity of this algorithm is O(ELogV), where E represents the number of edges,
88
while V represents the number of nodes.
99
1010
The space complexity of this algorithm is O(V + E), since we have to keep a couple of lists whose sizes are equal
@@ -39,40 +39,40 @@ def __init__(self, num_of_nodes: int) -> None:
3939
self.m_edges = []
4040
self.m_component = {}
4141

42-
def add_edge(self, u: int, v: int, weight: int) -> None:
42+
def add_edge(self, u_node: int, v_node: int, weight: int) -> None:
4343
"""Adds an edge in the format [first, second, edge weight] to graph."""
4444

45-
self.m_edges.append([u, v, weight])
45+
self.m_edges.append([u_node, v_node, weight])
4646

47-
def find_component(self, u: int) -> int:
47+
def find_component(self, u_node: int) -> int:
4848
"""Propagates a new component throughout a given component."""
4949

50-
if self.m_component[u] == u:
51-
return u
52-
return self.find_component(self.m_component[u])
50+
if self.m_component[u_node] == u_node:
51+
return u_node
52+
return self.find_component(self.m_component[u_node])
5353

54-
def set_component(self, u: int) -> None:
54+
def set_component(self, u_node: int) -> None:
5555
"""Finds the component index of a given node"""
5656

57-
if self.m_component[u] == u:
57+
if self.m_component[u_node] == u_node:
5858
return
5959
else:
6060
for k in self.m_component.keys():
6161
self.m_component[k] = self.find_component(k)
6262

63-
def union(self, component_size: list, u: int, v: int) -> None:
63+
def union(self, component_size: list, u_node: int, v_node: int) -> None:
6464
"""Union finds the roots of components for two nodes, compares the components in terms of size,
6565
and attaches the smaller one to the larger one to form single component """
6666

67-
if component_size[u] <= component_size[v]:
68-
self.m_component[u] = v
69-
component_size[v] += component_size[u]
70-
self.set_component(u)
67+
if component_size[u_node] <= component_size[v_node]:
68+
self.m_component[u_node] = v_node
69+
component_size[v_node] += component_size[u_node]
70+
self.set_component(u_node)
7171

72-
elif component_size[u] >= component_size[v]:
73-
self.m_component[v] = self.find_component(u)
74-
component_size[u] += component_size[v]
75-
self.set_component(v)
72+
elif component_size[u_node] >= component_size[v_node]:
73+
self.m_component[v_node] = self.find_component(u_node)
74+
component_size[u_node] += component_size[v_node]
75+
self.set_component(v_node)
7676

7777
print(self.m_component)
7878

0 commit comments

Comments
 (0)