这个算法来求关键路径,其实就是利用拓扑排序,首先求出,每个节点最晚开始时间,再倒退求每个最早开始的时间。

从而算出活动最早开始的时间和最晚开始的时间,如果这两个时间相等,则为关键路径。

时间复杂度为O(n+e)

主要算法:

int topSort(Graph *g){
EdgeNode *e;
int i,k,gettop;
int top = ;
int count = ;
int *stack;
stack = (int *)malloc(g->numVertexes * sizeof(int));
for(i=;i<g->numVertexes;i++){
if(g->headlist[i].in == ) //把入度为0的,即没有入度的点入栈
stack[++top] = i;
} top2 = ;
etv = (int *)malloc(g->numVertexes*sizeof(int));
for(i=;i<g->numVertexes;i++){
etv[i] = ;
}
stack2=(int *)malloc(g->numVertexes*sizeof(int)); while(top){
gettop = stack[top--];
printf("%d ",gettop);
count++;
stack2[++top2] = gettop; for(e = g->headlist[gettop].fnode; e ; e=e->next){ //一次遍历链表,减少各个子节点的入度
k = e->data;
if(!(--g->headlist[k].in))
stack[++top] = k;
if((etv[gettop]+e->weight)>etv[k]) //选取最大值
etv[k] = etv[gettop]+e->weight;
}
}
if(count < g->numVertexes)
return ERROR;
else
return OK;
}
int critical(Graph *g){
EdgeNode *e;
int i,gettop,k,j;
int ete,lte; topSort(g);
printf("\n-------------------------------------------\n");
for(i=;i<g->numVertexes;i++){
printf("%d ",etv[i]);
}
printf("\n-------------------------------------------\n");
ltv = (int *)malloc(sizeof(int)*g->numVertexes);
for(i=;i<g->numVertexes;i++){
ltv[i] = etv[g->numVertexes-];
} while(top2!=){
gettop = stack2[top2--];
for(e = g->headlist[gettop].fnode;e;e = e->next){
k = e->data;
if(ltv[k]-e->weight < ltv[gettop]){
ltv[gettop] = ltv[k] - e->weight;
}
}
} for(i=;i<g->numVertexes;i++){
printf("%d ",ltv[i]);
}
printf("\n-------------------------------------------\n");
printf("\n");
for(j=;j<g->numVertexes;j++){
for(e=g->headlist[j].fnode; e; e=e->next){
k = e->data;
ete = etv[j];//活动最早
lte = ltv[k] - e->weight;//活动最迟
if(ete == lte)
printf("<v%d v%d>length:%d\n",g->headlist[j].data,g->headlist[k].data,e->weight);
}
}
return ;
}

