Critical path problem--perfect version

Source: Internet
Author: User

#include <stdio.h>

#include <string.h>

#include <stdlib.h>


#define MAXV 50

#define MAXVEXNUM 100

#define TRUE 1

#define FALSE 0

#define OK 1

#define ERROR 0

#define OVERFLOW-2

#define SINITSIZE 100//stack storage space initial allocation

#define SINCREMENT 50//stack storage space allocation increased


int ve[maxvexnum];//Storage Earliest occurrence time


typedef char VERTEXTYPE[MAXV]; Vertex vector

typedef int SELEMTYPE;

typedef int STATUS;

/*

Www.quzhuanpan.comp

Interpretation of the power from the turntable network, reprint please inform

*/

typedef struct ARCNODE

{

int Adjvex; The position of the vertex to which the arc is pointing

struct Arcnode *nextarc; Pointer to next ARC

int weight;

} Arcnode; Information storage of Arcs


typedef struct

{

Vertextype data; Vertex information

Arcnode *firstarc; Address of the first table node, pointer to the first arc attached to the vertex

} Vnode,adjlist[maxvexnum]; Head Knot Point


typedef struct

{

Adjlist Vertexs;

int vexnum,arcnum;

} algraph;

/*

Www.quzhuanpan.comp

Interpretation of the power from the turntable network, reprint please inform

*/

typedef struct

{

Selemtype *base;

Selemtype *top;

int stacksize;

} Sqstack;


int Locatevex (algraph &g,vertextype u)//character type U

{

int i;

for (i = 0; i < g.vexnum; ++i)

if (strcmp (u,g.vertexs[i].data) = = 0)

return i;

return OK;

}//all Right


/*status Listinsert (algraph &g,int i,arcnode *e)//Insert element E before the first I position in the single-linked list L without the lead node

{


E->nextarc =&l;

L =*e;

return OK;

}*/


int creategraph (algraph &g)

{

int i,j,k,w;

Vertextype V1,v2;

Arcnode *e;

printf ("Please enter the number of vertices of the graph, the number of edges (with spaces as the interval): \ n");

scanf ("%d%d", &g.vexnum,&g.arcnum);

printf ("Please enter a value of%d vertices (less than%d characters): \ n", G.VEXNUM,MAXV);

for (i = 0; i<g.vexnum; ++i)//construct vertex vectors and initialize

{

scanf ("%s", G.vertexs[i].data);

G.vertexs[i].firstarc = NULL;

}


printf ("Please enter the arc end of the%d arc, the arc head and the weight (with space as the interval): \ n", g.arcnum);

for (k = 0; k < g.arcnum; ++k)

{

scanf ("%s%s%d", v1,v2,&w);

i = Locatevex (G,V1);

j = Locatevex (G,V2);

E = (Arcnode *) malloc (sizeof (Arcnode)); : Allocates memory for each arc, inserts the linked list

E->weight = W;

E->adjvex = j;

E->nextarc = NULL;

Listinsert (g.vertexs[i].firstarc,1,&e);

e->nextarc=g.vertexs[i].firstarc;

G.vertexs[i].firstarc=e;

}

return OK;

}

/*

Www.quzhuanpan.comp

Interpretation of the power from the turntable network, reprint please inform

*/

int Display (algraph &g)

{

int i;

Arcnode *p;

printf ("\ n");

printf ("%d vertices:", g.vexnum);

for (i = 0; i < g.vexnum; ++i)

{

printf ("%s", G.vertexs[i].data);

}


printf ("\n\n\n");


printf ("%d arcs: \ n", g.arcnum);


for (i = 0; i < g.vexnum; ++i)

{

p = G.vertexs[i].firstarc;

while (p)

{

printf ("%5s%5s%5d\n", g.vertexs[i].data,g.vertexs[p->adjvex].data,p->weight);

p = p->nextarc;

}


}

return OK;

}


void Findindegree (Algraph &g,int indegree[])

{

int i;

Arcnode *p;

for (i = 0; i < g.vexnum; ++i)

Indegree[i] = 0;

for (i = 0; i < g.vexnum; ++i)

{

p = G.vertexs[i].firstarc;

while (p)

{

indegree[p->adjvex]++;

p = p->nextarc;

}

}

}


void Initstack (Sqstack &s)

{

S.base = (Selemtype *) malloc (sinitsize * sizeof (Selemtype));

if (! ( S.base))

Exit (OVERFLOW);

S.top = S.base;

S.stacksize = sinitsize;

}


Status stackempty (Sqstack &s)

{

if (s.top = = s.base)

return TRUE;

else return ERROR;

}


