Critical Path algorithm

Source: Internet
Author: User
Tags stdin

Related concepts:

(1)AOE (activity on Edges) network if there is a forward edge in the non-forward loop of the weighted direction graph to indicate the activities in a project , the weight on the edge represents the duration of the activity (Duration), When a vertex is used to represent an event , such a graph is called a network with an edge representing the activity, or AOE (activity on Edges). The AOE Network is a weighted, forward-free, circular graph. AOE networks are very useful in some engineering estimates. For example, you can make people understand:

  A. How long does it take to complete the project at least (assuming there is no ring in the network )?

  B, what activities should be accelerated to shorten the time required to complete the project?

(2) Critical path (Critical path) in the AoE network , some activities are carried out sequentially, and some activities are carried out in parallel. There may be more than one path from the source point to each vertex, and from the source point to the sink point. These paths may also be of different lengths. The time required to complete a different path of activity is different, but only if all the activities on each path are completed, the entire project is completed. Therefore, the time required to complete the project depends on the longest path length from the source point to the meeting point, which is the sum of the durations of all activities on this path. The path with the longest length is called the critical path (Critical path).

(3) Since the actual project has only one start point and one end point, so the AOE network has a unique start point of 0 (aka source) and a unique end point of 0 (also known as the meeting point)

Parameter definition:

(1) The earliest occurrence of an event ETV (earliest time of vertex): The earliest occurrence of a vertex VK.

(2) The latest occurrence of the event LTV (latest time of vertex): The last occurrence of the vertex VK, that is, the time at which each vertex corresponds to the start of the event at the latest, and exceeding this event will delay the entire duration.

(3) The earliest commencement time of the activity ete (earliest time of edge): The earliest occurrence of the arc AK.

(4) The latest start time of the activity LTE (Latest time of Edge): The latest occurrence of the arc AK, that is, the latest start time without postponing the duration.

Code implementation:

