Skip to content

Commit 78a5d3a

Browse files
cclaussgithub-actions
and
github-actions
authored
boruvka.py: A few simplifications and f-strings (#4660)
* boruvka.py: A few simplifications and f-strings Python f-strings simplify the code and [should speed up execution](https://www.scivision.dev/python-f-string-speed). @srkchowdary2000 Your review, please. * updating DIRECTORY.md * fixup! Streamline the test Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 4ed7c7f commit 78a5d3a

File tree

2 files changed

+39
-55
lines changed

2 files changed

+39
-55
lines changed

DIRECTORY.md

+10
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
* [Peak Signal To Noise Ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py)
9898

9999
## Computer Vision
100+
* [Cnn Classification](https://github.com/TheAlgorithms/Python/blob/master/computer_vision/cnn_classification.py)
100101
* [Harris Corner](https://github.com/TheAlgorithms/Python/blob/master/computer_vision/harris_corner.py)
101102
* [Mean Threshold](https://github.com/TheAlgorithms/Python/blob/master/computer_vision/mean_threshold.py)
102103

@@ -300,6 +301,7 @@
300301
* [Bfs Zero One Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs_zero_one_shortest_path.py)
301302
* [Bidirectional A Star](https://github.com/TheAlgorithms/Python/blob/master/graphs/bidirectional_a_star.py)
302303
* [Bidirectional Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/bidirectional_breadth_first_search.py)
304+
* [Boruvka](https://github.com/TheAlgorithms/Python/blob/master/graphs/boruvka.py)
303305
* [Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search.py)
304306
* [Breadth First Search 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search_2.py)
305307
* [Breadth First Search Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search_shortest_path.py)
@@ -349,6 +351,7 @@
349351
* [Djb2](https://github.com/TheAlgorithms/Python/blob/master/hashes/djb2.py)
350352
* [Enigma Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/enigma_machine.py)
351353
* [Hamming Code](https://github.com/TheAlgorithms/Python/blob/master/hashes/hamming_code.py)
354+
* [Luhn](https://github.com/TheAlgorithms/Python/blob/master/hashes/luhn.py)
352355
* [Md5](https://github.com/TheAlgorithms/Python/blob/master/hashes/md5.py)
353356
* [Sdbm](https://github.com/TheAlgorithms/Python/blob/master/hashes/sdbm.py)
354357
* [Sha1](https://github.com/TheAlgorithms/Python/blob/master/hashes/sha1.py)
@@ -421,10 +424,12 @@
421424
* [Binomial Distribution](https://github.com/TheAlgorithms/Python/blob/master/maths/binomial_distribution.py)
422425
* [Bisection](https://github.com/TheAlgorithms/Python/blob/master/maths/bisection.py)
423426
* [Ceil](https://github.com/TheAlgorithms/Python/blob/master/maths/ceil.py)
427+
* [Check Valid Ip Address](https://github.com/TheAlgorithms/Python/blob/master/maths/check_valid_ip_address.py)
424428
* [Chudnovsky Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/chudnovsky_algorithm.py)
425429
* [Collatz Sequence](https://github.com/TheAlgorithms/Python/blob/master/maths/collatz_sequence.py)
426430
* [Combinations](https://github.com/TheAlgorithms/Python/blob/master/maths/combinations.py)
427431
* [Decimal Isolate](https://github.com/TheAlgorithms/Python/blob/master/maths/decimal_isolate.py)
432+
* [Double Factorial Recursive](https://github.com/TheAlgorithms/Python/blob/master/maths/double_factorial_recursive.py)
428433
* [Entropy](https://github.com/TheAlgorithms/Python/blob/master/maths/entropy.py)
429434
* [Euclidean Distance](https://github.com/TheAlgorithms/Python/blob/master/maths/euclidean_distance.py)
430435
* [Euclidean Gcd](https://github.com/TheAlgorithms/Python/blob/master/maths/euclidean_gcd.py)
@@ -539,6 +544,7 @@
539544

540545
## Other
541546
* [Activity Selection](https://github.com/TheAlgorithms/Python/blob/master/other/activity_selection.py)
547+
* [Date To Weekday](https://github.com/TheAlgorithms/Python/blob/master/other/date_to_weekday.py)
542548
* [Davis–Putnam–Logemann–Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davis–putnam–logemann–loveland.py)
543549
* [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py)
544550
* [Doomsday](https://github.com/TheAlgorithms/Python/blob/master/other/doomsday.py)
@@ -854,6 +860,7 @@
854860
* [Counting Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/counting_sort.py)
855861
* [Cycle Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/cycle_sort.py)
856862
* [Double Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/double_sort.py)
863+
* [Exchange Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/exchange_sort.py)
857864
* [External Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/external_sort.py)
858865
* [Gnome Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/gnome_sort.py)
859866
* [Heap Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/heap_sort.py)
@@ -893,6 +900,7 @@
893900

894901
## Strings
895902
* [Aho Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho_corasick.py)
903+
* [Alternative String Arrange](https://github.com/TheAlgorithms/Python/blob/master/strings/alternative_string_arrange.py)
896904
* [Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/anagrams.py)
897905
* [Autocomplete Using Trie](https://github.com/TheAlgorithms/Python/blob/master/strings/autocomplete_using_trie.py)
898906
* [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py)
@@ -902,6 +910,7 @@
902910
* [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py)
903911
* [Detecting English Programmatically](https://github.com/TheAlgorithms/Python/blob/master/strings/detecting_english_programmatically.py)
904912
* [Frequency Finder](https://github.com/TheAlgorithms/Python/blob/master/strings/frequency_finder.py)
913+
* [Indian Phone Validator](https://github.com/TheAlgorithms/Python/blob/master/strings/indian_phone_validator.py)
905914
* [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/is_palindrome.py)
906915
* [Jaro Winkler](https://github.com/TheAlgorithms/Python/blob/master/strings/jaro_winkler.py)
907916
* [Knuth Morris Pratt](https://github.com/TheAlgorithms/Python/blob/master/strings/knuth_morris_pratt.py)
@@ -941,6 +950,7 @@
941950
* [Instagram Crawler](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_crawler.py)
942951
* [Instagram Pic](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_pic.py)
943952
* [Instagram Video](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_video.py)
953+
* [Random Anime Character](https://github.com/TheAlgorithms/Python/blob/master/web_programming/random_anime_character.py)
944954
* [Recaptcha Verification](https://github.com/TheAlgorithms/Python/blob/master/web_programming/recaptcha_verification.py)
945955
* [Slack Message](https://github.com/TheAlgorithms/Python/blob/master/web_programming/slack_message.py)
946956
* [Test Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/test_fetch_github_info.py)

graphs/boruvka.py

+29-55
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""Borůvka's algorithm.
22
3-
Determines the minimum spanning tree(MST) of a graph using the Borůvka's algorithm.
3+
Determines the minimum spanning tree (MST) of a graph using the Borůvka's algorithm.
44
Borůvka's algorithm is a greedy algorithm for finding a minimum spanning tree in a
5-
graph,or a minimum spanning forest in the case of a graph that is not connected.
5+
connected graph, or a minimum spanning forest if a graph that is not connected.
66
77
The time complexity of this algorithm is O(ELogV), where E represents the number
88
of edges, while V represents the number of nodes.
9+
O(number_of_edges Log number_of_nodes)
910
1011
The space complexity of this algorithm is O(V + E), since we have to keep a couple
1112
of lists whose sizes are equal to the number of nodes, as well as keep all the
@@ -19,7 +20,7 @@
1920
doesn't need to presort the edges or maintain a priority queue in order to find the
2021
minimum spanning tree.
2122
Even though that doesn't help its complexity, since it still passes the edges logE
22-
times, it is a bit more simple to code.
23+
times, it is a bit simpler to code.
2324
2425
Details: https://en.wikipedia.org/wiki/Bor%C5%AFvka%27s_algorithm
2526
"""
@@ -31,13 +32,13 @@ def __init__(self, num_of_nodes: int) -> None:
3132
Arguments:
3233
num_of_nodes - the number of nodes in the graph
3334
Attributes:
34-
m_v - the number of nodes in the graph.
35+
m_num_of_nodes - the number of nodes in the graph.
3536
m_edges - the list of edges.
3637
m_component - the dictionary which stores the index of the component which
3738
a node belongs to.
3839
"""
3940

40-
self.m_v = num_of_nodes
41+
self.m_num_of_nodes = num_of_nodes
4142
self.m_edges = []
4243
self.m_component = {}
4344

@@ -57,7 +58,7 @@ def set_component(self, u_node: int) -> None:
5758
"""Finds the component index of a given node"""
5859

5960
if self.m_component[u_node] != u_node:
60-
for k in self.m_component.keys():
61+
for k in self.m_component:
6162
self.m_component[k] = self.find_component(k)
6263

6364
def union(self, component_size: list, u_node: int, v_node: int) -> None:
@@ -82,22 +83,18 @@ def boruvka(self) -> None:
8283
component_size = []
8384
mst_weight = 0
8485

85-
minimum_weight_edge = [-1] * self.m_v
86+
minimum_weight_edge = [-1] * self.m_num_of_nodes
8687

8788
# A list of components (initialized to all of the nodes)
88-
for node in range(self.m_v):
89+
for node in range(self.m_num_of_nodes):
8990
self.m_component.update({node: node})
9091
component_size.append(1)
9192

92-
num_of_components = self.m_v
93+
num_of_components = self.m_num_of_nodes
9394

9495
while num_of_components > 1:
95-
l_edges = len(self.m_edges)
96-
for i in range(l_edges):
97-
98-
u = self.m_edges[i][0]
99-
v = self.m_edges[i][1]
100-
w = self.m_edges[i][2]
96+
for edge in self.m_edges:
97+
u, v, w = edge
10198

10299
u_component = self.m_component[u]
103100
v_component = self.m_component[v]
@@ -113,59 +110,36 @@ def boruvka(self) -> None:
113110
observing right now, we will assign the value of the edge
114111
we're observing to it"""
115112

116-
if (
117-
minimum_weight_edge[u_component] == -1
118-
or minimum_weight_edge[u_component][2] > w
119-
):
120-
minimum_weight_edge[u_component] = [u, v, w]
121-
if (
122-
minimum_weight_edge[v_component] == -1
123-
or minimum_weight_edge[v_component][2] > w
124-
):
125-
minimum_weight_edge[v_component] = [u, v, w]
126-
127-
for node in range(self.m_v):
128-
if minimum_weight_edge[node] != -1:
129-
u = minimum_weight_edge[node][0]
130-
v = minimum_weight_edge[node][1]
131-
w = minimum_weight_edge[node][2]
113+
for component in (u_component, v_component):
114+
if (
115+
minimum_weight_edge[component] == -1
116+
or minimum_weight_edge[component][2] > w
117+
):
118+
minimum_weight_edge[component] = [u, v, w]
119+
120+
for edge in minimum_weight_edge:
121+
if edge != -1:
122+
u, v, w = edge
132123

133124
u_component = self.m_component[u]
134125
v_component = self.m_component[v]
135126

136127
if u_component != v_component:
137128
mst_weight += w
138129
self.union(component_size, u_component, v_component)
139-
print(
140-
"Added edge ["
141-
+ str(u)
142-
+ " - "
143-
+ str(v)
144-
+ "]\n"
145-
+ "Added weight: "
146-
+ str(w)
147-
+ "\n"
148-
)
130+
print(f"Added edge [{u} - {v}]\nAdded weight: {w}\n")
149131
num_of_components -= 1
150132

151-
minimum_weight_edge = [-1] * self.m_v
152-
print("The total weight of the minimal spanning tree is: " + str(mst_weight))
133+
minimum_weight_edge = [-1] * self.m_num_of_nodes
134+
print(f"The total weight of the minimal spanning tree is: {mst_weight}")
153135

154136

155137
def test_vector() -> None:
156138
"""
157-
>>> g=Graph(8)
158-
>>> g.add_edge(0, 1, 10)
159-
>>> g.add_edge(0, 2, 6)
160-
>>> g.add_edge(0, 3, 5)
161-
>>> g.add_edge(1, 3, 15)
162-
>>> g.add_edge(2, 3, 4)
163-
>>> g.add_edge(3, 4, 8)
164-
>>> g.add_edge(4, 5, 10)
165-
>>> g.add_edge(4, 6, 6)
166-
>>> g.add_edge(4, 7, 5)
167-
>>> g.add_edge(5, 7, 15)
168-
>>> g.add_edge(6, 7, 4)
139+
>>> g = Graph(8)
140+
>>> for u_v_w in ((0, 1, 10), (0, 2, 6), (0, 3, 5), (1, 3, 15), (2, 3, 4),
141+
... (3, 4, 8), (4, 5, 10), (4, 6, 6), (4, 7, 5), (5, 7, 15), (6, 7, 4)):
142+
... g.add_edge(*u_v_w)
169143
>>> g.boruvka()
170144
Added edge [0 - 3]
171145
Added weight: 5

0 commit comments

Comments
 (0)