算法说明:
求关键路径
1.输入e条弧<j,k>,建立AOE网的存储结构,顶点数为n,顶点从0开始编号,设源点为v0、汇点为vn-1
2.从源点v0出发,令ve[0]=0,按拓扑有序求其余各顶点事件的最早发生时间ve[i](1≤i≤n-1)。如果得到的拓扑有序序列中顶点个数小于网中的顶点数,则说明网中存在环,不能求关键路径,算法终止;否则执行步骤3;
3.从汇点vn-1出发,令vl[n-1]=ve[n-1],按逆拓扑有序求其余各顶点事件的最迟发生时间vl[i](2≤i≤n-2);
4.根据各顶点的ve、vl值,求每条弧s的最早开始时间e(s)和最迟开始时间l(s),若某条弧满足条件e(s)=l(s),则为关键活动。
1.自己定义栈
package com.cn.graph;
import java.util.Arrays;
class Stack {
private Object[] elementData;
private int currentCapacity;
private int base;
private int top;
private int capacityIncrements;
private int initCapacity;
public Stack(){
base = 0;
top = 0;
initCapacity = 10;
capacityIncrements = 10;
currentCapacity = initCapacity;
elementData = new Object[initCapacity];
}
public void push(Object obj) {
if (top < currentCapacity) {
elementData[top++] = obj;
}
else {
//扩容
currentCapacity += capacityIncrements;
ensureCapacityHelper();
elementData[top++] = obj;
}
}
private void ensureCapacityHelper() {
elementData = Arrays.copyOf(elementData, currentCapacity);
}
public int getSize() {
return top;
}
public Object pop() throws Exception {
if (top > base) {
Object obj = elementData[top - 1];
elementData[top--] = null;
return obj;
} else {
throw new ArrayIndexOutOfBoundsException("stack is null");
}
}
public String toString() {
String str = "";
for (int i = 0; i < top; i ++) {
str += elementData[i].toString() + " ";
}
ret