当前位置:   article > 正文

[数据结构]AOE网 关键活动 关键路径_aoe网的关键路径代码

aoe网的关键路径代码

学习记录~~

 

  1. #include<iostream>
  2. using namespace std;
  3. const int MaxSize = 100;
  4. const int MaxInt = 32767;
  5. typedef string DataType;
  6. const int MaxEdge = 100;
  7. struct EdgeNode
  8. {
  9. int adjvex;
  10. int weight;
  11. EdgeNode* next;
  12. };
  13. struct VertexNode
  14. {
  15. DataType vertex;//顶点信息
  16. int in_Top_Num;//每个顶点入度(求拓扑序列时会递减)
  17. int inNum;//每个顶点入度
  18. int in[MaxSize];//狐尾顶点的下标
  19. int outNum;//每个顶点出度
  20. int out[MaxSize];//狐头顶点的下标
  21. EdgeNode* firstEdge;
  22. };
  23. struct EdgeType
  24. {
  25. int from, to;
  26. int weigth;
  27. };
  28. class ALGraph
  29. {
  30. private:
  31. EdgeType edge[MaxEdge];//边集数组
  32. VertexNode adjList[MaxSize];
  33. int vertexNum, edgeNum;
  34. int Max(int i);
  35. int Min(int i);
  36. public:
  37. ALGraph(DataType a[], int n, int e);
  38. ~ALGraph();
  39. int *TopSort();//返回拓扑序列下标
  40. void CriticalPath(int *Top);
  41. };
  42. int vl[MaxSize], ve[MaxSize], ee[MaxSize], el[MaxSize],el_ee[MaxSize];
  43. /*
  44. 0 1 4
  45. 0 2 3
  46. 1 2 2
  47. 2 3 4
  48. 1 3 6
  49. */
  50. int main()
  51. {
  52. DataType s[] = { "v0","v1","v2","v3" };
  53. ALGraph G(s, 4, 5);
  54. G.CriticalPath(G.TopSort());
  55. return 0;
  56. }
  57. ALGraph::ALGraph(DataType a[], int n, int e)
  58. {
  59. vertexNum = n;
  60. edgeNum = e;
  61. int i, j, k, w=0;
  62. int* rd = new int[vertexNum];
  63. int* cd = new int[vertexNum];
  64. for (i = 0; i < vertexNum; i++)
  65. cd[i] = rd[i] = 0;
  66. for (i = 0; i < vertexNum; i++)
  67. {
  68. adjList[i].vertex = a[i];
  69. adjList[i].firstEdge = NULL;
  70. adjList[i].in_Top_Num = 0;
  71. adjList[i].inNum = 0;
  72. adjList[i].outNum = 0;
  73. }
  74. for (k = 0; k < edgeNum; k++)
  75. {
  76. cin >> i >> j >> w;
  77. EdgeNode* s = new EdgeNode;
  78. s->adjvex = j;
  79. s->weight = w;
  80. s->next = adjList[i].firstEdge;
  81. adjList[i].firstEdge = s;
  82. adjList[j].in_Top_Num++;
  83. adjList[j].inNum++;
  84. adjList[j].in[rd[j]++] = i;//j顶点的入度加一后这里把以j顶点做狐头的狐的狐尾顶点的下标存起来
  85. adjList[i].outNum++;
  86. adjList[i].out[cd[i]++] = j;//i顶点的出度加一后这里把以i顶点做为狐尾的狐的狐头的下标存起来
  87. edge[k].from = i;
  88. edge[k].to = j;
  89. edge[k].weigth = w;
  90. }
  91. delete[]rd;
  92. delete[]cd;
  93. }
  94. ALGraph::~ALGraph()
  95. {
  96. int i, j;
  97. EdgeNode* p = NULL, * q = NULL;
  98. for (i = 0; i < vertexNum; i++)
  99. {
  100. p = q = adjList[i].firstEdge;
  101. while (p != NULL)
  102. {
  103. p = p->next;
  104. delete q;
  105. q = p;
  106. }
  107. }
  108. }
  109. int* ALGraph::TopSort()
  110. {
  111. int Start[MaxSize],top=-1;
  112. int *topxl=new int[MaxSize];//保存拓扑序列下标
  113. int i, j, k, count = 0;
  114. EdgeNode* p = NULL;
  115. for (i = 0; i < vertexNum; i++)
  116. {
  117. if (adjList[i].in_Top_Num==0)
  118. Start[++top] = i;
  119. }
  120. while (top != -1)
  121. {
  122. j = Start[top--];
  123. cout << adjList[j].vertex<<" ";
  124. topxl[count++] = j;
  125. p = adjList[j].firstEdge;
  126. while (p != NULL)
  127. {
  128. k = p->adjvex;
  129. adjList[k].in_Top_Num--;
  130. if (adjList[k].in_Top_Num == 0)
  131. Start[++top] = k;
  132. p = p->next;
  133. }
  134. }
  135. if (count == vertexNum)
  136. return topxl;
  137. if (count < vertexNum)
  138. {
  139. cout << "有回路\n";
  140. return NULL;
  141. }
  142. delete[]topxl;
  143. }
  144. int ALGraph::Max(int i)
  145. {
  146. int max_ve =-1,w=0;
  147. for (int j = 0; j < adjList[i].inNum; j++)
  148. {
  149. for (int k = 0; k < edgeNum; k++)
  150. {
  151. if (edge[k].to == i && edge[k].from == adjList[i].in[j])
  152. {
  153. w = edge[k].weigth;
  154. break;
  155. }
  156. }
  157. if (ve[adjList[i].in[j]] + w > max_ve)
  158. max_ve = ve[adjList[i].in[j]] + w;
  159. }
  160. return max_ve;
  161. }
  162. int ALGraph::Min(int i)
  163. {
  164. int w, min_vl=MaxInt;
  165. for (int j = 0; j < adjList[i].outNum; j++)
  166. {
  167. for (int k = 0; k < edgeNum; k++)
  168. {
  169. if (edge[k].from == i && edge[k].to == adjList[i].out[j])
  170. {
  171. w = edge[k].weigth;
  172. break;
  173. }
  174. }
  175. if (vl[adjList[i].out[j]] - w < min_vl)
  176. min_vl = vl[adjList[i].out[j]] - w;
  177. }
  178. return min_vl;
  179. }
  180. void ALGraph::CriticalPath(int* Top)
  181. {
  182. int i, j = 0, w = 0;
  183. cout << "\n拓扑序列为\n";
  184. for (i = 0; i < vertexNum; i++)
  185. cout << adjList[Top[i]].vertex << " ";
  186. cout << endl;
  187. for (i = 0; i < vertexNum; i++)
  188. {
  189. if (i == 0)
  190. ve[Top[i]] = 0;
  191. else
  192. ve[Top[i]] = Max(Top[i]);
  193. cout << "顶点" << adjList[Top[i]].vertex << "的最早发生时间ve=" << ve[Top[i]] << endl;
  194. }
  195. cout << endl;
  196. for (i = vertexNum - 1; i >= 0; i--)
  197. {
  198. if (i == vertexNum - 1)
  199. vl[Top[i]] = ve[Top[i]];
  200. else
  201. vl[Top[i]] = Min(Top[i]);
  202. cout << "顶点" << adjList[Top[i]].vertex << "的最迟发生时间vl=" << vl[Top[i]] << endl;
  203. }
  204. cout << endl;
  205. for (j = 0; j < edgeNum; j++)
  206. {
  207. ee[j] = ve[edge[j].from];
  208. el[j] = vl[edge[j].to] - edge[j].weigth;
  209. el_ee[j] = el[j] - ee[j];
  210. }
  211. for (j = 0; j < edgeNum; j++)
  212. cout << "下标为" << j << "的边的最早发生时间ee=" << ee[j] << endl;
  213. cout << endl;
  214. for (j = 0; j < edgeNum; j++)
  215. cout << "下标为" << j << "的边的最迟发生时间el=" << el[j] << endl;
  216. cout << endl;
  217. cout << "时间余量为0的边为关键活动\n";
  218. for (j = 0; j < edgeNum; j++)
  219. cout << "下标为" << j << "的边的时间余量el-ee=" << el_ee[j] << endl;
  220. cout << endl;
  221. int sum = vl[Top[vertexNum - 1]];
  222. w = 0;
  223. cout << "关键路径为为:\n";
  224. for (i = 0; i < edgeNum; i++)
  225. {
  226. if (w == sum)
  227. break;
  228. if (el_ee[i] == 0)
  229. {
  230. cout << "("<<adjList[edge[i].from].vertex << " ";
  231. cout << edge[i].weigth<<" ";
  232. cout << adjList[edge[i].to].vertex << ")";
  233. w+= edge[i].weigth;
  234. }
  235. }
  236. }

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/690368
推荐阅读
相关标签
  

闽ICP备14008679号