Design and solve the critical path program of AOE network (detailed design, complete implementation code attached)

Table of contents

1. Course design content

2. Curriculum Design Realization Function

3. The specific implementation process and possible problems

Fourth, realize the program function module diagram

Five, the specific implementation code (complete code C language)

6. Demonstrate how to operate (note the input information, the input here is subject to the above AOE network)

6.1 First enter the number of vertices and edges of the AOE network

6.2 Input vertex elements (just copy and paste)

6.3 Enter side information (just copy and paste)

6.5 Operation result page display

6.5.1 AOE input infographic:

6.5.2 Adjacency representation graph of graph: 

6.5.3 Critical path diagram and minimum total engineering time:

7. Summary and experience


Persist in reading to discover the existence of beauty!

More excitement is waiting for you!

Let's communicate across time and space together.

1. Course design content

Design content : The work content of a fine decoration project for a new house mainly includes: (A) inspection and handover of the construction site; (B) window cabinet, door frame protection; (C) waterproofing work; (D) civil engineering renovation; (E) waterproofing inspection and acceptance; (F) Hydropower inspection and reception; (G) Original ground protection; (H) Tiling laying operation; (I) Office ceiling operation; (J) Ceiling operation; (K) Ceiling surface oiling once; (L) Wall surface once (M) cabinet installation; (N) secondary surface oiling for ceilings; (O) installation of kitchen and bathroom appliances; (P) wardrobe and door frame entry operations; (R) Air conditioner installation; (S) Door leaf installation; (T) Wooden floor operation; (U) Acceptance and rectification; (V) Construction completed and delivered (reserve and hand over the corresponding instructions to the property). According to the above work content, the specific working hours are shown in Table 1 below. Please construct the AOE network of this project, and find out its critical path and the shortest total construction period. The estimated construction period of the specific decoration project is as follows in Table 1:

Table 1: Estimated schedule of a renovation project

job title

work content

Estimated duration (unit: day)

A

Construction work site inspection and handover

3

B

Window display, door frame protection

3

C

Waterproofing

10

D

Civil renovation

20

E

Waterproof inspection reception

3

F

Hydropower inspection and reception

3

G

raw ground protection

2

H

Tiling work

6

I

Office ceiling work

10

J

Ceiling work

9

K

ceiling oil once

14

L

wall surface oil

10

M

cabinet installation

3

N

Ceiling secondary surface oil

10

O

Kitchen and bathroom appliance installation

5

P

Wardrobe, door frame work

4

Q

Secondary surface oiling for wallpaper or walls

10

R

air conditioner installation

3

S

door installation

3

T

wood floor work

10

U

Acceptance and rectification

20

V

Construction completed and handed over

17

2. Curriculum Design Realization Function

  1. Determine the sequence of each work and draw the AOE network diagram;
  2. Calculate the time of each activity in the AOE network diagram;
  3. determine the critical path;
  4. Calculate the minimum total duration.

3. The specific implementation process and possible problems

1. Build an AOE network: According to the number of vertices and edges input by the user, use the adjacency matrix or adjacency list to represent the structure of the graph. Each vertex is assigned a unique identifier and the start and end vertices of the edges are added to the graph. You can use a two-dimensional array to represent an adjacency matrix, or a linked list to represent an adjacency list.

2. Calculate the earliest start time and earliest finish time: Use topological sorting to determine the order of activities. Starting from the starting vertex, the earliest start time and earliest completion time of each activity are calculated in sequence according to the order of topological sorting. For each activity, the earliest completion time of its predecessor activities needs to be considered. Topological sorting can be done using depth-first search or breadth-first search.

3. Calculate the latest start time and latest finish time: You can use inverse topological sorting to determine the order of activities. Starting from the terminal vertex, calculate the latest start time and latest completion time of each activity in the order of reverse topological sorting. For each activity, the latest start times of its successor activities need to be considered. Inverse topological sorting can be done using depth-first search or breadth-first search.

4. Output critical path and minimum total duration: By comparing the earliest and latest completion times for each activity, the activities on the critical path can be identified. Activities on the critical path are those whose earliest start time and latest start time are equal. The shortest total duration can be determined by the earliest finish time of the destination vertex.

