Skip to content

Add/fix mypy type annotations at BFS, DFS in graphs #4488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions graphs/breadth_first_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

""" Author: OMKAR PATHAK """

from typing import Set
from typing import Dict, List, Set


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

def print_graph(self) -> None:
"""
Expand Down
13 changes: 7 additions & 6 deletions graphs/depth_first_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

from __future__ import annotations

from typing import Set

def depth_first_search(graph: dict, start: str) -> set[int]:

def depth_first_search(graph: dict, start: str) -> Set[str]:
"""Depth First Search on Graph
:param graph: directed graph in dictionary format
:param vertex: starting vertex as a string
:param start: starting vertex as a string
:returns: the trace of the search
>>> G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"],
>>> input_G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"],
... "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"],
... "F": ["C", "E", "G"], "G": ["F"] }
>>> start = "A"
>>> output_G = list({'A', 'B', 'C', 'D', 'E', 'F', 'G'})
>>> all(x in output_G for x in list(depth_first_search(G, "A")))
>>> all(x in output_G for x in list(depth_first_search(input_G, "A")))
True
>>> all(x in output_G for x in list(depth_first_search(G, "G")))
>>> all(x in output_G for x in list(depth_first_search(input_G, "G")))
True
"""
explored, stack = set(start), [start]
Expand Down