Sum of the minimum elements in all connected components of an undirected graph
Given an array A of N numbers where A
i
represent the value of the (i+1)
th
node. Also given are M pair of edges where u and v represent the nodes that are connected by an edge. The task is to find the sum of the minimum element in all the connected components of the given undirected graph. If a node has no connectivity to any other node, count it as a component with one node.
Examples:
Input: a[] = {1, 6, 2, 7, 3, 8, 4, 9, 5, 10} m = 5 1 2 3 4 5 6 7 8 9 10 Output: 15 Connected components are: 1–2, 3–4, 5–6, 7–8 and 9–10 Sum of Minimum of all them : 1 + 2 + 3 + 4 + 5 = 15 Input: a[] = {2, 5, 3, 4, 8} m = 2 1 4 4 5 Output: 10
Approach:
Finding connected components for an undirected graph is an easier task. Doing either a
BFS
or
DFS
starting from every unvisited vertex will give us our connected components. Create a
visited[]
array which has initially all nodes marked as False. Iterate all the nodes, if the node is not visited, call
DFS()
function so that all the nodes connected directly or indirectly to the node are marked as visited. While visiting all the directly or indirectly connected nodes, store the minimum value of all nodes. Create a variable
sum
which stores the summation of the minimum of all these connected components. Once all the nodes are visited,
sum
will have the answer to the problem. Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
vector< int > graph[N];
bool visited[N];
void dfs( int node, int a[], int mini)
{
mini = min(mini, a[node]);
visited[node] = true ;
for ( int i : graph[node]) {
if (!visited[i])
dfs(i, a, mini);
}
}
void addedge( int u, int v)
{
graph[u - 1].push_back(v - 1);
graph[v - 1].push_back(u - 1);
}
int minimumSumConnectedComponents( int a[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
if (!visited[i]) {
int mini = a[i];
dfs(i, a, mini);
sum += mini;
}
}
return sum;
}
int main()
{
int a[] = {1, 6, 2, 7, 3, 8, 4, 9, 5, 10};
addedge(1, 2);
addedge(3, 4);
addedge(5, 6);
addedge(7, 8);
addedge(9, 10);
int n = sizeof (a) / sizeof (a[0]);
cout << minimumSumConnectedComponents(a, n);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class ConnectedComponents {
static final int N = 10000 ;
static List<Integer>[] graph = new ArrayList[N];
static boolean [] visited = new boolean [N];
static void dfs( int node, int [] a, int [] mini)
{
mini[ 0 ] = Math.min(mini[ 0 ], a[node]);
visited[node] = true ;
for ( int i : graph[node]) {
if (!visited[i]) {
dfs(i, a, mini);
}
}
}
static void addEdge( int u, int v)
{
graph[u - 1 ].add(v - 1 );
graph[v - 1 ].add(u - 1 );
}
static int minimumSumConnectedComponents( int [] a, int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++) {
if (!visited[i]) {
int [] mini = { a[i] };
dfs(i, a, mini);
sum += mini[ 0 ];
}
}
return sum;
}
public static void main(String[] args)
{
for ( int i = 0 ; i < N; i++) {
graph[i] = new ArrayList<>();
}
int [] a = { 1 , 6 , 2 , 7 , 3 , 8 , 4 , 9 , 5 , 10 };
addEdge( 1 , 2 );
addEdge( 3 , 4 );
addEdge( 5 , 6 );
addEdge( 7 , 8 );
addEdge( 9 , 10 );
int n = a.length;
System.out.println(
minimumSumConnectedComponents(a, n));
}
}
|
Python
from collections import defaultdict
N = 10000
graph = defaultdict( list )
visited = [ False ] * N
def dfs(node, a, mini):
mini[ 0 ] = min (mini[ 0 ], a[node])
visited[node] = True
for i in graph[node]:
if not visited[i]:
dfs(i, a, mini)
def add_edge(u, v):
graph[u - 1 ].append(v - 1 )
graph[v - 1 ].append(u - 1 )
def minimum_sum_connected_components(a, n):
total_sum = 0
for i in range (n):
if not visited[i]:
mini = [a[i]]
dfs(i, a, mini)
total_sum + = mini[ 0 ]
return total_sum
if __name__ = = "__main__" :
a = [ 1 , 6 , 2 , 7 , 3 , 8 , 4 , 9 , 5 , 10 ]
add_edge( 1 , 2 )
add_edge( 3 , 4 )
add_edge( 5 , 6 )
add_edge( 7 , 8 )
add_edge( 9 , 10 )
n = len (a)
print (minimum_sum_connected_components(a, n))
|
C#
using System;
using System.Collections.Generic;
class Program {
const int N = 10000;
static List< int >[] graph = new List< int >[ N ];
static bool [] visited = new bool [N];
static void DFS( int node, int [] a, ref int mini)
{
mini = Math.Min(mini, a[node]);
visited[node] = true ;
foreach ( int i in graph[node])
{
if (!visited[i])
DFS(i, a, ref mini);
}
}
static void AddEdge( int u, int v)
{
graph[u - 1].Add(v - 1);
graph[v - 1].Add(u - 1);
}
static int MinimumSumConnectedComponents( int [] a, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
if (!visited[i]) {
int mini = a[i];
DFS(i, a, ref mini);
sum += mini;
}
}
return sum;
}
static void Main()
{
int [] a = { 1, 6, 2, 7, 3, 8, 4, 9, 5, 10 };
for ( int i = 0; i < N; i++) {
graph[i] = new List< int >();
}
AddEdge(1, 2);
AddEdge(3, 4);
AddEdge(5, 6);
AddEdge(7, 8);
AddEdge(9, 10);
int n = a.Length;
Console.WriteLine(
MinimumSumConnectedComponents(a, n));
}
}
|
Javascript
const N = 10000;
const graph = new Array(N).fill( null ).map(() => []);
const visited = new Array(N).fill( false );
function dfs(node, a, mini) {
mini[0] = Math.min(mini[0], a[node]);
visited[node] = true ;
for (const i of graph[node]) {
if (!visited[i]) {
dfs(i, a, mini);
}
}
}
function addEdge(u, v) {
graph[u - 1].push(v - 1);
graph[v - 1].push(u - 1);
}
function minimumSumConnectedComponents(a, n) {
let sum = 0;
for (let i = 0; i < n; i++) {
if (!visited[i]) {
const mini = [a[i]];
dfs(i, a, mini);
sum += mini[0];
}
}
return sum;
}
for (let i = 0; i < N; i++) {
graph[i] = [];
}
const a = [1, 6, 2, 7, 3, 8, 4, 9, 5, 10];
addEdge(1, 2);
addEdge(3, 4);
addEdge(5, 6);
addEdge(7, 8);
addEdge(9, 10);
const n = a.length;
console.log(minimumSumConnectedComponents(a, n));
|
Last Updated :
29 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...