数据结构C语言-关键路径

#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int status;
typedef int ElemType;
typedef int bool;
#define true 1
#define false 0

#define MaxInt 32767
#define MVNum 100
typedef char VerTexType;
typedef int ArcType;

typedef int OtherInfo;

//邻接表
typedef struct ArcNode
{
    int adjvex;
    struct ArcNode *nextarc;
    OtherInfo weight;
}ArcNode;

typedef struct VNode
{
    VerTexType data;
    ArcNode *firstarc;
}VNode,AdjList[MVNum];

typedef struct
{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;


int LocateVex_AL(ALGraph G,VerTexType v)
{
    int i=0;
    while(i<G.vexnum&&G.vertices[i].data!=v)
        i++;
    if(i>=G.vexnum)
        return ERROR;
    return i;
}

status CreateUDN_AL(ALGraph *G,VerTexType *V)
{
    int i=0,k=0,j,w;
    for(;i<(*G).vexnum;i++)//G->就完事了!
    {
        G->vertices[i].data=V[i];
        G->vertices[i].firstarc=NULL;
    }
    char v1,v2;
    for(;k<G->arcnum;k++)
    {
        scanf("%c %c %d",&v1,&v2,&w);
        getchar();
        int i=LocateVex_AL(*G,v1);
        int j=LocateVex_AL(*G,v2);
        ArcNode *p1=(ArcNode *)malloc(sizeof(ArcNode));
        p1->adjvex=j;
        p1->weight=w;
        p1->nextarc=G->vertices[i].firstarc;
        G->vertices[i].firstarc=p1;
        /*
        ArcNode *p2=(ArcNode *)malloc(sizeof(ArcNode));
        p2->adjvex=i;
        p2->nextarc=G->vertices[j].firstarc;
        G->vertices[j].firstarc=p2;
        */
    }
    return OK;
}

//栈
#define MAXSIZE 100
typedef struct
{
    ElemType *base;
    ElemType *top;
    int stacksize;
}SqStack;

status InitStack(SqStack *s)
{
    s->base=(ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
    if(!s->base)
        exit(OVERFLOW);
    s->top=s->base;
    s->stacksize=MAXSIZE;
    return OK;
}

status Push(SqStack *s,ElemType e)
{
    if(s->top-s->base==s->stacksize)
        return ERROR;
    *(s->top)=e;
    s->top++;
    return OK;
}

status Pop(SqStack *s,ElemType *e)
{
    if(s->top==s->base)
        return ERROR;
    s->top--;
    *e=*(s->top);
    return OK;
}

bool StackEmpty(SqStack s)
{
    return s.top==s.base;
}


void FindInDegree(ALGraph G,int indegree[])
{
    int i,d=0;
    for(i=0;i<G.vexnum;i++)
        indegree[i]=0;
    for(i=0;i<G.vexnum;i++)
    {
        ArcNode *p=G.vertices[i].firstarc;
        while(p!=NULL)
        {
            indegree[p->adjvex]++;
            p=p->nextarc;
        }
    }
}

status TopologicalSort(ALGraph G,int topo[])
{
    int indegree[G.vexnum],i;
    FindInDegree(G,indegree);
    SqStack S;
    InitStack(&S);
    for(i=0;i<G.vexnum;i++)
        if(!indegree[i])
           Push(&S,i);
    int m=0;
    while(!StackEmpty(S))
    {
        Pop(&S,&i);
        topo[m]=i;
        m++;
        ArcNode *p=G.vertices[i].firstarc;
        while(p!=NULL)
        {
            int k=p->adjvex;
            indegree[k]--;
            if(indegree[k]==0)
               Push(&S,k);
            p=p->nextarc;
        }
    }
    if(m<G.vexnum)
        return ERROR;
    else
        return OK;
}

status CriticalPath(ALGraph G)
{
    int topo[G.vexnum];
    if(!TopologicalSort(G,topo))
        return ERROR;
    int n=G.vexnum,i,ve[n],vl[n],k,j;
    ArcNode *p;
    for(i=0;i<n;i++)
        ve[i]=0;
    /*按拓扑正序求最早发生时间*/
    for(i=0;i<n;i++)
    {
        k=topo[i];
        p=G.vertices[k].firstarc;
        while(p!=NULL)
        {
            j=p->adjvex;
            if(ve[j]<ve[k]+p->weight)
                ve[j]=ve[k]+p->weight;
            p=p->nextarc;
        }
    }
    for(i=0;i<n;i++)
        vl[i]=ve[n-1];
    /*按拓扑逆序求最迟发生时间*/
    for(i=n-1;i>=0;i--)
    {
        k=topo[i];
        p=G.vertices[k].firstarc;
        while(p!=NULL)
        {
            j=p->adjvex;
            if(vl[k]>vl[j]-p->weight)
                vl[k]=vl[j]-p->weight;
            p=p->nextarc;
        }
    }
    /*判断是否为关键活动*/
    for(i=0;i<n;i++)
    {
        p=G.vertices[i].firstarc;
        while(p!=NULL)
        {
            j=p->adjvex;
            int e=ve[i];
            int l=vl[j]-p->weight;
            if(e==l)
                printf("(V%d,V%d)",i,j);
            p=p->nextarc;
        }
    }
}
int main()
{
    ALGraph G;
    G.vexnum=9;
    G.arcnum=11;
    VerTexType V[G.vexnum];
    int i;
    for(i=0;i<G.vexnum;i++)
        V[i]=i+'A';
    printf("Create ALGraph:\n");
    CreateUDN_AL(&G,V);
    printf("Critical Path:\n");
    CriticalPath(G);
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值