public void addNode(V node) { graph.addVertex(node); }
public void addNode(V node) { graph.addVertex(node); }
@Override public synchronized boolean addService(@Nonnull T service) { checkNotNull(service); log.debug("adding service"); if (graph.containsVertex(service)) { return false; } return graph.addVertex(service); }
/** * Returns the subtree of <code>tree</code> which is rooted at <code>root</code> as a <code>Forest</code>. * The tree returned is an independent entity, although it uses the same vertex and edge objects. * @param <V> the vertex type * @param <E> the edge type * @param forest the tree whose subtree is to be extracted * @param root the root of the subtree to be extracted * @return the subtree of <code>tree</code> which is rooted at <code>root</code> * @throws InstantiationException if a new tree of the same type cannot be created * @throws IllegalAccessException if a new tree of the same type cannot be created */ @SuppressWarnings("unchecked") public static <V,E> Tree<V,E> getSubTree(Forest<V,E> forest, V root) throws InstantiationException, IllegalAccessException { if (!forest.containsVertex(root)) throw new IllegalArgumentException("Specified tree does not contain the specified root as a vertex"); Forest<V,E> subforest = forest.getClass().newInstance(); subforest.addVertex(root); growSubTree(forest, subforest, root); return subforest.getTrees().iterator().next(); }
@Override public synchronized void createDependency(@Nonnull T dependant, @Nonnull T dependency, @Nonnull Dependency.Type type) { checkNotNull(dependant); checkNotNull(dependency); checkNotNull(type); if (hasDependency(dependant, dependency)) { throw new DuplicateDependencyException("There can only be one dependency between the same two services"); } if (!graph.containsVertex(dependant)) { graph.addVertex(dependant); } if (!graph.containsVertex(dependency)) { graph.addVertex(dependency); } ServiceEdge edge = new ServiceEdge(type); graph.addEdge(edge, dependant, dependency); if (detectCycle(dependant, dependency)) { throw new CycleDetectedException("Creating dependency from " + dependant + " to " + dependency + " has caused a loop"); } }
/** * Returns the subtree of <code>tree</code> which is rooted at * <code>root</code> as a <code>Forest<V,E></code>. The tree returned is an * independent entity, although it uses the same vertex and edge objects. * * @param <V> * the vertex type * @param <E> * the edge type * @param forest * the tree whose subtree is to be extracted * @param root * the root of the subtree to be extracted * @return the subtree of <code>tree</code> which is rooted at * <code>root</code> * @throws InstantiationException * if a new tree of the same type cannot be created * @throws IllegalAccessException */ public static <V, E> Tree<V, E> getSubTree(Forest<V, E> forest, V root) { if (!forest.containsVertex(root)) { throw new IllegalArgumentException( "Specified tree does not contain the specified root as a vertex"); } Forest<V, E> subforest = (Forest<V, E>) forest.newInstance(); subforest.addVertex(root); growSubTree(forest, subforest, root); return subforest.getTrees().iterator().next(); }
graph.addVertex("V0"); graph.addEdge(edgeFactory.get(), "V0", "V1"); graph.addEdge(edgeFactory.get(), "V0", "V2"); graph.addEdge(edgeFactory.get(), "V4", "V10"); graph.addVertex("A0"); graph.addEdge(edgeFactory.get(), "A0", "A1"); graph.addEdge(edgeFactory.get(), "A0", "A2"); graph.addEdge(edgeFactory.get(), "A0", "A3"); graph.addVertex("B0"); graph.addEdge(edgeFactory.get(), "B0", "B1"); graph.addEdge(edgeFactory.get(), "B0", "B2");
private void createTree() { graph.addVertex("V0"); graph.addEdge(edgeFactory.get(), "V0", "V1"); graph.addEdge(edgeFactory.get(), "V0", "V2"); graph.addEdge(edgeFactory.get(), "V1", "V4"); graph.addEdge(edgeFactory.get(), "V2", "V3"); graph.addEdge(edgeFactory.get(), "V2", "V5"); graph.addEdge(edgeFactory.get(), "V4", "V6"); graph.addEdge(edgeFactory.get(), "V4", "V7"); graph.addEdge(edgeFactory.get(), "V3", "V8"); graph.addEdge(edgeFactory.get(), "V6", "V9"); graph.addEdge(edgeFactory.get(), "V4", "V10"); graph.addVertex("A0"); graph.addEdge(edgeFactory.get(), "A0", "A1"); graph.addEdge(edgeFactory.get(), "A0", "A2"); graph.addEdge(edgeFactory.get(), "A0", "A3"); graph.addVertex("B0"); graph.addEdge(edgeFactory.get(), "B0", "B1"); graph.addEdge(edgeFactory.get(), "B0", "B2"); graph.addEdge(edgeFactory.get(), "B1", "B4"); graph.addEdge(edgeFactory.get(), "B2", "B3"); graph.addEdge(edgeFactory.get(), "B2", "B5"); graph.addEdge(edgeFactory.get(), "B4", "B6"); graph.addEdge(edgeFactory.get(), "B4", "B7"); graph.addEdge(edgeFactory.get(), "B3", "B8"); graph.addEdge(edgeFactory.get(), "B6", "B9"); }
graph.addVertex("V0"); graph.addEdge(edgeFactory.get(), "V0", "V1"); graph.addEdge(edgeFactory.get(), "V0", "V2"); graph.addEdge(edgeFactory.get(), "V4", "V10"); graph.addVertex("A0"); graph.addEdge(edgeFactory.get(), "A0", "A1"); graph.addEdge(edgeFactory.get(), "A0", "A2"); graph.addEdge(edgeFactory.get(), "A0", "A3"); graph.addVertex("B0"); graph.addEdge(edgeFactory.get(), "B0", "B1"); graph.addEdge(edgeFactory.get(), "B0", "B2");
graph.addVertex("V0"); graph.addEdge(edgeFactory.get(), "V0", "V1"); graph.addEdge(edgeFactory.get(), "V0", "V2"); graph.addEdge(edgeFactory.get(), "V4", "V10"); graph.addVertex("A0"); graph.addEdge(edgeFactory.get(), "A0", "A1"); graph.addEdge(edgeFactory.get(), "A0", "A2"); graph.addEdge(edgeFactory.get(), "A0", "A3"); graph.addVertex("B0"); graph.addEdge(edgeFactory.get(), "B0", "B1"); graph.addEdge(edgeFactory.get(), "B0", "B2");
@SuppressWarnings({ "unchecked", "rawtypes" }) public void collapse(Layout layout, Forest tree, Object subRoot) throws InstantiationException, IllegalAccessException { // get a sub tree from subRoot Forest subTree = TreeUtils.getSubTree(tree, subRoot); Object parent = null; Object edge = null; if(tree.getPredecessorCount(subRoot) > 0) { parent = tree.getPredecessors(subRoot).iterator().next(); edge = tree.getInEdges(subRoot).iterator().next(); } tree.removeVertex(subRoot); if(parent != null) { tree.addEdge(edge, parent, subTree); } else { tree.addVertex(subTree); } layout.setLocation(subTree, (Point2D)layout.apply(subRoot)); }
if(leftovers.size() > 0) { V anotherRoot = leftovers.iterator().next(); forest.addVertex(anotherRoot); updateForest(forest.getVertices(), unfinishedEdges);
this.forest.addVertex(root);
if(leftovers.size() > 0) { V anotherRoot = leftovers.iterator().next(); forest.addVertex(anotherRoot); updateForest(forest.getVertices(), unfinishedEdges);
this.forest.addVertex(root);
public static <V,E> void addFromSubTree(Forest<V,E> tree, Forest<V,E> subTree, E edge, V parent, V root) { // add edge connecting parent and root to tree if(edge != null && parent != null) { tree.addEdge(edge, parent, root); } else { tree.addVertex(root); } Collection<E> outEdges = subTree.getOutEdges(root); for(E e : outEdges) { V opposite = subTree.getOpposite(root, e); addFromSubTree(tree, subTree, e, root, opposite); } } }
this.forest.addVertex(root);
/** * Creates a minimum spanning forest from the supplied graph, populating the * supplied Forest, which must be empty. * If the supplied root is null, or not present in the Graph, * then an arbitrary Graph vertex will be selected as the root. * If the Minimum Spanning Tree does not include all vertices of the * Graph, then a leftover vertex is selected as a root, and another * tree is created * @param graph the Graph to find MST in * @param forest the Forest to populate. Must be empty * @param root first Tree root, may be null */ @SuppressWarnings("unchecked") public MinimumSpanningForest(Graph<V, E> graph, Forest<V,E> forest, V root) { if(forest.getVertexCount() != 0) { throw new IllegalArgumentException("Supplied Forest must be empty"); } this.graph = graph; this.forest = forest; this.weights = (Function<E, Double>) Functions.constant(1.0); Set<E> unfinishedEdges = new HashSet<E>(graph.getEdges()); if(graph.getVertices().contains(root)) { this.forest.addVertex(root); } updateForest(forest.getVertices(), unfinishedEdges); }
/** * Creates a minimum spanning forest from the supplied graph, populating the * supplied Forest, which must be empty. * If the supplied root is null, or not present in the Graph, * then an arbitrary Graph vertex will be selected as the root. * If the Minimum Spanning Tree does not include all vertices of the * Graph, then a leftover vertex is selected as a root, and another * tree is created * @param graph the Graph to find MST in * @param forest the Forest to populate. Must be empty * @param root first Tree root, may be null */ @SuppressWarnings("unchecked") public MinimumSpanningForest(Graph<V, E> graph, Forest<V,E> forest, V root) { if(forest.getVertexCount() != 0) { throw new IllegalArgumentException("Supplied Forest must be empty"); } this.graph = graph; this.forest = forest; this.weights = LazyMap.decorate(new HashMap<E,Double>(), new ConstantTransformer(1.0)); Set<E> unfinishedEdges = new HashSet<E>(graph.getEdges()); if(graph.getVertices().contains(root)) { this.forest.addVertex(root); } updateForest(forest.getVertices(), unfinishedEdges); }
Set<E> unfinishedEdges = new HashSet<E>(graph.getEdges()); if (graph.getVertices().contains(root)) { this.forest.addVertex(root);