深入理解数据结构——关键路径算法AOE

#include<iostream>
#include<string>
using namespace std;

//邻接表的实现

typedef int InfoType;

struct AdjNode {
	int another_vertex;//该条边另一个顶点在表头节点数组中的下标
	InfoType info;//
	AdjNode* next;//下一个节点地址
};

typedef char  EType;
struct AdjList {
	int indegree;//表示每个顶点的入度,该域的值建立在邻接表时动态计算
	EType data;
	AdjNode* head;
};

struct AdjGraph {
	AdjList* head_list;
	int vertex_num;
	int edge_num;
};


//基于邻接表表示的有向图计算入度的算法
void CalcIndegree(AdjGraph &g) {

	int* temp;
	int i;
	AdjNode* p;
	temp = new int[g.vertex_num];
	memset(temp, 0, sizeof(int) * g.vertex_num);

	for (i = 0; i < g.vertex_num; i++) {
		p = g.head_list[i].head;

		while (p) {
			temp[p->another_vertex]++;
			p = p->next;
		}
	}

	for (i = 0; i < g.vertex_num; i++) {
		g.head_list[i].indegree = temp[i];
	}
	delete[]temp;

}


//基于邻接表表示的有向图拓扑排序算法
#define MAXIMUM 100
void TopSort(AdjGraph g) {
	int stack[MAXIMUM], top;
	int i, j, number;

	AdjNode* p;
	top = -1;
	number = 0;
	for (i = 0; i < g.vertex_num; i++) {//所有度为0的顶点入栈
		if (g.head_list[i].indegree == 0)
		{
			top++;
			stack[top] = i;
		}
	}
	while (top>=0) {
		i = stack[top];
		top--;
		number++;
		cout << i << ",";

		p = g.head_list[i].head;
		while (p) {
			j = p->another_vertex;
			g.head_list[j].indegree--;
			if (!g.head_list[j].indegree)
			{
				top++;
				stack[top] = j;
			}
			p = p->next;
		}
	}

	if (number < g.vertex_num) {
		cout << "网中含有回路";
	}
}

//关键路径算法Critical path
//基于邻接表表示的AOE网络的拓扑算法

int *CSTACK, CTOP;//存放拓扑序列的栈
int* ve, * vl;

void CriticalPath(AdjGraph g) {
	int i, j;
	AdjNode* p;
	CTOP = -1;
	CSTACK = new int[g.vertex_num];
	ve = new int[g.vertex_num];
	vl = new int[g.vertex_num];

	memset(ve, 0, sizeof(int) * g.vertex_num);

	TopSort(g);//求解各事件的最早发生时间
	for (i = 0; i < g.vertex_num;i++) {
		vl[i] = ve[g.vertex_num - 1];
	}

	while (CTOP>=0) {//求解各事件的最迟发生时间
		i = CSTACK[CTOP];
		CTOP--;
		p = g.head_list[i].head;
		while (p) {
			j = p->another_vertex;
			if (vl[i] > (vl[j] - p->info))
				vl[i] = vl[j] - p->info;
			p = p->next;
		}
	}
	for (i = 0; i < g.vertex_num; i++) {
		cout << ve[i] << "," << vl[i] << endl;
	}
}


void TopSort(AdjGraph g) {
	int stack[MAXIMUM], top;
	int i, j, number;

	AdjNode* p;
	top = -1;
	number = 0;
	for (i = 0; i < g.vertex_num; i++) {//所有度为0的顶点入栈
		if (g.head_list[i].indegree == 0)
		{
			top++;
			stack[top] = i;
		}
	}
	while (top >= 0) {
		i = stack[top];
		top--;
		number++;
		cout << i << ",";

		CSTACK[++CTOP] = i;//保存拓扑序列
		p = g.head_list[i].head;
		while (p) {	//求解各事件的最早发生时间
			j = p->another_vertex;

			if (ve[i] < (ve[i] + p->info))
				ve[j] = ve[i] + p->info;
			p = p->next;
		}
		
		p = g.head_list[i].head;
		while (p) {
			j = p->another_vertex;
			g.head_list[j].indegree--;
			if (!g.head_list[j].indegree)
			{
				top++;
				stack[top] = j;
			}
			p = p->next;
		}
	
	}

	if (number < g.vertex_num) {
		cout << "网中含有环";
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南叔先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值