-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chap14: prim-janik, kruskal and disjoint sets
- Loading branch information
1 parent
950f5d0
commit 8700a4e
Showing
7 changed files
with
170 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__package__ = '' | ||
__package__ = 'python_algo_ds' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class Partition: | ||
"""Union-find structure for maintaining disjoint sets""" | ||
|
||
# ---- Nested Position class ---- | ||
|
||
class Position: | ||
__slots__ = '_container', '_element', '_size', '_parent' | ||
|
||
def __init__(self, container, e): | ||
"""Create a new position that is the leader of its own group | ||
""" | ||
self._container = container | ||
self._element = e | ||
self._size = 1 | ||
self._parent = self # convention for group leader | ||
|
||
def element(self): | ||
return self._element | ||
|
||
def make_group(self, e): | ||
"""Makes a new group containing element e, and returns its Position""" | ||
return self.Position(self, e) | ||
|
||
def find(self, p): | ||
"""Finds the group containing p and return the leader's position""" | ||
if p._parent != p: | ||
# overwrite p._parent after recursion | ||
p._parent = self.find(p._parent) | ||
return p._parent | ||
|
||
def union(self, p, q): | ||
"""Merges the groups containing element p & q (if distinct)""" | ||
a = self.find(p) | ||
b = self.find(q) | ||
|
||
if a is not b: | ||
if a._size > b._size: | ||
b._parent = a | ||
a._size += b._size | ||
else: | ||
a._parent = b | ||
b._size += a._size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__package__ = 'chap9' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters