Skip to content
Related Articles

Related Articles

Check if a given Graph is 2-edge connected or not
  • Last Updated : 23 Nov, 2020

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
 

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: 

  1. Create an array noOfEdges[] of size V which will store the number of edges connected to a node.
  2. For every edge (u, v) increment the number of edges for node u and v.
  3. 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.
  4. 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;
}

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

Python3




# Python3 program for the above approach
 
# Definition of a graph
class Graph:
 
    # No. of vertices
    V = 0
 
    # Array of lists for Adjacency
    # List Representation
    adj = [[]]
 
    # Constructor
    def __init__(self, v):
         
        self.V = v
        self.adj = [[] for i in range(v)]
 
    # Function to add an edge into the graph
    def addEdge(self, v, w):
     
        self.adj[v - 1].append(w - 1)
         
        # Add w to v's list.
        self.adj[w - 1].append(v - 1)
 
    # Function to find if the graph is
    # 2 edge connected or not
    def twoEdge(self, v):
 
        # To store number of edges for
        # each node
        noOfEdges = [len(self.adj[i]) for i in range(v)]
 
        flag = True
 
        # Check the number of edges
        # connected to each node
        for i in range(v):
            if (noOfEdges[i] < 2):
                flag = False
                break
             
        # Print the result
        if (flag):
            print("Yes")
        else:
            print("No")
     
# Driver code
if __name__=="__main__":
 
    # Number of nodes and edges
    V = 8
    E = 10
 
    # Given Edges
    edges = [ [ 1, 2 ], [ 1, 8 ],
              [ 1, 6 ], [ 2, 3 ],
              [ 2, 4 ], [ 3, 7 ],
              [ 3, 4 ], [ 7, 5 ],
              [ 7, 6 ], [ 7, 8 ] ]
 
    g = Graph(V)
 
    # Adding the edges to graph
    for i in range(E):
        g.addEdge(edges[i][0],
                  edges[i][1])
     
    # Function call
    g.twoEdge(V)
 
# This code is contributed by rutvik_56

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class Graph{
     
// No. of vertices    
private int V;
 
// Array of lists for Adjacency
// List Representation
private List<int> []adj;
 
// Constructor
Graph(int v)
{
    V = v;
    adj = new List<int>[v];
     
    for(int i = 0; i < v; ++i)
        adj[i] = new List<int>();
}
 
// Function to add an edge into the graph
void addEdge(int v, int w)
{
     
    // Add w to v's list.
    adj[v - 1].Add(w - 1);
    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].Count;
    }
 
    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)
        Console.Write("Yes");
    else
        Console.Write("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 Amit Katiyar
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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :