/** * Report the opposite inter across the given relation of the provided inter * * @param inter one side of the relation * @param relation the relation to cross * @return the vertex at the opposite side of the relation */ public Inter getOppositeInter (Inter inter, Relation relation) { return Graphs.getOppositeVertex(this, relation, inter); }
/** * Try to add new paths for the vertex. These new paths reached the specified vertex and ended * with the specified edge. A new intermediary path is stored in the paths list of the specified * vertex provided that the path can be extended to the end-vertex. * * @param vertex a vertex which has just been encountered. * @param edge the edge via which the vertex was encountered. */ private boolean tryToAddNewPaths(V vertex, E edge) { RankingPathElementList<V, E> data = this.seenDataContainer.get(vertex); V oppositeVertex = Graphs.getOppositeVertex(this.graph, edge, vertex); RankingPathElementList<V, E> oppositeData = this.prevSeenDataContainer.get(oppositeVertex); return data.addPathElements(oppositeData, edge); }
/** * The first time we see a vertex, make up a new entry for it. * * @param vertex a vertex which has just been encountered. * @param edge the edge via which the vertex was encountered. * * @return the new entry. */ private RankingPathElementList<V, E> createSeenData(V vertex, E edge) { V oppositeVertex = Graphs.getOppositeVertex(this.graph, edge, vertex); RankingPathElementList<V, E> oppositeData = this.prevSeenDataContainer.get(oppositeVertex); // endVertex in argument to ensure that stored paths do not disconnect // the end-vertex return new RankingPathElementList<>( this.graph, this.k, oppositeData, edge, this.endVertex, this.pathValidator); }
/** * Returns a set of vertices that are neighbors of a specified vertex. * * @param g the graph to look for neighbors in * @param vertex the vertex to get the neighbors of * @param <V> the graph vertex type * @param <E> the graph edge type * @return a set of the vertices that are neighbors of the specified vertex */ public static <V, E> Set<V> neighborSetOf(Graph<V, E> g, V vertex) { Set<V> neighbors = new LinkedHashSet<>(); for (E e : g.edgesOf(vertex)) { neighbors.add(Graphs.getOppositeVertex(g, e, vertex)); } return neighbors; }
/** * Returns a list of vertices that are the neighbors of a specified vertex. If the graph is a * multigraph vertices may appear more than once in the returned list. * * <p> * The method uses {@link Graph#edgesOf(Object)} to traverse the graph. * * @param g the graph to look for neighbors in * @param vertex the vertex to get the neighbors of * @param <V> the graph vertex type * @param <E> the graph edge type * * @return a list of the vertices that are the neighbors of the specified vertex. */ public static <V, E> List<V> neighborListOf(Graph<V, E> g, V vertex) { List<V> neighbors = new ArrayList<>(); for (E e : g.edgesOf(vertex)) { neighbors.add(getOppositeVertex(g, e, vertex)); } return neighbors; }
/** * Returns the predecessors of {@code vertex} in the order defined by {@code map}. More * precisely, returns those of {@code vertex}, whose mapped index in {@code map} is less then * the index of {@code vertex}. * * @param vertexInOrder defines the mapping of vertices in {@code graph} to their indices in * order. * @param vertex the vertex whose predecessors in order are to be returned. * @return the predecessors of {@code vertex} in order defines by {@code map}. */ private Set<V> getPredecessors(Map<V, Integer> vertexInOrder, V vertex) { Set<V> predecessors = new HashSet<>(); Integer vertexPosition = vertexInOrder.get(vertex); Set<E> edges = graph.edgesOf(vertex); for (E edge : edges) { V oppositeVertex = Graphs.getOppositeVertex(graph, edge, vertex); Integer destPosition = vertexInOrder.get(oppositeVertex); if (destPosition < vertexPosition) predecessors.add(oppositeVertex); } return predecessors; }
/** * Returns a list of vertices that are the direct predecessors of a specified vertex. If the * graph is a multigraph, vertices may appear more than once in the returned list. * * <p> * The method uses {@link Graph#incomingEdgesOf(Object)} to traverse the graph. * * @param g the graph to look for predecessors in * @param vertex the vertex to get the predecessors of * @param <V> the graph vertex type * @param <E> the graph edge type * * @return a list of the vertices that are the direct predecessors of the specified vertex. */ public static <V, E> List<V> predecessorListOf(Graph<V, E> g, V vertex) { List<V> predecessors = new ArrayList<>(); Set<? extends E> edges = g.incomingEdgesOf(vertex); for (E e : edges) { predecessors.add(getOppositeVertex(g, e, vertex)); } return predecessors; }
private void dfs(int u, int parent) { component[u] = numberComponent; timeIn[u] = ++clock; ancestors[0][u] = parent; for (int l = 1; l < maxLevel; l++) { if (ancestors[l - 1][u] != -1) ancestors[l][u] = ancestors[l - 1][ancestors[l - 1][u]]; } V vertexU = indexList.get(u); for (E edge : graph.outgoingEdgesOf(vertexU)) { int v = vertexMap.get(Graphs.getOppositeVertex(graph, edge, vertexU)); if (v != parent) { dfs(v, u); } } timeOut[u] = ++clock; }
/** * Computes and returns neighbours of {@code vertex} which haven't been visited by this * iterator. * * @param vertex the vertex, whose neighbours are being explored. * @return neighbours of {@code vertex} which have yet to be visited by this iterator. */ private Set<V> getUnvisitedNeighbours(V vertex) { Set<V> unmapped = new HashSet<>(); Set<E> edges = graph.edgesOf(vertex); for (E edge : edges) { V oppositeVertex = Graphs.getOppositeVertex(graph, edge, vertex); if (bucketList.containsBucketWith(oppositeVertex)) { unmapped.add(oppositeVertex); } } return unmapped; }
/** * Creates a path element by concatenation of an edge to a path element. * * @param pathElement * @param edge edge reaching the end vertex of the path element created. */ protected AbstractPathElement(Graph<V, E> graph, AbstractPathElement<V, E> pathElement, E edge) { this.vertex = Graphs.getOppositeVertex(graph, edge, pathElement.getVertex()); this.prevEdge = edge; this.prevPathElement = pathElement; this.nHops = pathElement.getHopCount() + 1; }
/** * @see CrossComponentIterator#encounterVertex(Object, Object) */ @Override protected void encounterVertex(V vertex, E edge) { int depth = (edge == null ? 0 : getSeenData(Graphs.getOppositeVertex(graph, edge, vertex)).depth + 1); putSeenData(vertex, new SearchNodeData<>(edge, depth)); queue.add(vertex); }
/** * Returns the parent node of vertex $v$ in the BFS search tree, or null if $v$ is the root * node. This method can only be invoked on a vertex $v$ once the iterator has visited vertex * $v$! * * @param v vertex * @return parent node of vertex $v$ in the BFS search tree, or null if $v$ is a root node */ public V getParent(V v) { assert getSeenData(v) != null; E edge = getSeenData(v).edge; if (edge == null) return null; else return Graphs.getOppositeVertex(graph, edge, v); }
/** * Increments the cardinalities of the neighbours of the {@code vertex} by 1. If the maximum * cardinality increases, increments {@code maxCardinality} by 1. * * @param vertex the vertex whose neighbours are to be updated. */ private void updateNeighbours(V vertex) { Set<V> processed = new HashSet<>(); for (E edge : graph.edgesOf(vertex)) { V opposite = Graphs.getOppositeVertex(graph, edge, vertex); if (cardinalityMap.containsKey(opposite) && !processed.contains(opposite)) { processed.add(opposite); addToBucket(opposite, removeFromBucket(opposite) + 1); } } if (maxCardinality < graph.vertexSet().size() && maxCardinality >= 0 && buckets.get(maxCardinality + 1) != null) { ++maxCardinality; } } }
private void exportAsAdjacencyList(Graph<V, E> g, PrintWriter out) { boolean exportEdgeWeights = parameters.contains(CSVFormat.Parameter.EDGE_WEIGHTS); for (V v : g.vertexSet()) { exportEscapedField(out, vertexIDProvider.getName(v)); for (E e : g.outgoingEdgesOf(v)) { V w = Graphs.getOppositeVertex(g, e, v); out.print(delimiter); exportEscapedField(out, vertexIDProvider.getName(w)); if (exportEdgeWeights) { out.print(delimiter); exportEscapedField(out, String.valueOf(g.getEdgeWeight(e))); } } out.println(); } }
@Override protected void addUnseenChildrenOf(Spot vertex) { int ts = vertex.getFeature(Spot.FRAME).intValue(); for (DefaultWeightedEdge edge : specifics.edgesOf(vertex)) { if (nListeners != 0) { fireEdgeTraversed(createEdgeTraversalEvent(edge)); } Spot oppositeV = Graphs.getOppositeVertex(graph, edge, vertex); int tt = oppositeV.getFeature(Spot.FRAME).intValue(); if (tt <= ts) { continue; } if ( seen.containsKey(oppositeV)) { encounterVertexAgain(oppositeV, edge); } else { encounterVertex(oppositeV, edge); } } }
@Override protected void addUnseenChildrenOf(Spot vertex) { int ts = vertex.getFeature(Spot.FRAME).intValue(); for (DefaultWeightedEdge edge : specifics.edgesOf(vertex)) { if (nListeners != 0) { fireEdgeTraversed(createEdgeTraversalEvent(edge)); } Spot oppositeV = Graphs.getOppositeVertex(graph, edge, vertex); int tt = oppositeV.getFeature(Spot.FRAME).intValue(); if (tt <= ts) { continue; } if ( seen.containsKey(oppositeV)) { encounterVertexAgain(oppositeV, edge); } else { encounterVertex(oppositeV, edge); } } }
private void addUnseenChildrenOf(V vertex) { for (E edge : graph.outgoingEdgesOf(vertex)) { if (nListeners != 0) { fireEdgeTraversed(createEdgeTraversalEvent(edge)); } V oppositeV = Graphs.getOppositeVertex(graph, edge, vertex); if (isSeenVertex(oppositeV)) { encounterVertexAgain(oppositeV, edge); } else { encounterVertex(oppositeV, edge); } } }
/** * Relaxes the edge outgoing from u and updates the queue appropriately. * * @param u Vertex u. * @param e Edge e. * @param queue The queue. */ protected void relax(V startNode, V u, E e, PriorityQueue<V> queue) { // Get the target vertex. V v = Graphs.getOppositeVertex(graph, e, u); // Get the weight. double uvWeight = graph.getEdgeWeight(e); // If a smaller distance estimate is available, make the necessary // updates. if (v.getDistance() > u.getDistance() + uvWeight) { shortestPathSoFarUpdate(startNode, u, v, uvWeight, e, queue); } else if (Math.abs(v.getDistance() - (u.getDistance() + uvWeight)) < TOLERANCE) { multipleShortestPathUpdate(u, v, e); } }
/** * Determine weighted path length to a vertex via an edge, using the path length for the * opposite vertex. * * @param vertex the vertex for which to calculate the path length. * @param edge the edge via which the path is being extended. * * @return calculated path length. */ private double calculatePathLength(V vertex, E edge) { assertNonNegativeEdge(edge); V otherVertex = Graphs.getOppositeVertex(getGraph(), edge, vertex); FibonacciHeapNode<QueueEntry<V, E>> otherEntry = getSeenData(otherVertex); return otherEntry.getKey() + getGraph().getEdgeWeight(edge); }
@Override public V next() { if (!hasNext()) { throw new NoSuchElementException(); } Set<? extends E> potentialEdges = graph.outgoingEdgesOf(currentVertex); // randomly select an edge from the set of potential edges. E nextEdge = drawEdge(potentialEdges); if (nextEdge != null) { V nextVertex; nextVertex = Graphs.getOppositeVertex(graph, nextEdge, currentVertex); encounterVertex(nextVertex, nextEdge); fireEdgeTraversed(createEdgeTraversalEvent(nextEdge)); fireVertexTraversed(createVertexTraversalEvent(nextVertex)); currentVertex = nextVertex; return nextVertex; } else { sinkReached = true; return currentVertex; } }