📜  检查给定的图是否是 2 边连接的

📅  最后修改于: 2021-09-03 13:39:44             🧑  作者: Mango

给定一个无向图G ,具有V个顶点和E 条边,任务是检查该图是否是 2 边连通的。

例子:

朴素的方法:朴素的方法是在删除任何边X时检查剩余的图G – X是否连接。如果图在一条一条地移除每条边时保持连通,那么它是一个 2 边连通图。为了实现上述想法,删除边并从任何顶点执行深度优先搜索(DFS)或广度优先搜索(BFS)并检查是否所有顶点都被覆盖。对所有E边重复此过程。如果任何边都不能遍历所有顶点,则打印No 。否则,打印Yes

时间复杂度: O(E * ( V + E))
辅助空间: O(1)

高效的方法:由于给定的图是无向图,因此只能通过计算连接到节点的边数来解决问题。如果对于任何节点,与其相连的边数为 1,则意味着在删除此边时,该节点将断开连接,并且无法从任何其他节点到达,因此该图不是 2 边连接的。以下是步骤:

  1. 创建一个大小为V的数组noOfEdges[] ,它将存储连接到节点的边数。
  2. 对于每条边(u, v)增加节点uv的边数。
  3. 现在迭代数组noOfEdges[]并检查是否有任何边只有 1 个边连接到它。如果是,则该图不是 2-edge connected
  4. 否则,该图是 2 边连通的。

下面是上述方法的实现:

C++14
// C++14 program for the above approach
 
#include 
using namespace std;
 
// Definition of a graph
class Graph {
 
    // No. of vertices
    int V;
 
    // To create adjacency list
    list* 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[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 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 []adj;
 
// Constructor
Graph(int v)
{
    V = v;
    adj = new List[v];
     
    for(int i = 0; i < v; ++i)
        adj[i] = new List();
}
 
// 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


输出:
No








时间复杂度: O(V + E)
辅助空间: O(V)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live