Find minimum weight valid edge .
let min = let_MAX , a = - 1 , b = - 1 ; for ( let i = 0 ; i < V ; i ++ ) { for ( let j = 0 ; j < V ; j ++ ) { if ( cost [ i ] [ j ] < min ) { if ( isValidEdge ( i , j , inMST ) ) { min = cost [ i ] [ j ] ; a = i ; b = j ; } } } } if ( a != - 1 && b != - 1 ) { document . write ( " " + edge_count ++ + " " + a + " " + b + " " + " " + min + " " ) ; mincost = mincost + min ; inMST [ b ] = inMST [ a ] = true ; } } document . write ( " " ) ; document . write ( " " ) ; document . write ( " " + mincost ) ; }