Skip to content

Commit ed4c92d

Browse files
[mypy] Type annotations for graphs directory (TheAlgorithms#5798)
* Type annotations for `breadth_first_search.py` * Type annotations for `breadth_first_search_2.py` * Remove from excluded in mypy.ini * Add doctest.testmod() * Type annotations for `graphs/check_cycle.py` * Type annotations for `graphs/greedy_min_vertex_cover.py` * Remove from excluded in mypy.ini
1 parent 4c9949f commit ed4c92d

5 files changed

+12
-12
lines changed

graphs/breadth_first_search.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def bfs(self, start_vertex: int) -> set[int]:
5353
visited = set()
5454

5555
# create a first in first out queue to store all the vertices for BFS
56-
queue = Queue()
56+
queue: Queue = Queue()
5757

5858
# mark the source node as visited and enqueue it
5959
visited.add(start_vertex)

graphs/breadth_first_search_2.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def breadth_first_search(graph: dict, start: str) -> set[str]:
3232
'ABCDEF'
3333
"""
3434
explored = {start}
35-
queue = Queue()
35+
queue: Queue = Queue()
3636
queue.put(start)
3737
while not queue.empty():
3838
v = queue.get()
@@ -44,4 +44,7 @@ def breadth_first_search(graph: dict, start: str) -> set[str]:
4444

4545

4646
if __name__ == "__main__":
47+
import doctest
48+
49+
doctest.testmod()
4750
print(breadth_first_search(G, "A"))

graphs/check_cycle.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
def check_cycle(graph: dict) -> bool:
77
"""
88
Returns True if graph is cyclic else False
9-
109
>>> check_cycle(graph={0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]})
1110
False
1211
>>> check_cycle(graph={0:[1, 2], 1:[2], 2:[0, 3], 3:[3]})
1312
True
1413
"""
1514
# Keep track of visited nodes
16-
visited = set()
15+
visited: set[int] = set()
1716
# To detect a back edge, keep track of vertices currently in the recursion stack
18-
rec_stk = set()
17+
rec_stk: set[int] = set()
1918
for node in graph:
2019
if node not in visited:
2120
if depth_first_search(graph, node, visited, rec_stk):
@@ -27,7 +26,6 @@ def depth_first_search(graph: dict, vertex: int, visited: set, rec_stk: set) ->
2726
"""
2827
Recur for all neighbours.
2928
If any neighbour is visited and in rec_stk then graph is cyclic.
30-
3129
>>> graph = {0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]}
3230
>>> vertex, visited, rec_stk = 0, set(), set()
3331
>>> depth_first_search(graph, vertex, visited, rec_stk)

graphs/greedy_min_vertex_cover.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
* Author: Manuel Di Lullo (https://github.com/manueldilullo)
33
* Description: Approximization algorithm for minimum vertex cover problem.
44
Greedy Approach. Uses graphs represented with an adjacency list
5-
65
URL: https://mathworld.wolfram.com/MinimumVertexCover.html
76
URL: https://cs.stackexchange.com/questions/129017/greedy-algorithm-for-vertex-cover
87
"""
98

109
import heapq
1110

1211

13-
def greedy_min_vertex_cover(graph: dict) -> set:
12+
def greedy_min_vertex_cover(graph: dict) -> set[int]:
1413
"""
1514
Greedy APX Algorithm for min Vertex Cover
1615
@input: graph (graph stored in an adjacency list where each vertex
@@ -21,7 +20,7 @@ def greedy_min_vertex_cover(graph: dict) -> set:
2120
{0, 1, 2, 4}
2221
"""
2322
# queue used to store nodes and their rank
24-
queue = []
23+
queue: list[list] = []
2524

2625
# for each node and his adjacency list add them and the rank of the node to queue
2726
# using heapq module the queue will be filled like a Priority Queue
@@ -61,5 +60,5 @@ def greedy_min_vertex_cover(graph: dict) -> set:
6160

6261
doctest.testmod()
6362

64-
# graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]}
65-
# print(f"Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}")
63+
graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]}
64+
print(f"Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}")

mypy.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
ignore_missing_imports = True
33
install_types = True
44
non_interactive = True
5-
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)
5+
exclude = (matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py)

0 commit comments

Comments
 (0)