数据结构之拓扑排序和关键路径

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangquan2015/article/details/82845942

拓扑排序

在一个表示工程的有向图中,用顶点表示活动,用弧表示活动之间的优先关系,这样的有向图为顶点表示活动的网,称为AOV网
在这里插入图片描述
基本思路:从AOV网中选择一个入度为0的顶点输出,然后删去此顶点,并删除以此顶点为尾的弧,继续重复此步骤,直到输出全部顶点或者AOV网中不存在入度为0的顶点为止。

在求最小生成树和最短路径时,我们用的都是邻接矩阵,但由于拓扑排序的过程中,需要删除顶点,显然用邻接表会更加方便。因此我们需要为AOV网建立一个邻接表。考虑到算法过程中始终要查找入度为0的顶点,我们在原来顶点表结点结构中,增加一个入度域in:

入度 Vi 指针
in data firstedge

在这里插入图片描述
在这里插入图片描述

  1. 把入度为0的都入栈,{V0,V1,V3}
    在这里插入图片描述
  2. V3出栈,打印此顶点,输出顶点个数count+1,将V3连接的两个顶点V2和V13入度减1。
    在这里插入图片描述
  3. 再处理V1,出栈、打印,count+1,将V1连接的顶点V2、V4、V8入度减1,此时V2入度为0,将V2入栈……如此循环,打印V2、V6、V0、V4、V5、V8、V7、V12、V9、V10、V13、V11,所以最终打印的结果为:3->1->2->6->0->4->5->8->7->12->9->10->13->11。入栈的复杂度为O(n),入度减1执行e次,所以整个算法的时间复杂度为O(n+e)
    在这里插入图片描述
#include "stdio.h"    
#include "stdlib.h"   
#include "io.h"  
#include "math.h"  
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXEDGE 20
#define MAXVEX 14
#define INFINITY 65535

typedef int Status;	/* Status是函数的类型,其值是函数结果状态代码,如OK等 */

					/* 邻接矩阵结构 */
typedef struct
{
	int vexs[MAXVEX];
	int arc[MAXVEX][MAXVEX];
	int numVertexes, numEdges;
}MGraph;

/* 邻接表结构****************** */
typedef struct EdgeNode /* 边表结点  */
{
	int adjvex;    /* 邻接点域,存储该顶点对应的下标 */
	int weight;		/* 用于存储权值,对于非网图可以不需要 */
	struct EdgeNode *next; /* 链域,指向下一个邻接点 */
}EdgeNode;

typedef struct VertexNode /* 顶点表结点 */
{
	int in;	/* 顶点入度 */
	int data; /* 顶点域,存储顶点信息 */
	EdgeNode *firstedge;/* 边表头指针 */
}VertexNode, AdjList[MAXVEX];

