/** * 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 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()); }