Topological sorting
Topological ordering is a sort of vertex of a direction-free graph, so that if there is a path from VI to VJ, the VJ must appear behind VI in the order.
So it's impossible to finish the sort without a circle in the fake.
The first of these methods
An easy way to do this is to find any vertex without an edge in the sorting algorithm, then display the vertex and delete it from the graph along with its edge. And so on to the last.
In degrees (indegree): The entry of Vertex v is, all variables pointing to vertex v (U, v).
Out degree (outdegree): The degree of vertex v is the number of edges emitted by vertex v (V, u).
Write down the pseudo-code for this method, because the time complexity O (| V|^2) So efficiency is not really good, the second method gives the real code.
void Topsort ( Graph G ){ int Counter; Vertex V, W; for0; Counter < NumVertex; Counter++ ) { V = FindNewVertexOfIndegreeZero(); if ( V == NotVertex ) { Error("Graph has a cycle"); break; } TopNum[V] = Counter; foreachto V Indegree[W]--; }}
Source of this pseudo-code: "Data Structures and algorithm analysis in C"
The second method of
This approach is also very well understood, that is, in the above method we found that, in fact, each time we put all the points into the queue for the first time, each time the result of a new entry of 0 points, it is due to the current traversal of the point, so we record each point in the degree of update, When 0 is added to the queue, it is possible.
////Main.cpp//Topologysort////Created by Alps on 15/3/3.//Copyright (c) 2015 Chen. All rights reserved.//#include <iostream>#include <queue>using namespace STD;structnode{intValintLength Node* Next; Node (): Val (0), Length (0), Next (NULL) {}};typedefnode* Graph;int*degree; Graph Createg (graph G) {intNumscanf("%d", &num);//Input The number of the vertexG = (Graph)malloc(sizeof(structNode) * (num+1));//malloc Memory for graphg[0].length = num;//save The graph vertex numberdegree = (int*)malloc((num+1) *sizeof(int));memset(Degree,0, num*sizeof(int)); for(inti =1; I <= num; i++) {g[i].val = i; G[i].next = NULL;intOutdegree =0;scanf("%d", &outdegree); for(intj =0; J < Outdegree; J + +) {node* temp = (node*)malloc(sizeof(structNode));scanf("%d%d",& (Temp->val), & (Temp->length)); Temp->next = G[i].next; G[i].next = temp; Degree[temp->val] + =1; } }returnG;}voidPRINTG (Graph G) {//int length = sizeof (G)/sizeof (struct Node); intLength = g[0].length; Node * TEMP; for(inti =1; I <= length; i++) {temp = &G[i];printf("Node:%d", Temp->val); while(Temp->next) {printf("-%d (%d)", Temp->next->val, temp->next->length); temp = temp->next; }printf("\ n"); }}int* Topologysort (Graph G) { queue<Node>Q;intCounter =0;intLength = g[0].length;int*topnum = (int*)malloc((length+1) *sizeof(int)); for(inti =1; I <= length; i++) {if(Degree[i] = =0) {Q.push (g[i]); } } while(! Q.empty ()) {Node V = Q.front (); Topnum[v.val] = ++counter; Node * temp = &V; while(Temp->next) {temp = temp->next; Degree[temp->val]-=1;if(Degree[temp->val] = =0) {Q.push (g[temp->val]); }} q.pop (); }if(Length! = counter) {returnNULL; }returnTopnum;}intMainintargcConst Char* argv[]) {//Insert code here ...Graph G; g = Createg (g); PRINTG (G);int*topnum = Topologysort (G); for(inti =1; Topnum! = NULL && i <= g[0].length; i++) {printf("%d", Topnum[i]); }//Std::cout << "Hello, world!\n"; return 0;}
Algorithm learning-topological ordering of graphs