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;
}
基本概念:
有向图强连通分量在有向图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;
}
相关文章推荐
- linux learning notes:1.filesystem and mount point
- 动态规划求最大子矩阵详解(hdu 1505,1506 , 2870)
- 一遍成功安装"Cocoapods"
- NodeJS学习历程(3)
- Java final
- Handler Looper MessageQueue 三者的深度解析
- 黑盒测试(二)
- 对于 @Autowired注解和@service注解的理解
- comparator接口与Comparable接口的区别
- 142. linkedlist cycle
- markdownpad
- Codeforces Round #360 (Div. 2) D. Remainders Game(中国剩余定理)
- MySQL 的 RowNum 实现
- 16.8
- 自动化测试.工具
- TP3.2.3 Auth认证
- 关于建站和域名的杂谈
- poj2112 Electricity(割点应用)
- 使用libnet_build_tcp_options设置options数据
- redis的发布订阅模式