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
103 changes: 103 additions & 0 deletions Python/Boruvkas Algorithm/Boruvkas_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
class Graph:
def __init__(self, vertices):
self.Destination = vertices
self.edges = []
self.component = {}
def add_edge(self, Source, Destination, weight):
self.edges.append([Source, Destination, weight])
def find_component(self, Source):
if self.component[Source] == Source:
return Source
return self.find_component(self.component[Source])

def set_component(self, Source):
if self.component[Source] == Source:
return
else:
for k in self.component.keys():
self.component[k] = self.find_component(k)

def union(self, component_size, Source, Destination):
if component_size[Source] <= component_size[Destination]:
self.component[Source] = Destination
component_size[Destination] += component_size[Source]
self.set_component(Source)

elif component_size[Source] >= component_size[Destination]:
self.component[Destination] = self.find_component(Source)
component_size[Source] += component_size[Destination]
self.set_component(Destination)

print(self.component)

def boruvka(self):
component_size = []
mst_weight = 0

minimum_weight_edge = [-1] * self.Destination

for node in range(self.Destination):
self.component.update({node: node})
component_size.append(1)

num_of_components = self.Destination

print("---------Forming MST------------")
while num_of_components > 1:
for i in range(len(self.edges)):

Source = self.edges[i][0]
Destination = self.edges[i][1]
w = self.edges[i][2]

Source_component = self.component[Source]
Destination_component = self.component[Destination]

if Source_component != Destination_component:
if minimum_weight_edge[Source_component] == -1 or \
minimum_weight_edge[Source_component][2] > w:
minimum_weight_edge[Source_component] = [Source, Destination, w]
if minimum_weight_edge[Destination_component] == -1 or \
minimum_weight_edge[Destination_component][2] > w:
minimum_weight_edge[Destination_component] = [Source, Destination, w]

for node in range(self.Destination):
if minimum_weight_edge[node] != -1:
Source = minimum_weight_edge[node][0]
Destination = minimum_weight_edge[node][1]
w = minimum_weight_edge[node][2]

Source_component = self.component[Source]
Destination_component = self.component[Destination]

if Source_component != Destination_component:
mst_weight += w
self.union(component_size, Source_component, Destination_component)
print("edge_Added [" + str(Source) + " - "
+ str(Destination) + "]\n"
+ "weight_Added: " + str(w) + "\n")
num_of_components -= 1

minimum_weight_edge = [-1] * self.Destination
print("-------------Hacktoberfest2022---------------------")
print("The minimum spanning tree is overall weighed is: " + str(mst_weight))

g = Graph(9)
g.add_edge(0, 1, 4)
g.add_edge(0, 6, 7)
g.add_edge(1, 6, 11)
g.add_edge(1, 7, 20)
g.add_edge(1, 2, 9)
g.add_edge(2, 3, 6)
g.add_edge(2, 4, 2)
g.add_edge(3, 4, 10)
g.add_edge(3, 5, 5)
g.add_edge(4, 5, 15)
g.add_edge(4, 7, 1)
g.add_edge(4, 8, 5)
g.add_edge(5, 8, 12)
g.add_edge(6, 7, 1)
g.add_edge(7, 8, 3)
g.boruvka()

#by: Max Muller
10 changes: 10 additions & 0 deletions Python/Boruvkas Algorithm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#Boruvka’s Algorithm in Python

We will learn how to code Boruvka’s Algorithm in Python. Algorithms are at the heart of computer science. They are expressed as a finite sequence of operations with well-defined input and output. Boruvka’s approach works by starting with nodes from the input network and growing that forest by adding minimal-weight edges from between its linked components until it becomes a minimum spanning tree of the input graph. Boruvka’s technique has the benefit of not requiring sophisticated data structures to achieve the time complexity constraint. In addition, we are implementing Boruvka’s Algorithm in the Python programming language.
In this article, we demonstrated a Python version of Boruvka’s algorithms. The algorithms are represented by classes, and graph objects are handled using the suggested graph interface. In some aspects, the provided implementation is unique. The source code is comprehensible in the same way that pseudocode from textbooks or scientific publications is. On the other hand, the code can be run with the efficiency specified by the associated theory. Python’s class system adds classes with little additional syntax, and it is simple to design desired data structures (e.g., an edge, a graph, a union-find data structure) or to use objects from standard modules (e.g., queues, stacks).

[Gravação de tela de 23-10-2022 15:19:14.webm](https://user-images.githubusercontent.com/89359847/197409023-8b7b582e-4b79-4db2-9375-566bb603921d.webm)


![Captura de tela de 2022-10-23 15-18-46](https://user-images.githubusercontent.com/89359847/197409032-47611709-fd1c-4997-8b90-d3f46503180c.png)