#include <stdio.h> #include <stdlib.h> #define MAXVERTEX 20#define incsize 10typedef int elemtype;typedef    char vertextype;typedef int edgetype;typedef struct edgenode{int adjvex;    Edgetype weight; struct Edgenode *next;}    edgenode;typedef struct vertexnode{vertextype data;    int in; struct Edgenode *first;}    vertexnode,adjlist[maxvertex];typedef struct adjgraphlist{adjlist adjlist;    int Numvertex; int Numedge;}    adjgraphlist;typedef struct sqstack{elemtype *base;    Elemtype *top; int stackSize;}                                          sqstack;typedef struct sqstack *linkstack;static int Etv[maxvertex];                                          Vertex (event) earliest start time static int Ltv[maxvertex];                                                     The latest start time of the vertex (event) static int ete;                                                     The earliest start time of the arc (activity) is static int LTE; The latest start time of the arc (activity)//build a stack void Initstack (Sqstack *s) {s->base = (Elemtype *) malloc (maxvertex*sizeof (ElemType));    if (!s->base) return;    S->top = s->base; S->stacksize = Maxvertex;} void Push (Sqstack *s,elemtype *e) {if (s->top-s->base >= s->stacksize-1) {s->base = (elemt        ype *) realloc (s->base, (s->stacksize + incsize) *sizeof (elemtype));    if (!s->base) return;    } * (s->top) = *e; s->top++;}    void Pop (Sqstack *s,elemtype *e) {if (s->top = = s->base) {return;    } s->top--; *e = * (S->top);} int Stacklen (Sqstack *s) {return (s->top-s->base);}    void Createadjgraphlist (Adjgraphlist *g)//tectonic chart, input data in graph {int i = 0,j = 0,k = 0,w = 0;    int indegree;    Edgenode *p;    Vertextype C;    printf ("Please enter the number of vertices and the number of sides, separated by commas: \ n");    scanf ("%d,%d", &g->numvertex,&g->numedge);    Fflush (stdin);    printf ("Please enter values for each vertex, and their entry, enter # to indicate the end: \ n");    scanf ("%c,%d", &c,&indegree);      while (I < G->numvertex) {if (c = = ' # ')      Break        G->adjlist[i].data = C;        g->adjlist[i].in = Indegree;        G->adjlist[i].first = NULL;        i++;        Fflush (stdin);        printf ("Please enter values for each vertex, and their entry, enter # to indicate the end: \ n");    scanf ("%c,%d", &c,&indegree);    } fflush (stdin);        for (k = 0;k < g->numedge;k++) {printf ("Please enter the subscript I and J for edges <Vi-Vj> attached vertices, and the weight w:\n");        scanf ("%d,%d,%d", &i,&j,&w);        p = (Edgenode *) malloc (sizeof (Edgenode));        P->adjvex = j;        P->weight = W;        P->next = g->adjlist[i].first;    G->adjlist[i].first = p;    }}void Findetv (adjgraphlist *g,sqstack *s1,sqstack *s2) {int i,k,count = 0;    Elemtype e;    Edgenode *p;//sqstack s1,s2;//initstack (&AMP;S1);//Initstack (&AMP;S2);        for (i = 0;i < g->numvertex;i++)//Initialize ETV = 0 {Etv[i] = 0;        if (g->adjlist[i].in = = 0) {Push (s1,&i); }} while (Stacklen (S1)) {Pop (s1,&e);        count++;                                          Push (s2,&e);        Press-in stack s2 p = g->adjlist[e].first;            while (p) {k = p->adjvex;            g->adjlist[k].in = g->adjlist[k].in-1;            if (g->adjlist[k].in = = 0) {Push (s1,&k); } if (Etv[k] < Etv[e] + p->weight)//construct array ETV {etv[k] = Etv[e] +            p->weight;        } p = p->next;                         }} printf ("\n\netv array is: \ n");    Test for (i = 0;i < g->numvertex;i++) {printf ("%d", etv[i]);    } printf ("\ n"); if (count! = G->numvertex) {printf ("There is a loop, the critical path cannot be found!")        \ n ");    Return    } else {return;    }}void Findcriticalpath (adjgraphlist *g,sqstack *s2) {int i = 0,k = 0;    Elemtype e;    Edgenode *p,*q;    Pop (s2,&e); for (i = 0;i < g->numvertex;i++)//Initialize vertex latest start time {ltv[i] = etv[e];    The maximum number of etv that pops up at the latest in topological sequencing is the time it takes to complete this direction-free graph} Push (s2,&e);        while (Stacklen (S2)) {Pop (s2,&e);        p = g->adjlist[e].first;            while (p) {k = p->adjvex;            if (Ltv[e] > Ltv[k]-p->weight) {ltv[e] = ltv[k]-p->weight;        } p = p->next;    }} printf ("\nltv array is: \ n");    for (i = 0;i < g->numvertex;i++) {printf ("%d", ltv[i]);    } printf ("\ n \ nthe key path contains an arc as follows: \ n");        for (i = 0;i < g->numvertex;i++) {q = g->adjlist[i].first;            while (q) {k = q->adjvex;       Ete = Etv[i];            Because the duration of the vertex (event) is 0, the earliest start time of the vertex is also the earliest start of the arc with this vertex as the arc tail LTE = Ltv[k]-q->weight; if (ete = = LTE) {printf ("<%c,%c> Weight:%d \ n", g->adjlist[i].data,g->adjlist[k].data,q-            >weight);       } q = q->next; }}}int Main () {adjgraphlist G;    Sqstack s1,s2;    Initstack (&AMP;S1);    Initstack (&AMP;S2);    Createadjgraphlist (&AMP;G);    Findetv (&AMP;G,&AMP;S1,&AMP;S2);    Findcriticalpath (&AMP;G,&AMP;S2); return 0;}

  

Critical Path 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.