principle
Critical path refers to the logical path with the longest delay from input to output in the design. Optimizing critical path is an effective method to improve design speed. Generally, the delay from input to output depends on the path with the maximum delay, and is independent of other paths with small delay.
understand
The critical path is the path that takes the most time from the start point to the end point
Critical path definition
Here are some definitions of critical path
ve: earliest start time of event (earliest start time of point)
vl: latest start time of the event (last start time of the point)
ee: earliest start time of activity (earliest start time of edge)
el: latest start time of activity (latest start time of edge)
Key event: ve == vl
Key activities: el == ee
ve: the earliest start time of events is obtained by topology sorting
In fact, the most important thing is that if a node has an in degree, the earliest start time of the point consists of the earliest start time of the source point of the in degree + the edge length. In fact, it is an iteration (if there are multiple, the maximum time is taken) (the first is 0)
vl: the latest start time of an event refers to the time obtained in the reverse order of topological sorting
In fact, the most important thing is that if a node has an exit degree, the latest start time of the point is determined by the latest start time of the target node of the edge of the exit degree - the weight of the edge (if there are multiple, take the minimum time) (the default value of the beginning is the earliest start time of the end point)
ee: earliest start time of the activity
The earliest start time of an activity is determined by the earliest start event of the event at the point of departure.
el: latest start time of the activity
The latest start time of the activity is the latest start time of the end point of the edge - the weight of the edge
code
package com.company; import java.util.*; class AoeNode{ String name; ArcAoeNode firstArc; public AoeNode(String name) { this.name = name; } } class ArcAoeNode{ int nextNode; int weight; String name; ArcAoeNode nextArc; public ArcAoeNode(int nextNode, int weight,String name) { this.nextNode = nextNode; this.weight = weight; this.name = name; } } public class CriticalPath { List<AoeNode> aoeNodes; List<ArcAoeNode> arcAoeNodes; /** ve Earliest occurrence time of the event */ int[] ve; /** vl Latest occurrence time of the event */ int[] vl; /** ee Earliest occurrence time of activity */ int[] ee; /** el Latest occurrence time of activity */ int[] el; /** Traversal stack */ Stack<Integer> viewStack = new Stack<>(); public CriticalPath(List<AoeNode> aoeNodes) { this.aoeNodes = aoeNodes; ve = new int[aoeNodes.size()]; vl = new int[aoeNodes.size()]; getAllArc(); ee = new int[arcAoeNodes.size()]; el = new int[arcAoeNodes.size()]; } public void getAllArc(){ arcAoeNodes = new ArrayList<>(); for(AoeNode aoeNode: aoeNodes){ for(ArcAoeNode arcAoeNode=aoeNode.firstArc;arcAoeNode!=null;arcAoeNode=arcAoeNode.nextArc){ arcAoeNodes.add(arcAoeNode); } } } public int getin(int nodeIndex){ int countIn=0; for(AoeNode aoe: aoeNodes){ ArcAoeNode arcAoeNode = aoe.firstArc; while (arcAoeNode!=null){ if(arcAoeNode.nextNode == nodeIndex ){ countIn+=1; } arcAoeNode = arcAoeNode.nextArc; } } return countIn; } // topologic public boolean topologic(){ Queue<Integer> queue = new LinkedList<>(); int[] book = new int[aoeNodes.size()]; // init for(int i = 0; i< aoeNodes.size(); i++){ book[i] = getin(i); if(book[i]==0){ queue.offer(i); } } int visitLen = 0; while (!queue.isEmpty()){ int poll = queue.poll(); viewStack.push(poll); visitLen++; ArcAoeNode arcAoeNode = aoeNodes.get(poll).firstArc; while (arcAoeNode!=null){ int node = arcAoeNode.nextNode; ve[node] = Math.max(ve[node],ve[poll]+ arcAoeNode.weight ); book[node]--; if(book[node]==0){ queue.offer(node); } arcAoeNode = arcAoeNode.nextArc; } } return visitLen== aoeNodes.size(); } public void criticalPath(){ if(!topologic()){ System.out.println("It's a ring diagram with no critical path"); return; } // vl // vl init for(int i=0;i<ve.length;i++){ vl[i] = ve[ve.length-1]; } while (!viewStack.isEmpty()){ int popValue = viewStack.pop(); ArcAoeNode arcAoeNode = aoeNodes.get(popValue).firstArc; while (arcAoeNode!=null){ int nextNode = arcAoeNode.nextNode; vl[popValue] = Math.min(vl[popValue],vl[nextNode]-arcAoeNode.weight); arcAoeNode = arcAoeNode.nextArc; } } // el ee int arcCount = 0; for(int i=0;i<aoeNodes.size();i++){ ArcAoeNode arcAoeNode = aoeNodes.get(i).firstArc; while (arcAoeNode!=null){ int nextNode = arcAoeNode.nextNode; ee[arcCount]=ve[i]; el[arcCount]=vl[nextNode]-arcAoeNode.weight; arcCount++; arcAoeNode = arcAoeNode.nextArc; } } // Key events System.out.println("Key events"); for(int i=0;i<aoeNodes.size();i++){ if(vl[i]==ve[i]){ System.out.println(aoeNodes.get(i).name); } } // Key activities System.out.println("Key activities"); for(int i=0;i<ee.length;i++){ if(ee[i]==el[i]){ System.out.println(arcAoeNodes.get(i).name); } } } } class testCriticalPath{ public static void main(String[] args) { List<AoeNode> list = new ArrayList<>(); // Add people for(int i=0;i<10;i++){ list.add(new AoeNode("V"+i)); } // Join edge // v0 AoeNode aoeNode = list.get(0); aoeNode.firstArc = new ArcAoeNode(1,3,"a0"); aoeNode.firstArc.nextArc = new ArcAoeNode(2,4,"a1"); // v1 aoeNode = list.get(1); aoeNode.firstArc = new ArcAoeNode(3,5,"a2"); aoeNode.firstArc.nextArc = new ArcAoeNode(4,6,"a3"); // v2 aoeNode = list.get(2); aoeNode.firstArc = new ArcAoeNode(3,8,"a4"); aoeNode.firstArc.nextArc = new ArcAoeNode(5,7,"a5"); // v3 aoeNode = list.get(3); aoeNode.firstArc = new ArcAoeNode(4,3,"a6"); // v4 aoeNode = list.get(4); aoeNode.firstArc = new ArcAoeNode(6,9,"a7"); aoeNode.firstArc.nextArc = new ArcAoeNode(7,4,"a8"); // v5 aoeNode = list.get(5); aoeNode.firstArc = new ArcAoeNode(7,6,"a9"); // v6 aoeNode = list.get(6); aoeNode.firstArc = new ArcAoeNode(9,2,"a10"); // v7 aoeNode = list.get(7); aoeNode.firstArc = new ArcAoeNode(8,5,"a11"); // v8 aoeNode = list.get(8); aoeNode.firstArc = new ArcAoeNode(9,3,"a12"); // Completion of drawings CriticalPath criticalPath = new CriticalPath(list); criticalPath.criticalPath(); } }