22.5-5求有向图的分量图

本文探讨了如何利用深度优先搜索(DFS)算法来查找有向图的强连通分量,并介绍了分量图的概念。通过实例P180 SccDAG进行详细解析。
摘要由CSDN通过智能技术生成
22.5-5求有向图的分量图


1.求出强连通分量
(1)第1次dfs,求出(逆)拓扑序
(2)第2次对以逆图的逆拓扑序dfs,得到的DFS森林,便求出强连通分量。期间要记录结点v所属的强连通分量为scc_count。
2.对于原图的每条边,若两个端点不属同一强连通分量,即!stronglyreachable(v,t),便加进DAG中


参考资料:C++算法--图算法(第3版)
P14-15 IO
P23-24 SparseMutiGRAPH
P28 InDegree
P173 StronglyConnected

P180 SccDAG



#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct Edge{
    int v,w;
    Edge(int v=-1,int w=-1):v(v),w(w){}
};
class SparseMutiGRAPH{//邻接链表表示的稀疏图
    int Vcount,Ecount;//点数,边数
    bool digraph;//true为有向图,false为无向图
    struct node{
        int v;node*next;
        node(int v,node*next):v(v),next(next){}
    };
    typedef node* link;
    vector<link> adjacency;//邻接链表
public:
    SparseMutiGRAPH(int Vcount,bool digraph=false):adjacency(Vcount),Vcount(Vcount),Ecount(0),digraph(digraph){
        adjacency.assign(Vcount,(node*)NULL);//adjacency大小置为Vcount,每个元素为NULL
    }
    int V() const{return Vcount;}
    int E() const{return Ecount;}
    bool directed() const{return digraph;}
    void insert(Edge e)
    {
        int v=e.v,w=e.w;
        //将w插入到v的邻接链表表头
        adjacency[v]=new node(w,adjacency[v]);
        if(!digraph) adjacency[w]=new node(v,adjacency[w]);
        Ecount&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值