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.
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