Skip to content

[mypy] Type annotations for graphs directory #5798

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 7 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion graphs/breadth_first_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def bfs(self, start_vertex: int) -> set[int]:
visited = set()

# create a first in first out queue to store all the vertices for BFS
queue = Queue()
queue: Queue = Queue()

# mark the source node as visited and enqueue it
visited.add(start_vertex)
Expand Down
5 changes: 4 additions & 1 deletion graphs/breadth_first_search_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def breadth_first_search(graph: dict, start: str) -> set[str]:
'ABCDEF'
"""
explored = {start}
queue = Queue()
queue: Queue = Queue()
queue.put(start)
while not queue.empty():
v = queue.get()
Expand All @@ -44,4 +44,7 @@ def breadth_first_search(graph: dict, start: str) -> set[str]:


if __name__ == "__main__":
import doctest

doctest.testmod()
print(breadth_first_search(G, "A"))
6 changes: 2 additions & 4 deletions graphs/check_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
def check_cycle(graph: dict) -> bool:
"""
Returns True if graph is cyclic else False

>>> check_cycle(graph={0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]})
False
>>> check_cycle(graph={0:[1, 2], 1:[2], 2:[0, 3], 3:[3]})
True
"""
# Keep track of visited nodes
visited = set()
visited: set[int] = set()
# To detect a back edge, keep track of vertices currently in the recursion stack
rec_stk = set()
rec_stk: set[int] = set()
for node in graph:
if node not in visited:
if depth_first_search(graph, node, visited, rec_stk):
Expand All @@ -27,7 +26,6 @@ def depth_first_search(graph: dict, vertex: int, visited: set, rec_stk: set) ->
"""
Recur for all neighbours.
If any neighbour is visited and in rec_stk then graph is cyclic.

>>> graph = {0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]}
>>> vertex, visited, rec_stk = 0, set(), set()
>>> depth_first_search(graph, vertex, visited, rec_stk)
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
ignore_missing_imports = True
install_types = True
non_interactive = True
exclude = (graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/greedy_min_vertex_cover.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py)
exclude = (graphs/check_cycle.py|graphs/greedy_min_vertex_cover.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py)