Minimize cost to color all the vertices of an Undirected Graph using given operation
Given two integers V and E representing the number of vertices and edges of an undirected graph G(V, E), a list of edges EdgeList, and an array A[] representing the cost to color each node, the task is to find the minimum cost to color the graph using the following operation:
When a node is colored, all the nodes that can be reached from it are colored without any additional cost.
Examples:
Input: V = 6, E = 5, A[] = {12, 25, 8, 11, 10, 7}, EdgeList = {{1, 2}, {1, 3}, {3, 2}, {2, 5}, {4, 6}}
Output: 15
Explanation:
On coloring the vertex 3 for a cost of 8, the vertices {1, 2, 5} gets colored at no additional cost.
On coloring the vertex 6 for a cost of 7, the only remaining vertex {4} also gets colored.
Therefore, the minimum cost = 8 + 7 = 15.
Input: V =7, E = 6, A[] = {3, 5, 8, 6, 9, 11, 10}, EdgeList = {{1, 4}, {2, 1}, {2, 7}, {3, 4}, {3, 5}, {5, 6}}
Output: 5
Approach:
Follow the steps below to solve the problem:
- All the nodes are reachable from a given node form a Connected Component.
- So for each connected component, using Depth First Search, find the minimum cost node in a connected component of a graph.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 10
vector< int > adj[MAX];
void addEdge( int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void dfs( int v, int cost[], bool vis[],
int & min_cost_node)
{
vis[v] = true ;
min_cost_node
= min(min_cost_node, cost[v - 1]);
for ( int i = 0; i < adj[v].size(); i++) {
if (vis[adj[v][i]] == false ) {
dfs(adj[v][i], cost, vis,
min_cost_node);
}
}
}
int minimumCost( int V, int cost[])
{
bool vis[V + 1];
memset (vis, false , sizeof (vis));
int min_cost = 0;
for ( int i = 1; i <= V; i++) {
if (!vis[i]) {
int min_cost_node = INT_MAX;
dfs(i, cost, vis, min_cost_node);
min_cost += min_cost_node;
}
}
return min_cost;
}
int main()
{
int V = 6, E = 5;
int cost[] = { 12, 25, 8, 11, 10, 7 };
addEdge(1, 2);
addEdge(1, 3);
addEdge(3, 2);
addEdge(2, 5);
addEdge(4, 6);
int min_cost = minimumCost(V, cost);
cout << min_cost << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int MAX = 10 ;
@SuppressWarnings ( "unchecked" )
static Vector<Integer> []adj = new Vector[MAX];
static int min_cost_node;
static void addEdge( int u, int v)
{
adj[u].add(v);
adj[v].add(u);
}
static void dfs( int v, int cost[], boolean vis[])
{
vis[v] = true ;
min_cost_node = Math.min(min_cost_node,
cost[v - 1 ]);
for ( int i = 0 ; i < adj[v].size(); i++)
{
if (vis[adj[v].get(i)] == false )
{
dfs(adj[v].get(i), cost, vis);
}
}
}
static int minimumCost( int V, int cost[])
{
boolean []vis = new boolean [V + 1 ];
Arrays.fill(vis, false );
int min_cost = 0 ;
for ( int i = 1 ; i <= V; i++)
{
if (!vis[i])
{
min_cost_node = Integer.MAX_VALUE;
dfs(i, cost, vis);
min_cost += min_cost_node;
}
}
return min_cost;
}
public static void main(String[] args)
{
int V = 6 , E = 5 ;
int cost[] = { 12 , 25 , 8 , 11 , 10 , 7 };
for ( int i = 0 ; i < adj.length; i++)
adj[i] = new Vector<Integer>();
addEdge( 1 , 2 );
addEdge( 1 , 3 );
addEdge( 3 , 2 );
addEdge( 2 , 5 );
addEdge( 4 , 6 );
int min_cost = minimumCost(V, cost);
System.out.print(min_cost + "\n" );
}
}
|
Python3
import sys
MAX = 10
adj = [[] for i in range ( MAX )]
def addEdge(u, v):
adj[u].append(v)
adj[v].append(u)
def dfs(v, cost, vis, min_cost_node):
vis[v] = True
min_cost_node = min (min_cost_node,
cost[v - 1 ])
for i in range ( len (adj[v])):
if (vis[adj[v][i]] = = False ):
min_cost_node = dfs(adj[v][i],
cost, vis,
min_cost_node)
return min_cost_node
def minimumCost(V, cost):
vis = [ False for i in range (V + 1 )]
min_cost = 0
for i in range ( 1 , V + 1 ):
if ( not vis[i]):
min_cost_node = sys.maxsize
min_cost_node = dfs(i, cost, vis,
min_cost_node)
min_cost + = min_cost_node
return min_cost
if __name__ = = "__main__" :
V = 6
E = 5
cost = [ 12 , 25 , 8 , 11 , 10 , 7 ]
addEdge( 1 , 2 )
addEdge( 1 , 3 )
addEdge( 3 , 2 )
addEdge( 2 , 5 )
addEdge( 4 , 6 )
min_cost = minimumCost(V, cost)
print (min_cost)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static readonly int MAX = 10;
static List< int > []adj = new List< int >[MAX];
static int min_cost_node;
static void addEdge( int u, int v)
{
adj[u].Add(v);
adj[v].Add(u);
}
static void dfs( int v, int []cost, bool []vis)
{
vis[v] = true ;
min_cost_node = Math.Min(min_cost_node,
cost[v - 1]);
for ( int i = 0; i < adj[v].Count; i++)
{
if (vis[adj[v][i]] == false )
{
dfs(adj[v][i], cost, vis);
}
}
}
static int minimumCost( int V, int []cost)
{
bool []vis = new bool [V + 1];
int min_cost = 0;
for ( int i = 1; i <= V; i++)
{
if (!vis[i])
{
min_cost_node = int .MaxValue;
dfs(i, cost, vis);
min_cost += min_cost_node;
}
}
return min_cost;
}
public static void Main(String[] args)
{
int V = 6;
int []cost = { 12, 25, 8, 11, 10, 7 };
for ( int i = 0; i < adj.Length; i++)
adj[i] = new List< int >();
addEdge(1, 2);
addEdge(1, 3);
addEdge(3, 2);
addEdge(2, 5);
addEdge(4, 6);
int min_cost = minimumCost(V, cost);
Console.Write(min_cost + "\n" );
}
}
|
Javascript
<script>
var MAX = 10
var adj = Array.from(Array(MAX), ()=> Array());
function addEdge(u, v)
{
adj[u].push(v);
adj[v].push(u);
}
function dfs(v, cost, vis, min_cost_node)
{
vis[v] = true ;
min_cost_node
= Math.min(min_cost_node, cost[v - 1]);
for ( var i = 0; i < adj[v].length; i++) {
if (vis[adj[v][i]] == false ) {
min_cost_node = dfs(adj[v][i], cost, vis,
min_cost_node);
}
}
return min_cost_node;
}
function minimumCost(V, cost)
{
var vis = Array(V + 1).fill( false );
var min_cost = 0;
for ( var i = 1; i <= V; i++) {
if (!vis[i]) {
var min_cost_node = 1000000000;
min_cost_node = dfs(i, cost, vis, min_cost_node);
min_cost += min_cost_node;
}
}
return min_cost;
}
var V = 6, E = 5;
var cost = [12, 25, 8, 11, 10, 7];
addEdge(1, 2);
addEdge(1, 3);
addEdge(3, 2);
addEdge(2, 5);
addEdge(4, 6);
var min_cost = minimumCost(V, cost);
document.write( min_cost );
</script>
|
Output:
15
Time Complexity: O(V+E)
Auxiliary Space: O(V)
Last Updated :
26 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...