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 1 commit
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
19 changes: 11 additions & 8 deletions graphs/minimum_spanning_tree_kruskal2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations


class Disjoint_Set_Tree_Node:
# Disjoint Set Node to store the parent and rank
def __init__(self, key):
def __init__(self, key: int) -> None:
self.key = key
self.parent = self
self.rank = 0
Expand All @@ -12,17 +15,17 @@ def __init__(self):
# map from node name to the node object
self.map = {}

def make_set(self, x):
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):
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

def link(self, x, y):
def link(self, x, y) -> None:
# helper function for union operation
if x.rank > y.rank:
y.parent = x
Expand All @@ -31,7 +34,7 @@ def link(self, x, y):
if x.rank == y.rank:
y.rank += 1

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

Expand All @@ -43,20 +46,20 @@ def __init__(self):
self.connections = {}
self.nodes = 0

def add_node(self, node):
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, node2, weight):
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):
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
Expand Down