List of usage examples for com.google.common.graph Network incidentNodes
Set<N> incidentNodes(Object edge);
From source file:edu.uci.ics.jung.algorithms.shortestpath.ShortestPathUtils.java
/** * Returns a <code>List</code> of the edges on the shortest path from <code>source</code> to * <code>target</code>, in order of their occurrence on this path. * * @param graph the graph for which the shortest path is defined * @param sp holder of the shortest path information * @param source the node from which the shortest path is measured * @param target the node to which the shortest path is measured * @param <N> the node type/*from w w w. jav a 2 s .c o m*/ * @param <E> the edge type * @return the edges on the shortest path from {@code source} to {@code target}, in the order * traversed */ public static <N, E> List<E> getPath(Network<N, E> graph, ShortestPath<N, E> sp, N source, N target) { LinkedList<E> path = new LinkedList<E>(); Map<N, E> incomingEdges = sp.getIncomingEdgeMap(source); if (incomingEdges.isEmpty() || incomingEdges.get(target) == null) { return path; } N current = target; while (!current.equals(source)) { E incoming = incomingEdges.get(current); path.addFirst(incoming); current = graph.incidentNodes(incoming).adjacentNode(current); } return path; }
From source file:org.apache.beam.runners.dataflow.worker.graph.Networks.java
public static <N, E> String toDot(Network<N, E> network) { StringBuilder builder = new StringBuilder(); builder.append("digraph network {\n"); Map<N, String> nodeName = Maps.newIdentityHashMap(); network.nodes().forEach(node -> nodeName.put(node, "n" + nodeName.size())); for (Entry<N, String> nodeEntry : nodeName.entrySet()) { builder.append(String.format(" %s [fontname=\"Courier New\" label=\"%s\"];\n", nodeEntry.getValue(), escapeDot(nodeEntry.getKey().toString()))); }/*w w w . j a v a 2 s. c o m*/ for (E edge : network.edges()) { EndpointPair<N> endpoints = network.incidentNodes(edge); builder.append(String.format(" %s -> %s [fontname=\"Courier New\" label=\"%s\"];\n", nodeName.get(endpoints.source()), nodeName.get(endpoints.target()), escapeDot(edge.toString()))); } builder.append("}"); return builder.toString(); }
From source file:org.apache.beam.runners.core.construction.graph.Networks.java
/** * Return a set of nodes in sorted topological order. * * <p>Note that back edges within directed graphs are "broken" returning a topological order for a * directed acyclic network which approximates the original network. * * <p>Nodes will be considered in the order specified by the {@link * ElementOrder#sorted(Comparator) sorted ElementOrder} created with the provided comparator. */// ww w . j a va2s .c o m public static <NodeT, EdgeT> Iterable<NodeT> topologicalOrder(Network<NodeT, EdgeT> network, Comparator<NodeT> nodeOrder) { // Copy the characteristics of the network to ensure that the result network can represent the // original network, just with the provided suborder MutableNetwork<NodeT, EdgeT> orderedNetwork = NetworkBuilder.from(network) .nodeOrder(ElementOrder.sorted(nodeOrder)).build(); for (NodeT node : network.nodes()) { orderedNetwork.addNode(node); } for (EdgeT edge : network.edges()) { EndpointPair<NodeT> incident = network.incidentNodes(edge); orderedNetwork.addEdge(incident.source(), incident.target(), edge); } return computeTopologicalOrder(orderedNetwork); }
From source file:org.apache.beam.runners.core.construction.graph.Networks.java
public static <NodeT, EdgeT> String toDot(Network<NodeT, EdgeT> network) { StringBuilder builder = new StringBuilder(); builder.append(String.format("digraph network {%n")); Map<NodeT, String> nodeName = Maps.newIdentityHashMap(); network.nodes().forEach(node -> nodeName.put(node, "n" + nodeName.size())); for (Entry<NodeT, String> nodeEntry : nodeName.entrySet()) { builder.append(String.format(" %s [fontname=\"Courier New\" label=\"%s\"];%n", nodeEntry.getValue(), escapeDot(nodeEntry.getKey().toString()))); }//www . j av a2 s .c o m for (EdgeT edge : network.edges()) { EndpointPair<NodeT> endpoints = network.incidentNodes(edge); builder.append(String.format(" %s -> %s [fontname=\"Courier New\" label=\"%s\"];%n", nodeName.get(endpoints.source()), nodeName.get(endpoints.target()), escapeDot(edge.toString()))); } builder.append("}"); return builder.toString(); }
From source file:org.apache.beam.runners.dataflow.worker.graph.Networks.java
/** * Returns a list of all distinct paths from roots of the network to leaves. The list can be in * arbitrary orders and can contain duplicate paths if there are multiple edges from two nodes. *///from w ww.ja v a 2 s. c o m public static <NodeT, EdgeT> List<List<NodeT>> allPathsFromRootsToLeaves(Network<NodeT, EdgeT> network) { ArrayDeque<List<NodeT>> paths = new ArrayDeque<>(); // Populate the list with all roots for (NodeT node : network.nodes()) { if (network.inDegree(node) == 0) { paths.add(ImmutableList.of(node)); } } List<List<NodeT>> distinctPathsFromRootsToLeaves = new ArrayList<>(); while (!paths.isEmpty()) { List<NodeT> path = paths.removeFirst(); NodeT lastNode = path.get(path.size() - 1); if (network.outDegree(lastNode) == 0) { distinctPathsFromRootsToLeaves.add(new ArrayList<>(path)); } else { for (EdgeT edge : network.outEdges(lastNode)) { paths.addFirst(ImmutableList.<NodeT>builder().addAll(path) .add(network.incidentNodes(edge).target()).build()); } } } return distinctPathsFromRootsToLeaves; }
From source file:edu.uci.ics.jung.visualization.util.ParallelEdgeIndexFunction.java
public int getIndex(Context<Network<N, E>, E> context) { Network<N, E> network = context.graph; E edge = context.element;//from ww w. j a v a 2 s .c o m Integer index = edge_index.get(edge); if (index == null) { EndpointPair<N> endpoints = network.incidentNodes(edge); N u = endpoints.nodeU(); N v = endpoints.nodeV(); int count = 0; for (E connectingEdge : network.edgesConnecting(u, v)) { edge_index.put(connectingEdge, count++); } return edge_index.get(edge); } return index; }
From source file:edu.uci.ics.jung.visualization.renderers.ReshapingEdgeRenderer.java
/** * Draws the edge <code>e</code>, whose endpoints are at <code>(x1,y1)</code> and <code>(x2,y2) * </code>, on the graphics context <code>g</code>. The <code>Shape</code> provided by the <code> * EdgeShapeFunction</code> instance is scaled in the x-direction so that its width is equal to * the distance between <code>(x1,y1)</code> and <code>(x2,y2)</code>. *///from w w w . j av a 2 s. com protected void drawSimpleEdge(RenderContext<N, E> renderContext, VisualizationModel<N, E> visualizationModel, E e) { TransformingGraphics g = (TransformingGraphics) renderContext.getGraphicsContext(); Network<N, E> graph = visualizationModel.getNetwork(); EndpointPair<N> endpoints = graph.incidentNodes(e); N v1 = endpoints.nodeU(); N v2 = endpoints.nodeV(); Point p1 = visualizationModel.getLayoutModel().apply(v1); Point p2 = visualizationModel.getLayoutModel().apply(v2); Point2D p12d = renderContext.getMultiLayerTransformer().transform(Layer.LAYOUT, new Point2D.Double(p1.x, p1.y)); Point2D p22d = renderContext.getMultiLayerTransformer().transform(Layer.LAYOUT, new Point2D.Double(p2.x, p2.y)); float x1 = (float) p12d.getX(); float y1 = (float) p12d.getY(); float x2 = (float) p22d.getX(); float y2 = (float) p22d.getY(); float flatness = 0; MutableTransformer transformer = renderContext.getMultiLayerTransformer().getTransformer(Layer.VIEW); if (transformer instanceof LensTransformer) { LensTransformer ht = (LensTransformer) transformer; RectangularShape lensShape = ht.getLens().getLensShape(); if (lensShape.contains(x1, y1) || lensShape.contains(x2, y2)) { flatness = .05f; } } boolean isLoop = v1.equals(v2); Shape s2 = renderContext.getNodeShapeFunction().apply(v2); Shape edgeShape = renderContext.getEdgeShapeFunction().apply(Context.getInstance(graph, e)); AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1); if (isLoop) { // this is a self-loop. scale it is larger than the node // it decorates and translate it so that its nadir is // at the center of the node. Rectangle2D s2Bounds = s2.getBounds2D(); xform.scale(s2Bounds.getWidth(), s2Bounds.getHeight()); xform.translate(0, -edgeShape.getBounds2D().getWidth() / 2); } else { // this is a normal edge. Rotate it to the angle between // node endpoints, then scale it to the distance between // the nodes float dx = x2 - x1; float dy = y2 - y1; float thetaRadians = (float) Math.atan2(dy, dx); xform.rotate(thetaRadians); float dist = (float) Math.sqrt(dx * dx + dy * dy); xform.scale(dist, 1.0); } edgeShape = xform.createTransformedShape(edgeShape); Paint oldPaint = g.getPaint(); // get Paints for filling and drawing // (filling is done first so that drawing and label use same Paint) Paint fill_paint = renderContext.getEdgeFillPaintFunction().apply(e); if (fill_paint != null) { g.setPaint(fill_paint); g.fill(edgeShape, flatness); } Paint draw_paint = renderContext.getEdgeDrawPaintFunction().apply(e); if (draw_paint != null) { g.setPaint(draw_paint); g.draw(edgeShape, flatness); } float scalex = (float) g.getTransform().getScaleX(); float scaley = (float) g.getTransform().getScaleY(); // see if arrows are too small to bother drawing if (scalex < .3 || scaley < .3) { return; } if (renderContext.renderEdgeArrow()) { Shape destNodeShape = renderContext.getNodeShapeFunction().apply(v2); AffineTransform xf = AffineTransform.getTranslateInstance(x2, y2); destNodeShape = xf.createTransformedShape(destNodeShape); AffineTransform at = edgeArrowRenderingSupport.getArrowTransform(renderContext, new GeneralPath(edgeShape), destNodeShape); if (at == null) { return; } Shape arrow = renderContext.getEdgeArrow(); arrow = at.createTransformedShape(arrow); g.setPaint(renderContext.getArrowFillPaintFunction().apply(e)); g.fill(arrow); g.setPaint(renderContext.getArrowDrawPaintFunction().apply(e)); g.draw(arrow); if (!graph.isDirected()) { Shape nodeShape = renderContext.getNodeShapeFunction().apply(v1); xf = AffineTransform.getTranslateInstance(x1, y1); nodeShape = xf.createTransformedShape(nodeShape); at = edgeArrowRenderingSupport.getReverseArrowTransform(renderContext, new GeneralPath(edgeShape), nodeShape, !isLoop); if (at == null) { return; } arrow = renderContext.getEdgeArrow(); arrow = at.createTransformedShape(arrow); g.setPaint(renderContext.getArrowFillPaintFunction().apply(e)); g.fill(arrow); g.setPaint(renderContext.getArrowDrawPaintFunction().apply(e)); g.draw(arrow); } } // use existing paint for text if no draw paint specified if (draw_paint == null) { g.setPaint(oldPaint); } // restore old paint g.setPaint(oldPaint); }
From source file:edu.uci.ics.jung.visualization.renderers.BasicEdgeRenderer.java
/** * Draws the edge <code>e</code>, whose endpoints are at <code>(x1,y1)</code> and <code>(x2,y2) * </code>, on the graphics context <code>g</code>. The <code>Shape</code> provided by the <code> * EdgeShapeFunction</code> instance is scaled in the x-direction so that its width is equal to * the distance between <code>(x1,y1)</code> and <code>(x2,y2)</code>. * * @param e the edge to be drawn//from w w w . ja va 2s . c om */ protected void drawSimpleEdge(RenderContext<N, E> renderContext, VisualizationModel<N, E> visualizationModel, E e) { int[] coords = new int[4]; boolean[] loop = new boolean[1]; Shape edgeShape = prepareFinalEdgeShape(renderContext, visualizationModel, e, coords, loop); int x1 = coords[0]; int y1 = coords[1]; int x2 = coords[2]; int y2 = coords[3]; boolean isLoop = loop[0]; GraphicsDecorator g = renderContext.getGraphicsContext(); Network<N, E> network = visualizationModel.getNetwork(); Paint oldPaint = g.getPaint(); // get Paints for filling and drawing // (filling is done first so that drawing and label use same Paint) Paint fill_paint = renderContext.getEdgeFillPaintFunction().apply(e); if (fill_paint != null) { g.setPaint(fill_paint); g.fill(edgeShape); } Paint draw_paint = renderContext.getEdgeDrawPaintFunction().apply(e); if (draw_paint != null) { g.setPaint(draw_paint); g.draw(edgeShape); } float scalex = (float) g.getTransform().getScaleX(); float scaley = (float) g.getTransform().getScaleY(); // see if arrows are too small to bother drawing if (scalex < .3 || scaley < .3) { return; } if (renderContext.renderEdgeArrow()) { Stroke new_stroke = renderContext.getEdgeArrowStrokeFunction().apply(e); Stroke old_stroke = g.getStroke(); if (new_stroke != null) { g.setStroke(new_stroke); } Shape destNodeShape = renderContext.getNodeShapeFunction().apply(network.incidentNodes(e).nodeV()); AffineTransform xf = AffineTransform.getTranslateInstance(x2, y2); destNodeShape = xf.createTransformedShape(destNodeShape); AffineTransform at = edgeArrowRenderingSupport.getArrowTransform(renderContext, edgeShape, destNodeShape); if (at == null) { return; } Shape arrow = renderContext.getEdgeArrow(); arrow = at.createTransformedShape(arrow); g.setPaint(renderContext.getArrowFillPaintFunction().apply(e)); g.fill(arrow); g.setPaint(renderContext.getArrowDrawPaintFunction().apply(e)); g.draw(arrow); if (!network.isDirected()) { Shape nodeShape = renderContext.getNodeShapeFunction().apply(network.incidentNodes(e).nodeU()); xf = AffineTransform.getTranslateInstance(x1, y1); nodeShape = xf.createTransformedShape(nodeShape); at = edgeArrowRenderingSupport.getReverseArrowTransform(renderContext, edgeShape, nodeShape, !isLoop); if (at == null) { return; } arrow = renderContext.getEdgeArrow(); arrow = at.createTransformedShape(arrow); g.setPaint(renderContext.getArrowFillPaintFunction().apply(e)); g.fill(arrow); g.setPaint(renderContext.getArrowDrawPaintFunction().apply(e)); g.draw(arrow); } // restore paint and stroke if (new_stroke != null) { g.setStroke(old_stroke); } } // restore old paint g.setPaint(oldPaint); }