mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
[mypy] Type annotations for graphs directory (#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
This commit is contained in:
parent
4c9949f636
commit
ed4c92d98a
|
@ -53,7 +53,7 @@ class Graph:
|
||||||
visited = set()
|
visited = set()
|
||||||
|
|
||||||
# create a first in first out queue to store all the vertices for BFS
|
# 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
|
# mark the source node as visited and enqueue it
|
||||||
visited.add(start_vertex)
|
visited.add(start_vertex)
|
||||||
|
|
|
@ -32,7 +32,7 @@ def breadth_first_search(graph: dict, start: str) -> set[str]:
|
||||||
'ABCDEF'
|
'ABCDEF'
|
||||||
"""
|
"""
|
||||||
explored = {start}
|
explored = {start}
|
||||||
queue = Queue()
|
queue: Queue = Queue()
|
||||||
queue.put(start)
|
queue.put(start)
|
||||||
while not queue.empty():
|
while not queue.empty():
|
||||||
v = queue.get()
|
v = queue.get()
|
||||||
|
@ -44,4 +44,7 @@ def breadth_first_search(graph: dict, start: str) -> set[str]:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
print(breadth_first_search(G, "A"))
|
print(breadth_first_search(G, "A"))
|
||||||
|
|
|
@ -6,16 +6,15 @@ Program to check if a cycle is present in a given graph
|
||||||
def check_cycle(graph: dict) -> bool:
|
def check_cycle(graph: dict) -> bool:
|
||||||
"""
|
"""
|
||||||
Returns True if graph is cyclic else False
|
Returns True if graph is cyclic else False
|
||||||
|
|
||||||
>>> check_cycle(graph={0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]})
|
>>> check_cycle(graph={0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]})
|
||||||
False
|
False
|
||||||
>>> check_cycle(graph={0:[1, 2], 1:[2], 2:[0, 3], 3:[3]})
|
>>> check_cycle(graph={0:[1, 2], 1:[2], 2:[0, 3], 3:[3]})
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
# Keep track of visited nodes
|
# 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
|
# 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:
|
for node in graph:
|
||||||
if node not in visited:
|
if node not in visited:
|
||||||
if depth_first_search(graph, node, visited, rec_stk):
|
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.
|
Recur for all neighbours.
|
||||||
If any neighbour is visited and in rec_stk then graph is cyclic.
|
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:[]}
|
>>> graph = {0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]}
|
||||||
>>> vertex, visited, rec_stk = 0, set(), set()
|
>>> vertex, visited, rec_stk = 0, set(), set()
|
||||||
>>> depth_first_search(graph, vertex, visited, rec_stk)
|
>>> depth_first_search(graph, vertex, visited, rec_stk)
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
* Author: Manuel Di Lullo (https://github.com/manueldilullo)
|
* Author: Manuel Di Lullo (https://github.com/manueldilullo)
|
||||||
* Description: Approximization algorithm for minimum vertex cover problem.
|
* Description: Approximization algorithm for minimum vertex cover problem.
|
||||||
Greedy Approach. Uses graphs represented with an adjacency list
|
Greedy Approach. Uses graphs represented with an adjacency list
|
||||||
|
|
||||||
URL: https://mathworld.wolfram.com/MinimumVertexCover.html
|
URL: https://mathworld.wolfram.com/MinimumVertexCover.html
|
||||||
URL: https://cs.stackexchange.com/questions/129017/greedy-algorithm-for-vertex-cover
|
URL: https://cs.stackexchange.com/questions/129017/greedy-algorithm-for-vertex-cover
|
||||||
"""
|
"""
|
||||||
|
@ -10,7 +9,7 @@ URL: https://cs.stackexchange.com/questions/129017/greedy-algorithm-for-vertex-c
|
||||||
import heapq
|
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
|
Greedy APX Algorithm for min Vertex Cover
|
||||||
@input: graph (graph stored in an adjacency list where each vertex
|
@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}
|
{0, 1, 2, 4}
|
||||||
"""
|
"""
|
||||||
# queue used to store nodes and their rank
|
# 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
|
# 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
|
# using heapq module the queue will be filled like a Priority Queue
|
||||||
|
@ -61,5 +60,5 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
|
||||||
# graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]}
|
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)}")
|
print(f"Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}")
|
||||||
|
|
2
mypy.ini
2
mypy.ini
|
@ -2,4 +2,4 @@
|
||||||
ignore_missing_imports = True
|
ignore_missing_imports = True
|
||||||
install_types = True
|
install_types = True
|
||||||
non_interactive = 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 = (matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user