数据结构之---C语言实现关键路径AOE图
2015-08-24 00:23
676 查看
//关键路径AOE //杨鑫 #include <stdio.h> #include <stdlib.h> #include <string.h> //定义邻接表 typedef struct node { int adjvex; int w; struct node *nextedge; }edgenode; //定义边集 typedef struct { char data; int id; edgenode *firstedge; }vexnode; void CreateGraph(vexnode* Graph,int vexnumber,int arcnumber) { int i = 0, j = 0, k = 0; int begin,end,duttem; char ch; edgenode *p; for(i=0;i<vexnumber;i++) { Graph[i].id =0; Graph[i].firstedge =NULL; } printf("请输入这个图中的各个顶点的值:\n"); for(i=0;i<vexnumber;i++) { scanf("%s",&ch); Graph[i].data=ch; } printf("请输入图中弧的起始点及权值:其格式为<起点,终点,权值>\n"); for(k=0;k<arcnumber;k++) { scanf("%d,%d,%d",&begin,&end,&duttem); p=(edgenode*)malloc(sizeof(edgenode)); p->adjvex =end-1; p->w =duttem; Graph[end-1].id ++; p->nextedge =Graph[begin-1].firstedge ; Graph[begin-1].firstedge =p; } } int SearchMapPath(vexnode* Graph,int vexnumber,int arcnumber) { int totaltime=0; int m=0; int i,j,k,t; char sv[100]; int front,rear; int *topology_queue,*vl,*ve,*el,*ee; front=rear=-1; t=0; topology_queue=(int*)malloc(vexnumber*sizeof(int)); vl=(int*)malloc(vexnumber*sizeof(int)); ve=(int*)malloc(vexnumber*sizeof(int)); el=(int*)malloc(arcnumber*sizeof(int)); ee=(int*)malloc(arcnumber*sizeof(int)); edgenode *p; for(i=0;i<vexnumber;i++) ve[i]=0; for(i=0;i<vexnumber;i++) { if(Graph[i].id==0) topology_queue[++rear]=i; m++; } while(front!=rear) { front++; j=topology_queue[front]; m++; p=Graph[j].firstedge ; while(p) { k=p->adjvex ; Graph[k].id --; if(ve[j]+p->w >ve[k]) ve[k]=ve[j]+p->w ; if(Graph[k].id ==0) topology_queue[++rear]=k; p=p->nextedge ; } } if(m<vexnumber) { printf("\n本程序所建立的图有回路不可计算出关键路径\n"); printf("将退出本程序\n"); return 0; } totaltime=ve[vexnumber-1]; for(i=0;i<vexnumber;i++) vl[i]=totaltime; for(i=vexnumber-2;i>=0;i--) { j=topology_queue[i]; p=Graph[j].firstedge; while(p) { k=p->adjvex ; if((vl[k]-p->w )<vl[j]) vl[j]=vl[k]-p->w; p=p->nextedge; } } printf("| 起点 | 终点 | 最早开始时间 | 最迟开始时间 | 差值 | 是否为关键路径 \n"); i=0; for(j=0;j<vexnumber;j++) { p=Graph[j].firstedge; while(p) { k=p->adjvex; ee[++i]=ve[j]; el[i]=vl[k]-p->w; printf("| %4c | %4c | %12d | %12d | %4d |",Graph[j].data ,Graph[k].data ,ee[i],el[i],el[i]-ee[i]); if(el[i]==ee[i]) { printf(" 此弧为关键活动 "); sv[t]=Graph[j].data;t++; } printf("\n"); p=p->nextedge; } } printf("关键路径节点为:"); sv[t]=Graph[vexnumber-1].data; for(i=0;i<=t;i++) { printf("%c",sv[i]); if(sv[i]!=Graph[vexnumber-1].data) printf("--->"); } printf("\n"); printf("关键路径长度为:%d个单位时间\n",totaltime); return 1; } int main( ) { int vexnumber,arcnumber,totaltime=0; printf("请输入这个图中的节点数:"); scanf("%d",&vexnumber); printf("请输入这个图中的弧数:"); scanf("%d",&arcnumber); vexnode* Graph=(vexnode*)malloc(vexnumber*sizeof(vexnode)); CreateGraph(Graph,vexnumber,arcnumber); SearchMapPath(Graph,vexnumber,arcnumber); return 0; }
结果:
相关文章推荐
- 栈和队列数据结构的基本概念及其相关的Python实现
- 数据结构与算法-线性表的实现(1)
- 数据结构与算法-如何计算时间复杂度
- 数据结构与算法-抽象数据类型
- 数据结构与算法-为什么要使用算法
- 数据机构与算法-数据结构的一些基本概念
- 数据结构与算法-函数的渐近增长
- 数据结构与算法-线性表的定义与特点
- 【数据结构导论】什么是数据结构?
- 数据结构_散列表
- 数据结构_散列表
- 黑马程序员——集合——Set集合,增强for循环,数据结构,泛型,Collections集合工具类
- Nginx基本数据结构之ngx_http_request_t结构体
- Accelerated C++学习笔记 5 管理内存和低级数据结构
- 数据结构~新学期寄予~你怎么知道我被翁凯虐惨了
- 数据结构之---C语言实现拓扑排序AOV图
- Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】
- HDU_2795 Billboard(线段树)
- 数据结构基础 后序遍历和中序遍历还原二叉树
- Red-Black Tree