Given an Undirected Graph consisting of **N** vertices and **M** edges, where node values are in the range **[1, N]**, and vertices specified by the array **colored[]** are colored, the task is to find the minimum color all vertices of the given graph. The cost to color a vertex is given by **vCost **and the cost to add a new edge between two vertices is given by **eCost**. If a vertex is colored, then all the vertices that can be reached from that vertex also becomes colored.

**Examples:**

Input:N = 3, M = 1, vCost = 3, eCost = 2, colored[] = {1}, source[] = {1} destination[] = {2}

Output:2

Explanation:

Vertex 1 is colored and it has an edge with 2.

So, vertex 2 is also colored.

Add an edge between 2 and 3, at a cost of eCost. < vCost.

Hence, the output is 2.

Input:N = 4, M = 2, vCost = 3, eCost = 7, colored[] = {1, 3}, source[] = {1, 2} destination[] = {4, 3}

Output:0

Explanation:

Vertex 1 is colored and it has an edge with 4. Hence, vertex 4 is also colored.

Vertex 2 is colored and it has an edge with 3. Hence, vertex 3 is also colored.

Since all the vertices are already colored, therefore, the cost is 0.

**Approach:**

The idea is to **count the number of sub-graphs** of uncolored vertices using DFS Traversal.

To minimize the cost of coloring an **uncolored Subgraph**, one of the following needs to be done:

- Color the subgraph
- Add an edge between any colored and uncolored vertex.

Based on the minimum of * eCost and vCost*, one of the above two steps needs to be chosen.

If the number of uncolored sub-graphs is given by

**X**, then the total cost of coloring all the vertices is given by

*.*

**X×min(eCost, vCost)**Follow the steps below to find the number of uncolored sub-graphs:

- Perform DFS Traversal on all the colored vertices and mark them visited to identify them as colored.
- The vertices that are not visited after DFS at step 1 are the uncolored vertices.
- For each uncolored vertex, mark all the vertices that can be reached from that vertex as visited using DFS.
- The number of uncolored vertices for which the DFS at step 3 occurs, is the number of sub-graphs
**X.** - Calculate the total cost of coloring all the vertices by the formula
**X×min(eCost, vCost).**

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to implement DFS Traversal ` `// to marks all the vertices visited ` `// from vertex U ` `void` `DFS(` `int` `U, ` `int` `* vis, vector<` `int` `> adj[]) ` `{ ` ` ` `// Mark U as visited ` ` ` `vis[U] = 1; ` ` ` ` ` `// Traverse the adjacency list of U ` ` ` `for` `(` `int` `V : adj[U]) { ` ` ` `if` `(vis[V] == 0) ` ` ` `DFS(V, vis, adj); ` ` ` `} ` `} ` ` ` `// Function to find the minimum cost ` `// to color all the vertices of graph ` `void` `minCost(` `int` `N, ` `int` `M, ` `int` `vCost, ` ` ` `int` `eCost, ` `int` `source[], ` ` ` `vector<` `int` `> colored, ` ` ` `int` `destination[]) ` `{ ` ` ` `// To store adjacency list ` ` ` `vector<` `int` `> adj[N + 1]; ` ` ` ` ` `// Loop through the edges to ` ` ` `// create adjacency list ` ` ` `for` `(` `int` `i = 0; i < M; i++) { ` ` ` ` ` `adj].push_back(destination[i]); ` ` ` `adj[destination[i]].push_back(source[i]); ` ` ` `} ` ` ` ` ` `// To check if a vertex of the ` ` ` `// graph is visited ` ` ` `int` `vis[N + 1] = { 0 }; ` ` ` ` ` `// Mark visited to all the vertices ` ` ` `// that can be reached by ` ` ` `// colored vertices ` ` ` `for` `(` `int` `i = 0; i < colored.size(); i++) { ` ` ` ` ` `// Perform DFS ` ` ` `DFS(colored[i], vis, adj); ` ` ` `} ` ` ` ` ` `// To store count of uncolored ` ` ` `// sub-graphs ` ` ` `int` `X = 0; ` ` ` ` ` `// Loop through vertex to count ` ` ` `// uncolored sub-graphs ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` ` ` `// If vertex not visited ` ` ` `if` `(vis[i] == 0) { ` ` ` ` ` `// Increase count of ` ` ` `// uncolored sub-graphs ` ` ` `X++; ` ` ` ` ` `// Perform DFS to mark ` ` ` `// visited to all vertices ` ` ` `// of current sub-graphs ` ` ` `DFS(i, vis, adj); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Calculate minimum cost to color ` ` ` `// all vertices ` ` ` `int` `mincost = X * min(vCost, eCost); ` ` ` ` ` `// Print the result ` ` ` `cout << mincost << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// Given number of ` ` ` `// vertices and edges ` ` ` `int` `N = 3, M = 1; ` ` ` ` ` `// Given edges ` ` ` `int` `source[] = { 1 }; ` ` ` `int` `destination[] = { 2 }; ` ` ` ` ` `// Given cost of coloring ` ` ` `// and adding an edge ` ` ` `int` `vCost = 3, eCost = 2; ` ` ` ` ` `// Given array of ` ` ` `// colored vertices ` ` ` `vector<` `int` `> colored = { 1}; ` ` ` ` ` `minCost(N, M, vCost, eCost, ` ` ` `source, colored, destination); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N + M)

**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimize cost to color all the vertices of an Undirected Graph using given operation
- Find if there is a path between two vertices in an undirected graph
- Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Sum of the minimum elements in all connected components of an undirected graph
- Print all the cycles in an undirected graph
- Product of lengths of all cycles in an undirected graph
- Maximum number of edges among all connected components of an undirected graph
- Sum of degrees of all nodes of a undirected graph
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Find all cliques of size K in an undirected graph
- Largest subarray sum of all connected components in undirected graph
- Print all shortest paths between given source and destination in an undirected graph
- Maximum sum of values of nodes among all connected components of an undirected graph
- Construct a graph from given degrees of all vertices
- Finding in and out degrees of all vertices in a graph

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.