标签:
import java.util.*;
/**
* 关键路径:路径长度最长的路径叫做关键路径。
* 关键活动:活动ai的最早和最迟开始时间相同的活动叫做关键活动。
* (1)输入e条弧<j,k>,建立AOE-网的存储结构
* (2)从源点v0出发,令ve[0]=0,按拓扑有序求其余各顶点的最早发生时间ve[i](1<= i <=n-1)>。如果得到的拓扑有序序列中顶点个数小于王忠顶点数n,
* 说明网中存在环,不能求关键路径,算法终止;否则执行步骤(3)。
* (3)从汇点vn出发,vl[n-1] = ve[n-1],按逆拓扑有序求其余各顶点的最迟发生时间vl[i](n-2>= i >=2)
* (4)根据各顶点的ve和vl值,求每条弧s的最早开始时间e(s)和最迟开始时间l(s),若某条弧满足条件e(s)=l(s),则为关键活动。
*/
public class TopologicalTest {
private static final int V_COUNT = 6;
public static void main(String[] args) {
//初始化节点
Topological topological1 = new Topological("C1", 0, new ArrayList<Topological>());
Topological topological2 = new Topological("C2", 1, new ArrayList<Topological>());
Topological topological3 = new Topological("C3", 1, new ArrayList<Topological>());
Topological topological4 = new Topological("C4", 2, new ArrayList<Topological>());
Topological topological5 = new Topological("C5", 1, new ArrayList<Topological>());
Topological topological6 = new Topological("C6", 3, new ArrayList<Topological>());
//将节点添加后驱
List<Topological> list = topological1.getTopologicals();
list.add(topological2);
list.add(topological3);
topological1.setTopologicals(list);
list = topological2.getTopologicals();
list.add(topological4);
list.add(topological5);
topological2.setTopologicals(list);
list = topological3.getTopologicals();
list.add(topological4);
list.add(topological6);
topological3.setTopologicals(list);
list = topological4.getTopologicals();
list.add(topological6);
topological4.setTopologicals(list);
list = topological5.getTopologicals();
list.add(topological6);
topological5.setTopologicals(list);
//将图中的节点放入列表
List<Topological> topologicals = new ArrayList<Topological>();
topologicals.add(topological1);
topologicals.add(topological2);
topologicals.add(topological3);
topologicals.add(topological4);
topologicals.add(topological5);
topologicals.add(topological6);
//定义一个列表来存放入度为零的节点
List<Topological> topologicalListForReturn = new ArrayList<Topological>();
int i = 0;
while(i++ <= V_COUNT) {
for(Topological each : topologicals) {
//判断一下是否已经在入度为0的列表中
//如果是,则返回,判断下一个
if(topologicalListForReturn.contains(each)) {
continue;
}
if(each.getIndegree() == 0) {
for(Topological topological : each.getTopologicals()) {
topological.setIndegree(topological.getIndegree() - 1);
}
topologicalListForReturn.add(each);
break;
}
}
}
//如果入度为0的节点数少于图总节点数,则存在环
if(topologicalListForReturn.size() < topologicals.size()) {
System.out.println("此图存在环");
System.exit(0);
}
//输出拓扑排序序列
for (Topological each : topologicalListForReturn) {
System.out.print(each.getName() + " ");
}
System.out.println();
//初始化二维数组
int p[][] = {{0,3,2,0,0,0},{0,0,0,2,3,0},{0,0,0,4,0,3},{0,0,0,0,0,2},{0,0,0,0,0,1},{0,0,0,0,0,0}};
//最早和最晚发生时间
int[] ve = new int[V_COUNT];
int[] vl = new int[V_COUNT];
//初始化最早发生时间
for(int j = 0; j < V_COUNT; j++) {
ve[j] = 0;
}
//初始化最晚发生时间
for(int j = 0; j < V_COUNT; j++) {
vl[j] = Integer.MAX_VALUE;
}
//开始给每个节点的最早发生时间赋值
for(int j = 0; j < V_COUNT; j++) {
for(int k = j +1; k < V_COUNT; k++) {
if(p[j][k] != 0 && ve[k] < ve[j] + p[j][k]) {
ve[k] = ve[j] + p[j][k];
}
}
}
//开始给每个节点的最晚发生时间赋值
vl[V_COUNT -1] = ve[V_COUNT -1];
for(int j = V_COUNT - 1; j > 0; j--) {
for(int k = j - 1; k >= 0; k--) {
if(p[k][j] != 0 && vl[k] > vl[j] - p[k][j]) {
vl[k] = vl[j] - p[k][j];
}
}
}
System.out.print("从c1-c6的最早开始时间为:");
for(int j = 0; j < V_COUNT; j++) {
System.out.print(ve[j] + " ");
}
System.out.println();
System.out.print("从c1-c6的最晚开始时间为:");
for(int j = 0; j < V_COUNT; j++) {
System.out.print(vl[j] + " ");
}
System.out.println();
//求关键路径
System.out.print("关键路径为:");
for(int j = 0; j < V_COUNT; j++) {
if(ve[j] == vl[j]) {
System.out.println(topologicals.get(j).getName() + " ");
}
}
}
}
问题:这里的拓扑有序序列感觉没怎么用,估计是还没有理解透彻,这里的最早和最晚开始时间都是基于拓扑有序序列?
标签:
原文地址:http://www.cnblogs.com/mucheng/p/5766059.html