Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 40371aa

Browse files
committedAug 13, 2020
pull from upstream repo
2 parents d5a446e + d687030 commit 40371aa

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed
 

‎DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
* [Karger](https://github.com/TheAlgorithms/Python/blob/master/graphs/karger.py)
268268
* [Minimum Spanning Tree Boruvka](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_boruvka.py)
269269
* [Minimum Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal.py)
270+
* [Minimum Spanning Tree Kruskal2](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal2.py)
270271
* [Minimum Spanning Tree Prims](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_prims.py)
271272
* [Multi Heuristic Astar](https://github.com/TheAlgorithms/Python/blob/master/graphs/multi_heuristic_astar.py)
272273
* [Page Rank](https://github.com/TheAlgorithms/Python/blob/master/graphs/page_rank.py)

‎data_structures/binary_tree/basic_binary_tree.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Node:
55
"""
66
A Node has data variable and pointers to Nodes to its left and right.
77
"""
8+
89
def __init__(self, data: int) -> None:
910
self.data = data
1011
self.left: Optional[Node] = None
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
from __future__ import annotations
2+
3+
4+
class DisjointSetTreeNode:
5+
# Disjoint Set Node to store the parent and rank
6+
def __init__(self, key: int) -> None:
7+
self.key = key
8+
self.parent = self
9+
self.rank = 0
10+
11+
12+
class DisjointSetTree:
13+
# Disjoint Set DataStructure
14+
def __init__(self):
15+
# map from node name to the node object
16+
self.map = {}
17+
18+
def make_set(self, x: int) -> None:
19+
# create a new set with x as its member
20+
self.map[x] = DisjointSetTreeNode(x)
21+
22+
def find_set(self, x: int) -> DisjointSetTreeNode:
23+
# find the set x belongs to (with path-compression)
24+
elem_ref = self.map[x]
25+
if elem_ref != elem_ref.parent:
26+
elem_ref.parent = self.find_set(elem_ref.parent.key)
27+
return elem_ref.parent
28+
29+
def link(self, x: int, y: int) -> None:
30+
# helper function for union operation
31+
if x.rank > y.rank:
32+
y.parent = x
33+
else:
34+
x.parent = y
35+
if x.rank == y.rank:
36+
y.rank += 1
37+
38+
def union(self, x: int, y: int) -> None:
39+
# merge 2 disjoint sets
40+
self.link(self.find_set(x), self.find_set(y))
41+
42+
43+
class GraphUndirectedWeighted:
44+
def __init__(self):
45+
# connections: map from the node to the neighbouring nodes (with weights)
46+
self.connections = {}
47+
48+
def add_node(self, node: int) -> None:
49+
# add a node ONLY if its not present in the graph
50+
if node not in self.connections:
51+
self.connections[node] = {}
52+
53+
def add_edge(self, node1: int, node2: int, weight: int) -> None:
54+
# add an edge with the given weight
55+
self.add_node(node1)
56+
self.add_node(node2)
57+
self.connections[node1][node2] = weight
58+
self.connections[node2][node1] = weight
59+
60+
def kruskal(self) -> GraphUndirectedWeighted:
61+
# Kruskal's Algorithm to generate a Minimum Spanning Tree (MST) of a graph
62+
"""
63+
Details: https://en.wikipedia.org/wiki/Kruskal%27s_algorithm
64+
65+
Example:
66+
67+
>>> graph = GraphUndirectedWeighted()
68+
>>> graph.add_edge(1, 2, 1)
69+
>>> graph.add_edge(2, 3, 2)
70+
>>> graph.add_edge(3, 4, 1)
71+
>>> graph.add_edge(3, 5, 100) # Removed in MST
72+
>>> graph.add_edge(4, 5, 5)
73+
>>> assert 5 in graph.connections[3]
74+
>>> mst = graph.kruskal()
75+
>>> assert 5 not in mst.connections[3]
76+
"""
77+
78+
# getting the edges in ascending order of weights
79+
edges = []
80+
seen = set()
81+
for start in self.connections:
82+
for end in self.connections[start]:
83+
if (start, end) not in seen:
84+
seen.add((end, start))
85+
edges.append((start, end, self.connections[start][end]))
86+
edges.sort(key=lambda x: x[2])
87+
# creating the disjoint set
88+
disjoint_set = DisjointSetTree()
89+
[disjoint_set.make_set(node) for node in self.connections]
90+
# MST generation
91+
num_edges = 0
92+
index = 0
93+
graph = GraphUndirectedWeighted()
94+
while num_edges < len(self.connections) - 1:
95+
u, v, w = edges[index]
96+
index += 1
97+
parentu = disjoint_set.find_set(u)
98+
parentv = disjoint_set.find_set(v)
99+
if parentu != parentv:
100+
num_edges += 1
101+
graph.add_edge(u, v, w)
102+
disjoint_set.union(u, v)
103+
return graph
104+
105+
106+
if __name__ == "__main__":
107+
import doctest
108+
109+
doctest.testmod()

‎maths/number_of_digits.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ def num_digits(n: int) -> int:
1010
5
1111
>>> num_digits(123)
1212
3
13+
>>> num_digits(0)
14+
1
15+
>>> num_digits(-1)
16+
1
17+
>>> num_digits(-123456)
18+
6
1319
"""
1420
digits = 0
15-
while n > 0:
21+
n = abs(n)
22+
while True:
1623
n = n // 10
1724
digits += 1
25+
if n == 0:
26+
break
1827
return digits
1928

2029

@@ -27,8 +36,14 @@ def num_digits_fast(n: int) -> int:
2736
5
2837
>>> num_digits_fast(123)
2938
3
39+
>>> num_digits_fast(0)
40+
1
41+
>>> num_digits_fast(-1)
42+
1
43+
>>> num_digits_fast(-123456)
44+
6
3045
"""
31-
return math.floor(math.log(abs(n), 10) + 1)
46+
return 1 if n == 0 else math.floor(math.log(abs(n), 10) + 1)
3247

3348

3449
def num_digits_faster(n: int) -> int:
@@ -40,6 +55,12 @@ def num_digits_faster(n: int) -> int:
4055
5
4156
>>> num_digits_faster(123)
4257
3
58+
>>> num_digits_faster(0)
59+
1
60+
>>> num_digits_faster(-1)
61+
1
62+
>>> num_digits_faster(-123456)
63+
6
4364
"""
4465
return len(str(abs(n)))
4566

@@ -133,3 +154,6 @@ def benchmark() -> None:
133154
medium_num = 1125899906842624
134155
large_num = 1267650600228229401496703205376
135156
benchmark()
157+
import doctest
158+
159+
doctest.testmod()

0 commit comments

Comments
 (0)
Please sign in to comment.