Skip to content

Commit 977511b

Browse files
authored
Add/fix mypy type annotations at BFS, DFS in graphs (TheAlgorithms#4488)
1 parent c824b90 commit 977511b

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

graphs/breadth_first_search.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
""" Author: OMKAR PATHAK """
44

5-
from typing import Set
5+
from typing import Dict, List, Set
66

77

88
class Graph:
99
def __init__(self) -> None:
10-
self.vertices = {}
10+
self.vertices: Dict[int, List[int]] = {}
1111

1212
def print_graph(self) -> None:
1313
"""

graphs/depth_first_search.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22

33
from __future__ import annotations
44

5+
from typing import Set
56

6-
def depth_first_search(graph: dict, start: str) -> set[int]:
7+
8+
def depth_first_search(graph: dict, start: str) -> Set[str]:
79
"""Depth First Search on Graph
810
:param graph: directed graph in dictionary format
9-
:param vertex: starting vertex as a string
11+
:param start: starting vertex as a string
1012
:returns: the trace of the search
11-
>>> G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"],
13+
>>> input_G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"],
1214
... "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"],
1315
... "F": ["C", "E", "G"], "G": ["F"] }
14-
>>> start = "A"
1516
>>> output_G = list({'A', 'B', 'C', 'D', 'E', 'F', 'G'})
16-
>>> all(x in output_G for x in list(depth_first_search(G, "A")))
17+
>>> all(x in output_G for x in list(depth_first_search(input_G, "A")))
1718
True
18-
>>> all(x in output_G for x in list(depth_first_search(G, "G")))
19+
>>> all(x in output_G for x in list(depth_first_search(input_G, "G")))
1920
True
2021
"""
2122
explored, stack = set(start), [start]

0 commit comments

Comments
 (0)