Given an undirected graph G, with V vertices and E edges, the task is to check whether the graph is 2-edge connected or not.
A graph is said to be 2-edge connected if, on removing any edge of the graph, it still remains connected, i.e. it contains no Bridges.
Input: V = 8, E = 10
Given any vertex in the graph, we can reach any other vertex in the graph. Moreover, removing any edge from the graph does not affect its connectivity. So, the graph is said to be 2-edge connected.
Naive Approach: The naive approach is to check that on removing any edge X, if the remaining graph G – X is connected or not. If the graph remains connected on removing every edge one by one then it is a 2-edge connected graph. To implement the above idea, remove an edge and perform Depth First Search(DFS) or Breadth-First Search(BFS) from any vertex and check if all vertices are covered or not. Repeat this process for all E edges. If all vertices cannot be traversed for any edge, print No. Otherwise, print Yes.
Time Complexity: O(E * ( V + E))
Auxiliary Space: O(1)
Efficient Approach: Since the given graph is undirected, the problem can be solved only by counting the number of edges connected to the nodes. If for any of the nodes, the number of edges connected to it is 1 it means on removing this edge the node becomes disconnected and it can’t be reached from any other node therefore the graph is not 2-edge connected. Below are the steps:
- Create an array noOfEdges of size V which will store the number of edges connected to a node.
- For every edge (u, v) increment the number of edges for node u and v.
- Now iterate over the array noOfEdges and check if any of the edge has only 1 edge connected to it. If yes then the graph is not 2-edge connected.
- Otherwise the graph is 2-edge connected.
Below is the implementation of the above approach:
Time Complexity: O(V + E)
Auxiliary Space: O(V)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert undirected connected graph to strongly connected directed graph
- Check if a directed graph is connected or not
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Check if there exists a connected graph that satisfies the given conditions
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Check if a graph is Strongly, Unilaterally or Weakly connected
- Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph
- Check if longest connected component forms a palindrome in undirected graph
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Connected Components in an undirected graph
- Cycles of length n in an undirected and connected graph
- Sum of the minimum elements in all connected components of an undirected graph
- All vertex pairs connected with exactly k edges in a graph
- Maximum number of edges among all connected components of an undirected graph
- Clone an undirected graph with multiple connected components
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Count of unique lengths of connected components for an undirected graph using STL
- Maximum sum of values of nodes among all connected components of an undirected graph
- Octal equivalents of connected components in Binary valued graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : offbeat