Find minimum weight valid edge .
int min = INT_MAX , a = - 1 , b = - 1 ; for ( int i = 0 ; i < V ; i ++ ) { for ( int 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 ) { Console . Write ( " Edge ▁ { 0 } : ( { 1 } , ▁ { 2 } ) ▁ cost : ▁ { 3 } ▁ STRNEWLINE " , edge_count ++ , a , b , min ) ; mincost = mincost + min ; inMST [ b ] = inMST [ a ] = true ; } } Console . Write ( " cost = { 0 } " }