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
function countWays ( mtrx , vrtx , i , dest , visited ) { if ( i == dest ) { return 1 ; } let total = 0 ; for ( let j = 0 ; j < vrtx ; j ++ ) { if ( mtrx [ i ] [ j ] == 1 && ! visited [ j ] ) { visited [ j ] = true ; total += countWays ( mtrx , vrtx , j , dest , visited ) ; visited [ j ] = false ; } } return total ; } function totalWays ( mtrx , vrtx , src , dest ) { let visited = new Array ( vrtx ) ; for ( let i = 0 ; i < vrtx ; i ++ ) { visited [ i ] = false ; } visited [ src ] = true ; return countWays ( mtrx , vrtx , src , dest , visited ) ; } let vrtx = 11 ; let mtrx = [ [ 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 ] , [ 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 ] , [ 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ] ; let src = 3 ; let dest = 9 ; document . write ( totalWays ( mtrx , vrtx , src - 1 , dest - 1 ) ) ;