typedef struct
{
	AdjList adjList;
	int numVertexes, numEdges; /* 图中当前顶点数和边数 */
}graphAdjList, *GraphAdjList;
/* **************************** */
void CreateMGraph(MGraph *G)/* 构件图 */
{
	int i, j;

	/* printf("请输入边数和顶点数:"); */
	G->numEdges = MAXEDGE;
	G->numVertexes = MAXVEX;

	for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
	{
		G->vexs[i] = i;
	}

	for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
	{
		for (j = 0; j < G->numVertexes; j++)
		{
			G->arc[i][j] = 0;
		}
	}
	G->arc[0][4] = 1;
	G->arc[0][5] = 1;
	G->arc[0][11] = 1;
	G->arc[1][2] = 1;
	G->arc[1][4] = 1;
	G->arc[1][8] = 1;
	G->arc[2][5] = 1;
	G->arc[2][6] = 1;
	G->arc[2][9] = 1;
	G->arc[3][2] = 1;
	G->arc[3][13] = 1;
	G->arc[4][7] = 1;
	G->arc[5][8] = 1;
	G->arc[5][12] = 1;
	G->arc[6][5] = 1;
	G->arc[8][7] = 1;
	G->arc[9][10] = 1;
	G->arc[9][11] = 1;
	G->arc[10][13] = 1;
	G->arc[12][9] = 1;

}
/* 利用邻接矩阵构建邻接表 */
void CreateALGraph(MGraph G, GraphAdjList *GL)
{
	int i, j;
	EdgeNode *e;

	*GL = (GraphAdjList)malloc(sizeof(graphAdjList));

	(*GL)->numVertexes = G.numVertexes;
	(*GL)->numEdges = G.numEdges;
	for (i = 0; i <G.numVertexes; i++) /* 读入顶点信息,建立顶点表 */
	{
		(*GL)->adjList[i].in = 0;
		(*GL)->adjList[i].data = G.vexs[i];
		(*GL)->adjList[i].firstedge = NULL; 	/* 将边表置为空表 */
	}

	for (i = 0; i<G.numVertexes; i++) /* 建立边表 */
	{
		for (j = 0; j<G.numVertexes; j++)
		{
			if (G.arc[i][j] == 1)
			{
				e = (EdgeNode *)malloc(sizeof(EdgeNode));
				e->adjvex = j;					/* 邻接序号为j  */
				e->next = (*GL)->adjList[i].firstedge;	/* 将当前顶点上的指向的结点指针赋值给e */
				(*GL)->adjList[i].firstedge = e;		/* 将当前顶点的指针指向e  */
				(*GL)->adjList[j].in++;

			}
		}
	}

}
/* 拓扑排序,若GL无回路,则输出拓扑排序序列并返回1,若有回路返回0。 */
Status TopologicalSort(GraphAdjList GL)
{
	EdgeNode *e;
	int i, k, gettop;
	int top = 0;  /* 用于栈指针下标  */
	int count = 0;/* 用于统计输出顶点的个数  */
	int *stack;	/* 建栈将入度为0的顶点入栈  */
	stack = (int *)malloc(GL->numVertexes * sizeof(int));

	for (i = 0; i<GL->numVertexes; i++)
		if (0 == GL->adjList[i].in) /* 将入度为0的顶点入栈 */
			stack[++top] = i;
	while (top != 0)
	{
		gettop = stack[top--];
		printf("%d -> ", GL->adjList[gettop].data);
		count++;        /* 输出i号顶点,并计数 */
		for (e = GL->adjList[gettop].firstedge; e; e = e->next)
		{
			k = e->adjvex;
			if (!(--GL->adjList[k].in))  /* 将i号顶点的邻接点的入度减1,如果减1后为0,则入栈 */
				stack[++top] = k;
		}
	}
	printf("\n");
	if (count < GL->numVertexes)
		return ERROR;
	else
		return OK;
}
int main(void)
{
	MGraph G;
	GraphAdjList GL;
	int result;
	CreateMGraph(&G);
	CreateALGraph(G, &GL);
	result = TopologicalSort(GL);
	printf("result:%d", result);
	system("pause");
	return 0;
}

最终打印的结果为:3->1->2->6->0->4->5->8->7->12->9->10->13->11

关键路径

拓扑排序主要是为了解决一个工程能否顺利进行的问题,但有时我们还需要解决工程完成需要的最短时间问题。

在一个表示工程的带权有向图中,用顶点表示事件,用有向边表示活动,用边上的权值表示活动的持续时间,这种网称为AOE网
在这里插入图片描述

就像写家庭作业这一事件的最早开始时间为现在,为0,最晚开始时间为两小时后,为2,最早开始时间不等于最晚开始时间就意味着有空闲。为此,我们需要定义几个参数:

  1. 事件的最早发生时间etv:顶点Vk的最早发生时间
  2. 事件的最晚发生时间ltv:顶点Vk的最晚发生时间
  3. 活动的最早开工时间ete:即弧Ak的最早发生时间
  4. 活动的最晚开工时间lte:即弧Ak的最晚发生时间

在这里插入图片描述

  1. 拓扑排序求每个事件的最早发生时间etv
    在这里插入图片描述
  2. 初始化全局变量ltv,因为etv[9]=27,所以数组ltv当前的值为{27,27,27,27,27,27,27,27,27,27}
  3. 将V9出栈,由于V9没有弧,所以继续往后执行。
  4. gettop=8,V8的弧只有一条<V8,V9>,ltv[8]=min{ltv[9]-3,ltv[8]}=24
  5. gettop=7,6,5时同理可得ltv的相应值为19,25,13,此时ltv值为{27,27,27,27,27,13,25,19,24,27}
  6. 当gettop=4时,V4有两条弧<V4,V6>、<V4,V7>,ltv[4]=min{ltv[7]-4,ltv[6]-9}=min{19-4,25-9}=15。同理得到ltv[0-3]的值:
    在这里插入图片描述
  7. ete=etv则是关键活动,没有任何空闲时间,打印;否则不是关键活动,不打印。如下<V0,V2>是关键活动,打印;<V0,V1>不打印。
    在这里插入图片描述
  8. 依次打印关键路径:
    在这里插入图片描述

