human
Can you provide a code to calculate number of nodes between two vertices in an acyclic Graph by DFS method and the other steps required given the following:
Calculate number of nodes between two vertices in an acyclic Graph by DFS method | Function to return the count of nodes in the path from source to destination.
Mark the node visited.
If dest is reached.
Traverse all adjacent nodes.
If not already visited.
If there is path , then include the current node.
Return 0 if there is no path between src and dest through the current node.
Function to return the count of nodes between two given vertices of the acyclic Graph.
Initialize an adjacency list.
Populate the edges in the list.
Mark all the nodes as not visited.
Count nodes in the path from src to dest.
Return the nodes between src and dest.
Driver Code.
Given number of vertices and edges.
Given source and destination vertices.
Given edges"
Solution in Python:
assistant
#Here is code to calculate number of nodes between two vertices in an acyclic Graph by DFS method and the other steps required.
 def dfs ( src , dest , vis , adj ) :
	vis [ src ] = 1
	if ( src == dest ) :
		return 1
	for u in adj [ src ] :
		if not vis [ u ] :
			temp = dfs ( u , dest , vis , adj )
			if ( temp != 0 ) :
				return temp + 1
	return 0
def countNodes ( V , E , src , dest , edges ) :
	adj = [ [ ] for i in range ( V + 1 ) ]
	for i in range ( E ) :
		adj [ edges [ i ] [ 0 ] ] . append ( edges [ i ] [ 1 ] )
		adj [ edges [ i ] [ 1 ] ] . append ( edges [ i ] [ 0 ] )
	vis = [ 0 ] * ( V + 1 )
	count = dfs ( src , dest , vis , adj )
	return count - 2
if __name__ == ' _ _ main _ _ ' :
	V = 8
	E = 7
	src = 5
	dest = 2
	edges = [ [ 1 , 4 ] , [ 4 , 5 ] , [ 4 , 2 ] , [ 2 , 6 ] , [ 6 , 3 ] , [ 2 , 7 ] , [ 3 , 8 ] ]
	print ( countNodes ( V , E , src , dest , edges ) )