Count of nodes with maximum connection in an undirected graph
Last Updated :
22 Feb, 2022
Given an undirected graph with N nodes and E edges, followed by E edge connections. The task is to find the count of nodes with maximum connections
Examples:
Input: N =10, E =13,
edges[][] = { {1, 4}, {2, 3}, {4, 5}, {3, 9}, {6, 9}, {3, 8}, {10, 4},
{2, 7}, {3, 6}, {2, 8}, {9, 2}, {1, 10}, {9, 10} }
Output: 3
Explanation: The connections for each of the nodes are show below:
- 1 -> 4, 10
- 2 -> 3, 7, 8, 9
- 3 -> 2, 9, 8, 6
- 4 -> 1, 5, 10
- 5 -> 4
- 6 -> 9, 3
- 7 -> 2
- 8 -> 3, 2
- 9 -> 3, 6, 2, 10
- 10 -> 4, 9, 1
Therefore, number of nodes with maximum connections are 3 viz. {2, 3, 9}
Input: N = 8, E = 7,
edges[][] = { {0, 2}, {1, 5}, {2, 3}, {5, 7}, {2, 4}, {5, 6}, {1, 2} }
Output: 3
Approach: The task can be solved by storing the number of connected nodes for every node inside a vector. And then, find the maximum connected nodes to any of the nodes & get its count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void get(map< int , vector< int > > graph)
{
vector< int > v;
int mx = -1;
for ( int i = 0; i < graph.size(); i++) {
v.push_back(graph[i].size());
mx = max(mx, ( int )graph[i].size());
}
int cnt = 0;
for ( auto i : v) {
if (i == mx)
cnt++;
}
cout << cnt << endl;
}
int main()
{
map< int , vector< int > > graph;
int nodes = 10, edges = 13;
graph[1].push_back(4);
graph[4].push_back(1);
graph[2].push_back(3);
graph[3].push_back(2);
graph[4].push_back(5);
graph[5].push_back(4);
graph[3].push_back(9);
graph[9].push_back(3);
graph[6].push_back(9);
graph[9].push_back(6);
graph[3].push_back(8);
graph[8].push_back(3);
graph[10].push_back(4);
graph[4].push_back(10);
graph[2].push_back(7);
graph[7].push_back(2);
graph[3].push_back(6);
graph[6].push_back(3);
graph[2].push_back(8);
graph[8].push_back(2);
graph[9].push_back(2);
graph[2].push_back(9);
graph[1].push_back(10);
graph[10].push_back(1);
graph[9].push_back(10);
graph[10].push_back(9);
get(graph);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.HashMap;
class GFG {
static void get(HashMap<Integer, ArrayList<Integer>> graph)
{
ArrayList<Integer> v = new ArrayList<Integer>();
int mx = - 1 ;
for ( int i = 0 ; i < graph.size(); i++) {
v.add(graph.get(i).size());
mx = Math.max(mx, ( int ) graph.get(i).size());
}
int cnt = 0 ;
for ( int i : v) {
if (i == mx)
cnt++;
}
System.out.println(cnt);
}
public static void main(String args[]) {
HashMap<Integer, ArrayList<Integer>> graph = new HashMap<Integer, ArrayList<Integer>>();
int nodes = 10 , edges = 13 ;
for ( int i = 0 ; i <= nodes; i++) {
graph.put(i, new ArrayList<>());
}
graph.get( 1 ).add( 4 );
graph.get( 4 ).add( 1 );
graph.get( 2 ).add( 3 );
graph.get( 3 ).add( 2 );
graph.get( 4 ).add( 5 );
graph.get( 5 ).add( 4 );
graph.get( 3 ).add( 9 );
graph.get( 9 ).add( 3 );
graph.get( 6 ).add( 9 );
graph.get( 9 ).add( 6 );
graph.get( 3 ).add( 8 );
graph.get( 8 ).add( 3 );
graph.get( 10 ).add( 4 );
graph.get( 4 ).add( 10 );
graph.get( 2 ).add( 7 );
graph.get( 7 ).add( 2 );
graph.get( 3 ).add( 6 );
graph.get( 6 ).add( 3 );
graph.get( 2 ).add( 8 );
graph.get( 8 ).add( 2 );
graph.get( 9 ).add( 2 );
graph.get( 2 ).add( 9 );
graph.get( 1 ).add( 10 );
graph.get( 10 ).add( 1 );
graph.get( 9 ).add( 10 );
graph.get( 10 ).add( 9 );
get(graph);
}
}
|
Python3
def get(graph):
v = [];
mx = - 1 ;
for arr in graph.values():
v.append( len (arr));
mx = max (mx, ( len (arr)));
cnt = 0 ;
for i in v:
if (i = = mx):
cnt + = 1
print (cnt)
graph = {}
nodes = 10
edges = 13 ;
for i in range ( 1 , nodes + 1 ):
graph[i] = []
graph[ 1 ].append( 4 );
graph[ 4 ].append( 1 );
graph[ 2 ].append( 3 );
graph[ 3 ].append( 2 );
graph[ 4 ].append( 5 );
graph[ 5 ].append( 4 );
graph[ 3 ].append( 9 );
graph[ 9 ].append( 3 );
graph[ 6 ].append( 9 );
graph[ 9 ].append( 6 );
graph[ 3 ].append( 8 );
graph[ 8 ].append( 3 );
graph[ 10 ].append( 4 );
graph[ 4 ].append( 10 );
graph[ 2 ].append( 7 );
graph[ 7 ].append( 2 );
graph[ 3 ].append( 6 );
graph[ 6 ].append( 3 );
graph[ 2 ].append( 8 );
graph[ 8 ].append( 2 );
graph[ 9 ].append( 2 );
graph[ 2 ].append( 9 );
graph[ 1 ].append( 10 );
graph[ 10 ].append( 1 );
graph[ 9 ].append( 10 );
graph[ 10 ].append( 9 );
get(graph);
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void get (Dictionary< int , List< int >> graph)
{
List< int > v = new List< int >();
int mx = -1;
for ( int i = 0; i < graph.Count; i++) {
v.Add(graph[i].Count);
mx = Math.Max(mx, ( int ) graph[i].Count);
}
int cnt = 0;
foreach ( int i in v) {
if (i == mx)
cnt++;
}
Console.WriteLine(cnt);
}
public static void Main(String []args) {
Dictionary< int , List< int >> graph = new Dictionary< int , List< int >>();
int nodes = 10;
for ( int i = 0; i <= nodes; i++) {
graph.Add(i, new List< int >());
}
graph[1].Add(4);
graph[4].Add(1);
graph[2].Add(3);
graph[3].Add(2);
graph[4].Add(5);
graph[5].Add(4);
graph[3].Add(9);
graph[9].Add(3);
graph[6].Add(9);
graph[9].Add(6);
graph[3].Add(8);
graph[8].Add(3);
graph[10].Add(4);
graph[4].Add(10);
graph[2].Add(7);
graph[7].Add(2);
graph[3].Add(6);
graph[6].Add(3);
graph[2].Add(8);
graph[8].Add(2);
graph[9].Add(2);
graph[2].Add(9);
graph[1].Add(10);
graph[10].Add(1);
graph[9].Add(10);
graph[10].Add(9);
get (graph);
}
}
|
Javascript
<script>
function get(graph)
{
let v = [];
let mx = -1;
for (let [key, arr] of graph) {
v.push(arr.length);
mx = Math.max(mx, (arr.length));
}
let cnt = 0;
for (let i of v) {
if (i == mx)
cnt++;
}
document.write(cnt + "<br>" )
}
let graph = new Map();
let nodes = 10, edges = 13;
for (let i = 1; i <= nodes; i++) {
graph.set(i, []);
}
graph.get(1).push(4);
graph.get(4).push(1);
graph.get(2).push(3);
graph.get(3).push(2);
graph.get(4).push(5);
graph.get(5).push(4);
graph.get(3).push(9);
graph.get(9).push(3);
graph.get(6).push(9);
graph.get(9).push(6);
graph.get(3).push(8);
graph.get(8).push(3);
graph.get(10).push(4);
graph.get(4).push(10);
graph.get(2).push(7);
graph.get(7).push(2);
graph.get(3).push(6);
graph.get(6).push(3);
graph.get(2).push(8);
graph.get(8).push(2);
graph.get(9).push(2);
graph.get(2).push(9);
graph.get(1).push(10);
graph.get(10).push(1);
graph.get(9).push(10);
graph.get(10).push(9);
get(graph);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...