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 become 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 sorc[], 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[sorc[i]].push_back(destination[i]); adj[destination[i]].push_back(sorc[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 sorc[] = { 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, sorc, colored, destination); return 0; } |
Java
// Java program to implement // the above approach import java.util.*; class GFG{ // Function to implement DFS Traversal // to marks all the vertices visited // from vertex U static void DFS( int U, int [] vis, ArrayList<ArrayList<Integer>> adj) { // Mark U as visited vis[U] = 1 ; // Traverse the adjacency list of U for (Integer V : adj.get(U)) { if (vis[V] == 0 ) DFS(V, vis, adj); } } // Function to find the minimum cost // to color all the vertices of graph static void minCost( int N, int M, int vCost, int eCost, int sorc[], ArrayList<Integer> colored, int destination[]) { // To store adjacency list ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); for ( int i = 0 ; i < N + 1 ; i++) adj.add( new ArrayList<Integer>()); // Loop through the edges to // create adjacency list for ( int i = 0 ; i < M; i++) { adj.get(sorc[i]).add(destination[i]); adj.get(destination[i]).add(sorc[i]); } // To check if a vertex of the // graph is visited int [] vis = new int [N + 1 ]; // 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.get(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 * Math.min(vCost, eCost); // Print the result System.out.println(mincost); } // Driver code public static void main(String[] args) { // Given number of // vertices and edges int N = 3 , M = 1 ; // Given edges int sorc[] = { 1 }; int destination[] = { 2 }; // Given cost of coloring // and adding an edge int vCost = 3 , eCost = 2 ; // Given array of // colored vertices ArrayList<Integer> colored = new ArrayList<>(); colored.add( 1 ); minCost(N, M, vCost, eCost, sorc, colored, destination); } } // This code is contributed by offbeat |
Python3
# Python3 program to implement # the above approach # Function to implement DFS Traversal # to marks all the vertices visited # from vertex U def DFS(U, vis, adj): # Mark U as visited vis[U] = 1 # Traverse the adjacency list of U for V in adj[U]: if (vis[V] = = 0 ): DFS(V, vis, adj) # Function to find the minimum cost # to color all the vertices of graph def minCost(N, M, vCost, eCost, sorc, colored, destination): # To store adjacency list adj = [[] for i in range (N + 1 )] # Loop through the edges to # create adjacency list for i in range (M): adj[sorc[i]].append(destination[i]) adj[destination[i]].append(sorc[i]) # To check if a vertex of the # graph is visited vis = [ 0 ] * (N + 1 ) # Mark visited to all the vertices # that can be reached by # colored vertices for i in range ( len (colored)): # Perform DFS DFS(colored[i], vis, adj) # To store count of uncolored # sub-graphs X = 0 # Loop through vertex to count # uncolored sub-graphs for i in range ( 1 , N + 1 ): # If vertex not visited if (vis[i] = = 0 ): # Increase count of # uncolored sub-graphs X + = 1 # Perform DFS to mark # visited to all vertices # of current sub-graphs DFS(i, vis, adj) # Calculate minimum cost to color # all vertices mincost = X * min (vCost, eCost) # Print the result print (mincost) # Driver Code if __name__ = = '__main__' : # Given number of # vertices and edges N = 3 M = 1 # Given edges sorc = [ 1 ] destination = [ 2 ] # Given cost of coloring # and adding an edge vCost = 3 eCost = 2 # Given array of # colored vertices colored = [ 1 ] minCost(N, M, vCost, eCost, sorc, colored, destination) # This code is contributed by mohit kumar 29 |
C#
// C# program to implement // the above approach using System; using System.Collections; using System.Collections.Generic; class GFG{ // Function to implement DFS Traversal // to marks all the vertices visited // from vertex U static void DFS( int U, int [] vis, ArrayList adj) { // Mark U as visited vis[U] = 1; // Traverse the adjacency list of U foreach ( int V in (ArrayList)adj[U]) { if (vis[V] == 0) DFS(V, vis, adj); } } // Function to find the minimum cost // to color all the vertices of graph static void minCost( int N, int M, int vCost, int eCost, int []sorc, ArrayList colored, int []destination) { // To store adjacency list ArrayList adj = new ArrayList(); for ( int i = 0; i < N + 1; i++) adj.Add( new ArrayList()); // Loop through the edges to // create adjacency list for ( int i = 0; i < M; i++) { ((ArrayList)adj[sorc[i]]).Add(destination[i]); ((ArrayList)adj[destination[i]]).Add(sorc[i]); } // To check if a vertex of the // graph is visited int [] vis = new int [N + 1]; // Mark visited to all the vertices // that can be reached by // colored vertices for ( int i = 0; i < colored.Count; i++) { // Perform DFS DFS(( int )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 * Math.Min(vCost, eCost); // Print the result Console.Write(mincost); } // Driver code public static void Main( string [] args) { // Given number of // vertices and edges int N = 3, M = 1; // Given edges int []sorc = {1}; int []destination = {2}; // Given cost of coloring // and adding an edge int vCost = 3, eCost = 2; // Given array of // colored vertices ArrayList colored = new ArrayList(); colored.Add(1); minCost(N, M, vCost, eCost, sorc, colored, destination); } } // This code is contributed by rutvik_56 |
Javascript
<script> // Javascript program to implement // the above approach // Function to implement DFS Traversal // to marks all the vertices visited // from vertex U function DFS(U, vis, adj) { // Mark U as visited vis[U] = 1; // Traverse the adjacency list of U for ( var V of adj[U]) { if (vis[V] == 0) DFS(V, vis, adj); } } // Function to find the minimum cost // to color all the vertices of graph function minCost( N, M, vCost, eCost, sorc, colored, destination) { // To store adjacency list var adj = []; for ( var i = 0; i < N + 1; i++) adj.push( new Array()); // Loop through the edges to // create adjacency list for ( var i = 0; i < M; i++) { (adj[sorc[i]]).push(destination[i]); (adj[destination[i]]).push(sorc[i]); } // To check if a vertex of the // graph is visited var vis = Array(N+1).fill(0); // Mark visited to all the vertices // that can be reached by // colored vertices for ( var i = 0; i < colored.length; i++) { // Perform DFS DFS(colored[i], vis, adj); } // To store count of uncolored // sub-graphs var X = 0; // Loop through vertex to count // uncolored sub-graphs for ( var 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 var mincost = X * Math.min(vCost, eCost); // Print the result document.write(mincost); } // Driver code // Given number of // vertices and edges var N = 3, M = 1; // Given edges var sorc = [1]; var destination = [2]; // Given cost of coloring // and adding an edge var vCost = 3, eCost = 2; // Given array of // colored vertices var colored = []; colored.push(1); minCost(N, M, vCost, eCost, sorc, colored, destination); </script> |
2
Time Complexity: O(N + M)
Auxiliary Space: O(N)
Please Login to comment...