function to find the second minimum edge cost having an end at the vertex i
int secondMin ( int adj [ N ] [ N ] , int i ) { int first = INT_MAX , second = INT_MAX ; for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) continue ; if ( adj [ i ] [ j ] <= first ) { second = first ; first = adj [ i ] [ j ] ; } else if ( adj [ i ] [ j ] <= second && adj [ i ] [ j ] != first ) second = adj [ i ] [ j ] ; } return second ; }