分析整个关键路径的算法,其时间复杂度为O(n+e)。这里是唯一一条关键路径,并不等于不存在多条关键路径的有向无环图。如果是多条关键路径,则单是提高一条关键路径上的关键活动速度并不能导致整个工程缩短工期,而必须同时提高几条关键路径上的活动速度。这就像不仅仅需要有事业的成功,还需要有健康的身体以及快乐的生活!

#include "stdio.h"    
#include "stdlib.h"   
#include "io.h"  
#include "math.h"  
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXEDGE 30
#define MAXVEX 30
#define INFINITY 65535

typedef int Status;	/* Status是函数的类型,其值是函数结果状态代码,如OK等 */

int *etv, *ltv; /* 事件最早发生时间和最迟发生时间数组,全局变量 */
int *stack2;   /* 用于存储拓扑序列的栈 */
int top2;	   /* 用于stack2的指针 */

			   /* 邻接矩阵结构 */
typedef struct
{
	int vexs[MAXVEX];
	int arc[MAXVEX][MAXVEX];
	int numVertexes, numEdges;
}MGraph;

/* 邻接表结构****************** */
typedef struct EdgeNode /* 边表结点  */
{
	int adjvex;    /* 邻接点域,存储该顶点对应的下标 */
	int weight;		/* 用于存储权值,对于非网图可以不需要 */
	struct EdgeNode *next; /* 链域,指向下一个邻接点 */
}EdgeNode;

typedef struct VertexNode /* 顶点表结点 */
{
	int in;	/* 顶点入度 */
	int data; /* 顶点域,存储顶点信息 */
	EdgeNode *firstedge;/* 边表头指针 */
}VertexNode, AdjList[MAXVEX];

typedef struct
{
	AdjList adjList;
	int numVertexes, numEdges; /* 图中当前顶点数和边数 */
}graphAdjList, *GraphAdjList;
/* **************************** */


void CreateMGraph(MGraph *G)/* 构件图 */
{
	int i, j;
	/* printf("请输入边数和顶点数:"); */
	G->numEdges = 13;
	G->numVertexes = 10;

	for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
	{
		G->vexs[i] = i;
	}

	for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
	{
		for (j = 0; j < G->numVertexes; j++)
		{
			if (i == j)
				G->arc[i][j] = 0;
			else
				G->arc[i][j] = INFINITY;
		}
	}

	G->arc[0][1] = 3;
	G->arc[0][2] = 4;
	G->arc[1][3] = 5;
	G->arc[1][4] = 6;
	G->arc[2][3] = 8;
	G->arc[2][5] = 7;
	G->arc[3][4] = 3;
	G->arc[4][6] = 9;
	G->arc[4][7] = 4;
	G->arc[5][7] = 6;
	G->arc[6][9] = 2;
	G->arc[7][8] = 5;
	G->arc[8][9] = 3;

}

/* 利用邻接矩阵构建邻接表 */
void CreateALGraph(MGraph G, GraphAdjList *GL)
{
	int i, j;
	EdgeNode *e;

	*GL = (GraphAdjList)malloc(sizeof(graphAdjList));

	(*GL)->numVertexes = G.numVertexes;
	(*GL)->numEdges = G.numEdges;
	for (i = 0; i <G.numVertexes; i++) /* 读入顶点信息,建立顶点表 */
	{
		(*GL)->adjList[i].in = 0;
		(*GL)->adjList[i].data = G.vexs[i];
		(*GL)->adjList[i].firstedge = NULL; 	/* 将边表置为空表 */
	}

	for (i = 0; i<G.numVertexes; i++) /* 建立边表 */
	{
		for (j = 0; j<G.numVertexes; j++)
		{
			if (G.arc[i][j] != 0 && G.arc[i][j]<INFINITY)
			{
				e = (EdgeNode *)malloc(sizeof(EdgeNode));
				e->adjvex = j;					/* 邻接序号为j */
				e->weight = G.arc[i][j];
				e->next = (*GL)->adjList[i].firstedge;	/* 将当前顶点上的指向的结点指针赋值给e */
				(*GL)->adjList[i].firstedge = e;		/* 将当前顶点的指针指向e  */
				(*GL)->adjList[j].in++;

			}
		}
	}

}


