Minimize cost to color all the vertices of an Undirected Graph

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:

  1. Perform DFS Traversal on all the colored vertices and mark them visited to identify them as colored.
  2. The vertices that are not visited after DFS at step 1 are the uncolored vertices.
  3. For each uncolored vertex, mark all the vertices that can be reached from that vertex as visited using DFS.
  4. The number of uncolored vertices for which the DFS at step 3 occurs, is the number of sub-graphs X.
  5. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.