2015
09-17

# k-edge connected components

Efficiently computing k-edge connected components in a large graph G = (V, E), where V is the vertex set and E is the edge set, is a long standing research problem. It is not only fundamental in graph analysis but also crucial in graph search optimization algorithms. Computing k-edge connected components has many real applications. For example, in social networks, computing k-edge connected components can identify the closely related entities to provide useful information for social behavior mining. In a web-link based graph, a highly connected graph may be a group of web pages with a high commonality, which is useful for identifying the similarities among web pages. In computational biology, a highly connected subgraph is likely to be a functional cluster of genes for biologist to conduct the study of gene microarrays. Computing k-edge connected components also potentially contributes to many other technology developments such as graph visualization, robust detection of communication networks, community detection in a social network.

Clearly, if a graph G is not k-edge connected, there must be a set C of edges, namely a cut, such that the number |C| of edges in C is smaller than k and the removal of the edges in C cuts the graph G into two disconnected subgraphs G1 and G2. A connected component is a maximal connected subgraph of G. Note that each vertex belongs to exactly one connected component, as does each edge.

Now, we give you a undirected graph G with n vertices and m edges without self-loop or multiple edge, your task is just find out the number of k-edge connected components in G.

Multicases. 3 integer numbers n, m and k are described in the first line of the testcase.(3≤n≤100, 1≤m≤n×(n-1)/2,2≤k≤n)The following m lines each line has 2 numbers u, v describe the edges of graph G.(1≤u,v≤n,u≠v)

Multicases. 3 integer numbers n, m and k are described in the first line of the testcase.(3≤n≤100, 1≤m≤n×(n-1)/2,2≤k≤n)The following m lines each line has 2 numbers u, v describe the edges of graph G.(1≤u,v≤n,u≠v)

5 6 3
1 3
2 3
1 4
2 4
1 5
2 5
9 11 2
1 2
1 3
2 3
4 5
4 6
5 6
7 8
7 9
8 9
1 4
1 7
16 30 3
1 2
1 3
1 4
2 3
2 4
3 4
5 6
5 7
5 8
6 7
6 8
7 8
9 10
9 11
9 12
10 11
10 12
11 12
13 14
13 15
13 16
14 15
14 16
15 16
1 5
2 6
1 9
2 10
1 13
2 14

5
3
4
Hint



http://acm.hdu.edu.cn/showproblem.php?pid=4654

stoer-wagner算法 + dfs

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define inf (1<<29)
const int maxn = 105;
int g[maxn][maxn],a[maxn][maxn],p[maxn];
bool vis[maxn],combine[maxn],par[maxn];
int d[maxn],node[maxn],st[maxn],k,s,t;
vector <int> vst[maxn];
vector <int> pa , pb;
int prim(int n) {
memset(vis,0,sizeof(vis));
memset(d,0,sizeof(d));
int mincut = 0 , tmp = -1;
s = t = -1;
int top = 0;
for(int i=0;i<k;i++) {
int maxi = -inf;
for(int j=0;j<k;j++) {
int u = node[j];
if(!combine[u]&&!vis[u]&&d[u]>maxi) {
tmp = u;
maxi = d[u];
}
}
st[top++] = tmp;
vis[tmp] = true;
if(i == k-1)
mincut = d[tmp];
for(int j=0;j<k;j++) {
int u = node[j];
if(!combine[u]&&!vis[u])
d[u] += g[tmp][u];
}
}
s = st[top-2];
t = st[top-1];
for(int i=0;i<top;i++) node[i] = st[i];
return mincut;
}
int stoer_wagner(int n) {
for(int i=0;i<n;i++) {
vst[i].clear();
vst[i].push_back(i);
}
int ans = inf;
memset(combine,0,sizeof(combine));
for(int i=0;i<n;i++) node[i] = i;
for(int i=1;i<n;i++) {
k = n - i + 1;
int cur = prim(n);
if(cur < ans) {
ans = cur;
for(int j=0;j<n;j++) par[j] = 0;
for(int j=0;j<vst[t].size();j++) par[ vst[t][j] ] = 1;
}
combine[t] = true;
for(int j=0;j<vst[t].size();j++) vst[s].push_back(vst[t][j]);
for(int j=0;j<n;j++) {
if(j == s) continue;
if(!combine[j]) {
g[s][j] += g[t][j];
g[j][s] += g[j][t];
}
}
}
pa.clear(); pb.clear();
for(int i=0;i<n;i++)
if(par[i]) pa.push_back(i);
else pb.push_back(i);
return ans;
}
int K;
int dfs(vector <int> t) {
int n = t.size();
for(int i=0;i<n;i++) for(int j=0;j<n;j++) g[i][j] = a[t[i]][t[j]];
if(stoer_wagner(n) >= K) return 1;
vector <int> x , y;
for(int i=0;i<pa.size();i++) x.push_back(t[pa[i]]);
for(int i=0;i<pb.size();i++) y.push_back(t[pb[i]]);
return dfs(x) + dfs(y);
}
int main() {
int n , m;
while(~scanf("%d%d%d",&n,&m,&K)) {
memset(a , 0 ,sizeof(a));
while(m--) {
int u , v;
scanf("%d%d",&u,&v);
u --; v --;
a[u][v] += 1;
a[v][u] += 1;
}
vector <int> t;
for(int i=0;i<n;i++) t.push_back(i);
printf("%d\n" , dfs(t));
}
return 0;
}