human
Minimize cost to color all the vertices of an Undirected Graph | Function to implement DFS Traversal to marks all the vertices visited from vertex U.
Mark U as visited.
Traverse the adjacency list of U.
Function to find the minimum cost to color all the vertices of graph.
To store adjacency list.
Loop through the edges to create adjacency list.
To check if a vertex of the graph is visited.
Mark visited to all the vertices that can be reached by colored vertices.
Perform DFS.
To store count of uncolored sub - graphs.
Loop through vertex to count uncolored sub - graphs.
If vertex not visited.
Increase count of uncolored sub - graphs.
Perform DFS to mark visited to all vertices of current sub - graphs.
Calculate minimum cost to color all vertices.
Print the result.
Driver Code.
Given number of vertices and edges.
Given edges.
Given cost of coloring and adding an edge.
Given array of colored vertices"
Solution in Python:

Can you provide a code to minimize cost to color all the vertices of an Undirected Graph and the other steps required?
assistant
#Here is code to minimize cost to color all the vertices of an Undirected Graph and the other steps required.
 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 )