From 8e5e05ca8ef3663c748952a114b96d36d5b21e68 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Mon, 8 Nov 2021 22:57:25 +0530 Subject: [PATCH 1/7] Type annotations for `breadth_first_search.py` --- graphs/breadth_first_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/breadth_first_search.py b/graphs/breadth_first_search.py index 9264f57b41b2..171d3875f3c5 100644 --- a/graphs/breadth_first_search.py +++ b/graphs/breadth_first_search.py @@ -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) From 56a19042f37632996bfae06c810964ce0cadc30f Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Mon, 8 Nov 2021 23:02:06 +0530 Subject: [PATCH 2/7] Type annotations for `breadth_first_search_2.py` --- graphs/breadth_first_search_2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index 4c8b69faf656..3a4ddbcc20cb 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -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() From 5bcea6627b2fe9614d5369a96dd68cd98b404809 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Mon, 8 Nov 2021 23:03:04 +0530 Subject: [PATCH 3/7] Remove from excluded in mypy.ini --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 429c6804daf5..c5a2df2c8d2b 100644 --- a/mypy.ini +++ b/mypy.ini @@ -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) From f1849969f04dc061000f9b98d8d5d85db4d0500b Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Mon, 8 Nov 2021 23:14:56 +0530 Subject: [PATCH 4/7] Add doctest.testmod() --- graphs/breadth_first_search_2.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py index 3a4ddbcc20cb..2f060a90d40d 100644 --- a/graphs/breadth_first_search_2.py +++ b/graphs/breadth_first_search_2.py @@ -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")) From 211dad6f005b5eaf61125d6749280990a6f8bf76 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Mon, 8 Nov 2021 23:29:59 +0530 Subject: [PATCH 5/7] Type annotations for `graphs/check_cycle.py` --- graphs/check_cycle.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/graphs/check_cycle.py b/graphs/check_cycle.py index 71d42b4689b7..dcc864988ca5 100644 --- a/graphs/check_cycle.py +++ b/graphs/check_cycle.py @@ -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): @@ -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) From 353c3fc1c121e5460370b07559e38b7624b7daf9 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Mon, 8 Nov 2021 23:41:14 +0530 Subject: [PATCH 6/7] Type annotations for `graphs/greedy_min_vertex_cover.py` --- graphs/greedy_min_vertex_cover.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/graphs/greedy_min_vertex_cover.py b/graphs/greedy_min_vertex_cover.py index 056c5b89bedf..cdef69141bd6 100644 --- a/graphs/greedy_min_vertex_cover.py +++ b/graphs/greedy_min_vertex_cover.py @@ -2,7 +2,6 @@ * Author: Manuel Di Lullo (https://github.com/manueldilullo) * Description: Approximization algorithm for minimum vertex cover problem. Greedy Approach. Uses graphs represented with an adjacency list - URL: https://mathworld.wolfram.com/MinimumVertexCover.html URL: https://cs.stackexchange.com/questions/129017/greedy-algorithm-for-vertex-cover """ @@ -10,7 +9,7 @@ import heapq -def greedy_min_vertex_cover(graph: dict) -> set: +def greedy_min_vertex_cover(graph: dict) -> set[int]: """ Greedy APX Algorithm for min Vertex Cover @input: graph (graph stored in an adjacency list where each vertex @@ -21,7 +20,7 @@ def greedy_min_vertex_cover(graph: dict) -> set: {0, 1, 2, 4} """ # queue used to store nodes and their rank - queue = [] + queue: list[list] = [] # for each node and his adjacency list add them and the rank of the node to queue # using heapq module the queue will be filled like a Priority Queue @@ -61,5 +60,5 @@ def greedy_min_vertex_cover(graph: dict) -> set: doctest.testmod() - # graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} - # print(f"Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}") + graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} + print(f"Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}") From 19685f8c8a92cf3e549189d99da32af71142a642 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Mon, 8 Nov 2021 23:43:22 +0530 Subject: [PATCH 7/7] Remove from excluded in mypy.ini --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index c5a2df2c8d2b..ce7c262ab059 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,4 +2,4 @@ ignore_missing_imports = True install_types = True non_interactive = True -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) +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)