public Graph(Cons lst) { // System.out.println("Entering Graph()"); // for (Cons ptr = lst; ptr != null; ptr = Cons.rest(ptr) ) { // if ( Cons.first(ptr) != null ) // System.out.println(Cons.first(ptr).toString()); } vertices = new TreeMap<String, Vertex>(); // put all vertices in first for (Cons ptr = lst; ptr != null; ptr = Cons.rest(ptr)) { Cons item = (Cons) Cons.first(ptr); // System.out.println("item = " + item.toString()); Vertex node = vert((String) Cons.first(item), (Double) Cons.second(item), (Double) Cons.third(item)); } for (Cons ptr = lst; ptr != null; ptr = Cons.rest(ptr)) { Cons item = (Cons) Cons.first(ptr); // System.out.println("item = " + item.toString()); Cons conns = (Cons) Cons.fourth(item); Vertex node = vertices.get((String) Cons.first(item)); for (; conns != null; conns = Cons.rest(conns)) { Cons conn = (Cons) Cons.first(conns); // System.out.println("conn = " + conn.toString()); Vertex target = vertices.get((String) Cons.first(conn)); node.add(target, (Integer) Cons.second(conn)); target.add(node, (Integer) Cons.second(conn)); } } }
public boolean hasEdge(Vertex v0, Vertex v1) { /* Validates that there is an Edge(v0,v1) in this graph. Returns true if adjMatric[v0][v1] is true. */ int origin = v0.getId(); int dest = v1.getId(); return adjMatrix[origin][dest]; }
public Flexible copyToGroup(String group) { String newName; if (group.equals(nullString)) newName = Group.substractObjectName(getName()); else newName = group + Constants.GROUP_SEPARATOR + Group.substractObjectName(getName()); // object with new name already exists, add suffix ///!!! while (Group.getRoot().findObject(newName, true) != null) newName = StringUtils.incrementName(newName, Constants.COPY_SUFFIX); Box grBox = new Box( newName, null, startVertex.getX(), startVertex.getY(), endVertex.getX(), endVertex.getY()); grBox.setColor(getColor()); Group.getRoot().addSubObject(newName, grBox, true); // ViewState view = ViewState.getInstance(); // grBox.move(20 - view.getRx(), 20 - view.getRy()); unconditionalValidation(); return grBox; }
/** * This returns an enumeration of all Darts in Graph G in a canonical order starting with * firstDart. The order is canonical in the sense that it only depends on the oriented isomorphism * class of G (and firstDart). That is, if there is an oriented iso G <-> G' sending firstDart <-> * firstDart', then that bijection will send getDarts <-> getDarts'. * * <p>This enumeration is useful in finding explicit bijections of isomorphic graphs. */ public static Dart[] getDarts(Dart firstDart, Graph G) { /** * Description of algorithm: Return all the couples around V0=firstDart.V, then all couples * around V1, .... The order for couples around firstDart.V is counterclockwise starting with * firstDart.F. clockwise around V1,... So we need to order the vertices V0,V1,... and pick the * first face to use at each vertex and the rest is determined. Order on V0,... = first * encountered. Order on faces at vertex = first encountered. */ distinctFIFO vQueue = new distinctFIFO(); util.ConditionGetter fQueue = new util.ConditionGetter(); fQueue.add(firstDart.getF()); vQueue.put(firstDart.getV()); Collection coups = new ArrayList(); int coupsIndex = 0; Vertex V; while ((V = (Vertex) vQueue.remove()) != null) { Face F = (Face) fQueue.get(new incidence(V)); for (int i = 0; i < V.size(); i++) { Face Fx = V.next(F, i); coups.add(new Dart(V, Fx)); vQueue.put(Fx.next(V, 1)); fQueue.add(Fx); } } return (Dart[]) coups.toArray(new Dart[coups.size()]); }
public GraphIndex(Graph graph) { LOG.info("Indexing graph..."); for (String feedId : graph.getFeedIds()) { for (Agency agency : graph.getAgencies(feedId)) { Map<String, Agency> agencyForId = agenciesForFeedId.getOrDefault(feedId, new HashMap<>()); agencyForId.put(agency.getId(), agency); this.agenciesForFeedId.put(feedId, agencyForId); } } Collection<Edge> edges = graph.getEdges(); /* We will keep a separate set of all vertices in case some have the same label. * Maybe we should just guarantee unique labels. */ Set<Vertex> vertices = Sets.newHashSet(); for (Edge edge : edges) { vertices.add(edge.getFromVertex()); vertices.add(edge.getToVertex()); if (edge instanceof TablePatternEdge) { TablePatternEdge patternEdge = (TablePatternEdge) edge; TripPattern pattern = patternEdge.getPattern(); patternForId.put(pattern.code, pattern); } } for (Vertex vertex : vertices) { vertexForId.put(vertex.getLabel(), vertex); if (vertex instanceof TransitStop) { TransitStop transitStop = (TransitStop) vertex; Stop stop = transitStop.getStop(); stopForId.put(stop.getId(), stop); stopVertexForStop.put(stop, transitStop); stopsForParentStation.put(stop.getParentStation(), stop); } } for (TransitStop stopVertex : stopVertexForStop.values()) { Envelope envelope = new Envelope(stopVertex.getCoordinate()); stopSpatialIndex.insert(envelope, stopVertex); } for (TripPattern pattern : patternForId.values()) { patternsForFeedId.put(pattern.getFeedId(), pattern); patternsForRoute.put(pattern.route, pattern); for (Trip trip : pattern.getTrips()) { patternForTrip.put(trip, pattern); tripForId.put(trip.getId(), trip); } for (Stop stop : pattern.getStops()) { patternsForStop.put(stop, pattern); } } for (Route route : patternsForRoute.asMap().keySet()) { routeForId.put(route.getId(), route); } // Copy these two service indexes from the graph until we have better ones. calendarService = graph.getCalendarService(); serviceCodes = graph.serviceCodes; this.graph = graph; LOG.info("Done indexing graph."); }
/** * This constructor accepts an ArrayList<Vertex> and populates this.vertices. If multiple Vertex * objects have the same label, then the last Vertex with the given label is used. * * @param vertices The initial Vertices to populate this Graph */ public Graph(ArrayList<Vertex> vertices) { this.vertices = new HashMap<String, Vertex>(); this.edges = new HashMap<Integer, Edge>(); for (Vertex v : vertices) { this.vertices.put(v.getLabel(), v); } }
public boolean addVertex(Vertex<T> v) { boolean added = false; if (!verticesHash.containsKey(v.getName())) { added = vertices.add(v); verticesHash.put(v.getName(), v); } return added; }
public boolean removeEdge(Vertex<T> from, Vertex<T> to) { Edge<T> e = from.findEdge(to); if (e == null) return false; else { from.remove(e); return true; } }
public void destroy() { super.destroy(); if (getParent() != null) getParent().removeObject(Group.substractObjectName(name)); if (!startVertex.isDestroyed()) startVertex.destroy(); if (!endVertex.isDestroyed()) endVertex.destroy(); }
public void print() { Collection c = vertices.values(); Iterator itr = c.iterator(); while (itr.hasNext()) { Vertex v = (Vertex) itr.next(); v.print(); } }
public int pathcost(Vertex v) { int pathcost = v.cost; while (v.parent() != null) { // walks from the v to the root and sums the cost of each edge in between pathcost += v.parent().cost(); v = v.parent(); } return pathcost; }
/** * @param label The label of the Vertex to remove * @return Vertex The removed Vertex object */ public Vertex removeVertex(String label) { Vertex v = vertices.remove(label); while (v.getNeighborCount() > 0) { this.removeEdge(v.getNeighbor((0))); } return v; }
public void revalidatePosition() { startVertex.revalidatePosition(); endVertex.revalidatePosition(); double rscale = getRscale(); setRx((int) (getX() * rscale)); setRy((int) (getY() * rscale)); }
public void addEdge(Vertex v0, Vertex v1) { /* Adds an edge to the adjacency matrix */ if (hasVertex(v0) && hasVertex(v1)) { int origin = v0.getId(); int dest = v1.getId(); adjMatrix[origin][dest] = true; edges++; } }
/** Code to test the correctness of the SimpleGraph methods. */ public static void main(String[] args) { // create graph a----b-----c, // X Y // X and Y are objects stored at edges. . // All Objects stored will be strings. SimpleGraph G = new SimpleGraph(); Vertex v, w, a, b, c; Edge e, x, y; v = G.insertVertex(null, "a"); a = v; w = G.insertVertex(null, "b"); b = w; e = G.insertEdge(a, b, null, "X"); x = e; v = G.insertVertex(null, "c"); c = v; e = G.insertEdge(b, c, null, "Y"); y = e; Iterator i; System.out.println("Iterating through vertices..."); for (i = G.vertices(); i.hasNext(); ) { v = (Vertex) i.next(); System.out.println("found vertex " + v.getName()); } System.out.println("Iterating through adjacency lists..."); for (i = G.vertices(); i.hasNext(); ) { v = (Vertex) i.next(); System.out.println("Vertex " + v.getName()); Iterator j; for (j = G.incidentEdges(v); j.hasNext(); ) { e = (Edge) j.next(); System.out.println(" found edge " + e.getName()); } } System.out.println("Testing opposite..."); System.out.println("aXbYc is "); System.out.println(a); System.out.println(x); System.out.println(b); System.out.println(y); System.out.println(c); System.out.println("opposite(a,x) is " + G.opposite(a, x)); System.out.println("opposite(a,y) is " + G.opposite(a, y)); System.out.println("opposite(b,x) is " + G.opposite(b, x)); System.out.println("opposite(b,y) is " + G.opposite(b, y)); System.out.println("opposite(c,x) is " + G.opposite(c, x)); System.out.println("opposite(c,y) is " + G.opposite(c, y)); }
private void computeMatchSet() { for (Vertex v : LV) { for (Vertex neighb : v.getNeighbors()) { if (neighb.getId() != SOURCE_ID && getFlow(v, neighb) > 0) { matches.add(v); matches.add(neighb); } } } }
@Override public Iterable<String> findRelatedEdges( Iterable<String> vertexIds, Authorizations authorizations) { Set<String> results = new HashSet<String>(); List<Vertex> vertices = toList(getVertices(vertexIds, authorizations)); // since we are checking bi-directional edges we should only have to check v1->v2 and not v2->v1 Map<String, String> checkedCombinations = new HashMap<String, String>(); for (Vertex sourceVertex : vertices) { for (Vertex destVertex : vertices) { if (checkedCombinations.containsKey(sourceVertex.getId() + destVertex.getId())) { continue; } Iterable<String> edgeIds = sourceVertex.getEdgeIds(destVertex, Direction.BOTH, authorizations); for (String edgeId : edgeIds) { results.add(edgeId); } checkedCombinations.put(sourceVertex.getId() + destVertex.getId(), ""); checkedCombinations.put(destVertex.getId() + sourceVertex.getId(), ""); } } return results; }
@Override public Vertex getVertex( String vertexId, EnumSet<FetchHint> fetchHints, Authorizations authorizations) { LOGGER.warn("Performing scan of all vertices! Override getVertex."); for (Vertex vertex : getVertices(fetchHints, authorizations)) { if (vertex.getId().equals(vertexId)) { return vertex; } } return null; }
protected void validate() { startVertex.validate(); endVertex.validate(); revalidatePosition(); double rscale = getRscale(); setRwidth((int) (getWidth() * rscale)); setRheight((int) (getHeight() * rscale)); }
public String verticesToString() { String result = "List of vertices\n"; Iterator it = this.vertices.iterator(); int cont = 0; while (it.hasNext()) { Vertex v = (Vertex) it.next(); result = result + "v" + cont + " " + v.getPoint().toString() + "\n"; cont++; } return result; }
/** * creates a new triangle facet within the hole incident to h by connecting the tip of h with two * new halfedges and a new vertex. Returns the halfedge of the new edge that is incident to the * new facet and the new vertex. */ public Halfedge<X> addTriangleToBorder(Halfedge h, X point) { if (h.face != null) throw new Error("no border edge"); System.out.println("adding triangle to " + h); Face<X> newFace = new Face<X>(); Vertex<X> newVertex = new Vertex<X>(point); Halfedge<X> hPrev = new Halfedge<X>(); Halfedge<X> hNext = new Halfedge<X>(); Halfedge<X> hPrevOpp = new Halfedge<X>(); Halfedge<X> hNextOpp = new Halfedge<X>(); // setting the new face newFace.setEdge(h); // setting hPrev (halfedge preceding h in the new face) hPrev.setFace(newFace); hPrev.setVertex(h.getOpposite().getVertex()); hPrev.setPrev(hNext); hPrev.setNext(h); hPrev.setOpposite(hPrevOpp); // setting hNext (halfedge following h in the new face) hNext.setFace(newFace); hNext.setVertex(newVertex); hNext.setPrev(h); hNext.setNext(hPrev); hNext.setOpposite(hNextOpp); // setting hPrevOpp (new boundary halfedge) hPrevOpp.setFace(null); hPrevOpp.setVertex(newVertex); hPrevOpp.setPrev(h.getPrev()); hPrevOpp.setNext(hNextOpp); hPrevOpp.setOpposite(hPrev); // setting hNextOpp (the other new boundary halfedge) hNextOpp.setFace(null); hNextOpp.setVertex(h.getVertex()); hNextOpp.setPrev(hPrevOpp); hNextOpp.setNext(h.getNext()); hNextOpp.setOpposite(hNext); // updating old boundary halfedge informations h.setFace(newFace); h.setPrev(hPrev); h.setNext(hNext); // setting newVertex newVertex.setEdge(hPrev); // LCA: a controler si c'est hPrev ou hNext // adding new facet, vertex and the four halfedges this.vertices.add(newVertex); this.facets.add(newFace); this.halfedges.add(hPrev); this.halfedges.add(hNext); this.halfedges.add(hPrevOpp); this.halfedges.add(hNextOpp); return hNext; }
public void relaxEdges(FibonacciHeap<Vertex> unvisited) { Vertex[] vertices = Graph.this.vertices; for (Edge edge : edges) { Vertex to = vertices[edge.to]; int newDistance = distance + edge.cost; if (newDistance < to.distance) { to.distance = newDistance; unvisited.decreaseKey(to.heapEntry, newDistance); } } }
/** * Method to find the DFS traversal for the Graph * * @param G : Input Graph G - undirected graph * @return : Returns the count for checking connected components */ static int dfsUtil(Graph<Vertex> G) { for (Vertex u : G) { u.seen = false; u.parent = null; } int cno = 0; // Initializing count variable to check for connected components for (Vertex u : G) { if (!u.seen) dfsVisit(u, ++cno); } return cno; }
/** * Method to do the DFS traversal - Recursive call * * @param u: Vertex for which the DFS checks the adjacency list. * @param cno: Count variable to keep the number of connected components. */ static void dfsVisit(Vertex u, int cno) { u.seen = true; u.cno = cno; for (Edge e : u.Adj) { Vertex v = e.otherEnd(u); if (!v.seen) { v.parent = u; dfsVisit(v, cno); } } }
public boolean move(int dx, int dy) { if (checkMove(dx, dy)) { startVertex.move(dx, dy); endVertex.move(dx, dy); revalidatePosition(); return true; } return false; }
public boolean addEdge(Vertex<T> from, Vertex<T> to, int cost) throws IllegalArgumentException { if (!verticesHash.containsKey(from.getName())) throw new IllegalArgumentException("from is not in graph"); if (!verticesHash.containsKey(to.getName())) throw new IllegalArgumentException("to is not in graph"); Edge<T> e = new Edge<T>(from, to, cost); if (from.findEdge(to) != null) return false; else { from.addEdge(e); return true; } }
private static List<Vertex> getPath(Vertex target) { List<Vertex> path = new LinkedList<Vertex>(); path.add(target); Vertex next = target; while (next.getPrevious().getMinDistance() != 0) { path.add(next.getPrevious()); next = next.getPrevious(); } Collections.reverse(path); return path; }
public VertexInfoDialog(Vertex vertex) { v = vertex; setTitle("Vertex: " + v.getName()); Container cp = getContentPane(); JPanel p = new JPanel(); p.setLayout(new GridLayout(1, 1)); opis = new TextArea(v.getInfo()); opis.setEditable(false); p.add(opis, new FlowLayout()); cp.add(p); setSize(200, 300); setDefaultCloseOperation(DISPOSE_ON_CLOSE); }
private void konigDFS(Set<Vertex> konigSet, Vertex v, boolean edgesInMatch) { if (!konigSet.contains(v)) { konigSet.add(v); for (Vertex neighb : v.getNeighbors()) { if (neighb.getId() != SOURCE_ID && neighb.getId() != SINK_ID) { if (edgesInMatch == (getFlow(v, neighb) > 0 || getFlow(neighb, v) > 0)) { konigDFS(konigSet, neighb, !edgesInMatch); } } } } }
private void setupSinkSide(Set<Employee> emps) { for (Employee e : emps) { Vertex u = Vertex.vertexFromEmployee(e); RV.add(u); sink.addToNeighbors(u); u.addToNeighbors(sink); setCapacity(u, sink, 1); setFlow(u, sink, 0); V.add(u); } }