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 connectedif, on removing any edge of the graph, it still remains connected, i.e. it contains no Bridges.

**Examples:**

Input:V = 8, E = 10

Output:Yes

Explanation:

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.

Input:V = 8, E = 9

Output:No

Explanation:

On removal of the edge between vertex 3 and vertex 4, the graph is not connected anymore. So, the graph is not 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:

## C++14

`// C++14 program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Definition of a graph ` `class` `Graph { ` ` ` ` ` `// No. of vertices ` ` ` `int` `V; ` ` ` ` ` `// To create adjacency list ` ` ` `list<` `int` `>* adj; ` ` ` `public` `: ` ` ` `// Constructor ` ` ` `Graph(` `int` `V); ` ` ` ` ` `// Function to add an edge to graph ` ` ` `void` `addEdge(` `int` `v, ` `int` `w); ` ` ` ` ` `// Function to check 2-edge ` ` ` `// 2-edge connectivity ` ` ` `void` `twoEdge(` `int` `v); ` `}; ` ` ` `// Initialize the graph ` `Graph::Graph(` `int` `V) ` `{ ` ` ` `this` `->V = V; ` ` ` `adj = ` `new` `list<` `int` `>[V]; ` `} ` ` ` `// Adding edges to adjacency list ` `void` `Graph::addEdge(` `int` `v, ` `int` `w) ` `{ ` ` ` `adj[v - 1].push_back(w - 1); ` ` ` `adj[w - 1].push_back(v - 1); ` `} ` ` ` `// Function to find if the graph is ` `// 2 edge connected or not ` `void` `Graph::twoEdge(` `int` `v) ` `{ ` ` ` `// To store number of edges for ` ` ` `// each node ` ` ` `int` `noOfEdges[v]; ` ` ` ` ` `for` `(` `int` `i = 0; i < v; i++) { ` ` ` `noOfEdges[i] = adj[i].size(); ` ` ` `} ` ` ` ` ` `bool` `flag = ` `true` `; ` ` ` ` ` `// Check the number of edges ` ` ` `// connected to each node ` ` ` `for` `(` `int` `i = 0; i < v; i++) { ` ` ` ` ` `if` `(noOfEdges[i] < 2) { ` ` ` `flag = ` `false` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the result ` ` ` `if` `(flag) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Number of nodes and edges ` ` ` `int` `V = 8; ` ` ` `int` `E = 10; ` ` ` ` ` `// Given Edges ` ` ` `int` `edges[E][2] = { { 1, 2 }, { 1, 8 }, { 1, 6 }, ` ` ` `{ 2, 3 }, { 2, 4 }, { 3, 7 }, ` ` ` `{ 3, 4 }, { 7, 5 }, { 7, 6 }, ` ` ` `{ 7, 8 } }; ` ` ` ` ` `// Initialize the graph ` ` ` `Graph g(V); ` ` ` ` ` `// Adding the edges to graph ` ` ` `for` `(` `int` `i = 0; i < E; i++) { ` ` ` `g.addEdge(edges[i][0], edges[i][1]); ` ` ` `} ` ` ` ` ` `// Function call ` ` ` `g.twoEdge(V); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` ` ` `class` `Graph{ ` ` ` `// No. of vertices ` `private` `int` `V; ` ` ` `// Array of lists for Adjacency ` `// List Representation ` `private` `LinkedList<Integer> adj[]; ` ` ` `// Constructor ` `@SuppressWarnings` `(` `"unchecked"` `) ` `Graph(` `int` `v) ` `{ ` ` ` `V = v; ` ` ` `adj = ` `new` `LinkedList[v]; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < v; ++i) ` ` ` `adj[i] = ` `new` `LinkedList(); ` `} ` ` ` `// Function to add an edge into the graph ` `void` `addEdge(` `int` `v, ` `int` `w) ` `{ ` ` ` `adj[v - ` `1` `].add(w - ` `1` `); ` `// Add w to v's list. ` ` ` `adj[w - ` `1` `].add(v - ` `1` `); ` `} ` ` ` `// Function to find if the graph is ` `// 2 edge connected or not ` `void` `twoEdge(` `int` `v) ` `{ ` ` ` ` ` `// To store number of edges for ` ` ` `// each node ` ` ` `int` `[] noOfEdges = ` `new` `int` `[v]; ` ` ` `for` `(` `int` `i = ` `0` `; i < v; i++) ` ` ` `{ ` ` ` `noOfEdges[i] = adj[i].size(); ` ` ` `} ` ` ` ` ` `boolean` `flag = ` `true` `; ` ` ` ` ` `// Check the number of edges ` ` ` `// connected to each node ` ` ` `for` `(` `int` `i = ` `0` `; i < v; i++) ` ` ` `{ ` ` ` `if` `(noOfEdges[i] < ` `2` `) ` ` ` `{ ` ` ` `flag = ` `false` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the result ` ` ` `if` `(flag) ` ` ` `System.out.print(` `"Yes"` `); ` ` ` `else` ` ` `System.out.print(` `"No"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `// Number of nodes and edges ` ` ` `int` `V = ` `8` `; ` ` ` `int` `E = ` `10` `; ` ` ` ` ` `// Given Edges ` ` ` `int` `edges[][] = { { ` `1` `, ` `2` `}, { ` `1` `, ` `8` `}, ` ` ` `{ ` `1` `, ` `6` `}, { ` `2` `, ` `3` `}, ` ` ` `{ ` `2` `, ` `4` `}, { ` `3` `, ` `7` `}, ` ` ` `{ ` `3` `, ` `4` `}, { ` `7` `, ` `5` `}, ` ` ` `{ ` `7` `, ` `6` `}, { ` `7` `, ` `8` `} }; ` ` ` ` ` `Graph g = ` `new` `Graph(V); ` ` ` ` ` `// Adding the edges to graph ` ` ` `for` `(` `int` `i = ` `0` `; i < E; i++) ` ` ` `{ ` ` ` `g.addEdge(edges[i][` `0` `], edges[i][` `1` `]); ` ` ` `} ` ` ` ` ` `// Function call ` ` ` `g.twoEdge(V); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

**Output:**

No

**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.

## Recommended Posts:

- 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 contribute@geeksforgeeks.org. 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.