Count total ways to reach destination from source in an undirected Graph | Utility Function to count total ways ; Base condition When reach to the destination ; Make vertex visited ; Recursive function , for count ways ; Backtracking Make vertex unvisited ; Return total ways ; Function to count total ways to reach destination ; Loop to make all vertex unvisited , Initially ; Make source visited ; Print total ways
def countWays ( mtrx , vrtx , i , dest , visited ) : NEW_LINE INDENT if ( i == dest ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT total = 0 NEW_LINE for j in range ( vrtx ) : NEW_LINE INDENT if ( mtrx [ i ] [ j ] == 1 and not visited [ j ] ) : NEW_LINE INDENT visited [ j ] = True ; NEW_LINE total += countWays ( mtrx , vrtx , j , dest , visited ) ; NEW_LINE visited [ j ] = False ; NEW_LINE DEDENT DEDENT return total NEW_LINE DEDENT def totalWays ( mtrx , vrtx , src , dest ) : NEW_LINE INDENT visited = [ False ] * vrtx NEW_LINE for i in range ( vrtx ) : NEW_LINE INDENT visited [ i ] = False NEW_LINE DEDENT visited [ src ] = True ; NEW_LINE return countWays ( mtrx , vrtx , src , dest , visited ) NEW_LINE DEDENT print ( totalWays ( mtrx , vrtx , src - 1 , dest - 1 ) ) NEW_LINE