{"id":25986,"date":"2017-10-26T09:28:23","date_gmt":"2017-10-26T03:58:23","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25986"},"modified":"2017-10-26T09:28:23","modified_gmt":"2017-10-26T03:58:23","slug":"c-algorithm-detect-cycle-undirected-graph","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-detect-cycle-undirected-graph\/","title":{"rendered":"Cpp Algorithm &#8211; Detect cycle in an undirected graph"},"content":{"rendered":"<p>Given an undirected graph, how to check if there is a cycle in the graph? For example, the following graph has a cycle 1-0-2-1.<\/p>\n<p>We have discussed cycle detection for directed graph. We have also discussed a <a href=\"http:\/\/www.geeksforgeeks.org\/union-find\/\" target=\"_blank\" rel=\"noopener noreferrer\">union-find algorithm for cycle detection in undirected graphs.<\/a> The time complexity of the union-find algorithm is O(ELogV). Like directed graphs, we can use <a href=\"http:\/\/www.geeksforgeeks.org\/depth-first-traversal-for-a-graph\/\" target=\"_blank\" rel=\"noopener noreferrer\">DFS <\/a>to detect cycle in an undirected graph in O(V+E) time. We do a DFS traversal of the given graph. For every visited vertex \u2018v\u2019, if there is an adjacent \u2018u\u2019 such that u is already visited and u is not parent of v, then there is a cycle in graph. If we don\u2019t find such an adjacent for any vertex, we say that there is no cycle. The assumption of this approach is that there are no parallel edges between any two vertices.<\/p>\n<p><strong>C++ Programming:<\/strong><\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20A%20C%2B%2B%20Program%20to%20detect%20cycle%20in%20an%20undirected%20graph%0A%23include%3Ciostream%3E%0A%23include%20%3Clist%3E%0A%23include%20%3Climits.h%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Class%20for%20an%20undirected%20graph%0Aclass%20Graph%0A%7B%0A%20%20%20%20int%20V%3B%20%20%20%20%2F%2F%20No.%20of%20vertices%0A%20%20%20%20list%3Cint%3E%20*adj%3B%20%20%20%20%2F%2F%20Pointer%20to%20an%20array%20containing%20adjacency%20lists%0A%20%20%20%20bool%20isCyclicUtil(int%20v%2C%20bool%20visited%5B%5D%2C%20int%20parent)%3B%0Apublic%3A%0A%20%20%20%20Graph(int%20V)%3B%20%20%20%2F%2F%20Constructor%0A%20%20%20%20void%20addEdge(int%20v%2C%20int%20w)%3B%20%20%20%2F%2F%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20bool%20isCyclic()%3B%20%20%20%2F%2F%20returns%20true%20if%20there%20is%20a%20cycle%0A%7D%3B%0A%20%0AGraph%3A%3AGraph(int%20V)%0A%7B%0A%20%20%20%20this-%3EV%20%3D%20V%3B%0A%20%20%20%20adj%20%3D%20new%20list%3Cint%3E%5BV%5D%3B%0A%7D%0A%20%0Avoid%20Graph%3A%3AaddEdge(int%20v%2C%20int%20w)%0A%7B%0A%20%20%20%20adj%5Bv%5D.push_back(w)%3B%20%2F%2F%20Add%20w%20to%20v%E2%80%99s%20list.%0A%20%20%20%20adj%5Bw%5D.push_back(v)%3B%20%2F%2F%20Add%20v%20to%20w%E2%80%99s%20list.%0A%7D%0A%20%0A%2F%2F%20A%20recursive%20function%20that%20uses%20visited%5B%5D%20and%20parent%20to%20detect%0A%2F%2F%20cycle%20in%20subgraph%20reachable%20from%20vertex%20v.%0Abool%20Graph%3A%3AisCyclicUtil(int%20v%2C%20bool%20visited%5B%5D%2C%20int%20parent)%0A%7B%0A%20%20%20%20%2F%2F%20Mark%20the%20current%20node%20as%20visited%0A%20%20%20%20visited%5Bv%5D%20%3D%20true%3B%0A%20%0A%20%20%20%20%2F%2F%20Recur%20for%20all%20the%20vertices%20adjacent%20to%20this%20vertex%0A%20%20%20%20list%3Cint%3E%3A%3Aiterator%20i%3B%0A%20%20%20%20for%20(i%20%3D%20adj%5Bv%5D.begin()%3B%20i%20!%3D%20adj%5Bv%5D.end()%3B%20%2B%2Bi)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20an%20adjacent%20is%20not%20visited%2C%20then%20recur%20for%20that%20adjacent%0A%20%20%20%20%20%20%20%20if%20(!visited%5B*i%5D)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20if%20(isCyclicUtil(*i%2C%20visited%2C%20v))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20an%20adjacent%20is%20visited%20and%20not%20parent%20of%20current%20vertex%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20then%20there%20is%20a%20cycle.%0A%20%20%20%20%20%20%20%20else%20if%20(*i%20!%3D%20parent)%0A%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20false%3B%0A%7D%0A%20%0A%2F%2F%20Returns%20true%20if%20the%20graph%20contains%20a%20cycle%2C%20else%20false.%0Abool%20Graph%3A%3AisCyclic()%0A%7B%0A%20%20%20%20%2F%2F%20Mark%20all%20the%20vertices%20as%20not%20visited%20and%20not%20part%20of%20recursion%0A%20%20%20%20%2F%2F%20stack%0A%20%20%20%20bool%20*visited%20%3D%20new%20bool%5BV%5D%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20visited%5Bi%5D%20%3D%20false%3B%0A%20%0A%20%20%20%20%2F%2F%20Call%20the%20recursive%20helper%20function%20to%20detect%20cycle%20in%20different%0A%20%20%20%20%2F%2F%20DFS%20trees%0A%20%20%20%20for%20(int%20u%20%3D%200%3B%20u%20%3C%20V%3B%20u%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(!visited%5Bu%5D)%20%2F%2F%20Don\u2019t%20recur%20for%20u%20if%20it%20is%20already%20visited%0A%20%20%20%20%20%20%20%20%20%20if%20(isCyclicUtil(u%2C%20visited%2C%20-1))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%0A%20%20%20%20return%20false%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20Graph%20g1(5)%3B%0A%20%20%20%20g1.addEdge(1%2C%200)%3B%0A%20%20%20%20g1.addEdge(0%2C%202)%3B%0A%20%20%20%20g1.addEdge(2%2C%200)%3B%0A%20%20%20%20g1.addEdge(0%2C%203)%3B%0A%20%20%20%20g1.addEdge(3%2C%204)%3B%0A%20%20%20%20g1.isCyclic()%3F%20cout%20%3C%3C%20%22Graph%20contains%20cycle%5Cn%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Graph%20doesn\u2019t%20contain%20cycle%5Cn%22%3B%0A%20%0A%20%20%20%20Graph%20g2(3)%3B%0A%20%20%20%20g2.addEdge(0%2C%201)%3B%0A%20%20%20%20g2.addEdge(1%2C%202)%3B%0A%20%20%20%20g2.isCyclic()%3F%20cout%20%3C%3C%20%22Graph%20contains%20cycle%5Cn%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Graph%20doesn\u2019t%20contain%20cycle%5Cn%22%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>Graph contains cycle\r\nGraph doesn't contain cycle<\/pre>\n<p><strong>Time Complexity:<\/strong> The program does a simple DFS Traversal of graph and graph is represented using adjacency list. So the time complexity is O(V+E)<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C++ Algorithm &#8211; Detect cycle in an undirected graph &#8211; Graph Algorithms &#8211; Given an undirected graph, how to check if there is a cycle in the graph<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,1,74168,73906],"tags":[76548,76546,76186,76185,76184,76187,76190,76547],"class_list":["post-25986","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-dfs-and-bfs","category-graph-algorithms","tag-cycle-detection-in-linked-list","tag-detect-cycle-in-directed-graph","tag-detect-cycle-in-directed-graph-c","tag-detect-cycle-in-directed-graph-in-c","tag-detect-cycle-in-directed-graph-java","tag-detect-cycle-in-undirected-graph-in-c","tag-find-all-cycles-in-a-directed-graph","tag-find-all-cycles-in-undirected-graph"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25986"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25986\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}