图的拓扑排序与关键路径

#include"StdAfx.h"
#include <iostream>
#include<string>
#include<stack>
using namespace std;
typedef struct ArcNode
{
char adjvex;
struct ArcNode *next;
int v;
}ArcNode;
typedef struct VNode
{
char vertex;
ArcNode *firstarc;
}VNode,*Adjvex;
typedef struct 
{
Adjvex vertices;
int vexnum,arcnum;
}ALGraph;
int search(ALGraph *G,char ch)
{
int i;
for(i=0;i<G->vexnum;i++)
{
if(ch==G->vertices[i].vertex)
{
return i;
}
}
return -1;
}
void GraphCreate(ALGraph *G)
{

int i,k,v,m,n;
char a,b;
cout<<"input the num of the graph's vex and arc:"<<endl;
cin>>G->vexnum>>G->arcnum;
G->vertices=(Adjvex)malloc(sizeof(VNode)*G->vexnum);
cout<<"input the information of the graph's vex :"<<endl;
for(i=0;i<G->vexnum;i++)
{
cin>>G->vertices[i].vertex;
G->vertices[i].firstarc=NULL;
}
cout<<"input the information of the graph's arc:"<<endl;
for(i=0;i<G->arcnum;i++)
{
cin>>a>>b>>v;
m=search(G,a);
n=search(G,b);
ArcNode *node=(ArcNode*)malloc(sizeof(ArcNode));
node->adjvex=b;
node->v=v;
node->next=G->vertices[m].firstarc;
G->vertices[m].firstarc=node;
}
 
}
void print(ALGraph *G)
{
ArcNode *node;
for(int i=0;i<G->vexnum;i++)
{
cout<<G->vertices[i].vertex;
node=G->vertices[i].firstarc;
while(node)
{
cout<<"->"<<node->adjvex;
node=node->next;
}
cout<<endl;
}
}
void FindIndegree(ALGraph *G,int *indegree)
{
int i,j;
ArcNode *node;
for(i=0;i<G->vexnum;i++)
indegree[i]=0;
for(i=0;i<G->vexnum;i++)
{
node=G->vertices[i].firstarc;
while(node)
{
j=search(G,node->adjvex);
indegree[j]++;
node=node->next;
}
}


}
int TopologicalSort(ALGraph *G)
{
int *degree,i,j,count=0;
int temp;
ArcNode *p;
stack<int> s;
s.empty();
degree=(int *)malloc(sizeof(int)*G->vexnum);
FindIndegree(G,degree);
for(i=0;i<G->vexnum;i++)
{
if(!degree[i])
{
s.push(i);
}
}
while(!s.empty())
{
temp=s.top();
s.pop();
cout<<G->vertices[temp].vertex<<" ";
count++;
p=G->vertices[temp].firstarc;
while(p)
{
j=search(G,p->adjvex);
if(!(--degree[j]))
{
s.push(j);
}
p=p->next;
}
}
if(count<G->vexnum)
return 0;
else
return 1;
}
int TopologicalOrder(ALGraph *G,stack <int>T,int *ve)//改进的拓扑排序 返回拓扑序列和事件的最早发生时间
{
int *degree,i,j,count=0;
int temp;
ArcNode *p;
stack<int> s;
s.empty();
T.empty();
degree=(int *)malloc(sizeof(int)*G->vexnum);
FindIndegree(G,degree);
for(i=0;i<G->vexnum;i++)
{
if(!degree[i])
{
s.push(i);
}
}
while(!s.empty())
{
temp=s.top();
s.pop();
T.push(temp);
cout<<G->vertices[temp].vertex<<" ";
count++;
p=G->vertices[temp].firstarc;
while(p)
{
j=search(G,p->adjvex);
if(!(--degree[j]))
{
s.push(j);
}
if((ve[temp]+p->v)>ve[j])
{
ve[j]=ve[j]+p->v;
}
p=p->next;
}
}
if(count<G->vexnum)
return 0;
else
return 1;
}
int CriticalPath(ALGraph *G)
{
stack<int> T;
int *ve,*vl,i,j,w,v,temp;
ArcNode *p;
ve=(int *)malloc(sizeof(int)*G->vexnum);
vl=(int *)malloc(sizeof(int)*G->vexnum);
for(i=0;i<G->vexnum;i++)
{
ve[i]=0;
vl[i]=0;
}
if(!TopologicalOrder(G,T,ve));
return 0;
for(i=0;i<G->vexnum;i++)
{
vl[i]=ve[G->vexnum-1];
cout<<ve[i]<<" ";
}
cout<<endl;
while(!T.empty()) //你拓扑排序 求最晚发生时间
       {
  temp=T.top();
  T.pop();
              p=G->vertices[temp].firstarc;
              while(p)
              {
                     j=search(G,p->adjvex);
                     if((vl[j]-p->v)<vl[temp])
                            vl[temp]=vl[j]-(p->v);
                     p=p->next;
              }
       }
       for(i=0;i<G->vexnum;i++)
       {    
              cout<<vl[i]<<" ";
       }
       cout<<endl;
       for(i=0;i<G->vexnum;i++)
       {
              p=G->vertices[i].firstarc;
              while(p)
              {
                     j=search(G,p->adjvex);
                     w=ve[i];
                     v=vl[j]-(p->v);
                     if(w==v)//判断是否为关键事件
cout<<G->vertices[i].vertex<<" "<<G->vertices[j].vertex<<endl;
                     p=p->next;
              }
       }
       return 1;
}
int main()
{
ALGraph *G;
G=(ALGraph*)malloc(sizeof(ALGraph));
GraphCreate(G);
// print(G);
// TopologicalSort(G);
CriticalPath(G);
system("pause");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值