Topological sequencing (for a directed acyclic graph Dag) is an application of depth-first search, with a linear arrangement of all vertices in the resulting graph.
The pseudo code is as follows:
EG:
The complete code for topological ordering is as follows:
#include <iostream> #include <iomanip> #include <string> #include <algorithm>using namespace STD; #define UDG 0#define DG 1#define White 0 #define GRAY 1 #define BLACK 2 #define NONE 0 #define TREE 1 #define Back 2 #define FORWARD 3 #define CROSS 4 typedef string VTYPE;TYPEDEF struct Gedge{vtype Adjvertex; The adjacency vertex pointed by this edge.int weight; The weight of this edgeint type; The type of Edgegedge *nextedge; Point to the next edge}gedge;typedef struct Gvertex{vtype key; The key of the Vertexint Color;int d,f; The discovered time and the finished Timevtype Pai; The parent node ' s key after Searchinggedge *firstedge; Point to the first edge attached to the vertex;} gvertex;typedef struct algraph{int vnum;int enum;int kind; The kind of Graph Gvertex *headvertex;} algraph;typedef struct edge{vtype start;vtype end;} Edge;int Locate (algraph &g,vtype s) {//locate the start vertex oF One edge in head vertex of the graphfor (int i=0;i<g.vnum;i++) if (G.headvertex[i].key = = s) return i;return-1;} void Linkedgetograph (Algraph &g, Edge e) {Gedge *arc=new gedge (); Arc->adjvertex=e.end;int headv_i=locate (G, E.start);arc->nextedge=g.headvertex[headv_i].firstedge; G.headvertex[headv_i].firstedge = arc;} void Graph_create (Algraph &g, VType v[], Edge e[]) {//init The head vertexg.headvertex= new gvertex[g.vnum];for (int i=0 ; i<g.vnum;i++) {g.headvertex[i].key=v[i]; G.headvertex[i].firstedge=null;} Add edge to head vertex on order to create a graphif (G.kind = = DG)//undirected graphfor (int i=0; i<g.enum; i++) LinkE Dgetograph (G,e[i]); if (G.kind = = UDG)//directed graphfor (int i=0; i<g.enum; i++) {linkedgetograph (g,e[i]);//Link Again after Reversededge Temp;temp.start = E[i].end;temp.end = E[i].start; Linkedgetograph (g,temp); }}void algraph_print (Algraph G) {for (int i=0; i<g.vnum; i++) {Cout<<g.headvertex[i].key;gedge *p = G.HeadVertex [I].firstedge;whiLe (P! = NULL) {cout<< "--" << p->adjvertex;p = p->nextedge; } cout<<endl; }}/*void edgetype_print (Algraph G) {for (int i=0; i<g.vnum; i++) {Gedge *p = G.headvertex[i].firstedge;while (p) {cout <<G.HeadVertex[i].key<< "-" <<p->adjVertex<< ":"; switch (p->type) {case Tree:cout << "Tree Edge" <<endl;break;case back:cout<< "back Edge" <<endl;break;case forward:cout< < "Forward Edge" <<endl;break;case cross:cout<< "Cross Edge" <<endl;break; }p = p->nextedge; }}}*//*--------------------DFS alogithms-----------------------*/int time0;int r_i=0;void graph_dfsvisit (algraph &g, Gvertex *u, VType *r) {TIME0 = TIME0 +1; White vertex u have just been discoveredu->d = TIME0; u->color = Gray;gedge *p = U->firstedge;while (p) {VType V = P->adjvertex;int h_i=locate (g,v); Gvertex *HV = &g.headvertex[h_i];//classify The Edge and recursive searchingif (Hv->color = = white) {Hv->pai = u->key; Graph_dfsvisit (g,hv,r);p->type = TREE; Tree Edge}else if (hv->color = = GRAY) {p->type = back; Back Edge}else if (Hv->color = = BLACK) {if (U->d < hv->d) P->type = FORWARD;//forward edgeelsep->type = C ROSS; Cross edge}p = P->nextedge;} U->color = BLACK; Backen U;it is finishedr[r_i++]=u->key; Store the DFS result into array RTIME0 = TIME0 +1;u->f = TIME0;} void Algraph_dfs (Algraph &g, VType *result) {//init all the Vertexgvertex *u;for (int i=0; i<g.vnum; i++) {u = &G. Headvertex[i];u->color = White;u->pai = ' 0 '; } TIME0 = 0; Time Stamp//explore every vertexfor (int i=0; i<g.vnum; i++) {u = &g.headvertex[i];if (U->color = = white) Graph_ Dfsvisit (G,u,result);} }/*------------------------------------------------------*//*-----------------topological Sort--------------------*/bool Compare (const Gvertex &a,const Gvertex &b) {return a.f > B.f; Descending order}void Algraph_tsort(Algraph &g, VType *result) {Algraph_dfs (G,result);//sorting the finished time in descending ordersort (g.headvertex,g.headvertex+g.vnum,compare) ; Call the system ' s sorting function://store the result int r_i=0;for (int i=0; i<g.vnum;i++) result[r_i++] = G.headverte X[i].key;} /*---------------------------------------------------*/int Main () {VType v[]={"shirt", "Tie", "jacket", "Belt", " Watch "," undershorts "," Pants "," shoes "," socks "};edge e[]={{" shirt "," Belt "},{" shirt "," Tie "},{" tie "," Jacket "},{" Belt "," Jacket "},{" pants "," Belt "},{" Pants "," Shoes "},{" undershorts "," Shoes "},{" undershorts "," Pants "},{" socks "," Shoes "}}; Algraph G; G.vnum=sizeof (V)/sizeof (VType); G.enum=sizeof (E)/sizeof (EDGE); G.KIND=DG; The kind of graphgraph_create (g,v,e);cout<< "-----------------Create Graph-----------------" <<endl; cout<< "The Created graph is:" <<endl; Algraph_print (G);cout<< "--------------toplogical sort-----------------" <<endl;vtype *tsortresult = new Vtype[g.vnum]; ALGraph_tsort (G,tsortresult);cout<< "The result of topological sort is:" <<endl;for (int i=0; i<g.vnum; i++) COUT<<SETW (one) <<TSortResult[i]<< ":" <<SETW (3) <<G.HEADVERTEX[I].D<<SETW ( 3) <<G.HeadVertex[i].f<<endl;cout<< "----------------------------------------------" << Endl;return 0;} Run Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to Algorithms Chapter 22nd: Topological sequencing