On the basis of FIG - FIG adjacency lists stored topological sorting +

FIG adjacency lists exist, in fact, there is simulated array of

Also known chain store law, would have been to use the list to achieve, but in most cases just use an array of analog to

Example:

u (side of the starting point) V (end side) w (weight side)
4 2 1
1 2 3
1 4 1
1 5 2
4 3 4
2 3 1

 

 

 

Man of few words said, directly on the code

for ( int i = . 1 ; i <= m; i ++ ) 
    { 
        Scanf ( " % D% D% D " , & U1, & V1, & W1); 
        E [i] .u = U1; // assigns the i-th starting edge         
        E [i] .v = V1; // assigns the i-th edge end 
        E [i] .W = W1; // assigns the right side of the i-th value of 
        e [i] .next = head [U1]; // now this edge on one side of an upper edge = now the starting point of this edge (u1 is the starting point) 
   head [U1] = I; // Accordingly, for the future side, now that edges on one side is the starting point}

NOTE: e [i] is a structural body , responsible for recording each edge information

struct the Node {
     int U; // start point side 
        int V; // end side 
        int W; // edge weights 
    int Next; // on a side edge (for connection) 
}. The maximum e [sides number];    

Overall, this is a way to save graph, graph theory is the basis of

 

Topological sorting

 Topological sort is determined there is a sequence of vertices directed acyclic graph (Directed Acyclic Graph abbreviated DAG), so as to satisfy: For any edge (u, v) εE, u overall position in the sequence prior to v. 

    Topological sorting method is as follows:
  (1) selectively from the drawing without a precursor (i.e., the 0 degree) and outputs it. // vertex penetration: the edge at this point how many
  (2) deleting the vertex from the graph, and emitted from the vertex deleting all directed edges , and the updating of the point.
  (3) repeating the above two steps until the remaining figures is no longer present before the apex of no trend.
 
 Usage: 1, and direct adjacency matrix method. (Large space overhead, can not be determined with a ring)
                   2, adjacent to the stack list + (a small space, based ring)
 So, generally use the stack to achieve
BOOL topsort () // topological sort, topological program returns true, otherwise returns FALSE 
{
     int P, Q;
     for ( int I = . 1 ; I <= n-; I ++) // to find a node of degree 0 of the stack 
    {
         IF s.push (i) (D [i]!); // D [i] denotes the i-degree point, the initialization edged D [i] 
    }
     the while (! s.empty ()) / / when the stack is nonempty operating 
    { 
        P = s.top (); // record stack node 
        s.pop (); // pop stack node 
        ANS [CNT ++] = P; // will pop up node stores the result array , and counted 
        for ( int I = head [P]; I = -! . 1; I = E [I] .next) // clear out of the node 
        { 
            Q = E [I] .v;
             IF -) s.push (Q) ((D [Q]!); // if also found that the degree of node 0, continue to push 
        } 
    } 
    iF (CNT <= n-) return  to false ; // if there is no node on the stack, then there is a ring 
    return  to true ; 
}

 

Also in the required minimum and maximum topological order , the deposit is necessary to use the priority queue

Code is subject almost, but with different priority queues defined stack Bale

Guess you like

Origin www.cnblogs.com/jhl0824/p/11618429.html