5. Error handling: Need to deal with input errors, such as the number of vertices or edges input is negative, the duration of the activity is negative, etc. You can use conditional judgment statements to check the validity of the input and give appropriate prompts when errors occur.

6. Memory management: After the program ends, the dynamically allocated memory needs to be released to prevent memory leaks. Memory can be allocated using the `malloc()` function and freed using the `free()` function.

Fourth, realize the program function module diagram

4.1 AOE network diagram 4.2 Program flow chart

Five, the specific implementation code (complete code C language)

#include <stdio.h>
#include <stdlib.h>
#define VertexType int //顶点的数据类型(char) 
#define VertexMax 40 //最大顶点个数 
typedef struct ArcNode//边表 
{
	int adjvex;//存储的是该顶点在顶点数组即AdjList[]中的位置 
	int weight;//权值 
	struct ArcNode* next;
}ArcNode;

typedef struct VNode //顶单个点 
{
	VertexType vertex;
	struct ArcNode* firstarc;
}VNode;

typedef struct //顶点表 
{
	VNode AdjList[VertexMax];//由顶点构成的结构体数组 
	int vexnum, arcnum; //顶点数和边数 
}ALGraph;

int LocateVex(ALGraph* G, VertexType v)
{
	int i;
	for (i = 0; i < G->vexnum; i++)
	{
		if (v == G->AdjList[i].vertex)
		{
			return i;
		}
	}

	printf("No Such Vertex!\n");
	return -1;
}

//有向图 
void CreateDN(ALGraph* G)
{
	int i, j;
	//1.输入顶点数和边数 
	printf("输入顶点个数和边数:\n");
	printf("顶点数 n=");
	scanf("%d", &G->vexnum);
	printf("边  数 e=");
	scanf("%d", &G->arcnum);
	printf("\n");

	printf("\n");
	//2.顶点表数据域填值初始化顶点表指针域 
	printf("输入顶点元素(用空格隔开):");
	for (i = 0; i < G->vexnum; i++)
	{
		/*printf("输入第%d个顶点信息:", i + 1);*/
		scanf(" %d", &G->AdjList[i].vertex);
		//printf("222%d", G->AdjList[i].vertex);
		G->AdjList[i].firstarc = NULL;
	}
	printf("\n");

	//3.输入边信息构造邻接表 
	int n, m;
	VertexType v1, v2;
	int weight;
	ArcNode* p1, * p2;

	printf("请输入边的信息:\n\n");
	for (i = 0; i < G->arcnum; i++)
	{   //输入边信息,并确定v1和v2在G中的位置,即顶点在AdjList[]数组中的位置(下标)  
		printf("输入第%d条边信息:", i + 1);
		scanf(" %d%d,%d", &v1, &v2, &weight);
		n = LocateVex(G, v1);
		m = LocateVex(G, v2);

		if (n == -1 || m == -1)
		{
			printf("NO This Vertex!\n");
			return;
		}

		p1 = (ArcNode*)malloc(sizeof(ArcNode));
		p1->adjvex = m;//填上坐标 
		p1->weight = weight;//填上权值 
		p1->next = G->AdjList[n].firstarc;//改链(头插法)  
		G->AdjList[n].firstarc = p1;

	}//for  
}

void print(ALGraph G)
{
	int i;
	ArcNode* p;
	printf("\n-------------------------------");
	printf("\n图的邻接表表示:\n");

	for (i = 0; i < G.vexnum; i++)
	{
		printf("\n   AdjList[%d]%4d", i, G.AdjList[i].vertex);
		p = G.AdjList[i].firstarc;

		while (p != NULL)
		{
			printf("-->%d(%d)", p->adjvex+1, p->weight);
			p = p->next;
		}
	}
	printf("\n");
	printf("\n-------------------------------\n");
}

