Maximum number of edges among all connected components of an undirected graph
Last Updated :
20 Feb, 2023
Given integers ‘N’ and ‘K’ where, N is the number of vertices of an undirected graph and ‘K’ denotes the number of edges in the same graph (each edge is denoted by a pair of integers where i, j means that the vertex ‘i’ is directly connected to the vertex ‘j’ in the graph).
The task is to find the maximum number of edges among all the connected components in the given graph.
Examples:
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
Approach:
- Using Depth First Search, find the sum of the degrees of each of the edges in all the connected components separately.
- Now, according to Handshaking Lemma, the total number of edges in a connected component of an undirected graph is equal to half of the total sum of the degrees of all of its vertices.
- Print the maximum number of edges among all the connected components.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int dfs( int s, vector< int > adj[], vector< bool > visited,
int nodes)
{
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< int > adj[], int nodes)
{
int res = INT_MIN;
vector< bool > 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;
}
int main()
{
int nodes = 3;
vector< int > adj[nodes+1];
adj[1].push_back(2);
adj[2].push_back(1);
adj[2].push_back(3);
adj[3].push_back(2);
cout << maxEdges(adj, nodes);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int dfs( int s, Vector<Vector<Integer>> adj, boolean visited[],
int nodes)
{
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<Vector<Integer>> 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;
}
public static void main(String args[])
{
int nodes = 3 ;
Vector<Vector<Integer>> adj= new Vector<Vector<Integer>>();
for ( int i = 0 ; i < nodes + 1 ; i++)
adj.add( new Vector<Integer>());
adj.get( 1 ).add( 2 );
adj.get( 2 ).add( 1 );
adj.get( 2 ).add( 3 );
adj.get( 3 ).add( 2 );
System.out.println(maxEdges(adj, nodes));
}
}
|
Python3
from sys import maxsize
INT_MIN = - maxsize
def dfs(s: int , adj: list ,
visited: list , nodes: int ) - > int :
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
if __name__ = = "__main__" :
nodes = 3
adj = [ 0 ] * (nodes + 1 )
for i in range (nodes + 1 ):
adj[i] = []
adj[ 1 ].append( 2 )
adj[ 2 ].append( 1 )
adj[ 2 ].append( 3 )
adj[ 3 ].append( 2 )
print (maxEdges(adj, nodes))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int dfs( int s, List<List< int >> adj,
bool []visited, int nodes)
{
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<List< int >> 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;
}
public static void Main(String []args)
{
int nodes = 3;
List<List< int >> adj = new List<List< int >>();
for ( int i = 0; i < nodes + 1; i++)
adj.Add( new List< int >());
adj[1].Add(2);
adj[2].Add(1);
adj[2].Add(3);
adj[3].Add(2);
Console.WriteLine(maxEdges(adj, nodes));
}
}
|
Javascript
<script>
function dfs(s,adj,visited,nodes)
{
let adjListSize = adj[s].length;
visited[s] = true ;
for (let i = 0; i < adj[s].length; i++)
{
if (visited[adj[s][i]] == false )
{
adjListSize += dfs(adj[s][i], adj, visited, nodes);
}
}
return adjListSize;
}
function maxEdges(adj,nodes)
{
let res = Number.MIN_VALUE;
let visited= new Array(nodes+1);
for (let i=0;i<nodes+1;i++)
{
visited[i]= false ;
}
for (let i = 1; i <= nodes; i++)
{
if (visited[i] == false )
{
let adjListSize = dfs(i, adj, visited, nodes);
res = Math.max(res, adjListSize/2);
}
}
return res;
}
let nodes = 3;
let adj=[];
for (let i = 0; i < nodes + 1; i++)
adj.push([]);
adj[1].push(2);
adj[2].push(1);
adj[2].push(3);
adj[3].push(2);
document.write(maxEdges(adj, nodes)+ "<br>" );
</script>
|
Time Complexity : O(nodes + edges) (Same as DFS)
Space Complexity: O(V). We use a visited array of size V.
Note : We can also use BFS to solve this problem. We simply need to traverse connected components in an undirected graph.
Share your thoughts in the comments
Please Login to comment...