{"id":262,"date":"2025-06-04T15:20:35","date_gmt":"2025-06-04T09:50:35","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=262"},"modified":"2025-06-04T15:20:36","modified_gmt":"2025-06-04T09:50:36","slug":"detect-cycle-in-a-directed-graph-kahns-algorithm-bfs","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/detect-cycle-in-a-directed-graph-kahns-algorithm-bfs\/","title":{"rendered":"Detect Cycle in a Directed Graph \u2013 Kahn\u2019s Algorithm (BFS Topological Check) in Python"},"content":{"rendered":"\n<p>Learn how to detect cycle in a directed graph with Kahn\u2019s algorithm. Simple intuition, commented Python code, step-by-step dry run, and clear time \/ space complexity.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/detect-cycle-in-a-directed-graph\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-a104413b-235c-4845-a266-385f639b0008\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/detect-cycle-in-a-directed-graph-kahns-algorithm-bfs\/#0-what-does-the-problem-ask\" style=\"\">What does the problem ask?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/detect-cycle-in-a-directed-graph-kahns-algorithm-bfs\/#1-examples\" style=\"\">Examples<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/detect-cycle-in-a-directed-graph-kahns-algorithm-bfs\/#2-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/detect-cycle-in-a-directed-graph-kahns-algorithm-bfs\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/detect-cycle-in-a-directed-graph-kahns-algorithm-bfs\/#4-code-walkthrough\" style=\"\">Code walkthrough<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/detect-cycle-in-a-directed-graph-kahns-algorithm-bfs\/#5-dry-run-on-the-first-example\" style=\"\">Dry run on the first example<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/detect-cycle-in-a-directed-graph-kahns-algorithm-bfs\/#6-complexity\" style=\"\">Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/detect-cycle-in-a-directed-graph-kahns-algorithm-bfs\/#7-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-what-does-the-problem-ask\">What does the problem ask?<\/h2>\n\n\n\n<p>Given a directed graph with <code>V<\/code> vertices (<code>0 \u2026 V-1<\/code>) and a list of directed edges, tell whether the graph contains <strong>at least one cycle<\/strong>.<br>Return <strong><code>True<\/code><\/strong> if a cycle exists; otherwise return <strong><code>False<\/code><\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-examples\">Examples<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><code>V<\/code><\/th><th><code>edges<\/code><\/th><th>Output<\/th><th>Why<\/th><\/tr><\/thead><tbody><tr><td>4<\/td><td><code>[(0,1),(1,2),(2,0),(2,3)]<\/code><\/td><td><strong>True<\/strong><\/td><td>0 \u2192 1 \u2192 2 \u2192 0 forms a loop<\/td><\/tr><tr><td>3<\/td><td><code>[(0,1),(1,2)]<\/code><\/td><td><strong>False<\/strong><\/td><td>No edge leads back to an earlier node<\/td><\/tr><tr><td>1<\/td><td><code>[(0,0)]<\/code><\/td><td><strong>True<\/strong><\/td><td>Self-loop counts as a cycle<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-intuition-amp-approach\">Intuition &amp; Approach<\/h2>\n\n\n\n<p>We use <strong>Kahn\u2019s algorithm<\/strong>, which is really a breadth-first topological sort:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Indegree count<\/strong><br>For every vertex count incoming edges. Nodes with <strong>indegree 0<\/strong> have no prerequisites.<\/li>\n\n\n\n<li><strong>Queue of ready nodes<\/strong><br>Put all indegree-0 nodes into a queue.<\/li>\n\n\n\n<li><strong>Process the queue<\/strong><br>Repeatedly pop a node, \u201cremove\u201d its outgoing edges by decrementing each neighbour\u2019s indegree.<br>Whenever a neighbour\u2019s indegree becomes 0, push it into the queue.<\/li>\n\n\n\n<li><strong>Cycle check<\/strong><br>If we can remove (process) <strong>all <code>V<\/code> nodes<\/strong>, the graph is acyclic.<br>If some nodes never reach indegree 0, they are locked in a cycle.<\/li>\n<\/ol>\n\n\n\n<p>So:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>All nodes processed?<\/strong> \u2192 <strong>No cycle<\/strong> (<code>False<\/code>)<\/li>\n\n\n\n<li><strong>Some left unprocessed?<\/strong> \u2192 <strong>Cycle exists<\/strong> (<code>True<\/code>)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-code\">Code<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from collections import deque\n\n\nclass Solution:\n    def isCycle(self, V, edges):\n        adj_list = [[] for _ in range(V)]\n        indegrees = [0 for _ in range(V)]      # incoming edge counts\n\n        # Build graph and indegree array\n        for u, v in edges:                     # edge u \u2192 v\n            adj_list[u].append(v)\n            indegrees[v] += 1\n\n        queue = deque()\n        result = []                            # list of processed nodes\n\n        # Put all sources (indegree 0) into the queue\n        for i in range(V):\n            if indegrees[i] == 0:\n                queue.append(i)\n\n        # Standard BFS over the DAG \/ graph\n        while len(queue) != 0:\n            current_node = queue.popleft()\n            result.append(current_node)\n            for adjNode in adj_list[current_node]:\n                indegrees[adjNode] -= 1        # remove edge current \u2192 adjNode\n                if indegrees[adjNode] == 0:    # neighbour becomes a new source\n                    queue.append(adjNode)\n\n        # If we processed every vertex, no cycle; otherwise a cycle exists\n        if len(result) == V:\n            return False                       # acyclic\n        return True                            # cycle present\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> collections <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> deque<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">isCycle<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">V<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">edges<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        adj_list = [[] <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(V)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        indegrees = [<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(V)]      <\/span><span style=\"color: #6A9955\"># incoming edge counts<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Build graph and indegree array<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> u, v <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> edges:                     <\/span><span style=\"color: #6A9955\"># edge u \u2192 v<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            adj_list[u].append(v)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            indegrees[v] += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        queue = deque()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result = []                            <\/span><span style=\"color: #6A9955\"># list of processed nodes<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Put all sources (indegree 0) into the queue<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(V):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> indegrees[i] == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                queue.append(i)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Standard BFS over the DAG \/ graph<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(queue) != <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            current_node = queue.popleft()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            result.append(current_node)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> adjNode <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> adj_list[current_node]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                indegrees[adjNode] -= <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># remove edge current \u2192 adjNode<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> indegrees[adjNode] == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:    <\/span><span style=\"color: #6A9955\"># neighbour becomes a new source<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    queue.append(adjNode)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># If we processed every vertex, no cycle; otherwise a cycle exists<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(result) == V:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><span style=\"color: #D4D4D4\">                       <\/span><span style=\"color: #6A9955\"># acyclic<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">                            <\/span><span style=\"color: #6A9955\"># cycle present<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-code-walkthrough\">Code walkthrough<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Graph build<\/strong> \u2013 <code>adj_list[u]<\/code> stores outgoing edges; <code>indegrees[v]<\/code> counts incoming edges.<\/li>\n\n\n\n<li><strong>Initial queue<\/strong> \u2013 all nodes with indegree 0 have no dependencies, so they can be deleted first.<\/li>\n\n\n\n<li><strong>BFS loop<\/strong> \u2013 for each popped node we \u201cdelete\u201d its edges by lowering neighbours\u2019 indegrees.<\/li>\n\n\n\n<li><strong>Cycle decision<\/strong> \u2013\n<ul class=\"wp-block-list\">\n<li>If <code>result<\/code> length equals <code>V<\/code>, every node was deletable \u2192 no cycle.<\/li>\n\n\n\n<li>Otherwise at least one node stayed stuck with positive indegree, proving a cycle.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-dry-run-on-the-first-example\">Dry run on the first example<\/h2>\n\n\n\n<p><code>V = 4<\/code>, <code>edges = [(0,1),(1,2),(2,0),(2,3)]<\/code><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Step<\/th><th>Queue<\/th><th><code>result<\/code><\/th><th>Key indegree changes<\/th><\/tr><\/thead><tbody><tr><td>Build<\/td><td>&#8211;<\/td><td>&#8211;<\/td><td>indeg = [1,1,1,1]<\/td><\/tr><tr><td>No node has indegree 0<\/td><td><code>[]<\/code><\/td><td><code>[]<\/code><\/td><td>queue empty from start<\/td><\/tr><tr><td>BFS ends immediately<\/td><td><code>[]<\/code><\/td><td><code>[]<\/code><\/td><td>processed = 0<\/td><\/tr><tr><td><code>processed \u2260 V<\/code> (0 \u2260 4) \u2192 <strong>cycle<\/strong><\/td><td><\/td><td><\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Algorithm returns <strong>True<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-complexity\">Complexity<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Measure<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td><strong>Time<\/strong><\/td><td><strong>O(V + E)<\/strong><\/td><\/tr><tr><td><strong>Space<\/strong><\/td><td><strong>O(V + E)<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-conclusion\">Conclusion<\/h2>\n\n\n\n<p>Kahn\u2019s algorithm offers a neat BFS way to detect cycles in directed graphs:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep indegrees.<\/li>\n\n\n\n<li>Remove zero-indegree nodes layer by layer.<\/li>\n\n\n\n<li>If anything remains, a cycle must be blocking progress.<\/li>\n<\/ul>\n\n\n\n<p>The method is linear, easy to code, and widely used in scheduling and dependency resolution tasks.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">Join our FREE Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to detect cycle in a directed graph with Kahn\u2019s algorithm. Simple intuition, commented Python code, step-by-step dry run, and clear time \/ space complexity. Here&#8217;s the [Problem Link] to begin with. What does the problem ask? Given a directed graph with V vertices (0 \u2026 V-1) and a list of directed edges, tell<\/p>\n","protected":false},"author":1,"featured_media":264,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[21,17,19],"class_list":{"0":"post-262","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-bfs","10":"tag-graph","11":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/cycle-detection-using-bfs-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=262"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/262\/revisions"}],"predecessor-version":[{"id":265,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/262\/revisions\/265"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/264"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}