# 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:

`// C++ program to find the sum ` `// of the minimum elements in all ` `// connected components of an undirected graph ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `const` `int` `N = 10000; ` `vector<` `int` `> graph[N]; ` ` ` `// Initially all nodes ` `// marked as unvisited ` `bool` `visited[N]; ` ` ` `// DFS function that visits all ` `// connected nodes from a given node ` `void` `dfs(` `int` `node, ` `int` `a[], ` `int` `mini) ` `{ ` ` ` `// Stores the minimum ` ` ` `mini = min(mini, a[node]); ` ` ` ` ` `// Marks node as visited ` ` ` `visited[node] = ` `true` `; ` ` ` ` ` `// Traversed in all the connected nodes ` ` ` `for` `(` `int` `i : graph[node]) { ` ` ` `if` `(!visited[i]) ` ` ` `dfs(i, a, mini); ` ` ` `} ` `} ` ` ` `// Function to add the edges ` `void` `addedge(` `int` `u, ` `int` `v) ` `{ ` ` ` `graph[u - 1].push_back(v - 1); ` ` ` `graph[v - 1].push_back(u - 1); ` `} ` ` ` `// Function that returns the sum of all minimums ` `// of connected componenets of graph ` `int` `minimumSumConnectedComponents(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Initially sum is 0 ` ` ` `int` `sum = 0; ` ` ` ` ` `// Traverse for all nodes ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(!visited[i]) { ` ` ` `int` `mini = a[i]; ` ` ` `dfs(i, a, mini); ` ` ` `sum += mini; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Returns the answer ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = {1, 6, 2, 7, 3, 8, 4, 9, 5, 10}; ` ` ` ` ` `// Add edges ` ` ` `addedge(1, 2); ` ` ` `addedge(3, 4); ` ` ` `addedge(5, 6); ` ` ` `addedge(7, 8); ` ` ` `addedge(9, 10); ` ` ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `// Calling Function ` ` ` `cout << minimumSumConnectedComponents(a, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

15

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Connected Components in an undirected graph
- Largest subarray sum of all connected components in undirected graph
- Clone an undirected graph with multiple connected components
- Count of unique lengths of connected components for an undirected graph using STL
- Maximum number of edges among all connected components of an undirected graph
- Maximum sum of values of nodes among all connected components of an undirected graph
- Program to count Number of connected components in an undirected graph
- Convert undirected connected graph to strongly connected directed graph
- Cycles of length n in an undirected and connected graph
- Number of single cycle components in an undirected graph
- Octal equivalents of connected components in Binary valued graph
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Check if longest connected component forms a palindrome in undirected graph
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Find minimum weight cycle in an undirected graph
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Strongly Connected Components
- Number of connected components in a 2-D matrix of strings
- Check if a Tree can be split into K equal connected components
- Check if the length of all connected components is a Fibonacci number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.