"Data structure" topological sorting algorithm

Source: Internet
Author: User

The basic idea of topological ordering for AOV Networks is:

Select a vertex output of 0 in the AoV net, then delete the vertex and remove the arc with this vertex as the tail, continue repeating this step until all vertices in the output or AOV are not present in the 0-degree vertex.

AOV Network and adjacency table data structure:

Code:

#include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h" #define OK 1#define ERROR 0# Define TRUE 1#define FALSE 0#define MAXEDGE 20#define maxvex 14#define INFINITY 65535typedef int status;/* Status is the type of the function, its value is the function result status code, such as OK et *//* adjacency matrix structure */typedef struct{int vexs[maxvex];int arc[maxvex][maxvex];int numvertexes, Numedges;}    mgraph;/* adjacency table structure ****************** */typedef struct edgenode/* Edge table node */{int Adjvex; /* adjacency point field, store this vertex corresponding subscript */int weight;/* for storing weights, for non-network diagram can not need */struct edgenode *next; /* Link field, point to next adjacency point */}edgenode;typedef struct vertexnode/* Vertex table node */{int in;/* vertex entry */int data;/* Vertex field, storage vertex information */edgenode *firs tedge;/* side Table head pointer */}vertexnode, adjlist[maxvex];typedef struct{adjlist adjlist;int numvertexes,numedges;/* The current number of vertices and the number of edges in the graph */ }graphadjlist,*graphadjlist;/* **************************** */void createmgraph (MGraph *G)/* component Diagram */{int I, j;/* printf ( "Please enter the number of sides and vertices:"); */g->numedges=maxedge; G->numvertexes=maxvex;for (i = 0; i < g->numvertexes; i++)/* Initialization diagram */{g->vexS[i]=i;} for (i = 0; i < g->numvertexes; i++)/* Initialization diagram */{for (j = 0; J < g->numvertexes; J + +) {g->arc[i][j]=0;}} g->arc[0][4]=1; g->arc[0][5]=1; g->arc[0][11]=1; g->arc[1][2]=1; g->arc[1][4]=1; g->arc[1][8]=1; g->arc[2][5]=1; g->arc[2][6]=1; g->arc[2][9]=1; g->arc[3][2]=1; g->arc[3][13]=1; g->arc[4][7]=1; g->arc[5][8]=1; g->arc[5][12]=1; g->arc[6][5]=1; g->arc[8][7]=1; g->arc[9][10]=1; g->arc[9][11]=1; g->arc[10][13]=1; G->arc[12][9]=1;} /* Use adjacency matrix to construct adjacency table */void createalgraph (mgraph g,graphadjlist *gl) {int i,j; Edgenode *E;*GL = (graphadjlist) malloc (sizeof (graphadjlist));(*gl)->numvertexes=g.numvertexes; (*GL) Numedges=g.numedges;for (i= 0;i <g.numvertexes;i++)/* Read in vertex information, create vertex table */{(*GL)->adjlist[i].in=0; (*GL), Adjlist[i].data=g.vexs[i];(*gl)->adjlist[i].firstedge=null; /* Set the edge table to empty table */}for (i=0;i<g.numvertexes;i++)/* Establish the Edge table */{for (j=0;j<g.numvertexes;j++) {if (g.arc[i][j]==1) {e= ( Edgenode *) malloc (sizeof (Edgenode)); E->adThe jvex=j;/* adjacency sequence number is J */e->next= (*GL)->adjlist[i].firstedge;/* assigns the node pointer on the current vertex to E */(*GL)->adjlist[i]. firstedge=e;/* the current vertex pointer to E */(*GL)->adjlist[j].in++;}}} /* Topological sort, if GL no loop, then output topological sort sequence and return 1, if the loop returns 0.  */status Topologicalsort (graphadjlist GL) {edgenode *e;int i,k,gettop;int top=0; /* For stack pointer subscript */int count=0;/* is used to count the number of output vertices */int *stack;/* stack the vertices into the stack */stack= (int *) malloc (gl->numvertexes * sizeof ( int)), for (i = 0; i<gl->numvertexes; i++) if (0 = = gl->adjlist[i].in)/* Enter the vertex of the 0 into the stack */stack[++top]=i;while (top!=0        {gettop=stack[top--];p rintf ("%d", gl->adjlist[gettop].data); count++; /* Output I vertex, and count */for (e = gl->adjlist[gettop].firstedge; e; e = e->next) {k=e->adjvex;if (!) ( --gl->adjlist[k].in)/* minus the adjacency point of the I vertex by 1, and if it is 0 after minus 1, then stack */stack[++top]=k;}} printf ("\ n"); if (Count < gl->numvertexes) return Error;elsereturn OK;} int main (void) {mgraph G; Graphadjlist Gl;int result; Createmgraph (&AMP;G); Createalgraph (G,&AMP;GL), Result=topologicalsort (GL);p rintf ("Result:%d ", result); return 0;} 

Results:

The time complexity of the whole algorithm is O (n+e) (n vertex e arc).

"Data structure" topological sorting algorithm

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.