/** * Generate the topological ordering for the given jobGraph. * * @param jobgraph the job graph * @return a {@link List} of the {@link IndigoJob} in topological order * @throws IllegalArgumentException if the graph has cycles (hence no topological order exists). */ protected List<IndigoJob> getJobsTopologicalOrder(Map<String, IndigoJob> jobgraph) { DefaultDirectedGraph<IndigoJob, DefaultEdge> graph = new DefaultDirectedGraph<IndigoJob, DefaultEdge>(DefaultEdge.class); for (IndigoJob job : jobgraph.values()) { graph.addVertex(job); } for (IndigoJob job : jobgraph.values()) { for (IndigoJob parent : job.getParents()) { graph.addEdge(parent, job); // job depends on parent } } LOG.debug("IndigoJob graph: {}", graph.toString()); // Are there cycles in the dependencies. CycleDetector<IndigoJob, DefaultEdge> cycleDetector = new CycleDetector<IndigoJob, DefaultEdge>(graph); if (cycleDetector.detectCycles()) { LOG.error("Job graph has cycles!"); throw new IllegalArgumentException( String.format( "Failed to generate topological order for a graph with cycles: <%s>", graph)); } TopologicalOrderIterator<IndigoJob, DefaultEdge> orderIterator = new TopologicalOrderIterator<IndigoJob, DefaultEdge>(graph); List<IndigoJob> topoOrder = Lists.newArrayList(orderIterator); LOG.debug("IndigoJob topological order: {}", topoOrder); return topoOrder; }
public void testStronglyConnected4() { DefaultDirectedGraph<Integer, String> graph = new DefaultDirectedGraph<Integer, String>( new EdgeFactory<Integer, String>() { public String createEdge(Integer from, Integer to) { return (from + "->" + to).intern(); } }); new RingGraphGenerator<Integer, String>(3) .generateGraph( graph, new VertexFactory<Integer>() { private int i = 0; public Integer createVertex() { return i++; } }, null); StrongConnectivityInspector<Integer, String> sc = new StrongConnectivityInspector<Integer, String>(graph); Set<Set<Integer>> expected = new HashSet<Set<Integer>>(); expected.add(graph.vertexSet()); assertEquals(expected, new HashSet<Set<Integer>>(sc.stronglyConnectedSets())); }
public ArrayList<tsNode> getNodeList() { ArrayList<tsNode> result = new ArrayList<tsNode>(); for (tsNode v : rawGraph.vertexSet()) { result.add(v); } return result; }
public static String run(DefaultDirectedGraph<CFGHandle, DefaultEdge> graph) { StringBuilder result = new StringBuilder(); result.append("digraph stronglyConnected {\n"); for (final CFGHandle cfgh : graph.vertexSet()) result.append('"').append(cfgh.toString()).append("\"\n"); for (DefaultEdge edge : graph.edgeSet()) result .append('"') .append(graph.getEdgeSource(edge)) .append("\" -> \"") .append(graph.getEdgeTarget(edge)) .append("\";\n"); result.append('}'); return result.toString(); }
public DirectedGraph<tsNode, DefaultEdge> getExpandedGraph() { expandedGraph = new DefaultDirectedGraph<tsNode, DefaultEdge>(DefaultEdge.class); Collection<tsNode> collection = rawGraph.vertexSet(); tsNode[] tsArray = collection.toArray(new tsNode[0]); int starterIndex = -1; for (int i = 0; i < tsArray.length; i++) { if (tsArray[i].isStart()) { starterIndex = 1; } } if (starterIndex == -1) { System.out.println("Graph has no starting node"); return null; } expandedGraph.addVertex(tsArray[starterIndex]); Collection<DefaultEdge> startEdges = rawGraph.edgesOf(tsArray[starterIndex]); // for (DefaultEdge edge : startEdges) { // expandedGraph.addVertex((tsNode)edge.getTarget()); // } return null; }
public TsGraph(ArrayList<tsNode> nodes) { rawGraph = new DefaultDirectedGraph<tsNode, DefaultEdge>(DefaultEdge.class); // TODOo make sure number of nodes is high enough // TODO is there an upper limit? for (int i = 0; i < nodes.size(); i++) { rawGraph.addVertex(nodes.get(i)); } // First generation nodes for (int i = 1; i < nodes.size(); i++) { rawGraph.addEdge(nodes.get(0), nodes.get(i)); } // Brute force connect all non zero nodes to each other for (int i = 1; i < nodes.size(); i++) { for (int j = 1; j < nodes.size(); j++) { if (i != j) { // TODO make this a weighted edge based on distance matrix rawGraph.addEdge(nodes.get(i), nodes.get(j)); } } } }
/** Rigourous Test :-) */ public void testBFS() { DefaultDirectedGraph<Vertex, Double> graph = new DefaultDirectedGraph<Vertex, Double>(Double.class); Vertex v = null; for (int i = 0; i < this.numOfVertices; i++) { v = new Vertex("v" + i); graph.addVertex(v); } // use last vertex as start point of search Vertex lastVertex = v; Set<Vertex> vertices = graph.vertexSet(); // add edges to graph Random rand = new Random(); Double nextEdge = 0.0; for (Vertex start : vertices) for (Vertex end : vertices) if (!start.equals(end) && !graph.containsEdge(start, end)) { nextEdge = rand.nextDouble(); if (nextEdge > 0.9) graph.addEdge(start, end, nextEdge); } System.out.println("Vertices: " + vertices.size()); System.out.println("Edges: " + graph.edgeSet().size()); BreadthFirstIterator<Vertex, Double> bfsIter = new BreadthFirstIterator<Vertex, Double>(graph, lastVertex); BFS<Double> bfs = new BFS<Double>(graph); Vertex[] bfsResult = bfs.breadthFirstTree(lastVertex); for (int i = 0; i < this.numOfVertices && bfsIter.hasNext(); i++) { System.out.println(bfsResult[i] + " " + bfsIter.next()); } }
@Test public void testAutomorphism() { /* * v1-----v2 * \ / * \ / * v3 */ SimpleGraph<String, DefaultEdge> g1 = new SimpleGraph<String, DefaultEdge>(DefaultEdge.class); String v1 = "v1", v2 = "v2", v3 = "v3"; g1.addVertex(v1); g1.addVertex(v2); g1.addVertex(v3); g1.addEdge(v1, v2); g1.addEdge(v2, v3); g1.addEdge(v3, v1); VF2GraphIsomorphismInspector<String, DefaultEdge> vf2 = new VF2GraphIsomorphismInspector<String, DefaultEdge>(g1, g1); Iterator<GraphMapping<String, DefaultEdge>> iter = vf2.getMappings(); Set<String> mappings = new HashSet<String>( Arrays.asList( "[v1=v1 v2=v2 v3=v3]", "[v1=v1 v2=v3 v3=v2]", "[v1=v2 v2=v1 v3=v3]", "[v1=v2 v2=v3 v3=v1]", "[v1=v3 v2=v1 v3=v2]", "[v1=v3 v2=v2 v3=v1]")); assertEquals(true, mappings.remove(iter.next().toString())); assertEquals(true, mappings.remove(iter.next().toString())); assertEquals(true, mappings.remove(iter.next().toString())); assertEquals(true, mappings.remove(iter.next().toString())); assertEquals(true, mappings.remove(iter.next().toString())); assertEquals(true, mappings.remove(iter.next().toString())); assertEquals(false, iter.hasNext()); /* * 1 ---> 2 <--- 3 */ DefaultDirectedGraph<Integer, DefaultEdge> g2 = new DefaultDirectedGraph<Integer, DefaultEdge>(DefaultEdge.class); g2.addVertex(1); g2.addVertex(2); g2.addVertex(3); g2.addEdge(1, 2); g2.addEdge(3, 2); VF2GraphIsomorphismInspector<Integer, DefaultEdge> vf3 = new VF2GraphIsomorphismInspector<Integer, DefaultEdge>(g2, g2); Iterator<GraphMapping<Integer, DefaultEdge>> iter2 = vf3.getMappings(); Set<String> mappings2 = new HashSet<String>(Arrays.asList("[1=1 2=2 3=3]", "[1=3 2=2 3=1]")); assertEquals(true, mappings2.remove(iter2.next().toString())); assertEquals(true, mappings2.remove(iter2.next().toString())); assertEquals(false, iter2.hasNext()); }