int TopologicalSort(ALGraph* G, int* topo)
{
	int i;
	int top = -1;//栈顶指针 
	int Gettop;//用于存储/获取栈的栈顶元素 
	int count = 0;//用于统计拓扑排序生成的结点数(若生成结点数 < 图的结点数,则代表图中有环,拓扑排序不成功) 
	int stack[VertexMax] = { 0 };//栈 
	int indegree[VertexMax] = { 0 };//入度数组 
	struct ArcNode* p;//临时变量 

	//1.计算顶点入度,并存入indegree数组中
	for (i = 0; i < G->vexnum; i++)
	{
		if (G->AdjList[i].firstarc != NULL)
		{
			p = G->AdjList[i].firstarc;
			while (p != NULL)
			{
				indegree[p->adjvex]++;
				p = p->next;
			}
		}
	}

	//2.初始化部分:将初始入度为0的顶点入栈
	for (i = 0; i < G->vexnum; i++)
	{
		if (indegree[i] == 0)
		{
			stack[++top] = i;//先将指针加一在进行存储 
		}
	}

	//3.拓扑排序
	int m = 0;
	while (top != -1)//栈不为空 
	{
		Gettop = stack[top--];//获取栈顶元素,并且栈顶指针减一 
		topo[m++] = Gettop;
		//printf(" %c(%d)",G->AdjList[Gettop].vertex,topo[m-1]);//输出栈顶元素 
		count++;

		p = G->AdjList[Gettop].firstarc;
		while (p != NULL)
		{
			indegree[p->adjvex]--;

			if (indegree[p->adjvex] == 0)
			{
				stack[++top] = p->adjvex;
			}
			p = p->next;
		}
	}

	//4.判断拓扑排序是否成功(生成结点数 < 图的结点数,则代表图中有环,拓扑排序不成功) 
	if (count < G->vexnum)
	{
		printf("TopologicalSort Failed!\n");
		return 0; //拓扑排序失败 
	}
	else
		return 1; //拓扑排序成功   
}

void CriticalPath(ALGraph* G)
{
	int i;
	int j, k;// <Vj,Vk>
	int e, l;//活动最早开始时间/活动最晚开始时间  
	int topo[VertexMax];//拓扑数组,用于存储拓扑排序结果(存储内容是每个结点的坐标) 
	int ve[VertexMax]; //事件vi的最早发生时间 
	int vl[VertexMax]; //事件vi的最晚发生时间 
	struct ArcNode* p;

	//1.调用拓扑排序,检测图是否存在环 
	if (!TopologicalSort(G, topo))//若拓扑排序成功,topo数组也将处理完毕 
	{
		return;
	}

	//2.正拓扑排序,求出事件最早发生时间 
	for (i = 0; i < G->vexnum; i++)
		ve[i] = 0;
	for (i = 0; i < G->vexnum; i++)
	{
		j = topo[i];//j为起始点,k为终点 
		p = G->AdjList[j].firstarc;//用指针p去依次寻找j的每一个邻接点 
		while (p)
		{
			k = p->adjvex;
			if (ve[k] < ve[j] + p->weight)//根据j的邻接点k,不断更新ve[]的值(选出最大值,原理类似于选择排序) 
			{
				ve[k] = ve[j] + p->weight;
			}
			p = p->next;
		}
	}
	//3.逆拓扑排序,求出事件最迟发生时间 
	for (i = 0; i < G->vexnum; i++)
		vl[i] = ve[G->vexnum - 1];
	for (i = G->vexnum - 1; i >= 0; i--)
	{
		j = topo[i];
		p = G->AdjList[j].firstarc;//让p去依次查找邻接点 
		while (p)
		{
			k = p->adjvex;
			if (vl[j] > vl[k] - p->weight)//根据j的邻接点k,不断更新vl[]的值(选出最小值,原理类似于选择排序)
			{
				vl[j] = vl[k] - p->weight;
			}
			p = p->next;
		}
	}

	//输出ve[i] 
	printf("\tve[i]:");
	for (i = 0; i < G->vexnum; i++)
	{
		printf("\t%d", ve[i]);
	}
	printf("\n");

	//输出vl[i]
	printf("\tvl[i]:");
	for (i = 0; i < G->vexnum; i++)
	{
		printf("\t%d", vl[i]);
	}
	printf("\n\n");

	//4.计算e和l,通过判断e是否等于l确定该活动是否是关键活动,从而确定关键路径
	for (i = 0; i < G->vexnum; i++)
	{
		p = G->AdjList[i].firstarc;//让p去依次查找邻接点 
		while (p)
		{
			j = p->adjvex;
			e = ve[i];//计算活动最早开始时间 e 
			l = vl[j] - p->weight;//计算活动最晚开始时间 l 


			if (e == l)//如果e=l,说明该活动是关键活动 
			{
				printf("\t%d->%d(%d)\n", G->AdjList[i].vertex, G->AdjList[j].vertex, p->weight);
			}
			p = p->next;
		}
	}
	printf("最短总工期:%d\n", ve[16]);

}