/* 拓扑排序 */
Status TopologicalSort(GraphAdjList GL)
{    /* 若GL无回路,则输出拓扑排序序列并返回1,若有回路返回0。 */
	EdgeNode *e;
	int i, k, gettop;
	int top = 0;  /* 用于栈指针下标  */
	int count = 0;/* 用于统计输出顶点的个数 */
	int *stack;	/* 建栈将入度为0的顶点入栈  */
	stack = (int *)malloc(GL->numVertexes * sizeof(int));
	for (i = 0; i<GL->numVertexes; i++)
		if (0 == GL->adjList[i].in) /* 将入度为0的顶点入栈 */
			stack[++top] = i;

	top2 = 0;
	etv = (int *)malloc(GL->numVertexes * sizeof(int)); /* 事件最早发生时间数组 */
	for (i = 0; i<GL->numVertexes; i++)
		etv[i] = 0;    /* 初始化 */
	stack2 = (int *)malloc(GL->numVertexes * sizeof(int));/* 初始化拓扑序列栈 */

	printf("TopologicalSort:\t");
	while (top != 0)
	{
		gettop = stack[top--];
		printf("%d -> ", GL->adjList[gettop].data);
		count++;        /* 输出i号顶点,并计数 */

		stack2[++top2] = gettop;        /* 将弹出的顶点序号压入拓扑序列的栈 */

		for (e = GL->adjList[gettop].firstedge; e; e = e->next)
		{
			k = e->adjvex;
			if (!(--GL->adjList[k].in))        /* 将i号顶点的邻接点的入度减1,如果减1后为0,则入栈 */
				stack[++top] = k;

			if ((etv[gettop] + e->weight)>etv[k])    /* 求各顶点事件的最早发生时间etv值 */
				etv[k] = etv[gettop] + e->weight;
		}
	}
	printf("\n");
	if (count < GL->numVertexes)
		return ERROR;
	else
		return OK;
}

/* 求关键路径,GL为有向网,输出G的各项关键活动 */
void CriticalPath(GraphAdjList GL)
{
	EdgeNode *e;
	int i, gettop, k, j;
	int ete, lte;  /* 声明活动最早发生时间和最迟发生时间变量 */
	TopologicalSort(GL);   /* 求拓扑序列,计算数组etv和stack2的值 */
	ltv = (int *)malloc(GL->numVertexes*sizeof(int));/* 事件最早发生时间数组 */
	for (i = 0; i<GL->numVertexes; i++)
		ltv[i] = etv[GL->numVertexes - 1];    /* 初始化 */

	printf("etv:\t");
	for (i = 0; i<GL->numVertexes; i++)
		printf("%d -> ", etv[i]);
	printf("\n");

	while (top2 != 0)    /* 出栈是求ltv */
	{
		gettop = stack2[top2--];
		for (e = GL->adjList[gettop].firstedge; e; e = e->next)        /* 求各顶点事件的最迟发生时间ltv值 */
		{
			k = e->adjvex;
			if (ltv[k] - e->weight < ltv[gettop])
				ltv[gettop] = ltv[k] - e->weight;
		}
	}

	printf("ltv:\t");
	for (i = 0; i<GL->numVertexes; i++)
		printf("%d -> ", ltv[i]);
	printf("\n");

	for (j = 0; j<GL->numVertexes; j++)        /* 求ete,lte和关键活动 */
	{
		for (e = GL->adjList[j].firstedge; e; e = e->next)
		{
			k = e->adjvex;
			ete = etv[j];        /* 活动最早发生时间 */
			lte = ltv[k] - e->weight; /* 活动最迟发生时间 */
			if (ete == lte)    /* 两者相等即在关键路径上 */
				printf("<v%d - v%d> length: %d \n", GL->adjList[j].data, GL->adjList[k].data, e->weight);
		}
	}
}


int main(void)
{
	MGraph G;
	GraphAdjList GL;
	CreateMGraph(&G);
	CreateALGraph(G, &GL);
	CriticalPath(GL);
	system("pause");
	return 0;
}

运行结果:

TopologicalSort:        0 -> 1 -> 2 -> 3 -> 4 -> 6 -> 5 -> 7 -> 8 -> 9 ->
etv:    0 -> 3 -> 4 -> 12 -> 15 -> 11 -> 24 -> 19 -> 24 -> 27 ->
ltv:    0 -> 7 -> 4 -> 12 -> 15 -> 13 -> 25 -> 19 -> 24 -> 27 ->
<v0 - v2> length: 4
<v2 - v3> length: 8
<v3 - v4> length: 3
<v4 - v7> length: 4
<v7 - v8> length: 5
<v8 - v9> length: 3

猜你喜欢

转载自blog.csdn.net/zhangquan2015/article/details/82845942