您的位置:首页 > 其它

Gbrbow算法 强连通分量 --有向图

2016-07-03 11:25 253 查看
借鉴网址:tarjan算法   http://www.cppblog.com/sosi/archive/2010/09/26/127797.aspx

基本概念:

        有向图强连通分量在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(strongly
connected)。如果有向图G的每两个顶点都强连通,称G是一个强连通图。有向图的极大强连通子图,称为强连通分量(strongly
connected components)。

基本算法有:Tarjan算法,Kosaraju算法,Garbow算法

Garbow伪代码:

(1)找一个没有被访问的节点v:否则算法结束。

(2)将v压入两个堆栈,

对于v所有的邻接顶点u;

a:如果没有被访问,转步骤2;

b:如果被访问过,但没有删除,维护二栈(处理环的过程);如果二栈的顶元素等于v,那么输出相应的强联通分量。

Garbow代码:
struct node{
int t, next;
}edge[maxn];

int Garbowbfs(int cur, int time, int &scc_num){
sta[n++] = cur;
stack[m++] = cur;
for (int i=head[cur]; i!=-1; i=edge[i].next){
if ( !low[edge[i].t]){
Garbowbfs(edge[i].t, time, scc_num);
}
else if (belg[edge[i].t] == 0){
while (low[stack[m]] > low[edge[i].t])
--m;
}
}
if (stack[m] == cur){
m--
scc_num++;
do{
belg[sta
] = scc_num;
}while (sta[n--] != cur)
}
return 0;
}

int Garbow(int n){
int scc_num = 0, time=0;
memset(belg, 0, sizeof(belg));
memset(low, 0, sizeof(low));
for (int i=0; i<n; i++)
if (low[i] == 0)
Garbowbfs(i, time, scc_num);
return scc_num;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: