From 08808cb8d5f24d9ddfeb0e9b69039773e7653936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20M=C3=BCller?= <89359847+MMVonnSeek@users.noreply.github.com> Date: Sun, 23 Oct 2022 15:14:16 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Boruvka=E2=80=99s=20Algorithm=20in=20Python?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We will learn how to code Boruvka’s Algorithm in Python. Algorithms are at the heart of computer science. --- .../Boruvkas Algorithm/Boruvkas_algorithm.py | 103 ++++++++++++++++++ Python/Boruvkas Algorithm/README.md | 5 + 2 files changed, 108 insertions(+) create mode 100644 Python/Boruvkas Algorithm/Boruvkas_algorithm.py create mode 100644 Python/Boruvkas Algorithm/README.md diff --git a/Python/Boruvkas Algorithm/Boruvkas_algorithm.py b/Python/Boruvkas Algorithm/Boruvkas_algorithm.py new file mode 100644 index 000000000..9ee263cb2 --- /dev/null +++ b/Python/Boruvkas Algorithm/Boruvkas_algorithm.py @@ -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 \ No newline at end of file diff --git a/Python/Boruvkas Algorithm/README.md b/Python/Boruvkas Algorithm/README.md new file mode 100644 index 000000000..497864061 --- /dev/null +++ b/Python/Boruvkas Algorithm/README.md @@ -0,0 +1,5 @@ + +#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). \ No newline at end of file From d9230337cb1b97172ec772145ee7eb7c2601779d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20M=C3=BCller?= <89359847+MMVonnSeek@users.noreply.github.com> Date: Sun, 23 Oct 2022 15:21:42 -0300 Subject: [PATCH 2/2] Update README.md --- Python/Boruvkas Algorithm/README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Python/Boruvkas Algorithm/README.md b/Python/Boruvkas Algorithm/README.md index 497864061..801a96080 100644 --- a/Python/Boruvkas Algorithm/README.md +++ b/Python/Boruvkas Algorithm/README.md @@ -2,4 +2,9 @@ #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). \ No newline at end of file +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)