From 1f0d3301a5f24a8265328482c09dca5c59c7cf37 Mon Sep 17 00:00:00 2001 From: atishaye Date: Wed, 20 Oct 2021 18:19:19 +0530 Subject: [PATCH 1/4] add check_cycle.py --- graphs/check_cycle.py | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 graphs/check_cycle.py diff --git a/graphs/check_cycle.py b/graphs/check_cycle.py new file mode 100644 index 000000000000..45b5641a6a3b --- /dev/null +++ b/graphs/check_cycle.py @@ -0,0 +1,57 @@ +""" +Program to check if a cycle is present in a given graph +""" + + +def chk_cycle(graph: dict) -> bool: + """ + Returns True if graph is cyclic else False + + >>> graph1 = {0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]} + >>> chk_cycle(graph1) + False + >>> graph2 = {0:[1, 2], 1:[2], 2:[0, 3], 3:[3]} + >>> chk_cycle(graph2) + True + """ + # Keep track of visited nodes + visited = set() + # To detect a back edge, keep track of vertices currently in the recursion stack + rec_stk = set() + for node in graph: + if node not in visited: + if depth_first_search(graph, node, visited, rec_stk): + return True + return False + + +def depth_first_search(graph: dict, vertex: int, visited: set, rec_stk: set) -> bool: + """ + 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) + False + """ + # Mark current node as visited and add to recursion stack + visited.add(vertex) + rec_stk.add(vertex) + + for node in graph[vertex]: + if node not in visited: + if depth_first_search(graph, node, visited, rec_stk): + return True + elif node in rec_stk: + return True + + # The node needs to be removed from recursion stack before function ends + rec_stk.remove(vertex) + return False + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From 3ac7fb179c156760c85151b6ea773e5201094f37 Mon Sep 17 00:00:00 2001 From: Atishaye Jain <64211411+atishaye@users.noreply.github.com> Date: Fri, 22 Oct 2021 14:39:37 +0530 Subject: [PATCH 2/4] Update graphs/check_cycle.py Co-authored-by: John Law --- graphs/check_cycle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/check_cycle.py b/graphs/check_cycle.py index 45b5641a6a3b..177e75f2d67d 100644 --- a/graphs/check_cycle.py +++ b/graphs/check_cycle.py @@ -3,7 +3,7 @@ """ -def chk_cycle(graph: dict) -> bool: +def check_cycle(graph: dict) -> bool: """ Returns True if graph is cyclic else False From 54e473dde9df35f1bfaa1ba6931bdadb3b3d9f97 Mon Sep 17 00:00:00 2001 From: atishaye Date: Fri, 22 Oct 2021 16:05:20 +0530 Subject: [PATCH 3/4] Update check_cycle.py --- graphs/check_cycle.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphs/check_cycle.py b/graphs/check_cycle.py index 177e75f2d67d..68141aa4e853 100644 --- a/graphs/check_cycle.py +++ b/graphs/check_cycle.py @@ -8,10 +8,10 @@ def check_cycle(graph: dict) -> bool: Returns True if graph is cyclic else False >>> graph1 = {0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]} - >>> chk_cycle(graph1) + >>> check_cycle(graph1) False >>> graph2 = {0:[1, 2], 1:[2], 2:[0, 3], 3:[3]} - >>> chk_cycle(graph2) + >>> check_cycle(graph2) True """ # Keep track of visited nodes From e8dafec7610eac5e48669278631587f8ac4c9a5f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Oct 2021 12:25:49 +0200 Subject: [PATCH 4/4] Apply suggestions from code review --- 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 68141aa4e853..71d42b4689b7 100644 --- a/graphs/check_cycle.py +++ b/graphs/check_cycle.py @@ -7,11 +7,9 @@ def check_cycle(graph: dict) -> bool: """ Returns True if graph is cyclic else False - >>> graph1 = {0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]} - >>> check_cycle(graph1) + >>> check_cycle(graph={0:[], 1:[0, 3], 2:[0, 4], 3:[5], 4:[5], 5:[]}) False - >>> graph2 = {0:[1, 2], 1:[2], 2:[0, 3], 3:[3]} - >>> check_cycle(graph2) + >>> check_cycle(graph={0:[1, 2], 1:[2], 2:[0, 3], 3:[3]}) True """ # Keep track of visited nodes