数据结构_图_求有向图的强连通分量

'head.h'


#include<iostream> using namespace std; #define MAX_VEX_NUM 20 class ArcNode//记录一条弧 { public: ArcNode(); int headvex,tailvex; ArcNode *hlink,*tlink; }; ArcNode::ArcNode() { headvex=tailvex=0; hlink=tlink=NULL; } class VexNode//记录一个顶点连接的所有弧 { public: VexNode(); bool visited; char vexname; ArcNode *firstin,*firstout; }; VexNode::VexNode() { visited=false; firstin=firstout=NULL; } class VexBox//顶点集合 { public: VexBox(); VexNode vexlist[MAX_VEX_NUM]; int vexnum; }; VexBox::VexBox() { vexnum=0; } class Graph//图类 { public: void ShowGraph();//接口函数 private: void GetVexList();//得到顶点列表 void CreatGraph();//建图 void Print();//打印图信息 void StrongComp();//求强连通分量 void DFSHead(int);//正向DFS void DFSTail(int);//反向DFS VexBox vbox; int count,finished[MAX_VEX_NUM],ans[MAX_VEX_NUM],nans; }; void Graph::ShowGraph()//接口函数 { GetVexList();//得到顶点列表 CreatGraph();//建图 Print();//打印图信息 StrongComp();//求强连通分量 } void Graph::GetVexList()//得到顶点列表 { cout<<"Please Input Vertexes :"<<endl<<endl; char name; while(cin>>name) { vbox.vexlist[vbox.vexnum++].vexname=name; } cin.clear(); } void Graph::CreatGraph()//建图 { cout<<"Please Input Arces :"<<endl<<endl; int head,tail; ArcNode * newnode,*p; while(cin>>tail>>head) { newnode=new ArcNode; newnode->headvex=head; newnode->tailvex=tail; p=vbox.vexlist[tail].firstout; if(p==NULL) { vbox.vexlist[tail].firstout=newnode; } else { while(p->tlink!=NULL) { p=p->tlink; } p->tlink=newnode; } p=vbox.vexlist[head].firstin; if(p==NULL) { vbox.vexlist[head].firstin=newnode; } else { while(p->hlink!=NULL) { p=p->hlink; } p->hlink=newnode; } } cin.clear(); } void Graph::Print()//打印图信息 { ArcNode *p; for(int i=0;i<vbox.vexnum;i++) { cout<<vbox.vexlist[i].vexname<<endl; p=vbox.vexlist[i].firstout; while(p!=NULL) { cout<<"\t"<<vbox.vexlist[p->tailvex].vexname<<"->"<<vbox.vexlist[p->headvex].vexname<<endl; p=p->tlink; } } } void Graph::StrongComp()//求强连通分量 { count=0; for(int i=0;i<vbox.vexnum;i++) { if(vbox.vexlist[i].visited==false) DFSHead(i); } for(int i=0;i<vbox.vexnum;i++) vbox.vexlist[i].visited=false; for(int i=count-1;i>=0;i--) { if(vbox.vexlist[i].visited==false) { nans=0; DFSTail(i); if(nans>0) { cout<<vbox.vexlist[ans[0]].vexname; for(int i=1;i<nans;i++) cout<<"<->"<<vbox.vexlist[ans[i]].vexname; cout<<endl; } } } } void Graph::DFSHead(int k)//正向DFS { vbox.vexlist[k].visited=true; ArcNode *p=vbox.vexlist[k].firstout; while(p!=NULL) { if(vbox.vexlist[p->headvex].visited==false) DFSHead(p->headvex); p=p->tlink; } finished[count++]=k; } void Graph::DFSTail(int k)//反向DFS { ans[nans++]=k; vbox.vexlist[k].visited=true; ArcNode *p=vbox.vexlist[k].firstin; while(p!=NULL) { if(vbox.vexlist[p->tailvex].visited==false) { DFSTail(p->tailvex); } p=p->tlink; } }


'main.cpp'


#include"head.h" int main() { Graph g; g.ShowGraph(); system("pause"); }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值