1. topological sorting (1) for example, some courses must be learned first.
Use a graph to describe this stuff:
So, the question is, can we find a sequence so that all the courses in this sequence meet the requirements: the first course is before the last course? Such a sequence is the topological sequence... (2) how to find the topological sequence? Simply put, the vertices without the precursor are removed, and the obtained vertices are topological sequences. Or in the example above: Step 1: 9 without the precursor, remove (the edges related to it are also removed ); step 2: At this time, there are 8, 6, 4, and three vertices without a precursor. You can choose to remove one of them. (this means that the topological sequence is not unique ~) ... (Below, you know ~) (3) The algorithm needs to use the inbound degree of the image because there is no precursor. The simulation process above knows that it is actually BFS:. create a vertex queue with zero inbound level B. scan the vertex table and queue the vertex with an inbound degree of 0. while (the queue is not empty) {output queue head node; write down the number of output nodes; Delete the associated outbound edge; if there is a node with 0 inbound, enter} d. if the number of output nodes is smaller than the total number of vertices, the output has a loop. (4) Symbolic code:
Void topsort (Adgraph * G) {queue <int> Q; int x, count = 0; for (int I = 1; I <= G-> n; I ++) if (G-> Ad [I]. indegree = 0) Q. push (I); // The vertex with an inbound degree of 0 enters the queue while (! Q. empty () {x = Q. front (); Q. pop (); cout <G-> Ad [x]. element; // output point count ++; // counter ++ link * m = G-> Ad [x]. firstedge; while (m! = NULL) {if (-- G-> Ad [m-> v]. indegree) = 0) Q. push (m-> v); // whenever a vertex is removed, the inbound degree --; if it becomes a vertex without a forward, enter m = m-> next ;}} if (count <G-> n) cout <"loops in the figure" <endl;} void topsort (Adgraph * G) {queue <int> Q; int x, count = 0; for (int I = 1; I <= G-> n; I ++) if (G-> Ad [I]. indegree = 0) Q. push (I); // The vertex with an inbound degree of 0 enters the queue while (! Q. empty () {x = Q. front (); Q. pop (); cout <G-> Ad [x]. element; // output point count ++; // counter ++ link * m = G-> Ad [x]. firstedge; while (m! = NULL) {if (-- G-> Ad [m-> v]. indegree) = 0) Q. push (m-> v); // whenever a vertex is removed, the inbound degree --; if it becomes a vertex without a forward, enter m = m-> next ;}} if (count <G-> n) cout <"loops in the figure" <endl ;}
2. AOE Network (1) What is AOE?
A. vertex on AOE indicates an event, and edge indicates an activity. The weight on the edge indicates the time required for the activity. A vertex with an inbound degree of 0 is called a Source Vertex (V1 ), the point with an exit degree of 0 is called the end point (v9); B. the problem we want to solve: the maximum number of activities that pass through from the source point to the end point (maximum !) Time, for example, the red line above is the maximum time spent to complete, the key path is the longest path (a1-> a4-> a7-> a10 or a1-> a4-> a8-> a11 [the Key Path is not unique]) (2) how to solve the problem? A. the earliest occurrence time of an eVent --- the longest path from the source point to this point --- VE [j]; B. the latest eVent occurrence time-on the premise that the eVent Vn is completed at VE (n) Time, the latest start time of the event Vk license ----- VL (k) c. the earliest (Early) start time of an Activity: if the Activity I is between <event j, event k>, it is easy to know that the earliest start time of activity I is the same as the earliest occurrence time of activity j. AE (I) = VE (j); d. the latest Late occurrence time of an Activity: the latest start time allowed by the Activity ai without causing any delay in the construction period. if this activity I is between <event j, event k>, for do not delay the construction period, the latest start time of activity I AL (I) it should be the latest completion time of I. VL (k) minus the duration of I, that is, AL (I) = VL (k)-ACT [j] [k]; e. share time: The difference between the latest start time and the earliest start time of this activity: the relaxation time of AL [I]-AE [I] is 0, so this activity is a key activity; (The preceding things have a major premise: when an activity starts, all its previous activities must be completed.) f. reverse topological sequence: reverse topological sequence; g. how can we find AE, VE, AL, VL? Based on the above definition, we can use the formula sub-Simple Representation: VE: From VE [1] = 0 start forward recursion, VE [I] = max {VE [j] + ACT <Vj, Vi >}, where <Vj, Vi> is an element of the set {all edges pointing to Vi; VL: reverse recursion starts from VL [n] = VE [n], VL [I] = min {VL [j]-ACT <Vi, Vj >}, medium <Vi, vj> is an element of the set {all edges from Vi}. AE: Activity k is represented by <Vi, Vj>, AE [k] = VE [I]; AL: activity k is represented by <Vi, Vj>, AL [k] = AL [j]-ACT <Vi, Vj> h. algorithm 1. create an adjacent table. 2. starting from the source point, let VE [1] = 0 and solve VE [I] In the topological order (to judge whether there is a ring); 3. starting from the end point, VL [n] = VE [n] solves VL [I] According to the inverse topological sequence; 4. solving AE [I], AL [I]; 5. if it is a key activity, output; hint: All of the above are self-generated, which is not strictly proven by any technical terms. For more information, see the definition and solution! Post a symbolic piece of code ~
// Topsort And AOE # include <iostream> # include <stack> # include <queue> # include <cstdio> using namespace std; struct link {int v; // event no. int count; // event no. int weight; // link * next;}; struct node {int indegree; // char element; // event struct link * firstedge;}; // header node struct Adgraph {int n, e; struct node Ad [101];}; // void Create_AOE (struct Adgraph * G) {int k, I, j, t; cin> G-> n> G-> e; // node and edge For (k = 1; k <= G-> n; k ++) {cin> G-> Ad [k]. element; G-> Ad [k]. firstedge = NULL; G-> Ad [k]. indegree = 0;} // initialization of the header node for (k = 1; k <= G-> e; k ++) {printf ("input two vertices (event number), Edge Weight (activity time) \ n"); cin >>> j >> I >> t; g-> Ad [I]. indegree ++; link * p = new link; p-> v = I; p-> weight = t; p-> next = G-> Ad [j]. firstedge; G-> Ad [j]. firstedge = p; // insert in the header} printf ("AOE network construction completed \ n ----- people are Lili split line ----- \ n print neighbor list: \ n "); for (I = 1; I <= G-> n; I ++) {cout <G-> D [I]. element; link * m = G-> Ad [I]. firstedge; while (m! = NULL) {printf ("-> % c, % d", G-> Ad [m-> v]. element, m-> weight); m = m-> next;} printf ("-> ^ \ n ");} // print printf ("\ n") in the adjacent table;} void Criticalpath (Adgraph * G) // G is the list of neighboring tables with weights {queue <int> Q; stack <int> S; int I, j, k, count = 0, ve [101], vl [101], AE, al; // The earliest occurrence time and the latest occurrence time of the time, the earliest occurrence time and the latest occurrence time of the activity // m is used to count and determine whether there is a loop for (I = 1; I <= G-> n; I ++) ve [I] = 0; // The earliest occurrence time of each event is 0 for (I = 1; I <= G-> n; I ++) if (G-> Ad [I]. indegree = 0) Q. push (I); // Add vertices with 0 inbound to the pr Intf ("Topsort:"); while (! Q. empty () {j = Q. front (); Q. pop (); count ++; cout <G-> Ad [j]. element; S. push (j); // Add the topological subscripts in positive order to the stack link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; G-> Ad [k]. indegree --; if (ve [j] + p-> weight> ve [k]) ve [k] = ve [j] + p-> weight; if (G-> Ad [k]. indegree = 0) Q. push (k); p = p-> next ;}// use topsort to find the earliest occurrence time printf ("\ n"); if (count <G-> n) {printf ("loop exists! \ N "); return;} for (I = 1; I <= G-> n; I ++) // for each event v (I) the latest occurrence time of vl [I] set the initial value vl [I] = ve [G-> n]; printf ("Opp_Topsort:"); while (! S. empty () // obtains the vertex {j = S. top (); S. pop (); // output stack cout <G-> Ad [j]. element; link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; if (vl [k]-p-> weight) <vl [j]) vl [j] = vl [k]-p-> weight; // modify vl [j] p = p-> next ;}} printf ("\ nActivity <EnventA-> EnventB> AE AL Share time Is Criticalpath?: \ N "); for (j = 1; j <= G-> n; j ++) // scan the vertex table {link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; AE = ve [j]; al = vl [k]-p-> weight; printf ("<event % c, event % c> \ t % d \ t ", G-> Ad [j]. element, G-> Ad [k]. element, AE, al, al-AE); if (al = AE) // key activities printf ("Yes"); else printf ("No "); printf ("\ n"); p = p-> next ;}} int main () {struct Adgraph G; Create_AOE (& G); Criticalpath (& G ); return 0;} // Topsort And AOE # include <iostream> # include <stack> # include <queue> # include <cstdio> using namespace std; Struct link {int v; // event no. int count; // activity no. int weight; // activity time link * next ;}; struct node {int indegree; // input char element; // event struct link * firstedge;}; // the header node struct Adgraph {int n, e; struct node Ad [101];}; // void Create_AOE (struct Adgraph * G) {int k, I, j, t; cin> G-> n> G-> e; // node and edge for (k = 1; k <= G-> n; k ++) {cin> G-> Ad [k]. element; G-> Ad [k]. firstedge = NULL; G-> Ad [k]. indegree = 0;} // The Beginning of the header Node Initial for (k = 1; k <= G-> e; k ++) {printf ("input two vertices (event number), Edge Weight (activity time) \ n "); cin> j> I> t; G-> Ad [I]. indegree ++; link * p = new link; p-> v = I; p-> weight = t; p-> next = G-> Ad [j]. firstedge; G-> Ad [j]. firstedge = p; // insert in the header} printf ("AOE network construction completed \ n ----- people are Lili split line ----- \ n print neighbor list: \ n "); for (I = 1; I <= G-> n; I ++) {cout <G-> Ad [I]. element; link * m = G-> Ad [I]. firstedge; while (m! = NULL) {printf ("-> % c, % d", G-> Ad [m-> v]. element, m-> weight); m = m-> next;} printf ("-> ^ \ n ");} // print printf ("\ n") in the adjacent table;} void Criticalpath (Adgraph * G) // G is the list of neighboring tables with weights {queue <int> Q; stack <int> S; int I, j, k, count = 0, ve [101], vl [101], AE, al; // The earliest occurrence time and the latest occurrence time of the time, the earliest occurrence time and the latest occurrence time of the activity // m is used to count and determine whether there is a loop for (I = 1; I <= G-> n; I ++) ve [I] = 0; // The earliest occurrence time of each event is 0 for (I = 1; I <= G-> n; I ++) if (G-> Ad [I]. indegree = 0) Q. push (I); // Add vertices with 0 inbound to the pr Intf ("Topsort:"); while (! Q. empty () {j = Q. front (); Q. pop (); count ++; cout <G-> Ad [j]. element; S. push (j); // Add the topological subscripts in positive order to the stack link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; G-> Ad [k]. indegree --; if (ve [j] + p-> weight> ve [k]) ve [k] = ve [j] + p-> weight; if (G-> Ad [k]. indegree = 0) Q. push (k); p = p-> next ;}// use topsort to find the earliest occurrence time printf ("\ n"); if (count <G-> n) {printf ("loop exists! \ N "); return;} for (I = 1; I <= G-> n; I ++) // for each event v (I) the latest occurrence time of vl [I] set the initial value vl [I] = ve [G-> n]; printf ("Opp_Topsort:"); while (! S. empty () // obtains the vertex {j = S. top (); S. pop (); // output stack cout <G-> Ad [j]. element; link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; if (vl [k]-p-> weight) <vl [j]) vl [j] = vl [k]-p-> weight; // modify vl [j] p = p-> next ;}} printf ("\ nActivity <EnventA-> EnventB> AE AL Share time Is Criticalpath?: \ N "); for (j = 1; j <= G-> n; j ++) // scan the vertex table {link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; AE = ve [j]; al = vl [k]-p-> weight; printf ("<event % c, event % c> \ t % d \ t ", G-> Ad [j]. element, G-> Ad [k]. element, AE, al, al-AE); if (al = AE) // key activities printf ("Yes"); else printf ("No "); printf ("\ n"); p = p-> next ;}} int main () {struct Adgraph G; Create_AOE (& G); Criticalpath (& G ); return 0;} // Topsort And AOE # include <iostream> # include <stack> # include <queue> # include <cstdio> using namespace std; Struct link {int v; // event no. int count; // activity no. int weight; // activity time link * next ;}; struct node {int indegree; // input char element; // event struct link * firstedge;}; // the header node struct Adgraph {int n, e; struct node Ad [101];}; // void Create_AOE (struct Adgraph * G) {int k, I, j, t; cin> G-> n> G-> e; // node and edge for (k = 1; k <= G-> n; k ++) {cin> G-> Ad [k]. element; G-> Ad [k]. firstedge = NULL; G-> Ad [k]. indegree = 0;} // The Beginning of the header Node Initial for (k = 1; k <= G-> e; k ++) {printf ("input two vertices (event number), Edge Weight (activity time) \ n "); cin> j> I> t; G-> Ad [I]. indegree ++; link * p = new link; p-> v = I; p-> weight = t; p-> next = G-> Ad [j]. firstedge; G-> Ad [j]. firstedge = p; // insert in the header} printf ("AOE network construction completed \ n ----- people are Lili split line ----- \ n print neighbor list: \ n "); for (I = 1; I <= G-> n; I ++) {cout <G-> Ad [I]. element; link * m = G-> Ad [I]. firstedge; while (m! = NULL) {printf ("-> % c, % d", G-> Ad [m-> v]. element, m-> weight); m = m-> next;} printf ("-> ^ \ n ");} // print printf ("\ n") in the adjacent table;} void Criticalpath (Adgraph * G) // G is the list of neighboring tables with weights {queue <int> Q; stack <int> S; int I, j, k, count = 0, ve [101], vl [101], AE, al; // The earliest occurrence time and the latest occurrence time of the time, the earliest occurrence time and the latest occurrence time of the activity // m is used to count and determine whether there is a loop for (I = 1; I <= G-> n; I ++) ve [I] = 0; // The earliest occurrence time of each event is 0 for (I = 1; I <= G-> n; I ++) if (G-> Ad [I]. indegree = 0) Q. push (I); // Add vertices with 0 inbound to the pr Intf ("Topsort:"); while (! Q. empty () {j = Q. front (); Q. pop (); count ++; cout <G-> Ad [j]. element; S. push (j); // Add the topological subscripts in positive order to the stack link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; G-> Ad [k]. indegree --; if (ve [j] + p-> weight> ve [k]) ve [k] = ve [j] + p-> weight; if (G-> Ad [k]. indegree = 0) Q. push (k); p = p-> next ;}// use topsort to find the earliest occurrence time printf ("\ n"); if (count <G-> n) {printf ("loop exists! \ N "); return;} for (I = 1; I <= G-> n; I ++) // for each event v (I) the latest occurrence time of vl [I] set the initial value vl [I] = ve [G-> n]; printf ("Opp_Topsort:"); while (! S. empty () // obtains the vertex {j = S. top (); S. pop (); // output stack cout <G-> Ad [j]. element; link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; if (vl [k]-p-> weight) <vl [j]) vl [j] = vl [k]-p-> weight; // modify vl [j] p = p-> next ;}} printf ("\ nActivity <EnventA-> EnventB> AE AL Share time Is Criticalpath?: \ N "); for (j = 1; j <= G-> n; j ++) // scan the vertex table {link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; AE = ve [j]; al = vl [k]-p-> weight; printf ("<event % c, event % c> \ t % d \ t ", G-> Ad [j]. element, G-> Ad [k]. element, AE, al, al-AE); if (al = AE) // key activities printf ("Yes"); else printf ("No "); printf ("\ n"); p = p-> next ;}} int main () {struct Adgraph G; Create_AOE (& G); Criticalpath (& G ); return 0;} // Topsort And AOE # include <iostream> # include <stack> # include <queue> # include <cstdio> using namespace std; Struct link {int v; // event no. int count; // activity no. int weight; // activity time link * next ;}; struct node {int indegree; // input char element; // event struct link * firstedge;}; // the header node struct Adgraph {int n, e; struct node Ad [101];}; // void Create_AOE (struct Adgraph * G) {int k, I, j, t; cin> G-> n> G-> e; // node and edge for (k = 1; k <= G-> n; k ++) {cin> G-> Ad [k]. element; G-> Ad [k]. firstedge = NULL; G-> Ad [k]. indegree = 0;} // The Beginning of the header Node Initial for (k = 1; k <= G-> e; k ++) {printf ("input two vertices (event number), Edge Weight (activity time) \ n "); cin> j> I> t; G-> Ad [I]. indegree ++; link * p = new link; p-> v = I; p-> weight = t; p-> next = G-> Ad [j]. firstedge; G-> Ad [j]. firstedge = p; // insert in the header} printf ("AOE network construction completed \ n ----- people are Lili split line ----- \ n print neighbor list: \ n "); for (I = 1; I <= G-> n; I ++) {cout <G-> Ad [I]. element; link * m = G-> Ad [I]. firstedge; while (m! = NULL) {printf ("-> % c, % d", G-> Ad [m-> v]. element, m-> weight); m = m-> next;} printf ("-> ^ \ n ");} // print printf ("\ n") in the adjacent table;} void Criticalpath (Adgraph * G) // G is the list of neighboring tables with weights {queue <int> Q; stack <int> S; int I, j, k, count = 0, ve [101], vl [101], AE, al; // The earliest occurrence time and the latest occurrence time of the time, the earliest occurrence time and the latest occurrence time of the activity // m is used to count and determine whether there is a loop for (I = 1; I <= G-> n; I ++) ve [I] = 0; // The earliest occurrence time of each event is 0 for (I = 1; I <= G-> n; I ++) if (G-> Ad [I]. indegree = 0) Q. push (I); // Add vertices with 0 inbound to the pr Intf ("Topsort:"); while (! Q. empty () {j = Q. front (); Q. pop (); count ++; cout <G-> Ad [j]. element; S. push (j); // Add the topological subscripts in positive order to the stack link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; G-> Ad [k]. indegree --; if (ve [j] + p-> weight> ve [k]) ve [k] = ve [j] + p-> weight; if (G-> Ad [k]. indegree = 0) Q. push (k); p = p-> next ;}// use topsort to find the earliest occurrence time printf ("\ n"); if (count <G-> n) {printf ("loop exists! \ N "); return;} for (I = 1; I <= G-> n; I ++) // for each event v (I) the latest occurrence time of vl [I] set the initial value vl [I] = ve [G-> n]; printf ("Opp_Topsort:"); while (! S. empty () // obtains the vertex {j = S. top (); S. pop (); // output stack cout <G-> Ad [j]. element; link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; if (vl [k]-p-> weight) <vl [j]) vl [j] = vl [k]-p-> weight; // modify vl [j] p = p-> next ;}} printf ("\ nActivity <EnventA-> EnventB> AE AL Share time Is Criticalpath?: \ N "); for (j = 1; j <= G-> n; j ++) // scan the vertex table {link * p = G-> Ad [j]. firstedge; while (p! = NULL) {k = p-> v; AE = ve [j]; al = vl [k]-p-> weight; printf ("<event % c, event % c> \ t % d \ t ", G-> Ad [j]. element, G-> Ad [k]. element, AE, al, al-AE); if (al = AE) // key activities printf ("Yes"); else printf ("No "); printf ("\ n"); p = p-> next ;}} int main () {struct Adgraph G; Create_AOE (& G); Criticalpath (& G ); return 0 ;}