|
| 1 | +// A C++ Program to detect |
| 2 | +// cycle in an undirected graph |
| 3 | +#include <iostream> |
| 4 | +#include <limits.h> |
| 5 | +#include <list> |
| 6 | +using namespace std; |
| 7 | +/* |
| 8 | + description |
| 9 | + //Use DFS from every unvisited node. Depth First Traversal can be used to detect a cycle in a Graph. |
| 10 | + //There is a cycle in a graph only if there is a back edge present in the graph. |
| 11 | + // A back edge is an edge that is indirectly joining a node to itself (self-loop) or one of its ancestors in the tree produced by DFS. |
| 12 | + //To find the back edge to any of its ancestors keep a visited array and if there is a back edge to any visited node then there is a loop and return true. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +*/ |
| 17 | + |
| 18 | +// Class for an undirected graph |
| 19 | +class Graph { |
| 20 | + |
| 21 | + // No. of vertices |
| 22 | + int V; |
| 23 | + |
| 24 | + // Pointer to an array |
| 25 | + // containing adjacency lists |
| 26 | + list<int>* adj; |
| 27 | + bool isCyclicUtil(int v, bool visited[], int parent); |
| 28 | + |
| 29 | +public: |
| 30 | + // Constructor |
| 31 | + Graph(int V); |
| 32 | + |
| 33 | + // To add an edge to graph |
| 34 | + void addEdge(int v, int w); |
| 35 | + |
| 36 | + // Returns true if there is a cycle |
| 37 | + bool isCyclic(); |
| 38 | +}; |
| 39 | + |
| 40 | +Graph::Graph(int V) |
| 41 | +{ |
| 42 | + this->V = V; |
| 43 | + adj = new list<int>[V]; |
| 44 | +} |
| 45 | + |
| 46 | +void Graph::addEdge(int v, int w) |
| 47 | +{ |
| 48 | + |
| 49 | + // Add w to v’s list. |
| 50 | + adj[v].push_back(w); |
| 51 | + |
| 52 | + // Add v to w’s list. |
| 53 | + adj[w].push_back(v); |
| 54 | +} |
| 55 | + |
| 56 | +// A recursive function that |
| 57 | +// uses visited[] and parent to detect |
| 58 | +// cycle in subgraph reachable |
| 59 | +// from vertex v. |
| 60 | +bool Graph::isCyclicUtil(int v, bool visited[], int parent) |
| 61 | +{ |
| 62 | + |
| 63 | + // Mark the current node as visited |
| 64 | + visited[v] = true; |
| 65 | + |
| 66 | + // Recur for all the vertices |
| 67 | + // adjacent to this vertex |
| 68 | + list<int>::iterator i; |
| 69 | + for (i = adj[v].begin(); i != adj[v].end(); ++i) { |
| 70 | + |
| 71 | + // If an adjacent vertex is not visited, |
| 72 | + // then recur for that adjacent |
| 73 | + if (!visited[*i]) { |
| 74 | + if (isCyclicUtil(*i, visited, v)) |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + // If an adjacent vertex is visited and |
| 79 | + // is not parent of current vertex, |
| 80 | + // then there exists a cycle in the graph. |
| 81 | + else if (*i != parent) |
| 82 | + return true; |
| 83 | + } |
| 84 | + return false; |
| 85 | +} |
| 86 | + |
| 87 | +// Returns true if the graph contains |
| 88 | +// a cycle, else false. |
| 89 | +bool Graph::isCyclic() |
| 90 | +{ |
| 91 | + |
| 92 | + // Mark all the vertices as not |
| 93 | + // visited and not part of recursion |
| 94 | + // stack |
| 95 | + bool* visited = new bool[V]; |
| 96 | + for (int i = 0; i < V; i++) |
| 97 | + visited[i] = false; |
| 98 | + |
| 99 | + // Call the recursive helper |
| 100 | + // function to detect cycle in different |
| 101 | + // DFS trees |
| 102 | + for (int u = 0; u < V; u++) { |
| 103 | + |
| 104 | + // Don't recur for u if |
| 105 | + // it is already visited |
| 106 | + if (!visited[u]) |
| 107 | + if (isCyclicUtil(u, visited, -1)) |
| 108 | + return true; |
| 109 | + } |
| 110 | + return false; |
| 111 | +} |
| 112 | + |
| 113 | +// Driver program to test above functions |
| 114 | +int main() |
| 115 | +{ |
| 116 | + Graph g1(5); |
| 117 | + g1.addEdge(1, 0); |
| 118 | + g1.addEdge(0, 2); |
| 119 | + g1.addEdge(2, 1); |
| 120 | + g1.addEdge(0, 3); |
| 121 | + g1.addEdge(3, 4); |
| 122 | + g1.isCyclic() ? cout << "Graph contains cycle\n" |
| 123 | + : cout << "Graph doesn't contain cycle\n"; |
| 124 | + |
| 125 | + Graph g2(3); |
| 126 | + g2.addEdge(0, 1); |
| 127 | + g2.addEdge(1, 2); |
| 128 | + g2.isCyclic() ? cout << "Graph contains cycle\n" |
| 129 | + : cout << "Graph doesn't contain cycle\n"; |
| 130 | + |
| 131 | + return 0; |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +//Time Complexity: O(V+E) |
| 136 | +//Auxiliary Space: O(V) |
0 commit comments