What is topological sequence in topological sorting?
Generally, such a linear sequence is called a sequence that satisfies the Topological Order. In short, a full order is obtained from a partial order on a set. This operation is called topological sorting. Definitions of partial and full order in discrete mathematics:
If the relationship on set X is R, and R is self-inverse, inverse, and transmitted, R is called the partial order relationship on set X.
If R is set to Partial Order on X, if for each x, y belongs to X, there must be xRy or yRx, then R is the full Order relation on X.
A relatively simple understanding: Partial Order means that only some Members in the set can be compared, and full order means that all members in the set can be compared.
Note:
① If the vertex in the graph is arranged in a line in the topological order, all the directed edges in the graph are directed from left to right.
② If a directed ring exists in the graph, the vertex cannot meet the topological order.
③ The topological sequence of a DAG usually indicates that a scheme is feasible.
I spent a day studying the topological sorting yesterday and thought I had learned a lot. So let's make a summary.
<1> the representation of a directed graph can be divided into an adjacent matrix and an adjacent table.
An adjacent matrix is an array of n x n with the subscript I. The position of j records the relationship between vertex I and vertex j. (Applicable to sparse matrices with high requirements on memory space)
An adjacent table is an array that records links. For example, if I and j are related, j is placed at the position of [I] [k] (k is the number of points currently associated with I ), this notation applies when there are few relationships, but after referencing the vector in STL, it seems that the size of the data volume has no significant impact on it.
<2> topological sorting: finds the point with an inbound degree of 0 and clears the outbound degree (DFS or BFS can be used), which is different from the data representation method, operations are also different.
<3> after a directed graph is given, there will be topological sorting and no topological sorting. If there is a directed ring in the graph, there is no topological sorting (that is, directly and indirectly pointing to each other during the traversal process). In terms of processing, you only need to record the access status of each vertex, whether it is being accessed (not satisfied), not accessed, or accessed (DFS ).
If there is a topological sorting, there will also be a problem with the unique Topology Sorting (if you do not need to consider this, use the DFS to do some), this is actually better to deal, only one entry point (BFS) is allowed ).
DFS + adjacent table
DFS + adjacent matrix. Int vis [MAXN]; int topo [MAXN], cnt; bool DFS (int u) {vis [u] =-1; // indicates the current access, if the access request is accessed again, the topo sorting does not exist. For (int I = 0; I <n; I ++) {if (G [u] [I]) {if (vis [I] <0) return false; // If a directed ring exists, it fails to exit. Else if (! Vis [I] &! DFS (I) return false;} vis [u] = 1; topo [-- cnt] = u; return true; // success record, return true.} Bool toposort () {cnt = n; memset (vis, 0, sizeof (vis); // If You Want To determine whether the sorting is unique, each time, we need to traverse all vertices, and each time we can only have a bit of 0. For (int u = 0; u <n; u ++) if (! DFS (u) return false; return true;} // DFS + adjacent matrix. Int vis [MAXN]; int topo [MAXN], cnt; bool DFS (int u) {vis [u] =-1; // indicates the current access, if the access request is accessed again, the topo sorting does not exist. For (int I = 0; I <n; I ++) {if (G [u] [I]) {if (vis [I] <0) return false; // If a directed ring exists, it fails to exit. Else if (! Vis [I] &! DFS (I) return false;} vis [u] = 1; topo [-- cnt] = u; return true; // success record, return true .} Bool toposort () {cnt = n; memset (vis, 0, sizeof (vis); // If You Want To determine whether the sorting is unique, each time, we need to traverse all vertices, and each time we can only have a bit of 0. For (int u = 0; u <n; u ++) if (! DFS (u) return false; return true;} BFS (with STL-queue) + adjacent table (with STL-vector)
// BFS + adjacent table. Vector <int> G [MAXN]; // list of adjacent tables. Int son [MAXN]; // input level. Void topo () {queue <int> que; int OK = 0; for (int I = 0; I <n; I ++) if (! Son [I]) que. push (I); // enter the team when the input degree is 0. While (! Que. empty () {if (que. size ()> 1) OK = 1; // when the number of queues exceeds 1, it indicates that there is a non-unique solution. Int t = que. front (); que. pop (); cnt --; // If the queue is empty and the counter is greater than 0, the ring structure exists. For (int I = 0; I <G [t]. size (); I ++) if (-- son [G [t] [I] = 0) // after determining the relationship between the current vertex, whether the entry degree of a vertex is 0. Que. push (G [t] [I]);}