public static Digraph merge(Digraph destination, DigraphIteration graphToMerge) { for (Iterator i = graphToMerge.vertexIterator(); i.hasNext(); ) { destination.addVertex(i.next()); } for (ArcIterator i = graphToMerge.arcIterator(); i.hasNext(); ) { Object arc = i.next(); Object origin = i.getOrigin(); Object dst = i.getDestination(); destination.putArc(origin, dst, arc); } return destination; }
public static Map computeLevels( Map vertexLevelMap, DigraphIteration digraph, Object root, boolean longest) { if (vertexLevelMap == null) vertexLevelMap = new HashMap(); MutableInteger rootLevel = (MutableInteger) vertexLevelMap.get(root); if (rootLevel == null) { rootLevel = new MutableInteger(0); vertexLevelMap.put(root, rootLevel); } for (ArcIterator i = digraph.outgoingIterator(root); i.hasNext(); ) { i.next(); Object child = i.getDestination(); int childLevelCandidate = rootLevel.intValue() + 1; MutableInteger childLevel = (MutableInteger) vertexLevelMap.get(child); if (childLevel == null) { childLevel = new MutableInteger(childLevelCandidate); vertexLevelMap.put(child, childLevel); computeLevels(vertexLevelMap, digraph, child, longest); } else if ((longest && childLevel.intValue() < childLevelCandidate) || (!longest && childLevel.intValue() > childLevelCandidate)) { childLevel.setValue(childLevelCandidate); computeLevels(vertexLevelMap, digraph, child, longest); } } return vertexLevelMap; }
public static List findCycles(DigraphIteration graph) { ArrayStack stack = new ArrayStack(); ArrayStack path = new ArrayStack(); Set seen = new HashSet(); List cycles = new ArrayList(); Iterator vertexIterator = graph.vertexIterator(); while (vertexIterator.hasNext()) { while (vertexIterator.hasNext()) { Object vertex = vertexIterator.next(); if (seen.add(vertex)) { stack.push(graph.outgoingIterator(vertex)); path.push(vertex); break; } } while (!stack.isEmpty()) { ArcIterator i = (ArcIterator) stack.peek(); Object origin = i.getOrigin(); boolean subtreeIsTraversed = true; while (i.hasNext()) { i.next(); Object dst = i.getDestination(); int index = path.indexOf(dst); if (index < 0) { seen.add(dst); stack.push(graph.outgoingIterator(dst)); path.push(dst); subtreeIsTraversed = false; break; } else { cycles.add(new ArrayList(path.subList(index, path.size()))); } } if (subtreeIsTraversed) { stack.pop(); path.pop(); } } } return cycles; }
public static Digraph transform( Digraph result, DigraphIteration source, Transformer vertexTransform, Transformer arcTransform) { for (Iterator i = new TransformIterator(source.vertexIterator(), vertexTransform); i.hasNext(); ) { result.addVertex(i.next()); } for (ArcIterator i = new TransformArcIterator(source.arcIterator(), vertexTransform, arcTransform); i.hasNext(); ) { Object arc = i.next(); Object origin = i.getOrigin(); Object dst = i.getDestination(); result.putArc(origin, dst, arc); } return result; }
public static Map shiftLevelsDown(Map vertexLevelMap, DigraphIteration digraph, Object root) { int minChildLevel = Integer.MAX_VALUE; for (ArcIterator i = digraph.outgoingIterator(root); i.hasNext(); ) { i.next(); Object child = i.getDestination(); shiftLevelsDown(vertexLevelMap, digraph, child); MutableInteger childLevel = (MutableInteger) vertexLevelMap.get(child); minChildLevel = (minChildLevel <= childLevel.intValue() ? minChildLevel : childLevel.intValue()); } if (minChildLevel != Integer.MAX_VALUE) { MutableInteger rootLevel = (MutableInteger) vertexLevelMap.get(root); rootLevel.setValue(minChildLevel - 1); } return vertexLevelMap; }