Find if there is a path between two vertices in a directed graph | Set 2 | Javascript program to find if there is a path between two vertices in a directed graph using Dynamic Programming ; Function to find if there is a path between two vertices in a directed graph ; mat matrix ; set mat [ i , j ] = true if there is edge between i to j ; Check for all intermediate vertex ; If vertex is invalid ; If there is a path ; Driver code
var X = 6 ; var Z = 2 ; function existPath ( V , edges , u , v ) { var mat = Array . from ( Array ( V ) , ( ) => Array ( V ) ) ; for ( var i = 0 ; i < X ; i ++ ) mat [ edges [ i ] [ 0 ] ] [ edges [ i ] [ 1 ] ] = true ; for ( var k = 0 ; k < V ; k ++ ) { for ( var i = 0 ; i < V ; i ++ ) { for ( var j = 0 ; j < V ; j ++ ) { mat [ i ] [ j ] = mat [ i ] [ j ] || mat [ i ] [ k ] && mat [ k ] [ j ] ; } } } if ( u >= V v >= V ) { return false ; } if ( mat [ u ] [ v ] ) return true ; return false ; } var V = 4 ; var edges = [ [ 0 , 2 ] , [ 0 , 1 ] , [ 1 , 2 ] , [ 2 , 3 ] , [ 2 , 0 ] , [ 3 , 3 ] ] ; var u = 1 , v = 3 ; if ( existPath ( V , edges , u , v ) ) document . write ( " " ) ; else document . write ( " " ) ;