//拓扑排序
#include <iostream.h>
#include <stdlib.h>
const int MaxEdgeNum=100;
const int MaxVertexNum=100;
typedef int VertexType;
typedef int ElemType;
typedef VertexType vexlist[MaxVertexNum];//定义存储顶点信息的数组类型
struct edgenode{//定义邻接表中的边结点类型
int adjvex;//邻接点域
edgenode *next;
};
typedef edgenode *adjlist[MaxVertexNum];//定义存储N个表头指针的数组类型
bool visited[MaxVertexNum];
void Create(vexlist GV,adjlist GL,int v,int e)
{
int i,j,k;
cout<<"输入"<<v<<"个顶点:"<<endl;
for(i=0;i<v;i++)
cin>>GV[i];
for(i=0;i<v;i++)
GL[i]=NULL;
cout<<"输入"<<e<<"条边:"<<endl;
for(k=1;k<=e;i++)
{
cin>>i>>j;
edgenode *p=new edgenode;//申请新结点
p->adjvex=j;//将新结点插入到邻接表的表头
p->next=GL[i];
GL[i]=p;
}
}
void Toposort(adjlist GL,int n)
{
int i,j,k,top,m=0;
edgenode *p;
int *d=new int[n];
for(i=0;i<n;i++)
d[i]=0;
for(i=0;i<n;i++)
{
p=GL[i];
while(p!=NULL)
{
j=p->adjvex;
d[j]++;
p=p->next;
}
}
top=-1;
for(i=0;i<n;i++)
if(d[i]==0)
{
d[i]=top;
top=i;
}
while(top!=1)
{
j=top;
top=d[top];
cout<<j<<' ';
m++;
p=GL[j];
while(p!=NULL)
{
k=p->adjvex;
d[k]--;
if(d[k]==0)
{
d[k]=top;
top=k;
}
p=p->next;
}
}
cout<<endl;
if(m<n)
cout<<"The network has a cycle!"<<endl;
}
void main()
{
adjlist GL;
vexlist GV;
int v,e;
cout<<"请输入顶点数:";
cin>>v;
cout<<"请输入边数:";
cin>>e;
Create(GV,GL,v,e);
Toposort(GL,v);
}
tuopupaixu.rar_拓扑排序
版权申诉
56 浏览量
2022-09-14
14:55:36
上传
评论
收藏 1KB RAR 举报
钱亚锋
- 粉丝: 90
- 资源: 1万+
最新资源
- 基于LUT查找表方法的正弦信号产生器FPGA实现,包含testbench,包括程序,注释,操作步骤
- Screenshot_20240618_174113.jpg
- matlab画正余弦函数图的代码!!!!!
- 2_期末网店运营报告模版.pdf
- MyBatisCodeHelperPro 3.3.2-2322 2023.2-2024.1
- 基于Python的简单的学生成绩管理程序设计(课程设计)
- jdk-8u20-windows-x64安装版本-jdk-8u301-linux-x64解压版
- 植物大战僵尸杂交版 修改阳光和冷却
- html css js网页设计ntion-model-for-开发笔记
- 数据库课程设计-processing开发笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