1. #include "stdio.h"
    2. #include "stdlib.h"
    3. #include "io.h"
    4. #include "math.h"
    5. #include "time.h"
    6. #define OK 1
    7. #define ERROR 0
    8. #define TRUE 1
    9. #define FALSE 0
    10. #define MAXEDGE 30
    11. #define MAXVEX 30
    12. #define INFINITY 65535
    13. typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
    14. int *etv,*ltv; /* 事件最早发生时间和最迟发生时间数组,全局变量 */
    15. int *stack2; /* 用于存储拓扑序列的栈 */
    16. int top2; /* 用于stack2的指针 */
    17. /* 邻接矩阵结构 */
    18. typedef struct
    19. {
    20. int vexs[MAXVEX];
    21. int arc[MAXVEX][MAXVEX];
    22. int numVertexes, numEdges;
    23. }MGraph;
    24. /* 邻接表结构****************** */
    25. typedef struct EdgeNode /* 边表结点 */
    26. {
    27. int adjvex; /* 邻接点域,存储该顶点对应的下标 */
    28. int weight; /* 用于存储权值,对于非网图可以不需要 */
    29. struct EdgeNode *next; /* 链域,指向下一个邻接点 */
    30. }EdgeNode;
    31. typedef struct VertexNode /* 顶点表结点 */
    32. {
    33. int in; /* 顶点入度 */
    34. int data; /* 顶点域,存储顶点信息 */
    35. EdgeNode *firstedge;/* 边表头指针 */
    36. }VertexNode, AdjList[MAXVEX];
    37. typedef struct
    38. {
    39. AdjList adjList;
    40. int numVertexes,numEdges; /* 图中当前顶点数和边数 */
    41. }graphAdjList,*GraphAdjList;
    42. /* **************************** */
    43. void CreateMGraph(MGraph *G)/* 构件图 */
    44. {
    45. int i, j;
    46. /* printf("请输入边数和顶点数:"); */
    47. G->numEdges=13;
    48. G->numVertexes=10;
    49. for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
    50. {
    51. G->vexs[i]=i;
    52. }
    53. for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
    54. {
    55. for ( j = 0; j < G->numVertexes; j++)
    56. {
    57. if (i==j)
    58. G->arc[i][j]=0;
    59. else
    60. G->arc[i][j]=INFINITY;
    61. }
    62. }
    63. G->arc[0][1]=3;
    64. G->arc[0][2]=4;
    65. G->arc[1][3]=5;
    66. G->arc[1][4]=6;
    67. G->arc[2][3]=8;
    68. G->arc[2][5]=7;
    69. G->arc[3][4]=3;
    70. G->arc[4][6]=9;
    71. G->arc[4][7]=4;
    72. G->arc[5][7]=6;
    73. G->arc[6][9]=2;
    74. G->arc[7][8]=5;
    75. G->arc[8][9]=3;
    76. }
    77. /* 利用邻接矩阵构建邻接表 */
    78. void CreateALGraph(MGraph G,GraphAdjList *GL)
    79. {
    80. int i,j;
    81. EdgeNode *e;
    82. *GL = (GraphAdjList)malloc(sizeof(graphAdjList));
    83. (*GL)->numVertexes=G.numVertexes;
    84. (*GL)->numEdges=G.numEdges;
    85. for(i= 0;i <G.numVertexes;i++) /* 读入顶点信息,建立顶点表 */
    86. {
    87. (*GL)->adjList[i].in=0;
    88. (*GL)->adjList[i].data=G.vexs[i];
    89. (*GL)->adjList[i].firstedge=NULL; /* 将边表置为空表 */
    90. }
    91. for(i=0;i<G.numVertexes;i++) /* 建立边表 */
    92. {
    93. for(j=0;j<G.numVertexes;j++)
    94. {
    95. if (G.arc[i][j]!=0 && G.arc[i][j]<INFINITY)
    96. {
    97. e=(EdgeNode *)malloc(sizeof(EdgeNode));
    98. e->adjvex=j; /* 邻接序号为j */
    99. e->weight=G.arc[i][j];
    100. e->next=(*GL)->adjList[i].firstedge; /* 将当前顶点上的指向的结点指针赋值给e */
    101. (*GL)->adjList[i].firstedge=e; /* 将当前顶点的指针指向e */
    102. (*GL)->adjList[j].in++;
    103. }
    104. }
    105. }
    106. }
    107. /* 拓扑排序 */
    108. Status TopologicalSort(GraphAdjList GL)
    109. { /* 若GL无回路,则输出拓扑排序序列并返回1,若有回路返回0。 */
    110. EdgeNode *e;
    111. int i,k,gettop;
    112. int top=0; /* 用于栈指针下标 */
    113. int count=0;/* 用于统计输出顶点的个数 */
    114. int *stack; /* 建栈将入度为0的顶点入栈 */
    115. stack=(int *)malloc(GL->numVertexes * sizeof(int) );
    116. for(i = 0; i<GL->numVertexes; i++)
    117. if(0 == GL->adjList[i].in) /* 将入度为0的顶点入栈 */
    118. stack[++top]=i;
    119. top2=0;
    120. etv=(int *)malloc(GL->numVertexes * sizeof(int) ); /* 事件最早发生时间数组 */
    121. for(i=0; i<GL->numVertexes; i++)
    122. etv[i]=0; /* 初始化 */
    123. stack2=(int *)malloc(GL->numVertexes * sizeof(int) );/* 初始化拓扑序列栈 */
    124. printf("TopologicalSort:\t");
    125. while(top!=0)
    126. {
    127. gettop=stack[top--];
    128. printf("%d -> ",GL->adjList[gettop].data);
    129. count++; /* 输出i号顶点,并计数 */
    130. stack2[++top2]=gettop; /* 将弹出的顶点序号压入拓扑序列的栈 */
    131. for(e = GL->adjList[gettop].firstedge; e; e = e->next)
    132. {
    133. k=e->adjvex;
    134. if( !(--GL->adjList[k].in) ) /* 将i号顶点的邻接点的入度减1,如果减1后为0,则入栈 */
    135. stack[++top]=k;
    136. if((etv[gettop] + e->weight)>etv[k]) /* 求各顶点事件的最早发生时间etv值 */
    137. etv[k] = etv[gettop] + e->weight;
    138. }
    139. }
    140. printf("\n");
    141. if(count < GL->numVertexes)
    142. return ERROR;
    143. else
    144. return OK;
    145. }
    146. /* 求关键路径,GL为有向网,输出G的各项关键活动 */
    147. void CriticalPath(GraphAdjList GL)
    148. {
    149. EdgeNode *e;
    150. int i,gettop,k,j;
    151. int ete,lte; /* 声明活动最早发生时间和最迟发生时间变量 */
    152. TopologicalSort(GL); /* 求拓扑序列,计算数组etv和stack2的值 */
    153. ltv=(int *)malloc(GL->numVertexes*sizeof(int));/* 事件最早发生时间数组 */
    154. for(i=0; i<GL->numVertexes; i++)
    155. ltv[i]=etv[GL->numVertexes-1]; /* 初始化 */
    156. printf("etv:\t");
    157. for(i=0; i<GL->numVertexes; i++)
    158. printf("%d -> ",etv[i]);
    159. printf("\n");
    160. while(top2!=0) /* 出栈是求ltv */
    161. {
    162. gettop=stack2[top2--];
    163. for(e = GL->adjList[gettop].firstedge; e; e = e->next) /* 求各顶点事件的最迟发生时间ltv值 */
    164. {
    165. k=e->adjvex;
    166. if(ltv[k] - e->weight < ltv[gettop])
    167. ltv[gettop] = ltv[k] - e->weight;
    168. }
    169. }
    170. printf("ltv:\t");
    171. for(i=0; i<GL->numVertexes; i++)
    172. printf("%d -> ",ltv[i]);
    173. printf("\n");
    174. for(j=0; j<GL->numVertexes; j++) /* 求ete,lte和关键活动 */
    175. {
    176. for(e = GL->adjList[j].firstedge; e; e = e->next)
    177. {
    178. k=e->adjvex;
    179. ete = etv[j]; /* 活动最早发生时间 */
    180. lte = ltv[k] - e->weight; /* 活动最迟发生时间 */
    181. if(ete == lte) /* 两者相等即在关键路径上 */
    182. printf("<v%d - v%d> length: %d \n",GL->adjList[j].data,GL->adjList[k].data,e->weight);
    183. }
    184. }
    185. }
    186. int main(void)
    187. {
    188. MGraph G;
    189. GraphAdjList GL;
    190. CreateMGraph(&G);
    191. CreateALGraph(G,&GL);
    192. CriticalPath(GL);
    193. return 0;
    194. }