human
What is a code to eulerian Path in undirected graph and the other steps required given the following:
Eulerian Path in undirected graph | Function to find out the path It takes the adjacency matrix representation of the graph as input.
Find out number of edges each vertex has.
Find out how many vertex has odd number edges.
If number of vertex with odd number of edges is greater than two return " No ▁ Solution " ..
If there is a path find the path Initialize empty stack and path take the starting current as discussed.
Loop will run until there is element in the stack or current edge has some neighbour ..
If current node has not any neighbour add it to path and pop stack set new current to the popped element.
If the current vertex has at least one neighbour add the current vertex to stack , remove the edge between them and set the current to its neighbour ..
Print the path.
Driver Code.
Test case 1.
Test case 2.
Test case 3"
Solution in Python:
assistant
#Here is code to eulerian Path in undirected graph and the other steps required.
 def findpath ( graph , n ) :
	numofadj = [ ]
	for i in range ( n ) :
		numofadj . append ( sum ( graph [ i ] ) )
	startpoint , numofodd = 0 , 0
	for i in range ( n - 1 , - 1 , - 1 ) :
		if ( numofadj [ i ] % 2 == 1 ) :
			numofodd += 1
			startpoint = i
	if ( numofodd > 2 ) :
		print ( " No ▁ Solution " )
		return
	stack = [ ]
	path = [ ]
	cur = startpoint
	while ( len ( stack ) > 0 or sum ( graph [ cur ] ) != 0 ) :
		if ( sum ( graph [ cur ] ) == 0 ) :
			path . append ( cur )
			cur = stack [ - 1 ]
			del stack [ - 1 ]
		else :
			for i in range ( n ) :
				if ( graph [ cur ] [ i ] == 1 ) :
					stack . append ( cur )
					graph [ cur ] [ i ] = 0
					graph [ i ] [ cur ] = 0
					cur = i
					break
	for ele in path :
		print ( ele , end = " ▁ - > ▁ " )
	print ( cur )
if __name__ == ' _ _ main _ _ ' :
	graph1 = [ [ 0 , 1 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 1 , 0 ] , [ 0 , 1 , 0 , 1 , 0 ] , [ 0 , 1 , 1 , 0 , 0 ] , [ 1 , 0 , 0 , 0 , 0 ] ]
	n = len ( graph1 )
	findpath ( graph1 , n )
	graph2 = [ [ 0 , 1 , 0 , 1 , 1 ] , [ 1 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 , 1 ] , [ 1 , 1 , 1 , 0 , 0 ] , [ 1 , 0 , 1 , 0 , 0 ] ]
	n = len ( graph2 )
	findpath ( graph2 , n )
	graph3 = [ [ 0 , 1 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 1 , 1 ] , [ 0 , 1 , 0 , 1 , 0 ] , [ 0 , 1 , 1 , 0 , 1 ] , [ 1 , 1 , 0 , 1 , 0 ] ]
	n = len ( graph3 )
	findpath ( graph3 , n )