A complex engineering usually can be broken down into a set of small tasks, completing these small tasks means completion of the entire project. For example, the automotive assembly project can be broken down to the following tasks: put the chassis to the wiring, shaft, put the seat on the chassis, paint, brake, door, etc. The task has a relay relationship, such as before the shaft must be put on the floor. The order of the task can be used to represented the map - called the Activity ON Vertex, AOV) network. The vertex of the map represents the task, and the side (i, j) represents a successful relationship: Task J begins before the task I must be completed. Figure 1 - 4 shows the projects of six tasks, while (1, 4) indicates that the task 1 is completed before the task 4 starts, the same edge (4, 6) means task 4 is completed before the task 6 begins, side (1, 4) ) With (4, 6), you know that task 1 is completed before the task 6 begins, ie the front-rear relationship is passed. It can be seen that the side (1, 4) is excess because the edges (1, 3) and (3, 4) have suggested this relationship.
Under many conditions, the implementation of the task is continuous, such as car assembly issues or usually purchased consumer goods (bicycles, children's swings, mower, etc.). We can assemble according to the suggested order. In the tandem, side (i, j) representing the task I in the assembly sequence, in front of the task J, the sequence having this property is referred to as a topology sequence (Topological Orders or Topological Sequences). The process of establishing a topological sequence based on the task is called topology sort. Figure 1 - 4 The task has a variety of topology sequences, three of which are 1 2 3 4 5 6, 1 3 2 4 5 6 and 2 1 5 3 4 6, sequence 1 4 2 3 5 6 is not Topological sequences, because in this sequence, the task 4 is in front of 3, and the task has the edges (3, 4) in the figure, such sequence contradicts the sequence indicated by the edges (3, 4) and other sides. The topology sequence can be established with a greedy algorithm. The algorithm constructs a topology sequence from the left to right, and each step is added to the sequence of ranks. Use the following greedy criteria to select the vertex: From the remaining vertices, select the vertex W, so that W does not exist in such input (V, W), where the vertex V is not in the sequence structure that has been rated. Note that if the vertex W is added to this criterion (ie, there are edges (V, W) in the figure, and V is not in the constructed sequence), the topology is not completed, because the vertex V must follow after the vertex W. The pseudo code of the greedy algorithm is shown in Figure 1 3 - 5. Every iteration of the While loop represents a step of greedy algorithm.
Now use a greedy algorithm to solve the drawing of Figure 1 - 4. First start from an empty sequence V, the first step in the first step is selected. At this time, there are two candidate vertices 1 and 2 in the figure, if the vertex 2 is selected, the sequence V = 2, the first step is completed. The second step selects V's second vertex, which can know the candidate vertex is 1 and 5 according to the greedde standard. If 5, V = 2 5 is selected. Next, the vertex 1 is the only candidate, so V = 2 5 1. In the fourth step, the vertex 3 is the only candidate, so add the vertex 3 to V
V = 2 5 1 3 is obtained. Add vertices 4 and 6 in the last two steps, V = 2 5 1 3 4 6.1. The correctness of the greedy algorithm
In order to ensure the correctness of the greedy algorithm, it is necessary to prove: 1) When the algorithm fails, there is no topology sequence; 2)
The algorithm does not fail, V is the topology sequence. 2) That is to select the direct result of the next vertex, 1) of the provenation of the next vertex, which demonstrates that there is a loop in the map if the algorithm fails. If there is a loop QJ 1.QK QJ in the figure, it has no topology sequence, because the sequence suggests that QJ must be completed before the QJ start.
Theorem 1-2 If the algorithm of Figure 1 3 - 5 fails, the map contains a loop.
Certificate noted that when it fails | V |
2. Selection of data structure
To implement Figure 1 - 5, the description method of the sequence V is considered, and how to find a candidate vertex that can be added to V. An efficient implementation method is to describe the sequence V with a one-dimensional array V, and save the candidate vertices that can be added to V. Another one-dimensional array I n d egree, I n d egree [j] represents the number of node i connected to the vertex j, where the vertex i is not a member of the V, and the edges between the maps are indicated as ( I, j). e e [j] represents a j when the node becomes a candidate I n D e g r becomes 0 when. The sequence V is initially empty. I N D E G R E E E E [J] is the degree of inner j. When an apex is added to the V, all the vertices adjacent to the newly added vertices J, which i n d E G R E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E For the presentation of Fig. 1 - 4, I N d E G R e e [1: 6] = [0, 0, 1, 3, 1, 3]. Since the I n d E G R e value of the vertices 1 and 2 is 0, they are candidate vertices that can be added to V, thereby, the vertices 1 and 2 first in the stack. Each step, takes a vertex from the stack to add it to V while subtracting the I n d E g r e value of the vertex adjacent to its adjacent. If the vertex 2 is removed from the stack at the first step and add it V, it is obtained by V [0] = 2, and I n d egree [1: 6] = [0, 0, 1, 2, 0, 3]. Since I N d E G R e E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E - enable the vertex 5 into the stack.
Program 1 3 - 2 gives the corresponding C code, which is defined as a member function of N e t w o r k. Moreover, it applies to the unbalanced directional maps. However, if used for non-map (no matter whether it is unwilling), the error will be obtained, because the topology sort is defined for the figure. To solve this problem, use the same template to define member functions AdjacencyGraph, AdjacencyWGRAPH, L i n k E d g R a p H and L i n k e d w g r a p h. These functions can override the functions in N e t w o R K and output an error message. If the topology sequence is found, the topological function returns T R u e; if the input-to-picture has no topological sequence, F a L s E is returned. When the topology sequence is found, it returns it to V [0: N- 1].
3. Network: Topological complexity
The time overhead of the first and third F O R cycles (N). If used (consuming) adjacent matrix, the time used in the second FOR cycle is (N2); if the adjacent link list is used, the time is (N E). In two nesting While cycles, the outer loop needs to perform N times, each time the vertex W is added to V, and initialize the inner WHILE loop. When using an adjacency matrix, the inner WHILE loop requires time (N) for each vertex W; (N E). Therefore, if the adjacent matrix is utilized, the time complexity of the program 1 3 - 2 is (N2), and if the adjacent link is used (N E). Procedure 13-2 topology
Bool Network :: Topological (int V [])
{// calculate topology of the vertices in the figure
/ / If you find a topology order, return T R u e, at this time, record the topology order in V [0: N - 1]
// If there is no topology order, return F a L S e
INT N = VE R T I c e s ();
// Calculation degree
INT * indegree = new int [n 1];
InitializePos (); // Drawing Care array
For (int i = 1; i <= n; i ) // initialization
Indegree [i] = 0;
For (i = 1; i <= n; i ) {// Side starting from I
INT u = begin (i);
While (u) {
I N D e g r e e [u] ;
U = NextVE R t e x (i);}
}
/ / Press the vertex of 0 into the stack
LinkedStack
S;
For (i = 1; i <= n; i )
IF (! indegree [i]) S.Add (i);
// Produce topology order
i = 0; // array V cursor
While (! sisempty ()) {// Select from the stack
INT W; // Next vertex
S. D e l e t e (w);
v [i ] = W;
INT u = begin (w);
While (u) {// Modify degree
I n D e g r e e e [u] -
IF (! Indegree [u]) S.Add (u);
u = nextVe r t e x (w);}
}
DE A C T I v A T E P O S ();
delete [] indegree;
Return (i == n);
}
Reprinted from Shada Studio