Introduction to the algorithm classic sixth chapter 6-15 to the task sort

Source: Internet
Author: User

Suppose there are n variables, and a M two-tuple (U,V), which indicates that the variable u is less than V, respectively. So what should all the variables look like from small to large? For example, there are 4 variables a,b,c,d, and if A<b,c<b,d<c is known, the ordering of these 4 variables may be a<d<c<b. Although there are other possibilities (like d<a<c<b), you just need to find one.

Sample Input

5 4

1 2

2 3

1 3

1 5

0 0

Sample Output

1 4 2) 5 3

Test instructions: Suppose there are n variables, and a M two-tuple (U,V), which indicates that the variable u is less than V, respectively. So what should all the variables look like from small to large?

Each variable is considered as a point, and the "less than" relation is considered as a forward edge, then a direction graph is obtained. In this way, our task is actually to sort all the nodes of a graph so that each of the u,v corresponding U is in front of v.

Complete the topology sort with DFS, after accessing a node, it adds to the header of the current topology sequence.

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#defineMax 100+5intC[max],topo[max],t,n,g[max][max];//as g[1,2] from 1 to 2 of the directed segmentintDfsintu) {intv; c[u]= -1;//access flag, 1 for being accessed, 0 is not visited, 1 is already visited for(v =1; V <= N; v++)//look at the comparison with you v there is no re-access toif(G[u][v]) {if(C[v] <0)return 0;//If you access a node that is marked as 1 in C, it indicates that there is a forward loop.Else if(!c[v] &&!dfs (v))return 0;//V has not been visited with DFS recursion if return 0 indicates failure} C[u]=1; topo[--T] =u;return 1; } intToposort () {inti; t=N; memset (c,0,sizeof(c));  for(i =1; I <= N; i++) if(!c[i] &&!dfs (i))return 0; return 1; } intMain () {intm,i; while(SCANF ("%d%d", &n,&m), n | |m) {intu,v; memset (G,0,sizeof(G));  for(i =0; I < m; i++) {scanf ("%d%d",&u,&v); G[U][V]=1; } if(Toposort ()) { for(i =0; I < n1; i++) printf ("%d", Topo[i]); printf ("%d\n", Topo[i]); } } return 0; }

Analysis

The n variables in the title are considered as n nodes in the graph, the side of the graph is a two-yuan relationship, the M two-tuple represents the two-ary relationship represented by the M-Bar, (u,v) means V is greater than u, and a forward edge from U to V, if this m is two yuan
Group does not contradict, then this figure is a DAG otherwise there is a forward ring, the solution does not exist, for example a<b,b<c, can be introduced a<c, but now appear c connected to a meaning is a>c, with the previous recursive phase
Paradox, so if there is a forward ring in the diagram, there is no topological order, whereas a DAG diagram has topological order, and the topological order can be obtained by DFS. Start with a C array marker, 1 indicates that the node is
In the access, then if again access to a node in C marked as-1 indicates that there is a forward ring, 0 indicates that the node has not been visited, and that its descendants have not been visited without knowing, because it is possible from its descendants
Beginning dfs,1 indicates that this node and its descendants have been visited, each time Dfs accesses a node and adds it to the current topology header, since it can only be guaranteed to be back, we do not know the start of Dfs
The node has a parent node, so if there are nodes that are definitely smaller than the nodes that are currently accessed, they must be placed in front of them.

Expand

Directed acyclic graphs (Directed acyclic graph, DAG) are one of the directed graphs, the literal meaning of which is the absence of a ring in the graph. is often used to represent the driver dependencies between events and to manage the scheduling between tasks. Topological ordering is the sorting of the vertices of the DAG so that you (in the sort record) have a U (in the sorted records) that appears first for each directed Edge (U, v). It is also understood that V can only occur if all the source points of V are present at a point v.

Gives the topological ordering of a forward-free graph:

Dfs

In Dfs, the vertices that are traversed are printed sequentially, and the vertices must appear first than their adjacency points when the topology is sorted. In, vertices 5 0 appear first than vertices, and vertices 4 1 appear first than vertices.

When the DFS implements topology ordering, the stack is used to save the sequence of vertices of the topological sort, and all its adjacency points are guaranteed to stack before a vertex is put into the stack.

Introduction to the algorithm classic sixth chapter 6-15 to the task sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.