From 83b825027e33c2885b6d9c9f2fd06f848f449f10 Mon Sep 17 00:00:00 2001 From: Meysam Date: Thu, 15 Oct 2020 22:38:52 +0330 Subject: [PATCH] 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> --- graphs/minimum_spanning_tree_kruskal.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/graphs/minimum_spanning_tree_kruskal.py b/graphs/minimum_spanning_tree_kruskal.py index 610baf4b5..a51f97034 100644 --- a/graphs/minimum_spanning_tree_kruskal.py +++ b/graphs/minimum_spanning_tree_kruskal.py @@ -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]) parent = list(range(num_nodes))