_datastructure_c_impl:criticalpath#include<stdio.h> #include <stdlib.h> #include <string.h># The adjacency table type definition for the include "SeqStack.h"//graph typedef char VERTEXTYPE[4];TYPEDEF int infoptr;//is defined as integral type, in order to hold the weights typedef int vrtype;# Define MAXSIZE 50//maximum number of vertices typedef type of enum{dg,dn,ug,un}graphkind;//graph: defined typedef struct arcnode{for a type that has a direction graph, a forward mesh, a non-direction graph, and a non-network/EDGE node. int adjvex;//The adjacency point field, the position of the vertex pointed by the arc infoptr the weight of the *info;//arc struct Arcnode *nextarc;//indicates the next vertex adjacent to the vertex}arcnode;//head node type definition typedef struct Vnode{vertextype data;//is used to store vertex Arcnode *firstarc;//indicates the first vertex adjacent to that vertex}vnode,adjlist[maxsize];//diagram type definition typedef Struct{adjlist Vertex;int vexnum,arcnum;//The number of vertices of the graph and the number of arcs Graphkind kind;//diagram type}adjgraph;//returns the position of the vertex in the graph int Locatevertex ( Adjgraph G,vertextype v) {int i;for (i=0;i<g.vexnum;i++) if (strcmp (g.vertex[i].data,v) ==0) return i;return-1;} The adjacency table storage structure is used to create a forward mesh nvoid creategraph (adjgraph *n) {int i,j,k,w; Vertextype v1,v2;//defines two vertices v1 and v2arcnode *p;printf ("Please enter the number of vertices of the graph, the number of sides (comma delimited):"), scanf ("%d,%d",& (*n) .vexnum,& (*n). Arcnum);p rintf ("Please enter a value of%d vertices: \ n", N->vexnum); for (i=0;i<n->vexnum;i++) {scanf ("%s", N->vertex[i].data); n->vertex[i].firstarc=null;//The associated vertex to an empty}printf ("Enter the arc tail and the arc head (with space as the interval): \ n"); for (k=0;k<n->arcnum;k++) {// Establish the Edge list scanf ("%s%s%*c%d", V1,v2,&w), I=locatevertex (*N,V1), J=locatevertex (*N,V2),//j create adjacency table for ARC head I for arc tail p= (Arcnode *) malloc (sizeof (Arcnode));p->adjvex=j;p->info= (infoptr*) malloc (sizeof (INFOPTR)); * (P->info) =w;// Inserts a node of P points into the edge table p->nextarc=n->vertex[i].firstarc; N->vertex[i].firstarc=p;} (*n). Kind=dn;} Destroy the Gvoid destroygraph (adjgraph *g) {int i; Arcnode *p,*q;for (i=0;i< (*g). Vexnum;++i) {//Release Benzi node in diagram p=g->vertex[i].firstarc;//p points to the first node of the edge table if (p!=null) {// If the edge table is not empty, the node of the edge table is released Q=p->nextarc;free (p);p =q;}} (*g). vexnum=0;//set the vertex number to 0 (*g). arcnum=0;//the number of edges is set to the adjacency table of the 0}//output graph void Displaygraph (Adjgraph G) {int i; Arcnode *p;printf ("%d vertices: \ n", G.vexnum), for (i=0;i<g.vexnum;i++) printf ("%s", G.vertex[i].data);p rintf ("\n%d Edge: \ n ", G.arcnum); for (i=0;i<g.vexnum;i++) {P=g.vertex[i].firstarc;while (p) {printf (" <%s,%s,%d> ", G.vertex[i]. Data,g. vertex[p->adjvex].data,* (P->info));p =p->nextarc;} printf ("\ n");}} int ve[maxsize]; ve storage Events The earliest occurrence time//* Using the adjacency table storage structure of the network n topological ordering, and the corresponding events of the earliest occurrence time ve*//* if n no loop, then use the stack T to return a topological sequence of N, and return 1, otherwise 0*/int Topologicalorder (adjgraph n,seqstack *t) {int I,k,count=0;int indegree[maxsize];//array Indegree stores the degree of each vertex in SeqStack S; Arcnode *p;//saves the degrees of each vertex in the graph in the array indegree for (i=0;i<n.vexnum;i++)//assigns an array indegree the initial value indegree[i]=0;for (i=0;i< n.vexnum;i++) {p=n.vertex[i].firstarc;while (p!=null) {k=p->adjvex;indegree[k]++;p =p->nextarc;}} Initialize stack sinitstack (&s);p rintf ("Topological sequence:"), for (i=0;i<n.vexnum;i++) if (!indegree[i])//To enter a zero-degree vertex into the stack pushstack (& S,i); Initstack (T);//Initialize inverse topology sort vertex stack for (i=0;i<n.vexnum;i++)//Initialize Veve[i]=0;while (! Stackempty (s)) {//If the stack s is not empty popstack (&s,&i);//from Stack s will have the topological sort vertex i eject printf ("%s", N.vertex[i].data); Pushstack (t,i);//i vertex into inverse topological sort stack tcount++;//vertex count to Stack T for (P=N.VERTEX[I].FIRSTARC;P;P=P->NEXTARC) {// Each adjacency point of the vertex with the number I is processed k=p->adjvex;//the vertex number is kif (--indegree[k]==0)If the degree of K is reduced by 1 to 0, then K is put into the stack spushstack (&s,k), if (ve[i]+* (P->info) >ve[k])//Calculates the earliest occurrence of the event for Vertex K ve[k]=ve[i]+* (P- >info);}} if (count<n.vexnum) {printf ("The network has a loop \ n"); return 0;} Elsereturn 1;} Output AOE network N critical path int Criticalpath (adjgraph N) {int vl[maxsize];//event latest occurrence time Seqstack t;int i,j,k,e,l,dut,value,count,e1[ Maxsize],e2[maxsize]; Arcnode *p;if (! Topologicalorder (n,&t))//If a ring exists, then return 0return 0;value=ve[0];for (i=1;i<n.vexnum;i++) if (ve[i]>value) value= Ve[i];//value is the maximum value for the earliest occurrence of an event for (i=0;i<n.vexnum;i++)//Initializes the latest occurrence of the vertex event Vl[i]=value;while (! Stackempty (T))//The VL value for each vertex is sorted by inverse topology for (Popstack (&T,&J), P=n.vertex[j].firstarc;p;p=p->nextarc) {//pops up the element of the stack T, Assign to j,p the subsequent event kk=p->adjvex;dut=* (P->info) that points to J,//dut is the value of Arc <j,k> if (vl[k]-dut<vl[j])//Calculates the latest occurrence of event J vl[j]= Vl[k]-dut;} printf ("\ n The earliest occurrence of the event and the latest occurrence time \ni Ve[i] vl[i]\n"); for (i=0;i<n.vexnum;i++)//The earliest occurrence of the event corresponding to the output vertex printf ("%d%d%d\n ", I,ve[i],vl[i]);p rintf (" Critical Path is: ("); for (i=0;i<n.vexnum;i++)//output critical path through vertex if (Ve[i]==vl[i]) printf ("%s ", N.vertex[I].data];p rintf (") \ n"), count=0;printf ("Activity earliest start time and latest start time \ arc e l l-e\n"); for (j=0;j<n.vexnum;j++) for (p=n.vertex[ J].FIRSTARC;P;P=P->NEXTARC) {k=p->adjvex;dut=* (p->info);//dut is the right value for arc <j,k> e=ve[j];//e is activity <j,k > 's earliest start time l=vl[k]-dut;//l is the latest start time for activity <j,k> printf ("%s→%s%3d%3d%3d\n", N.vertex[j].data,n.vertex[k].data,e, L,L-E); if (e==l) {//Save key activity in array e1[count]=j;e2[count]=k;count++;}} printf ("Key activity is:"); for (k=0;k<count;k++)//Output critical path {I=e1[k];j=e2[k];p rintf ("(%s→%s)", N.vertex[i].data,n.vertex[j] . data);} printf ("\ n"); return 1;} void Main () {adjgraph N; Creategraph (&n);/* The adjacency table storage structure is used to create a forward network n*/displaygraph (n);/* Output has a forward network N*/criticalpath (n);/* The critical path to Net N is */destroygraph ( &N);/* Destroy Net N*/system ("pause");}
#pragma once#include<stdio.h> #include <stdlib.h> #define StackSize 100typedef int datatype;// stack element type definition typedef struct{datatype STACK[STACKSIZE];INT top;} seqstack;//the stack is initialized to empty stack only need to put the top pointer to void Initstack (Seqstack *s) {s->top=0;//The stack top pointer to 0}//to determine whether the stack is empty, the stack is empty return 1, otherwise return 0int Stackempty (Seqstack S) {if (s.top==0) return 1;elsereturn 0;} Take the top element of the stack. Returns the value of the top element of the stack to E and returns 1 for success; otherwise, 0 indicates failure. int GetTop (Seqstack s,datatype *e) {if (s.top<=0) {///before fetching the top element of the stack, determine if the stack is empty printf ("Stack is empty!\n"); return 0;} else{*e=s.stack[s.top-1];//the top element of the stack return 1;}} Put the element e into the stack, the element into the stack successfully returned 1, otherwise return 0int Pushstack (Seqstack *s,datatype e) {if (s->top>=stacksize) {//before the element into the stack, determine whether the stack is full of printf ("Stack is full, can't go into the stack!") \ n "); return 0;} else{s->stack[s->top]=e;//element e into the stack s->top++;//modify the stack top pointer return 1;}} The stack operation. Stack the top element of the stack and assign it to E. The stack successfully returns 1, otherwise returns 0int Popstack (Seqstack *s,datatype *e) {if (s->top<=0) {//element before the stack, determine whether the stack is empty printf ("stack has no elements, cannot be out of the stack!\n"); return 0;} else{s->top--;//first modify the stack top pointer, that is, the stack *e=s->stack[s->top];//The stack element is assigned to Ereturn 1;}} Calculate the length of the stack, that is, the number of elements in the stack, the value of the top pointer is equal to the number of elements in the stack int stacklength (Seqstack S) {RETurn s.top;} Empty stack operation void Clearstack (Seqstack *s) {s->top=0;}
Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint |copyright©2011-2015,supernatural, all rights Reserved.
Key Path of _DATASTRUCTURE_C_IMPL:AOE network