全部代码:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 9
#define ERROR 1
#define OK 0
typedef struct edgeNode{
int data;
int weight;
struct edgeNode *next;
}EdgeNode;
typedef struct headNode{
int in;
int data;
EdgeNode *fnode;
}HeadNode,HeadList[MAX];
typedef struct{
HeadList headlist;
int numEdges,numVertexes;
}Graph,*graph; int *etv,*ltv;
int *stack2;
int top2; void initialGraph(Graph *g);
void headinfo(Graph *g,int in,int node);
void linkinfo(Graph *g,int node,int link,int weight);
void printGraph(Graph *g);
int topSort(Graph *g);
int critical(Graph *g); int main(){
Graph *g = (Graph *)malloc(sizeof(Graph));
g->numEdges = ;
g->numVertexes = ;
initialGraph(g);
printGraph(g); critical(g);
//topSort(g); getchar();
return ;
}
int critical(Graph *g){
EdgeNode *e;
int i,gettop,k,j;
int ete,lte; topSort(g);
printf("\n-------------------------------------------\n");
for(i=;i<g->numVertexes;i++){
printf("%d ",etv[i]);
}
printf("\n-------------------------------------------\n");
ltv = (int *)malloc(sizeof(int)*g->numVertexes);
for(i=;i<g->numVertexes;i++){
ltv[i] = etv[g->numVertexes-];
} while(top2!=){
gettop = stack2[top2--];
for(e = g->headlist[gettop].fnode;e;e = e->next){
k = e->data;
if(ltv[k]-e->weight < ltv[gettop]){
ltv[gettop] = ltv[k] - e->weight;
}
}
} for(i=;i<g->numVertexes;i++){
printf("%d ",ltv[i]);
}
printf("\n-------------------------------------------\n");
printf("\n");
for(j=;j<g->numVertexes;j++){
for(e=g->headlist[j].fnode; e; e=e->next){
k = e->data;
ete = etv[j];//活动最早
lte = ltv[k] - e->weight;//活动最迟
if(ete == lte)
printf("<v%d v%d>length:%d\n",g->headlist[j].data,g->headlist[k].data,e->weight);
}
}
return ;
} int topSort(Graph *g){
EdgeNode *e;
int i,k,gettop;
int top = ;
int count = ;
int *stack;
stack = (int *)malloc(g->numVertexes * sizeof(int));
for(i=;i<g->numVertexes;i++){
if(g->headlist[i].in == ) //把入度为0的,即没有入度的点入栈
stack[++top] = i;
} top2 = ;
etv = (int *)malloc(g->numVertexes*sizeof(int));
for(i=;i<g->numVertexes;i++){
etv[i] = ;
}
stack2=(int *)malloc(g->numVertexes*sizeof(int)); while(top){
gettop = stack[top--];
printf("%d ",gettop);
count++;
stack2[++top2] = gettop; for(e = g->headlist[gettop].fnode; e ; e=e->next){ //一次遍历链表,减少各个子节点的入度
k = e->data;
if(!(--g->headlist[k].in))
stack[++top] = k;
if((etv[gettop]+e->weight)>etv[k]) //选取最大值
etv[k] = etv[gettop]+e->weight;
}
}
if(count < g->numVertexes)
return ERROR;
else
return OK;
} void printGraph(graph g){
int i;
printf("vertex:%d,edges:%d\n",g->numVertexes,g->numEdges);
EdgeNode *e = (EdgeNode *)malloc(sizeof(EdgeNode));
for(i=;i<MAX;i++){
printf("[in:%d]%d",g->headlist[i].in,g->headlist[i].data);
e = g->headlist[i].fnode;
while(e != NULL){
printf("->%d(%d)",e->data,e->weight);
e = e->next;
}
printf("\n");
}
free(e);
}
void initialGraph(Graph *g){
headinfo(g,,);
linkinfo(g,,,);
linkinfo(g,,,); headinfo(g,,);
linkinfo(g,,,);
linkinfo(g,,,); headinfo(g,,);
linkinfo(g,,,);
linkinfo(g,,,); headinfo(g,,);
linkinfo(g,,,); headinfo(g,,);
linkinfo(g,,,);
linkinfo(g,,,); headinfo(g,,);
linkinfo(g,,,); headinfo(g,,);
linkinfo(g,,,); headinfo(g,,);
linkinfo(g,,,); headinfo(g,,);
linkinfo(g,,,); headinfo(g,,);
}
void headinfo(Graph *g,int in,int node){
g->headlist[node].in = in;
g->headlist[node].data = node;
g->headlist[node].fnode = NULL;
g->numVertexes++;
}
void linkinfo(Graph *g,int node,int link,int weight){
EdgeNode *en = (EdgeNode *)malloc(sizeof(EdgeNode));
if(g->headlist[node].fnode != NULL){
en->next = g->headlist[node].fnode;
}else{
en->next = NULL;
}
g->headlist[node].fnode = en;
en->data = link;
en->weight = weight;
g->numEdges++;
}

运行示例:

最新文章

  1. MySql免安装版安装配置,附MySQL服务无法启动解决方案
  2. CSS 巧用 :before和:after
  3. 将图片插入到excel中
  4. Exchange 2013 、Lync 2013、SharePoint 2013 二
  5. WPF常用方法,事件驱动和控件遍历
  6. Git搭建团队开发环境操作演练
  7. Qt源码分析之QObject
  8. bzoj 3931 [CQOI2015]网络吞吐量(最短路,最大流)
  9. 关于Git和SVN的对比
  10. 使用pip install 或者easy_install安装Python的各种包出现cc failed with exit status 1
  11. Servlet 浅谈(一)
  12. 1104. Don’t Ask Woman about Her Age(数论)
  13. POJ 1678 I Love this Game!#dp博弈
  14. Spring.NET 的IOC(依赖注入)
  15. 微服务框架surging学习之路——序列化
  16. mybatis_08 mybatis与hibernate的区别
  17. 移动端touchstart,touchmove,touchend
  18. FreeBie—免费设计师专用素材网
  19. Mayor&#39;s posters POJ - 2528(线段树 + 离散化)
  20. HDU 6158 笛卡尔定理+韦达定理

热门文章

  1. Grunt + Bower—前端构建利器(转)
  2. Java [leetcode 1] Two Sum
  3. 转载RabbitMQ入门(3)--发布和订阅
  4. liux之我用过的zip解压命令
  5. ubuntu鼠标突然不能使用的解决方法
  6. HDU5772 String problem 最大权闭合图+巧妙建图
  7. Multiple View Geometry in Computer Vision Second Edition by Richard Hartley 读书笔记(一)
  8. OpenGL超级宝典第5版&amp;&amp;开发环境搭建
  9. duilib relativepos属性导致控件错误的bug修复
  10. andriod的简单用法2