1 Const intMax_n =1;2 intG[max_n][max_n];3 intV[max_n];//V[i] represents the vertex to which node I is merged4 intW[max_n];//define W (a,x) =∑w (v[i],x), V[i]∈a5 BOOLVisited[max_n];//used to mark whether the point has been added to a collection6 7 intStoer_wagner (intN)8 {9 intMin_cut =inf;Ten for(inti =0; I < n; ++i) One { AV[i] = i;//the initial is not merged, all of which represent the node itself - } - the while(N >1) - { - intPre =0;//The pre is used to indicate the point before adding a set (a point added before T) -Memset (visited,0,sizeof(visited)); +Memset (W,0,sizeof(w)); - for(inti =1; I < n; ++i) + { A intK =-1; at for(intj =1; J < N; ++J)//Select W (a,x) largest point x in V-a to join the collection - { - if(!Visited[v[j]]) - { -W[V[J]] + =G[v[pre]][v[j]]; - if(k = =-1|| W[v[k]] <W[v[j]]) in { -K =J; to } + } - } the *Visited[v[k]] =true;//mark the point X has joined a set $ if(i = = N-1)//If | a|=| V| (all points are added a), endPanax Notoginseng { - Const ints = v[pre], t = v[k];//make the second-to-last point (V[pre]) to S, and the final point with a (V[k]) is T themin_cut = min (min_cut, w[t]);//The s-t min Cut is w (a,t), updated with its min_cut + for(intj =0; J < N; ++J)//contract (S, T) A { theG[S][V[J]] + =G[v[j]][t]; +G[v[j]][s] + =G[v[j]][t]; - } $V[K] = V[--n];//Delete the last point (that is, delete T and merge T into s) $ } - //Else Continue -Pre =K; the } - }Wuyi returnMin_cut; the}
A minimum cut stoer_wagner algorithm for non-direction graphs