1 25 package classycle.graph; 26 27 import java.util.Arrays ; 28 import java.util.Comparator ; 29 30 36 public class LongestWalkProcessor extends GraphProcessor { 37 38 protected void initializeProcessing(Vertex[] graph) {} 39 40 45 protected void processBefore(Vertex vertex) { 46 StrongComponent component = castAsStrongComponent(vertex); 47 component.setActive(true); 48 component.setLongestWalk(0); 49 } 50 51 58 protected void processArc(Vertex tail, Vertex head) { 59 StrongComponent t = castAsStrongComponent(tail); 60 StrongComponent h = castAsStrongComponent(head); 61 if (!h.isVisited()) { 62 process(h); 63 } else if (h.isActive()) { 64 throw new IllegalArgumentException (h + " is not a strong component."); 67 } 68 t.setLongestWalk(Math.max(t.getLongestWalk(), 1 + h.getLongestWalk())); 69 } 70 71 76 protected void processAfter(Vertex vertex) { 77 castAsStrongComponent(vertex).setActive(false); 78 } 79 80 84 protected void finishProcessing(Vertex[] graph) { 85 Arrays.sort(graph, new Comparator () { 86 public int compare(Object obj1, Object obj2) { 87 return ((StrongComponent) obj1).getLongestWalk() 88 - ((StrongComponent) obj2).getLongestWalk(); 89 } 90 }); 91 } 92 93 98 private StrongComponent castAsStrongComponent(Vertex vertex) { 99 if (vertex instanceof StrongComponent) { 100 return (StrongComponent) vertex; 101 } else { 102 throw new IllegalArgumentException ( 103 vertex + " is not an instance of StrongComponent"); 104 } 105 } 106 } | Popular Tags |