Topological sorting algorithm

Source: Internet
Author: User
Tags in degrees

Tag: Type does not record TED RET cal BSP for error new

#include <stdio.h>
#include <stdlib.h>
#define MAXVEX 100//maximum top points
typedef char Vertextype; Vertex
typedef int EDGETYPE; Weighted value
#define UNVISITED-1//Tag not accessed
#define VISITED 1//Mark not visited
#define OK 1
#define ERROR 0
typedef int STATUS;

typedef struct EDGENODE
{
int Adjvex; The subscript corresponding to the vertex
Edgetype weight; Weight
struct Edgenode * NEXT;
}edgenode;

typedef struct//Vertex structure
{
int in; Recording in degrees
Vertextype data;
Edgenode * Firstedge;
}vertexnode,adjlist[maxvex];

typedef struct
{
Adjlist adjlist;
int numvertexes;
int numedges;
int Mark[maxvex]; Whether the tag has been accessed
}graphadjlist; Graph structure


Initializing adjacency tables
void Initgraphadjlist (Graphadjlist * g,int numver,int numed)//incoming vertices and number of edges
{
g->numvertexes=numver;
g->numedges=numed;
for (int i=0;i<g->numvertexes;i++)
{
g->mark[i]=unvisited;
g->adjlist[i].in=0;
g->adjlist[i].firstedge=null; Empty table with Edge table
}
}

Create an edge (a forward graph)
void Creat_edge (Graphadjlist * g,int from,int to,int weight)
{
Edgenode * temp= g->adjlist[from].firstedge;
if (temp==null)
{
Edgenode * newedgenode= (Edgenode *) malloc (sizeof (Edgenode));
newedgenode->adjvex=to;
newedgenode->weight=weight;
newedgenode->next=null;
g->adjlist[from].firstedge=newedgenode;
g->adjlist[to].in++;
}
Else
{
while (Temp->next!=null)
{
temp=temp->next;
}
Edgenode * newedgenode= (Edgenode *) malloc (sizeof (Edgenode));
newedgenode->adjvex=to;
newedgenode->weight=weight;
newedgenode->next=null;
temp->next=newedgenode;
g->adjlist[to].in++;

}
}


/* Create adjacency table structure for graph (with graph) */
void Greatealgraph (Graphadjlist * G)
{
int i,j,k,w;
printf ("Please enter%d elements: \ n", g >numvertexes);
for (i=0;i<g->numvertexes;i++)/* Reads in vertex information, establishes the vertex table */
{
scanf ("%c", &g->adjlist[i].data);/* Enter vertex information */
g->adjlist[i].firstedge=null;/* Empty table for edge table */
}
for (k=0;k<g->numedges;k++)/* Create edge Table */
{
printf (the vertex ordinal and weight on the input edge (VI,VJ): \ n ");
scanf ("%d%d%d", &i,&j,&w);/* The vertex number on the input (VI,VJ) */
Creat_edge (g,i,j,w);
}
}


Topological sorting algorithm
Status Topologicalsort (graphadjlist * G)
{
Edgenode * e;
int i,k,gettop;
int top=0; For stack pointer subscript
int count=0; Number of vertices of the statistic output
int * STACK; Stack storage vertex count
stack= (int *) malloc (g->numvertexes * sizeof (int));
for (i=0;i<g->numvertexes;i++)
{
if (g->adjlist[i].in==0)//The subscript of the vertex element with a degree of 0 into the stack
{
Stack[++top]=i;
}
}
while (top!=0)//stack is not empty
{
gettop=stack[top--];
printf ("%c", g->adjlist[gettop].data);
count++; Record the number of vertices of the output
for (E=g->adjlist[gettop].firstedge;e;e=e->next)
{
k=e->adjvex; Record the current adjacency point
if (! ( --g->adjlist[k].in)//Reduce the degree of the vertex adjacency of K by 1, to see if the degree of penetration is 0, and if 0, the stack
{
Stack[++top]=k;
}
}
}
if (count<g->numvertexes)//output vertex number is less than the number of vertices, indicating a ring
{
printf ("This picture has a ring!") \ n ");
return ERROR;
}
Else
{
return OK;
}
}


int main ()
{

Graphadjlist G;
int numver,numed;
int start;
int choose;
printf ("Please enter the number of vertex elements and the number of sides: \ n");
scanf ("%d%d", &numver,&numed);
Initgraphadjlist (&g,numver,numed);
Greatealgraph (&G);
Topologicalsort (&G);
printf ("\ n");
return 0;
}

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.