Data structure---C language topology sorting AoV diagram

Source: Internet
Author: User

Topological ordering of the graphs//Xin Yang # include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME 3#define Max_vertex_num 20typedef int infotype;//net weight typedef char vertextype[max_name];//String type typedef ENUM{DG, DN, AG, an} Graphkind;//{directed graph, directed net, non-directed graph,}//graph adjacency table store typedef struct ARCNODE{INT adjvex;//The position of the vertex pointed by the arc struct Arcnode *nextarc;// Pointing to a pointer infotype *info;//the weight pointer of the net}arcnode;typedef struct Vnode{vertextype data;//vertex information Arcnode *firstarc;//the address of the first table node, Pointer to the first arc attached to the vertex}vnode, adjlist[max_vertex_num];//head node typedef struct{adjlist Vertices;int Vexnum, arcnum;// The current vertex number of the graph and the number of arcs int kind;//The type of the graph flag}algraph;//if there is a vertex u in G, the position of the vertex in the diagram is returned. Both return -1int Locatevex (algraph G, Vertextype u) {int i;for (i = 0; i < G.vexnum; ++i) {if (strcmp (u, g.vertices[i].data) = = 0) return i;return-1;}} Using adjacency table storage structure, constructs the graph G without related information (constructs 4 kinds of graphs with a function) int creategraph (algraph *g) {int I, J, k;int w;//weight vertextype va, vb; Arcnode *p;printf ("Please enter the type of graph (with direction graph: 0, with a mesh: 1. No-map: 2, no-mesh: 3): "scanf" ("%d", & (*g). Kind);p rintf ("Please enter the number of vertices and sides of the graph: (in space): \ n"); scanf ("%d%d", &(*g). Vexnum, & (*g). Arcnum);p rintf ("Please enter a value of%d vertices (less than%d characters): \ n", (*g). Vexnum, Max_name); for (i = 0; i < (*g). Vexnum; + +i)//construct vertex vector {scanf ("%s", (*g). Vertices[i].data);(*g). Vertices[i].firstarc = NULL;} if ((*g). Kind = = 1 | | (*g). Kind = = 3)//Net {printf ("Enter the weight of each arc (edge) in order, arc tail and arc head (with space as interval): \ n");} else//Diagram {printf ("Enter the arc-tail and Arc-head (with spaces as intervals) for each arc (edge) in order: \ n");} for (k = 0; k < (*G). Arcnum; ++k) {if (*g). Kind = = 1 | | (*g). Kind = = 3) scanf ("%d%s%s", &w, VA, VB), elsescanf ("%s%s", VA, vb); i = Locatevex (*g, VA);//Arc Tail j = Locatevex (*g, VB); /ARC Head p = (arcnode*) malloc (sizeof (Arcnode));p->adjvex = J;if ((*g). Kind = = 1 | | (*g). Kind = = 3) {p->info = (int *) malloc (sizeof (int)); * (p->info) = w;} Else{p->info = NULL;}                 P->nextarc = (*g). Vertices[i].firstarc; Plug in the table header (*g). Vertices[i].firstarc = P;if ((*g). Kind >= 2)//no graph or net. Produces a second table node {p = (arcnode*) malloc (sizeof (Arcnode));p->adjvex = I;if ((*g). Kind = = 3) {P->info = (int*) malloc (sizeof ( int)); * (p->info) = w;} Else{p->info = NULL;} P-> Nextarc = (*g). Vertices[j].firstarc;     Plug in the table header (*g). Vertices[j].firstarc = P; }}return 1;} The adjacency table of the output graph gvoid Display (algraph G) {int i; Arcnode *p;switch (g.kind) {case dg:printf ("Map \ n"), Break;case dn:printf ("NET \ n"), Break;case ag:printf ("no map \ n"); Break;case an:printf ("no net \ n");} printf ("%d vertices: \ n", G.vexnum), for (i = 0; i < G.vexnum; ++i) {printf ("%s", G.vertices[i].data);} printf ("\n%d arc (EDGE): \ n", G.arcnum), for (i = 0; i < G.vexnum; i++) {p = G.vertices[i].firstarc;while (p) {if (G.kind <= 1) { printf ("%s->%s", G.vertices[i].data, G.vertices[p->adjvex].data); if (G.kind = = DN) printf (":%d", * (P->info)) ;} Else{if (I < P->adjvex) {printf ("%s--%s", G.vertices[i].data, G.vertices[p->adjvex].data); if (G.kind = = an) printf (":%d", * (P->info));}} p = P->nextarc;} printf ("\ n");}} Findindegree (algraph G, int indegree[]) for vertex entry void {int i; Arcnode *p;//Assign the initial value for (i = 0; i < G.vexnum; i++) {indegree[i] = 0;} for (i = 0; i < G.vexnum; i++) {p = G.vertices[i].firstarc;while (p) {INDegree[p->adjvex]++;p = P->nextarc;}}} Stack type typedef int SELEMTYPE; #define STACK_INIT_SIZE 10//storage space Initial allocation # define Stackincrement 2//storage space allocation increment//                                              The sequential storage structure of the stack represents the typedef struct SQSTACK{SELEMTYPE *base;//base address selemtype *top;//stack top pointer int stacksize;//The storage space currently allocated}sqstack; Constructs an empty stack int initstack (Sqstack *s) {//allocates a specified size of storage space (*s) for the bottom of the stack. Base = (Selemtype *) malloc (S Tack_init_size*sizeof (Selemtype)); if (!) ( *s) (base) exit (0);(*s). Top = (*s). base;//the bottom of the stack is the same as the top pointer (*s). stacksize = Stack_init_size;return 1;} If stack s is an empty stack (the bottom pointer and the top pointer of the stack are the same), 1 is returned. Otherwise return 0int Stackempty (Sqstack S) {if (s.top = = s.base) return 1;elsereturn 0;} Insert element E for the new stack top element int Push (Sqstack *s, Selemtype e) {if (*s). Top-(*s). Base >= (*s). stacksize) {(*s). Base = (Selemtype *) re Alloc ((*s). Base, ((*s). StackSize + stackincrement) *sizeof (Selemtype));   *s). Base) exit (0);(*s). Top = (*s). Base + (*s). stacksize; (*s). StackSize + = stackincrement;} * ((*s). top) ++= E;return 1;} If the stack is not empty, delete the top element of the s stack with E to return its value. and returns 1. Otherwise return 0int Pop (sqstack *s, Selemtype *e) {iF ((*s). Top = = (*s). Base) {return 0;} *e = *--(*s). Top;return 1;} The graph of G uses adjacency table storage structure. If G has no loop, then a topological structure of the vertex of the output G int topologicalsort (algraph G) {int I, K, Count, Indegree[max_vertex_num]; Sqstack S; Arcnode *p; Findindegree (G, Indegree); Initstack (&s); for (i = 0; i < G.vexnum; ++i) {if (!indegree[i]) Push (&s, i); count = 0;/ /Stack not empty while (! Stackempty (S)) {Pop (&s, &i);p rintf ("%s", g.vertices[i].data),//Output I number vertex and count ++count;//minus 1for for each adjacency point of the i vertex (p = = G.vertices[i].firstarc; P p = p->nextarc) {k = P->adjvex;if (!) ( --INDEGREE[K])//If the entry is reduced to 0, then enter the stack push (&s, k);}} if (Count < G.vexnum) {printf ("This has a loop \ n"); return 0;} else{printf ("For a topological sequence!! \ n ");}}} int main () {algraph f;printf ("Please select a graph \ n"); Creategraph (&f);D isplay (f); Topologicalsort (f); return 0;}

Results:


Data structure---C language topology sorting AoV diagram

Related Article

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.