Graphs/kruskal: adding doctest & type hints (#3101)

* graphs/kruskal: add doctest & type hints

this is a child of a previous PR #2443

its ancestor is #2128

* updating DIRECTORY.md

* graphs/kruskal: fix max-line-length violation

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Meysam 2020-10-15 22:38:52 +03:30 committed by GitHub
parent 316fc41d6d
commit 83b825027e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,18 @@
def kruskal(num_nodes, num_edges, edges): from typing import List, Tuple
def kruskal(num_nodes: int, num_edges: int, edges: List[Tuple[int, int, int]]) -> int:
"""
>>> kruskal(4, 3, [(0, 1, 3), (1, 2, 5), (2, 3, 1)])
[(2, 3, 1), (0, 1, 3), (1, 2, 5)]
>>> kruskal(4, 5, [(0, 1, 3), (1, 2, 5), (2, 3, 1), (0, 2, 1), (0, 3, 2)])
[(2, 3, 1), (0, 2, 1), (0, 1, 3)]
>>> kruskal(4, 6, [(0, 1, 3), (1, 2, 5), (2, 3, 1), (0, 2, 1), (0, 3, 2),
... (2, 1, 1)])
[(2, 3, 1), (0, 2, 1), (2, 1, 1)]
"""
edges = sorted(edges, key=lambda edge: edge[2]) edges = sorted(edges, key=lambda edge: edge[2])
parent = list(range(num_nodes)) parent = list(range(num_nodes))