有向图的拓扑排序和关键路径计算

实现有向图的拓扑排序和关键路径计算。在此基础上,借助OpenCV将图及其关键路径画出来。函数CriticalPath() -> 源文件CriticalPath.cpp*/

下面将代码贴出 里面有详细的注释

最终的结果:由opencv制成有向图的拓扑排序和关键路径计算_第1张图片

input.txt的内容

1 2 5
1 3 7
2 4 3
3 4 6
3 5 3
4 5 4
4 6 4
4 7 4
5 7 2
6 9 4
7 9 5
5 8 5
8 9 2
#include 
#include 
#include 
#include 
#include 
using namespace std;
using namespace cv;
Mat P(800, 800, CV_8UC3, Scalar(255, 255, 255));
#define Max 999
#define MAX_NUM 10  //顶点最多个数
int ve[MAX_NUM] = { 0 };//事件最早发生时间
int vl[MAX_NUM] = { Max };//事件最迟发生时间
int a[50], b[50], c[50];//初始化时用的初始点 终点 权值
typedef struct _Graph
{
	int matrix[MAX_NUM][MAX_NUM];  //邻接矩阵 
	int vexnum;                                  //顶点个数
	int arcs;                                    //弧的个数
}Graph;
struct datas//点的颜色
{
	int r, g, b;
}color[11];
struct location//点的位置
{
	int x, y;
}local[11];

void startdata();//颜色 坐标初始值
void CreateGraph(Graph &g);//图的建造
void FindInDegree(Graph g, int in[]);//查找入度为0的顶点
void TopologicalSort(Graph g, stack &s);//拓扑排序
void CriticalPath(Graph g);//关键路径

int main()
{
	Graph graph; int i;
	CreateGraph(graph);
	startdata();
	for (i = 0; i < 13; i++)//打印原始的点
	{
		Point s;
		circle(P, Point(local[a[i]].x, local[a[i]].y), 40, Scalar(color[a[i]].r, color[a[i]].g, color[a[i]].b), -1);
		circle(P, Point(local[b[i]].x, local[b[i]].y), 40, Scalar(color[b[i]].r, color[b[i]].g, color[b[i]].b), -1);
		line(P, Point(local[a[i]].x, local[a[i]].y), Point(local[b[i]].x, local[b[i]].y), Scalar(0, 0, 0), 5, CV_AA);
		imshow("out", P);
		waitKey(20);
	}
	CriticalPath(graph);
	waitKey(10000);
}

void startdata()//颜色 坐标初始值
{
	int i;
	for (i = 1; i < 11; i++)
	{
		color[i].r = rand() % 255;
		color[i].b = rand() % 255;
		color[i].g = rand() % 255;
	}
	local[1].x = 80; local[1].y = 300; local[2].x = 150; local[2].y = 100; local[3].x = 150; local[3].y = 500; local[4].x = 300; local[4].y = 100;
	local[5].x = 300; local[5].y = 500; local[6].x = 500; local[6].y = 100; local[7].x = 350; local[7].y = 300; local[8].x = 600; local[8].y = 500;
	local[9].x = 700; local[9].y = 300;
}

void CreateGraph(Graph &g)//图的建造
{
	FILE *fp; int cost[15][15];
	fopen_s(&fp, "input.txt", "r");
	int i = 0, sum = 0;
	while (!feof(fp))
	{
		fscanf_s(fp, "%d %d %d\n", &a[i], &b[i],&c[i]);
		i++; sum++;
	}
	fclose(fp);	
	g.vexnum = 9;
	for (int i = 0; i <= g.vexnum; i++)
	{
		for (int j = 0; j <= g.vexnum; j++)
		{
			g.matrix[i][j] = Max;
		}
	}
	g.arcs = sum;
	for (int i = 0; i < g.arcs; i++)
	{
		g.matrix[a[i]][b[i]] = c[i];//权值
	}
}

void FindInDegree(Graph g, int in[])//查找入度为0的顶点
{
	for (int i = 1; i <= g.vexnum; i++)
	{
		for (int j = 1; j <= g.vexnum; j++)
		{
			if (g.matrix[i][j] != Max)
			{
				in[j]++;
			}
		}
	}
}
void TopologicalSort(Graph g, stack &s)//拓扑排序,并寻找事件最早发生时间 堆栈s存储拓扑排序的顺序
{
	int in[MAX_NUM] = { 0 };
	FindInDegree(g, in);
	queue q;//定义一个队列q
	int i;
	for (i=1; i <= g.vexnum; i++)
	{
		if (in[i] == 0)
		{
			q.push(i);//入队
		}
	}
	int count = 0;
	while (!q.empty())
	{
		i = q.front();//查看队列的第一个元素
		s.push(i);//堆栈入栈
		q.pop();//弹出队列最后一个元素
		count++;
		for (int j = 1; j <= g.vexnum; j++)
		{
			if (g.matrix[i][j] != Max)//如果该节点到其他节点有路
			{
				if (!(--in[j]))//如果某一节点去掉一条边后入度为0
				{
					q.push(j);//入队
				}
				if (ve[i] + g.matrix[i][j] > ve[j])//如果该入度为0的节点通往其他节点的权值大于某一节点的最早发生时间(权值)就更新他们的最早发生时间
				{
					ve[j] = ve[i] + g.matrix[i][j];
				}
			}
		}
	}
}

void CriticalPath(Graph g)//关键路径
{
	stack s;//定义一个堆栈
	TopologicalSort(g, s);//拓扑排序
	for (int i = 1; i <= g.vexnum; i++)//初始化事件最迟发生时间
	{
		vl[i] = Max;
	}
	vl[g.vexnum - 1] = ve[g.vexnum - 1];//从尾部开始
	int i = 1,j,max=-1;
	while (!s.empty())//堆栈不空 计算事件最迟发生时间 运用逆的拓扑排序 类似于求ve【】
	{
		i = s.top();//获取栈顶元素
		s.pop();//弹出栈顶元素
		for (int j = 1; j <= g.vexnum; j++)
		{
			if (g.matrix[i][j] != Max)
			{
				int tmp = g.matrix[i][j];
				if (vl[j] - g.matrix[i][j] < vl[i])
				{
					vl[i] = vl[j] - g.matrix[i][j];
				}
			}
		}
	}
	for (i = 1; i <= g.vexnum; i++)//计算关键路径长度
	{
		for (j = 1; j <= g.vexnum; j++)
		{
			if (g.matrix[i][j] != Max)
			{
				if (ve[i] == vl[j] - g.matrix[i][j])
				{
					max = j;
					line(P, Point(local[i].x, local[i].y), Point(local[j].x, local[j].y), Scalar(0, 0, 255), 5, CV_AA);
					imshow("out", P);
					waitKey(50);
				}
			}

		}
	}
	line(P, Point(local[max].x, local[max].y), Point(local[9].x, local[9].y), Scalar(0, 0, 255), 5, CV_AA);
	imshow("out", P);
	waitKey(50);
	printf("关键路径%d\n" ,vl[g.vexnum - 1]);
}

 

你可能感兴趣的:(有向图的拓扑排序和关键路径计算)