无向图的所有连通分量之间的最大边数
给定整数“N”和“K”,其中,N 是无向图的顶点数,“K”表示同一图中的边数(每条边由一对整数表示,其中 i,j 表示顶点“i”直接连接到图中的顶点“j”)。
任务是在给定图中找到所有连接组件中的最大边数。
例子:
Input: N = 6, K = 4,
Edges = {{1, 2}, {2, 3}, {3, 1}, {4, 5}}
Output: 3
Here, graph has 3 components
1st component 1-2-3-1 : 3 edges
2nd component 4-5 : 1 edges
3rd component 6 : 0 edges
max(3, 1, 0) = 3 edges
Input: N = 3, K = 2,
Edges = {{1, 2}, {2, 3}}
Output: 2
方法:
- 使用深度优先搜索,分别找出所有连通分量中每条边的度数之和。
- 现在,根据握手引理,无向图的连通分量中的边总数等于其所有顶点度数总和的一半。
- 打印所有连接组件中的最大边数。
下面是上述方法的实现:
C++
// C++ program to find the connected component
// with maximum number of edges
#include
using namespace std;
// DFS function
int dfs(int s, vector adj[], vector visited,
int nodes)
{
// Adding all the edges connected to the vertex
int adjListSize = adj[s].size();
visited[s] = true;
for (long int i = 0; i < adj[s].size(); i++) {
if (visited[adj[s][i]] == false) {
adjListSize += dfs(adj[s][i], adj, visited, nodes);
}
}
return adjListSize;
}
int maxEdges(vector adj[], int nodes)
{
int res = INT_MIN;
vector visited(nodes, false);
for (long int i = 1; i <= nodes; i++) {
if (visited[i] == false) {
int adjListSize = dfs(i, adj, visited, nodes);
res = max(res, adjListSize/2);
}
}
return res;
}
// Driver code
int main()
{
int nodes = 3;
vector adj[nodes+1];
// Edge from vertex 1 to vertex 2
adj[1].push_back(2);
adj[2].push_back(1);
// Edge from vertex 2 to vertex 3
adj[2].push_back(3);
adj[3].push_back(2);
cout << maxEdges(adj, nodes);
return 0;
}
Java
// Java program to find the connected component
// with maximum number of edges
import java.util.*;
class GFG
{
// DFS function
static int dfs(int s, Vector> adj,boolean visited[],
int nodes)
{
// Adding all the edges connected to the vertex
int adjListSize = adj.get(s).size();
visited[s] = true;
for (int i = 0; i < adj.get(s).size(); i++)
{
if (visited[adj.get(s).get(i)] == false)
{
adjListSize += dfs(adj.get(s).get(i), adj, visited, nodes);
}
}
return adjListSize;
}
static int maxEdges(Vector> adj, int nodes)
{
int res = Integer.MIN_VALUE;
boolean visited[]=new boolean[nodes+1];
for (int i = 1; i <= nodes; i++)
{
if (visited[i] == false)
{
int adjListSize = dfs(i, adj, visited, nodes);
res = Math.max(res, adjListSize/2);
}
}
return res;
}
// Driver code
public static void main(String args[])
{
int nodes = 3;
Vector> adj=new Vector>();
for(int i = 0; i < nodes + 1; i++)
adj.add(new Vector());
// Edge from vertex 1 to vertex 2
adj.get(1).add(2);
adj.get(2).add(1);
// Edge from vertex 2 to vertex 3
adj.get(2).add(3);
adj.get(3).add(2);
System.out.println(maxEdges(adj, nodes));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 program to find the connected
# component with maximum number of edges
from sys import maxsize
INT_MIN = -maxsize
# DFS function
def dfs(s: int, adj: list,
visited: list, nodes: int) -> int:
# Adding all the edges
# connected to the vertex
adjListSize = len(adj[s])
visited[s] = True
for i in range(len(adj[s])):
if visited[adj[s][i]] == False:
adjListSize += dfs(adj[s][i], adj,
visited, nodes)
return adjListSize
def maxEdges(adj: list, nodes: int) -> int:
res = INT_MIN
visited = [False] * (nodes + 1)
for i in range(1, nodes + 1):
if visited[i] == False:
adjListSize = dfs(i, adj,
visited, nodes)
res = max(res, adjListSize // 2)
return res
# Driver Code
if __name__ == "__main__":
nodes = 3
adj = [0] * (nodes + 1)
for i in range(nodes + 1):
adj[i] = []
# Edge from vertex 1 to vertex 2
adj[1].append(2)
adj[2].append(1)
# Edge from vertex 2 to vertex 3
adj[2].append(3)
adj[3].append(2)
print(maxEdges(adj, nodes))
# This code is contributed by sanjeev2552
C#
// C# program to find the connected component
// with maximum number of edges
using System;
using System.Collections.Generic;
class GFG
{
// DFS function
static int dfs(int s, List> adj,
bool []visited, int nodes)
{
// Adding all the edges connected to the vertex
int adjListSize = adj[s].Count;
visited[s] = true;
for (int i = 0; i < adj[s].Count; i++)
{
if (visited[adj[s][i]] == false)
{
adjListSize += dfs(adj[s][i], adj,
visited, nodes);
}
}
return adjListSize;
}
static int maxEdges(List> adj, int nodes)
{
int res = int.MinValue;
bool []visited = new bool[nodes + 1];
for (int i = 1; i <= nodes; i++)
{
if (visited[i] == false)
{
int adjListSize = dfs(i, adj, visited, nodes);
res = Math.Max(res, adjListSize / 2);
}
}
return res;
}
// Driver code
public static void Main(String []args)
{
int nodes = 3;
List> adj = new List>();
for(int i = 0; i < nodes + 1; i++)
adj.Add(new List());
// Edge from vertex 1 to vertex 2
adj[1].Add(2);
adj[2].Add(1);
// Edge from vertex 2 to vertex 3
adj[2].Add(3);
adj[3].Add(2);
Console.WriteLine(maxEdges(adj, nodes));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
2
时间复杂度: O(nodes + edges)(与 DFS 相同)
注意:我们也可以使用 BFS 来解决这个问题。我们只需要遍历无向图中的连通分量。