数据结构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的序号附值为j
	    p->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为弧尾的顶点,将他们的入度减1
		if(!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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值