int main()
{
    ALGraph G;//声明顶点表 顶点数和边数 
    CreateDN(&G);//根据输入顶点数和边,生成有向图 
    print(G); //打印有向图邻接表 
    
    printf("关键路径:\n\n");
   	CriticalPath(&G);  //输出关键路径 正向拓扑排序,逆向拓扑排序,确定关键路径,输出关键路径 
	
	system("pause"); 
	return 0;
}

6. Demonstrate how to operate (note the input information, the input here is subject to the above AOE network)

6.1 First enter the number of vertices and edges of the AOE network

n= 17  

e = 22

6.2 Input vertex elements (just copy and paste)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

6.3 Enter side information (just copy and paste)

1 2,3

2 3,3

3 4,10

3 5,20

4 6,3

5 6,3

6 7,6

6 8,10

6 11,2

7 11,9

8 9,14

8 10,10

9 11,10

10 11,10

11 12,3

11 13,3

11 14,4

12 15,5

13 15,3

14 15,10

15 16,20

16 17,17

 6.5 Operation result page display

6.5.1 AOE input infographic:

6.5.2 Adjacency representation graph of graph: 

 

 6.5.3 Critical path diagram and minimum total engineering time:

7. Summary and experience

     In the process of AOE topology sorting program design and development, I deeply realized the following points: 1. The importance of planning and organizational skills: Before starting the course design, I was fully aware of the importance of planning and organization. I made a detailed plan and timetable, and clarified the tasks and time nodes of each stage. I decomposed the entire course design into multiple small tasks, and reasonably arranged the time and resources for each task. In this way, I can better grasp the progress of the whole project, adjust and arrange in time to ensure the smooth progress of the project. 2. The importance of needs analysis: Before the course design, I conducted a full needs analysis. I conducted in-depth communication with users and stakeholders to understand their needs and expectations. I have described and analyzed user needs in detail through user stories, user portraits, etc. In this way, I can better understand the needs of users, determine key functions and priorities, and provide a clear direction for subsequent design and development. 3. Application of design thinking: In the course design process, I used the methods and tools of design thinking. Through user stories, user portraits, prototype design, etc., I deeply understand user needs and propose innovative solutions. I put user experience first, pay attention to user feelings and needs, and design solutions that better meet user expectations. 4. The importance of teamwork: I work closely with team members during the course design process. We discuss problems together, propose solutions, divide labor and cooperate, and work together to achieve the goals of the project. Through effective communication and collaboration, we have solved many difficulties and problems. The power of teamwork is huge, it can stimulate the creativity and potential of team members and achieve better results. 5. The importance of testing and debugging: After the course design is completed, I have fully tested and debugged. I have functionally tested the program to make sure that the various functions of the program are working properly. I ran a performance test to check if the performance of the program meets the requirements. I conducted a security test to ensure the security and stability of the program. Through testing and debugging, I found some problems, and repaired and optimized them in time to ensure the correctness and reliability of the program.

     In short, through this course design, I not only acquired professional knowledge and skills, but also cultivated my planning and organizational skills, analytical and problem-solving skills, and teamwork skills. I deeply understand that curriculum design is a test of comprehensive ability, which requires the comprehensive use of various skills and methods in order to achieve good results. At the same time, I also realized that curriculum design is a process of continuous learning and improvement, which requires constant reflection and summary, and continuous improvement and perfection.

Success requires hard work and persistence, don't give up easily.

Creation is not easy, thank you for your support, your likes and attention are my inexhaustible motivation for creation! Help each other, grow you and me, small acts of kindness, please believe that the world will become a better place!

 Creation is not easy, if it is helpful to you, please invite the author to drink coffee! Thanks!

 

Guess you like

Origin blog.csdn.net/a910247/article/details/131351919