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++
#include <bits/stdc++.h>
using namespace std;
void DFS( int U, int * vis, vector< int > adj[])
{
vis[U] = 1;
for ( int V : adj[U]) {
if (vis[V] == 0)
DFS(V, vis, adj);
}
}
void minCost( int N, int M, int vCost,
int eCost, int sorc[],
vector< int > colored,
int destination[])
{
vector< int > adj[N + 1];
for ( int i = 0; i < M; i++) {
adj[sorc[i]].push_back(destination[i]);
adj[destination[i]].push_back(sorc[i]);
}
int vis[N + 1] = { 0 };
for ( int i = 0; i < colored.size(); i++) {
DFS(colored[i], vis, adj);
}
int X = 0;
for ( int i = 1; i <= N; i++) {
if (vis[i] == 0) {
X++;
DFS(i, vis, adj);
}
}
int mincost = X * min(vCost, eCost);
cout << mincost << endl;
}
int main()
{
int N = 3, M = 1;
int sorc[] = { 1 };
int destination[] = { 2 };
int vCost = 3, eCost = 2;
vector< int > colored = { 1};
minCost(N, M, vCost, eCost,
sorc, colored, destination);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void DFS( int U, int [] vis,
ArrayList<ArrayList<Integer>> adj)
{
vis[U] = 1 ;
for (Integer V : adj.get(U))
{
if (vis[V] == 0 )
DFS(V, vis, adj);
}
}
static void minCost( int N, int M, int vCost,
int eCost, int sorc[],
ArrayList<Integer> colored,
int destination[])
{
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for ( int i = 0 ; i < N + 1 ; i++)
adj.add( new ArrayList<Integer>());
for ( int i = 0 ; i < M; i++)
{
adj.get(sorc[i]).add(destination[i]);
adj.get(destination[i]).add(sorc[i]);
}
int [] vis = new int [N + 1 ];
for ( int i = 0 ; i < colored.size(); i++)
{
DFS(colored.get(i), vis, adj);
}
int X = 0 ;
for ( int i = 1 ; i <= N; i++)
{
if (vis[i] == 0 )
{
X++;
DFS(i, vis, adj);
}
}
int mincost = X * Math.min(vCost, eCost);
System.out.println(mincost);
}
public static void main(String[] args)
{
int N = 3 , M = 1 ;
int sorc[] = { 1 };
int destination[] = { 2 };
int vCost = 3 , eCost = 2 ;
ArrayList<Integer> colored = new ArrayList<>();
colored.add( 1 );
minCost(N, M, vCost, eCost, sorc,
colored, destination);
}
}
|
Python3
def DFS(U, vis, adj):
vis[U] = 1
for V in adj[U]:
if (vis[V] = = 0 ):
DFS(V, vis, adj)
def minCost(N, M, vCost, eCost, sorc,
colored, destination):
adj = [[] for i in range (N + 1 )]
for i in range (M):
adj[sorc[i]].append(destination[i])
adj[destination[i]].append(sorc[i])
vis = [ 0 ] * (N + 1 )
for i in range ( len (colored)):
DFS(colored[i], vis, adj)
X = 0
for i in range ( 1 , N + 1 ):
if (vis[i] = = 0 ):
X + = 1
DFS(i, vis, adj)
mincost = X * min (vCost, eCost)
print (mincost)
if __name__ = = '__main__' :
N = 3
M = 1
sorc = [ 1 ]
destination = [ 2 ]
vCost = 3
eCost = 2
colored = [ 1 ]
minCost(N, M, vCost, eCost,
sorc, colored, destination)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static void DFS( int U, int [] vis, ArrayList adj)
{
vis[U] = 1;
foreach ( int V in (ArrayList)adj[U])
{
if (vis[V] == 0)
DFS(V, vis, adj);
}
}
static void minCost( int N, int M, int vCost,
int eCost, int []sorc,
ArrayList colored,
int []destination)
{
ArrayList adj = new ArrayList();
for ( int i = 0; i < N + 1; i++)
adj.Add( new ArrayList());
for ( int i = 0; i < M; i++)
{
((ArrayList)adj[sorc[i]]).Add(destination[i]);
((ArrayList)adj[destination[i]]).Add(sorc[i]);
}
int [] vis = new int [N + 1];
for ( int i = 0; i < colored.Count; i++)
{
DFS(( int )colored[i], vis, adj);
}
int X = 0;
for ( int i = 1; i <= N; i++)
{
if (vis[i] == 0)
{
X++;
DFS(i, vis, adj);
}
}
int mincost = X * Math.Min(vCost, eCost);
Console.Write(mincost);
}
public static void Main( string [] args)
{
int N = 3, M = 1;
int []sorc = {1};
int []destination = {2};
int vCost = 3, eCost = 2;
ArrayList colored = new ArrayList();
colored.Add(1);
minCost(N, M, vCost, eCost,
sorc, colored,
destination);
}
}
|
Javascript
<script>
function DFS(U, vis, adj)
{
vis[U] = 1;
for ( var V of adj[U])
{
if (vis[V] == 0)
DFS(V, vis, adj);
}
}
function minCost( N, M, vCost, eCost, sorc, colored, destination)
{
var adj = [];
for ( var i = 0; i < N + 1; i++)
adj.push( new Array());
for ( var i = 0; i < M; i++)
{
(adj[sorc[i]]).push(destination[i]);
(adj[destination[i]]).push(sorc[i]);
}
var vis = Array(N+1).fill(0);
for ( var i = 0; i < colored.length; i++)
{
DFS(colored[i], vis, adj);
}
var X = 0;
for ( var i = 1; i <= N; i++)
{
if (vis[i] == 0)
{
X++;
DFS(i, vis, adj);
}
}
var mincost = X * Math.min(vCost, eCost);
document.write(mincost);
}
var N = 3, M = 1;
var sorc = [1];
var destination = [2];
var vCost = 3, eCost = 2;
var colored = [];
colored.push(1);
minCost(N, M, vCost, eCost,
sorc, colored,
destination);
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(N)
Last Updated :
30 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...