Skip to content

Commit

Permalink
Graphs/kruskal: adding doctest & type hints (TheAlgorithms#3101)
Browse files Browse the repository at this point in the history
* graphs/kruskal: add doctest & type hints

this is a child of a previous PR TheAlgorithms#2443

its ancestor is TheAlgorithms#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>
  • Loading branch information
meysam81 and github-actions authored Oct 15, 2020
1 parent 316fc41 commit 83b8250
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion graphs/minimum_spanning_tree_kruskal.py
Original file line number Diff line number Diff line change
@@ -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))
Expand Down

0 comments on commit 83b8250

Please sign in to comment.