1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include<iostream> #include<cstring> #include"ADL.h" #define inp(i,x,y) for(i=x;i<=y;i++) #define Max_Vertex_Num 20
Status FindInDegree(ALGraph G, int *indegree); Status TopologicalSort(ALGraph G, int *T, int &Ttop); Status CriticalPath(ALGraph G); int ve[Max_Vertex_Num]; int vl[Max_Vertex_Num]; int main() { ALGraph G, GR; CreateGraph(G, GR); GraphPrint(G, GR); CriticalPath(G); return 0; } Status TopologicalSort(ALGraph G, int *T, int &Ttop) { int S[Max_Vertex_Num], Stop = -1, i = 0; int *indegree = new int[G.vexnum]; if(!FindInDegree(G, indegree)) return false; inp(i,0,G.vexnum-1) if(!indegree[i]) S[++Stop] = i; int count = 0; cout << "Directed acycline graph topologicalsort:\n"; while(Stop > -1) { i = S[Stop--]; T[++Ttop] = i; count++; cout << G.Vextices[i].data << " "; for(ArcNode* p=G.Vextices[i].firstarc; p; p=p->nextarc){ int k = p->adjvex; if(--indegree[k]==0) S[++Stop] = k; if(ve[i]+p->weight > ve[k]) ve[k] = ve[i] + p->weight; } } cout << endl; delete[] indegree; if(count < G.vexnum) return false; else return true; }
Status FindInDegree(ALGraph G, int *indegree) { int i; memset(indegree, 0, G.vexnum*sizeof(int)); inp(i,0,G.vexnum-1){ for(ArcNode *p=G.Vextices[i].firstarc; p; p=p->nextarc) indegree[p->adjvex]++; } return true; } Status CriticalPath(ALGraph G) { int T[Max_Vertex_Num], Ttop = -1, j = 0; if(!TopologicalSort(G, T, Ttop)){ cout << "This graph has a cicle.\n"; return false; }else cout << "--------------Critical Path Table-----------------\n"; inp(j,0,G.vexnum-1) vl[j] = ve[G.vexnum-1]; while (Ttop > -1){ j = T[Ttop--]; for(ArcNode *p=G.Vextices[j].firstarc; p; p=p->nextarc){ int k = p->adjvex, dut = p->weight; if(vl[k] - dut < vl[j]) vl[j] = vl[k] - dut; } }
cout << "Activities on edge:\n"; cout << "---------------------------------------------\n"; cout << "| Edge | duration | e | l | l-e | CriActi |\n"; inp(j,0,G.vexnum-1) for (ArcNode *p = G.Vextices[j].firstarc; p; p = p->nextarc) { int k = p->adjvex, dut= p->weight; int ee = ve[j], el = vl[k]-dut; string tag = (ve[j]==(vl[k]-dut))? "Yes":"No "; cout << "|<" << G.Vextices[j].data << ", " << G.Vextices[k].data << ">| "; cout << dut << " | " << ee << " | " << el << " | " << el-ee << " | " << tag << " |\n"; } cout << "---------------------------------------------\n"; cout << "Events on vertex:\n"; cout << "--------------------\n"; cout << "| Vertex | ve | vl |\n"; inp(j,0,G.vexnum-1) cout << "| " << G.Vextices[j].data << " | " << ve[j] << " | " << vl[j] << " |" << endl; cout << "--------------------\n"; return true; }
|