mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
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:
parent
316fc41d6d
commit
83b825027e
@ -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))
|
||||||
|
Loading…
Reference in New Issue
Block a user