void Push (Sqstack &s,int &e)

{

if (s.top-s.base >= s.stacksize)

{

S.base = (Selemtype *) realloc (s.base, (s.stacksize + sincrement) * sizeof (Selemtype));

if (! ( S.base))

Exit (OVERFLOW);

S.top = S.base + s.stacksize;

S.stacksize + = sincrement;

}

* (s.top) + + = e;

}


Status Pop (sqstack &s,int &e)

{

if (s.top = = s.base)

return ERROR;

E = * (--s.top);

return OK;

}


Status Topologicalorder (algraph &g, Sqstack &t)

{

int count1=0;

int count2=0;

int i,k,n=0;

int Indegree[maxvexnum];

Sqstack S;

Arcnode *p;

Findindegree (G,indegree);

Initstack (S);

for (i = 0; i < g.vexnum; ++i)

{

if (indegree[i]==0)

{

Push (S,i); Zero entry into the stack

count1++;

}

if (g.vertexs[i].firstarc==null)

n++;

}

if (count1>1)

{

printf ("There are%d source points in the figure that cannot find the critical path!!! \ n ", count1);

return 0;

}


else if (n>1)

{

printf ("There are%d meeting points in the figure that cannot find the critical path!!! \ n);

return 0;

}//an activity has only one start and one end, so the source and meeting points are only one


printf ("\ n");

printf ("Topological sequence:");

Initstack (T);

for (i = 0; i < g.vexnum; ++i)

Ve[i] = 0;

while (! Stackempty (S))

{


Pop (S,i);

printf ("%s", G.vertexs[i].data);

Push (T,i);

++count2; Count of output vertices

for (p = G.vertexs[i].firstarc; p; p = p->nextarc)

{

K = p->adjvex; Minus one for each adjacency point of the I vertex

if (--indegree[k] = = 0)

Push (S,K);

if (Ve[i] + p->weight > ve[k])

VE[K] = Ve[i] + p->weight;

}

}

printf ("\ n");

if (Count2 < G.vexnum)

{

printf ("This has a loop to the net \ n");

return 0;

}

else return 1;


}


Status Criticalpath (algraph &g)

{

int Vl[maxvexnum];

Sqstack T;

int I,j,k,ee,el,dut;

Arcnode *p;

if (! Topologicalorder (g,t))

Return 0;//if returned is not 1, the back does not execute

J= Ve[0];

for (i = 0; i < g.vexnum; i++)

if (Ve[i] > J)

j = The earliest occurrence of the activity corresponding to the ve[i];//endpoint

for (i = 0; i < g.vexnum; i++)

Vl[i] = The earliest occurrence of the activity corresponding to the j;//all assigned to an end point

while (! Stackempty (T))

{

For (Pop (t,j), p = G.vertexs[j].firstarc; p; p = p->nextarc)

{

K = p->adjvex;

Dut = p->weight;

if (Vl[k]-dut < VL[J])

VL[J] = vl[k]-dut;

The}//stack T is placed in the inverse topological sequence, that is, the corresponding subscript

}

printf ("V ve[i] vl[i]\n");

for (i = 0; i < g.vexnum; ++i)

{

printf ("%s%d%5d", g.vertexs[i].data,ve[i],vl[i]);

if (ve[i] = = Vl[i])

printf ("Vertex of Critical Path");

printf ("\ n");

}

printf ("\ n");

printf ("Note: V is event, Ve[i] is the earliest start time, Vl[i] is the latest start time. \ n ");

printf ("\ n");

printf ("j----k----weights----EE----el\n");

for (j = 0; j < G.vexnum; ++j)//Seek ee,el and key activities

for (p = G.vertexs[j].firstarc; p; p = p->nextarc)

{

K = p->adjvex;

Dut = p->weight;

EE = ve[j];

El = vl[k]-dut;

printf ("%s-->%s%3d%5d%5d", G.vertexs[j].data,g.vertexs[k].data,dut,ee,el);

if (ee = = EL)

printf ("Key activities");

printf ("\ n");


}//for

printf ("\ n Note: EE is the earliest start time for the activity, and the El is the latest start time for the activity.") ");

j--;

printf ("\ n");

printf ("Time required to complete the project:%d", ve[j]);

printf ("\ n");

return OK;

}

/*

Www.quzhuanpan.comp

Interpretation of the power from the turntable network, reprint please inform

*/

int main ()

{

System ("Color 5f");

Algraph h;

Creategraph (h);

Display (h);

Criticalpath (h);

return 0;

}


Critical path problem--perfect version

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.