09 Topological sequencing _topologicalsort

Source: Internet
Author: User

Tag: INF typedef get [1] topology NULL table structure returns return

#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 65535

typedef int STATUS;/* Status is the type of function whose value is the function result status code, such as OK, etc. */

/* 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, which stores the subscript for that vertex */
The int weight;/* is used to store weights, and it is not required for non-network diagrams.
struct Edgenode *next; /* Link field, point to next adjacency point */
}edgenode;

typedef struct VERTEXNODE/* Vertex table node */
{
int in;/* vertex-in degree */
int data; /* Vertex fields, storing vertex information */
Edgenode *firstedge;/* Side Table head pointer */
}vertexnode, Adjlist[maxvex];

typedef struct
{
Adjlist adjlist;
int numvertexes,numedges; /* Current number of vertices and 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;

}

/* Build adjacency table using adjacency Matrix */
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 a vertex table */
{
(*GL)->adjlist[i].in=0;
(*GL)->adjlist[i].data=g.vexs[i];
(*GL)->adjlist[i].firstedge=null; /* Set the edge table to an empty table */
}

for (i=0;i<g.numvertexes;i++)/* Create edge Table */
{
for (j=0;j<g.numvertexes;j++)
{
if (g.arc[i][j]==1)
{
E= (Edgenode *) malloc (sizeof (Edgenode));
e->adjvex=j;/* adjacency Sequence Number is J */
e->next= (*GL)->adjlist[i].firstedge;/* assigns the point pointer on the current vertex to E */
(*GL)->adjlist[i].firstedge=e;/* points 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 with a degree of 0 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--];
printf ("%d", gl->adjlist[gettop].data);
count++; /* Output I number 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, if it is 0 after minus 1, then enter the stack */
Stack[++top]=k;
}
}
printf ("\ n");
if (Count < gl->numvertexes)
return ERROR;
Else
return OK;
}


int main (void)
{
Mgraph G;
Graphadjlist GL;
int result;
Createmgraph (&G);
Createalgraph (G,&GL);
Result=topologicalsort (GL);
printf ("result:%d", result);

return 0;
}

09 Topological sequencing _topologicalsort

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.