Skip to content

Added Kruskal's Algorithm (more organized than the one present) #2218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 12, 2020
Merged
Changes from 2 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
111 changes: 111 additions & 0 deletions graphs/minimum_spanning_tree_kruskal2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from __future__ import annotations


class Disjoint_Set_Tree_Node:
# Disjoint Set Node to store the parent and rank
def __init__(self, key: int) -> None:
self.key = key
self.parent = self
self.rank = 0


class Disjoint_Set_Tree:
# Disjoint Set DataStructure
def __init__(self):
# map from node name to the node object
self.map = {}

def make_set(self, x) -> None:
# create a new set with x as its member
self.map[x] = Disjoint_Set_Tree_Node(x)

def find_set(self, x: int) -> Disjoint_Set_Tree_Node:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed the type hints in a few places, will add them

# find the set x belongs to (with path-compression)
if self.map[x] != self.map[x].parent:
self.map[x].parent = self.find_set(self.map[x].parent.key)
return self.map[x].parent
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self.map[x] != self.map[x].parent:
self.map[x].parent = self.find_set(self.map[x].parent.key)
return self.map[x].parent
self_map_x = self.map[x]
if self_map_x != self_map_x.parent:
self_map_x.parent = self.find_set(self_map_x.parent.key)
return self_map_x.parent

OPTIONAL: You might gain a bit of performance...
https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Loops

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm keeping it as list comprehension, converting

[disjoint_set.make_set(node) for node in self.connections]

to

map(disjoint_set.make_set, self.connections.keys())

yields:

**********************************************************************
File "d:/Desktop/Projects/GitUploads/Python/graphs/minimum_spanning_tree_kruskal2.py", line 77, in __main__.GraphUndirectedWeighted.kruskal
Failed example:
    mst = graph.kruskal()
Exception raised:
    Traceback (most recent call last):
      File "C:\Program Files\Python37\lib\doctest.py", line 1329, in __run
        compileflags, 1), test.globs)
      File "<doctest __main__.GraphUndirectedWeighted.kruskal[7]>", line 1, in <module>
        mst = graph.kruskal()
      File "d:/Desktop/Projects/GitUploads/Python/graphs/minimum_spanning_tree_kruskal2.py", line 100, in kruskal
        parentu = disjoint_set.find_set(u)
      File "d:/Desktop/Projects/GitUploads/Python/graphs/minimum_spanning_tree_kruskal2.py", line 24, in find_set
        elem_ref = self.map[x]
    KeyError: 1
**********************************************************************
File "d:/Desktop/Projects/GitUploads/Python/graphs/minimum_spanning_tree_kruskal2.py", line 78, in __main__.GraphUndirectedWeighted.kruskal
Failed example:
    assert 5 not in mst.connections[3]
Exception raised:
    Traceback (most recent call last):
      File "C:\Program Files\Python37\lib\doctest.py", line 1329, in __run
        compileflags, 1), test.globs)
      File "<doctest __main__.GraphUndirectedWeighted.kruskal[8]>", line 1, in <module>
        assert 5 not in mst.connections[3]
    NameError: name 'mst' is not defined
**********************************************************************
1 items had failures:
   2 of   9 in __main__.GraphUndirectedWeighted.kruskal
***Test Failed*** 2 failures.


def link(self, x, y) -> None:
# helper function for union operation
if x.rank > y.rank:
y.parent = x
else:
x.parent = y
if x.rank == y.rank:
y.rank += 1

def union(self, x, y) -> None:
# merge 2 disjoint sets
self.link(self.find_set(x), self.find_set(y))


class GraphUndirectedWeighted:
def __init__(self):
# connections: map from the node to the neighbouring nodes (with weights)
# nodes: counting the number of nodes in the graph
self.connections = {}
self.nodes = 0

def add_node(self, node: int) -> None:
# add a node ONLY if its not present in the graph
if node not in self.connections:
self.connections[node] = {}
self.nodes += 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what circumstances is self.nodes != len(self.connections)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah its always same. Should I remove it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your call. You already have my approval so you can squash & merge whenever you want (as long as the tests are green). Well done!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the changes, but I cannot merge since I'm not a member


def add_edge(self, node1: int, node2: int, weight: int) -> None:
# add an edge with the given weight
self.add_node(node1)
self.add_node(node2)
self.connections[node1][node2] = weight
self.connections[node2][node1] = weight

def kruskal(self) -> GraphUndirectedWeighted:
# Kruskal's Algorithm to generate a Minimum Spanning Tree (MST) of a graph
"""
Details: https://en.wikipedia.org/wiki/Kruskal%27s_algorithm

Example:

>>> graph = GraphUndirectedWeighted()
>>> graph.add_edge(1, 2, 1)
>>> graph.add_edge(2, 3, 2)
>>> graph.add_edge(3, 4, 1)
>>> graph.add_edge(3, 5, 100) # Removed in MST
>>> graph.add_edge(4, 5, 5)
>>> assert 5 in graph.connections[3]
>>> mst = graph.kruskal()
>>> assert 5 not in mst.connections[3]
"""

# getting the edges in ascending order of weights
edges = []
seen = set()
for start in self.connections:
for end in self.connections[start]:
if (start, end) not in seen:
seen.add((end, start))
edges.append((start, end, self.connections[start][end]))
edges.sort(key=lambda x: x[2])
# creating the disjoint set
disjoint_set = Disjoint_Set_Tree()
[disjoint_set.make_set(node) for node in self.connections]
# MST generation
num_edges = 0
index = 0
graph = GraphUndirectedWeighted()
while num_edges < self.nodes - 1:
u, v, w = edges[index]
index += 1
parentu = disjoint_set.find_set(u)
parentv = disjoint_set.find_set(v)
if parentu != parentv:
num_edges += 1
graph.add_edge(u, v, w)
disjoint_set.union(u, v)
return graph


if __name__ == "__main__":
import doctest

doctest.testmod()