Detect Cycle in Undirected Graph in PythonIn this problem, we will be given an undirected graph. Our task in this problem is to tell if the given has a cycle or not. Let us see some illustrations to understand what a cycle in a graph looks like. Example: Input: N = 8, E = 8 Output: Yes Explanation: The diagram shows that there is a cycle. Approach - 1In the first approach, we will use the Depth First Search or DFS algorithm to find the cycle in the graph. The plan is to use the DFS algorithm for all the unvisited nodes of the adjacency list. We have to use the algorithm multiple times to ensure that the connected components are also traversed and checked if they contain a cycle or not. We will create an array that will store the record of whether the current node is already visited or not. Also, we need to keep a record of the parent node of the current node. For every neighbor of the current node, we will recursively call the function to detect the cycle only if that node is not visited. However, if that node is already visited, we will check if that node is the parent node of the current; then, it cannot be a cycle. But if the current node is not the parent node and still it is already visited, this means that there is a cycle in the graph. Let us understand this concept with an example: Consider a Graph:
Code Output The graph contains a cycle The graph doesn't contain a cycle Time Complexity: Since we are maintaining the visited node array, we are not calling the detect() function on a node more than once. Therefore, we are visiting every node and edge once. Therefore, the time complexity of the DFS algorithm is linear. The total time complexity is equal to the sum of vertices and edges, i.e., O(V+E), where V is the number of vertices and E is the number of edges. Auxiliary Space: We have used space to store the visited array. Hence, the space complexity is O(V). There is space used for storing the recursion stack. Approach - 2In this approach, we will use the BFS algorithm to detect the cycle in the given graph. In the Breadth First Search algorithm, we visit the nodes level-wise. Firstly, we complete visiting the nodes of a particular level, and then we move on to the next level. The BFS algorithm is implemented using the Queue data structure. The queue is used in BFS because a Queue follows the First-In-First-Out approach. Hence, we visit all the nodes of a common level since they will be the ones added first in the queue then the nodes of the next level. The BFS here will be modified. As we saw in the earlier approach, the basic idea of detecting a cycle is to check if the node is already visited or not, and if it is already visited, it should not be the parent node of the current source node. Hence, we need to keep track of the parent node of the current source node. So, in the queue, we will not add just the node but a list containing the node and its corresponding parent node so as to check in each iteration if the visited node is different from the parent node. We will follow these steps to solve this problem:
Below is the Python program for this approach. Code Output The graph contains a cycle The graph doesn't have a cycle Time Complexity: We are using linear loops to traverse the graph. Since we are visiting each node of the adjacency list for once, only the number of iterations is equal to the sum of the vertex and edges. Therefore, the time complexity is O(V+E). Space Complexity: We have used O(V) memory to store the visited array. Next TopicNumber-of-islands-problem-in-python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India