数据结构AOE网

阅读: 评论:0

数据结构AOE网

数据结构AOE网

AOE 网数据结构

AOE 网

一、目的与要求
1)掌握AOE网的邻接表存储结构表示及创建算法的c语言实现;
2)理解AOE网的拓扑排序算法(算法7.12)的实现原理及应用;
3)掌握AOE网关键路径的计算算法(算法7.13,7.14)及C语言实现与应用;
4)按照实验题目要求独立正确地完成实验内容(提交程序清单及相关实验数据与运行结果);
5)认真书写实验报告,并按时提交。。

二、实验内容
题目: 图的应用实验——计算AOE网的关键路径
内容:按照图的“邻接表”存储结构表示AOE网,实现求其关键路径的算法,并验证如下图1所示AOE网的关键路径。

include <iostream.h>

include <stdlib.h>

define max_vertex_num 50

define maxvalue 32760

define NULL 0

typedef struct edgenode{
int adjvex;
struct edgenode next;
int weight; }edgenode,
pedgenode;

typedef struct vnode{
int data;
struct edgenode firstarc;
}vnode,adjlist[max_vertex_num],
pvnode;

void creatgraphadlist(pvnode &pn,int n)
{
//依次输入顶点偶对,建立无向图邻接表
edgenode *p;
int i,j,k,wei;
i=j=1;
pn=(pvnode)malloc((n+1)*sizeof(vnode));
for(k=1;k<=n;k++)
pn[k].firstarc=NULL;//初始化每个链表为空

for(int f=1;!(i==0&&j==0);)
{cout<<"Please input number of-"<<f<<"-vertices of an arc,End with (0,0): ";cin>>i>>j;if(i>0&&i<=n&&j>0&&j<=n){p=(edgenode *)malloc(sizeof(edgenode));//生成一个节点p->adjvex=j;//为节点j的序号附值为jp->next=pn[i].firstarc;//将该p节点作为第i个链表的第一个节点插入pn[i]之后pn[i].firstarc=p;//将接点j作为第一个接点插入连表i的后面cout<<"Please enter the weight of the edge:";cin>>wei;p->weight =wei;f++;}else if(i==0&&j==0);//什么也不做else cout<<"Please re-enter .n";
}//for
cout<<"---------------------------------------------------------n";

}

void findindegree(pvnode pn,int n,int *deg)
{
for(int i=1;i<=n;i++)//第0个元素不用
deg[i]=0;//对数组进行初始化,全部置0
pedgenode pk;
for(i=1;i<=n;i++)//对邻接表进行遍历
{
pk=pn[i].firstarc;
for(;pk;pk=pk->next)
deg[pk->adjvex]++;//数组相应元素自增
}
}

int ve[max_vertex_num];//存放各点发生的最早时刻(全局变量)
int st[max_vertex_num];//存放拓扑序列各顶点的栈(全局变量)
int top2;//(全局变量)

void topologicalorder(pvnode pn,int n,int *tt)
{
int indegree[max_vertex_num];//该数组存放各顶点的入度
int ss[max_vertex_num];//设计栈ss,用以存放入度为0的顶点的信息
int *deg;int j;
deg=indegree;
findindegree(pn,n,deg);
for(int i=1;i<=n;i++)//ve[0]不用
ve[i]=0;//初始化

int top1;
top1=top2=0;//top1和top2为栈顶指针,分别指向栈ss和tt,初始值均为0(即栈为空)
for(i=1;i<=n;i++)if(!indegree[i])//若顶点i入度为0,则进栈ss[++top1]=i;
int count=0;//对输出顶点计数
cout<<"The topological order of the graph is:n";
while(top1)//若ss非空,进入循环
{j=ss[top1--];//i出栈cout<<j<<"  ";//输出i顶点序号tt[++top2]=j;//将i进入tt栈count++;//计数for(pedgenode p=pn[j].firstarc ;p;p=p->next ){int k=p->adjvex ;indegree[k]--;//对每一个以i为弧尾的顶点,将他们的入度减1if(!indegree[k])ss[++top1]=k;//如果减为0,则入栈。因为若入度为x,必然要自减x次才能如栈,所以可以避免重复进栈if((ve[j]+p->weight)>ve[k])ve[k]=ve[j]+p->weight;//显然此时j是k的前驱,且ve[j]是j发生的最早时间,若}
}
if(count<n)
{cout<<"nThere are rings in the graph --error!n";return;
}
cout<<"n---------------------------------------------------------"<<endl;
cout<<"The earliest time of each vertex :"<<endl;
for(i=1;i<=n;i++)cout<<"vertex("<<i<<")earliest time is:"<<ve[i]<<endl;
cout<<"n---------------------------------------------------------"<<endl;

}

void criticalpath(pvnode pn,int n,int tt)
{
int j,k,dut,ee,el,tag,f=1;
pedgenode p;
int vl[max_vertex_num];//存放各点发生的最迟时刻
for(int i=1;i<=n;i++)
vl[i]=ve[n];//用顶点n的最早时间初始化各顶点的最迟时间
/
while(top2)
{
j=tt[top2–];//出栈
for(p=pn[j].firstarc ;p;p=p->next )
{
k=p->adjvex ;
dut=p->weight;
if((vl[k]-dut)<vl[j]) vl[j]=vl[k]-dut;

	}
}
cout<<"After calculation, the key path of the figure is as follows n";
for(j=1;j<=n;j++)for(p=pn[j].firstarc ;p;p=p->next ){k=p->adjvex  ;dut=p->weight ;ee=ve[j];el=vl[k]-dut;if(ee==el)          //ee==el,说明是关键活动cout<<"The"<<f++<<"Key activities are:"<<j<<"--"<<k<<endl;//打印关键活动}

}

int main()
{
int n,*tt;
pvnode pn;
tt=st;
cout<<“Please enter the number of vertices of the graph :”;
cin>>n;
cout<<"---------------------------------------------------------"<<endl;
creatgraphadlist(pn,n);
topologicalorder(pn,n,tt);
criticalpath(pn,n,tt);
return 0;
}

本文发布于:2024-01-31 21:28:19,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170670770131472.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据结构   AOE
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23