/** * Compute weight and add edge to the graph * * @param way * @param from * @param to */ private void addEdge(Way way, Node from, Node to) { LatLon fromLL = from.getCoor(); LatLon toLL = from.getCoor(); if (fromLL == null || toLL == null) { return; } double length = fromLL.greatCircleDistance(toLL); OsmEdge edge = new OsmEdge(way, from, to); edge.setSpeed(12.1); graph.addEdge(from, to, edge); // weight = getWeight(way); double weight = getWeight(way, length); setWeight(edge, length); logger.debug( "edge for way " + way.getId() + "(from node " + from.getId() + " to node " + to.getId() + ") has weight: " + weight); ((DirectedWeightedMultigraph<Node, OsmEdge>) graph).setEdgeWeight(edge, weight); }
E addEdge(V jtSource, V jtTarget) { E jtEdge = graph.getEdgeFactory().createEdge(jtSource, jtTarget); jtElementsBeingAdded.add(jtEdge); boolean added = graph.addEdge(jtSource, jtTarget, jtEdge); jtElementsBeingAdded.remove(jtEdge); return added ? jtEdge : null; }
/** * Returns a graph, each vertex corresponding with an vector in R, and edges connecting vertices * (vectors) if they are obtuse or at right angles. * * @param TOL > 0 decides how close to zero is considered zero. */ public static Graph<Matrix, DefaultEdge> obtuseConnectedGraph(Set<Matrix> R, double TOL) { Graph G = new SimpleGraph<>(DefaultEdge.class); for (Matrix v : R) G.addVertex(v); // graph contains the relevant vectors int N = R.size(); Matrix[] b = new Matrix[N]; R.toArray(b); // build an array with pointers to vectors in R (also vertices in G) // add edges between obtuse relevant vectors. for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) if (dot(b[i], b[j]) < Math.abs(TOL)) G.addEdge(b[i], b[j]); return G; }
/** {@inheritDoc} */ public void generateGraph( Graph<V, E> target, final VertexFactory<V> vertexFactory, Map<String, V> resultMap) { if (size < 1) { return; } // A little trickery to intercept the rim generation. This is // necessary since target may be initially non-empty, meaning we can't // rely on its vertex set after the rim is generated. final Collection<V> rim = new ArrayList<V>(); VertexFactory<V> rimVertexFactory = new VertexFactory<V>() { public V createVertex() { V vertex = vertexFactory.createVertex(); rim.add(vertex); return vertex; } }; RingGraphGenerator<V, E> ringGenerator = new RingGraphGenerator<V, E>(size - 1); ringGenerator.generateGraph(target, rimVertexFactory, resultMap); V hubVertex = vertexFactory.createVertex(); target.addVertex(hubVertex); if (resultMap != null) { resultMap.put(HUB_VERTEX, hubVertex); } for (V rimVertex : rim) { if (inwardSpokes) { target.addEdge(rimVertex, hubVertex); } else { target.addEdge(hubVertex, rimVertex); } } }
@Override public <N extends Node, E extends Edge> Graph<N, E> createGraph( VertexFactory<N> vfac, EdgeFactory<N, E> efac) { Integer inp = InputDialog.getIntegerInput("Game Tree Factory", "No. of levels?", 2, Integer.MAX_VALUE); if (inp == null) { return null; } int levels = inp; final double levelSep = ((2 << levels - 1) * (5.0 + 5.0)) / (Math.PI * levels); final double shift = levels * levelSep; Graph<N, E> graph = new SimpleGraph<>(efac); List<N> parents = new LinkedList<>(); List<N> children = new LinkedList<>(); Node.PropChanger npr = Node.PropChanger.create(); N n = vfac.createVertex(); npr.setPosition(n, polarToCartesian(0, 0, shift)); graph.addVertex(n); parents.add(n); for (int i = 1; i <= levels; i++) { final double curRadius = i * levelSep; double th = 0; final double thDelta = Math.PI / parents.size(); for (N p : parents) { N c1 = vfac.createVertex(); N c2 = vfac.createVertex(); npr.setPosition(c1, polarToCartesian(curRadius, th + 0.5 * thDelta, shift)); npr.setPosition(c2, polarToCartesian(curRadius, th + 1.5 * thDelta, shift)); th += 2 * thDelta; graph.addVertex(c1); graph.addVertex(c2); graph.addEdge(p, c1); graph.addEdge(p, c2); children.add(c1); children.add(c2); } parents = children; children = new LinkedList<>(); } return graph; }
/** {@inheritDoc} */ public void generateGraph( Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap) { if (size < 1) { return; } // Add all the vertices to the set for (int i = 0; i < size; i++) { V newVertex = vertexFactory.createVertex(); target.addVertex(newVertex); } /* * We want two iterators over the vertex set, one fast and one slow. * The slow one will move through the set once. For each vertex, * the fast iterator moves through the set, adding an edge to all * vertices we haven't connected to yet. * * If we have an undirected graph, the second addEdge call will return * nothing; it will not add a second edge. */ Iterator<V> slowI = target.vertexSet().iterator(); Iterator<V> fastI; while (slowI.hasNext()) { // While there are more vertices in the set V latestVertex = slowI.next(); fastI = target.vertexSet().iterator(); // Jump to the first vertex *past* latestVertex while (fastI.next() != latestVertex) {; } // And, add edges to all remaining vertices V temp; while (fastI.hasNext()) { temp = fastI.next(); target.addEdge(latestVertex, temp); target.addEdge(temp, latestVertex); } } }
@Override public void generateGraph( Graph<V, E> veGraph, VertexFactory<V> vVertexFactory, Map<String, V> stringVMap) { Map<Integer, V> intToNodeMap = new HashMap<Integer, V>(); for (Integer nodeID : selected.vertices.keySet()) { V node = vVertexFactory.createVertex(); if (!veGraph.containsVertex(node)) veGraph.addVertex(node); intToNodeMap.put(nodeID, node); } for (PajekArc arc : selected.arcs) { V source = intToNodeMap.get(arc.source); V target = intToNodeMap.get(arc.target); E edge = null; if (!veGraph.containsEdge(source, target)) edge = veGraph.addEdge(source, target); else edge = veGraph.getEdge(source, target); if (veGraph instanceof WeightedGraph) ((WeightedGraph) veGraph).setEdgeWeight(edge, arc.weight); } }
/** {@inheritDoc} */ public void generateGraph( Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap) { V lastVertex = null; for (int i = 0; i < size; ++i) { V newVertex = vertexFactory.createVertex(); target.addVertex(newVertex); if (lastVertex == null) { if (resultMap != null) { resultMap.put(START_VERTEX, newVertex); } } else { target.addEdge(lastVertex, newVertex); } lastVertex = newVertex; } if ((resultMap != null) && (lastVertex != null)) { resultMap.put(END_VERTEX, lastVertex); } }
public void addEdge(Edge toAdd) { myGraph.addEdge(toAdd.getSource(), toAdd.getDest(), toAdd); }
/** * @param connectedOnly if true, the result will be a connected graph * @return */ public Graph<V, E> toGraph() { if (subgraph != null) return subgraph; if (directed) { subgraph = new DirectedMultigraph<V, E>(g2.getEdgeFactory()); } else { subgraph = new Multigraph<V, E>(g2.getEdgeFactory()); } E edge; V source; V target; for (int x = 0; x < dimx; x++) { for (int y = 0; y < dimy; y++) { if (matrix[x][y]) { edge = edgeList2.get(y); source = g2.getEdgeSource(edge); target = g2.getEdgeTarget(edge); if (mappedVerticesFromG2.contains(source) && mappedVerticesFromG2.contains(target)) { // make sure the source and target vertices have been added, then add the edge subgraph.addVertex(source); subgraph.addVertex(target); subgraph.addEdge(source, target, edge); } } } } if (connectedOnly) { // make sure this subgraph is connected, if it is not return the largest connected part List<Set<V>> connectedVertices = new ArrayList<Set<V>>(); for (V v : subgraph.vertexSet()) { if (!SharedStaticMethods.containsV(connectedVertices, v)) { connectedVertices.add(SharedStaticMethods.getConnectedVertices(subgraph, v)); } } // ConnectedVertices now contains Sets of connected vertices every vertex of the subgraph is // contained exactly once in the list // if there is more then 1 set, then this method should return the largest connected part of // the graph if (connectedVertices.size() > 1) { Graph<V, E> largestResult = null; Graph<V, E> currentGraph; int largestSize = -1; Set<V> currentSet; for (int i = 0; i < connectedVertices.size(); i++) { currentSet = connectedVertices.get(i); /*note that 'subgraph' is the result from the Mcgregor algorithm, 'currentGraph' is an * induced subgraph of 'subgraph'. 'currentGraph' is connected, because the vertices in * 'currentSet' are connected with edges in 'subgraph' */ currentGraph = new Subgraph<V, E, Graph<V, E>>(subgraph, currentSet); if (currentGraph.edgeSet().size() > largestSize) { largestResult = currentGraph; } } return largestResult; } } return subgraph; }