private List<Vertex> getAdjacentes(Vertex vert) { List<Vertex> adj = new ArrayList<Vertex>(); for (Edge arco : arcos) { if (arco.getSource().equals(vert) && !isVisited(arco.getDestination())) { adj.add(arco.getDestination()); } } return adj; }
private List<Vertex> getNeighbors(Vertex node) { List<Vertex> neighbors = new ArrayList<Vertex>(); for (Edge edge : edges) { if (edge.getSource().equals(node) && !isSettled(edge.getDestination())) { neighbors.add(edge.getDestination()); } } return neighbors; }
public boolean removeEdgeTo(Vertex destination) { for (Edge edge : getOutgoingEdges()) { if (edge.getDestination() == destination) { getOutgoingEdges().remove(edge); edge.getDestination().getIncomingEdges().remove(edge); graph.edges().remove(edge); return true; } } return false; }
private static <T> void getPathFind(Graph<T> graph, HashMap<T, nodeBody<T>> path, T n1, T n2) { if (!(n1.equals(n2))) { for (Edge<T> e : graph.getEdgesFrom(n1)) { T neighbor = e.getDestination(); if (!(path.containsKey(neighbor))) { path.put(neighbor, new nodeBody<T>()); } int newCost = path.get(n1).getCost() + e.getWeight(); nodeBody<T> nBody = path.get(neighbor); if (newCost < nBody.getCost()) { nBody.setCost(newCost); nBody.setFrom(n1); } } T next = null; int lowestCost = Integer.MAX_VALUE; for (T n : path.keySet()) { nodeBody<T> c = path.get(n); if (!c.isDone()) { if (c.getCost() < lowestCost) { next = n; lowestCost = c.getCost(); } } } path.get(next).setDone(true); getPathFind(graph, path, next, n2); } else return; }
public void compAdjEdges(Vertex s, int w) { Vertex source = s; int vertWeight = w; int tempWeight; int origWeight; /* Each adjacent edge to the source Vertex, * (if it has not yet been handled) * has a weight which is added to the current pathWeight. * If this pathWeight is smaller than the one associated with * the given edge's vertex (destination.getWeight()) * then the vertex's weight is updated and this path is * added to the priority queue */ for (Edge e : source.adjacentD) { Edge curEdge = e; Vertex curVer = e.getDestination(); origWeight = curVer.getWeight(); if (curVer.known == false) { tempWeight = curEdge.getWeight(); tempWeight = tempWeight + vertWeight; if (tempWeight < origWeight) { curVer.setWeight(tempWeight); curVer.previous = source; pq.add(curVer); } } } }
private int getDistance(Vertex node, Vertex target) { for (Edge edge : edges) { if (edge.getSource().equals(node) && edge.getDestination().equals(target)) { return edge.getWeight(); } } throw new RuntimeException("Should not happen"); }
private int getDistance(Vertex node, Vertex target) { for (Edge edge : arcos) { if (edge.getSource().equals(node) && edge.getDestination().equals(target)) { return edge.getWeight(); } } throw new RuntimeException("Inalcancavel"); }
@Override public T next() { if (hasNext()) { Edge<T> tmp = nextEdge; nextEdge = null; return source ? tmp.getSource().getData() : tmp.getDestination().getData(); } throw new NoSuchElementException(); }
public void removeAllEdges() { for (Edge edge : getIncomingEdges()) { edge.getSource().getOutgoingEdges().remove(edge); } incoming = null; for (Edge edge : getOutgoingEdges()) { edge.getDestination().getIncomingEdges().remove(edge); } outgoing = null; }
private void start(Graph<E> graph) { E min; while ((min = getMin()) != null) { // label min as visited unvisited.remove(min); // for each min neighbor... for (Edge<E> edge : graph.getOutboundEdges(min)) { int alt = dist.get(min) + edge.getWeight(); if (alt < dist.get(edge.getDestination())) { // a shorter path was found! dist.put(edge.getDestination(), alt); prev.get(edge.getDestination()).clear(); prev.get(edge.getDestination()).add(min); } else if (alt == dist.get(edge.getDestination())) { // another path was found! prev.get(edge.getDestination()).add(min); } } } }
private static <T> List<Edge<T>> getPathReturn( Graph<T> graph, HashMap<T, nodeBody<T>> path, T n1, T n2) { if (!(n1.equals(n2))) { T next = path.get(n1).getFrom(); Edge<T> RetE = null; int lowestCost = Integer.MAX_VALUE; for (Edge<T> e : graph.getEdgesBetween(n1, next)) { if ((e.getWeight() < lowestCost) && (e.getDestination().equals(n1))) { RetE = e; lowestCost = e.getWeight(); } } for (Edge<T> e : graph.getEdgesBetween(next, n1)) { if ((e.getWeight() < lowestCost) && (e.getDestination().equals(n1))) { RetE = e; lowestCost = e.getWeight(); } } List<Edge<T>> l = getPathReturn(graph, path, next, n2); l.add(RetE); return l; } else return new ArrayList<Edge<T>>(); }
@Override /** Computes the shortest path from the startId given to all other vertices */ public void computeShortestPath() throws IllegalStateException { vertices = new ArrayList<Vertex>(); isComputed = true; Vertex currentVertex = startGraph.getData(startId); PriorityQueue<Vertex> vertexList = new PriorityQueue<Vertex>(); vertexList.add(currentVertex); currentVertex.setDistance(0); // Here we reset the vertex colors after every calculation for (Vertex vertex : (ArrayList<Vertex>) ((CoffeeGraph) startGraph).getVertexList()) { vertex.setColor(0); } // Basic implementation of Dijkstras algorithm. while (!vertexList.isEmpty()) { currentVertex = vertexList.poll(); if (currentVertex.getColor() == 0) { currentVertex.setColor(1); for (Edge edge : currentVertex.getEdgeSet()) { Vertex tempVertex = edge.getDestination(); double tempDistance = weighing.weight(edge); double totalDistance = currentVertex.getDistance() + tempDistance; if (totalDistance < tempVertex.getDistance()) { vertexList.remove(tempVertex); tempVertex.setDistance(totalDistance); tempVertex.setPrevious(currentVertex); vertexList.add(tempVertex); if (!vertices.contains(tempVertex)) { vertices.add(tempVertex); } } } } } }
/** Construct the parallel composition of two PTAs. */ public PTA compose(PTA pta1, PTA pta2) { Set<String> alpha1, alpha2, alpha1only, alpha2only, sync; Transition transition; Edge edge; double prob; IndexPair state; int src, dest; // Store PTAs locally and create new one to store parallel composition this.pta1 = pta1; this.pta2 = pta2; par = new PTA(); // New set of clocks is union of sets for two PTAs for (String s : pta1.clockNames) { par.getOrAddClock(s); } for (String s : pta2.clockNames) { par.getOrAddClock(s); } // Get alphabets, compute intersection etc. alpha1 = pta1.getAlphabet(); alpha2 = pta2.getAlphabet(); // System.out.println("alpha1: " + alpha1); // System.out.println("alpha2: " + alpha2); sync = new LinkedHashSet<String>(); alpha1only = new LinkedHashSet<String>(); alpha2only = new LinkedHashSet<String>(); for (String a : alpha1) { if (!("".equals(a)) && alpha2.contains(a)) { sync.add(a); } else { alpha1only.add(a); } } for (String a : alpha2) { if (!alpha1.contains(a)) { alpha2only.add(a); } } // Explicitly add tau to action lists alpha1only.add(""); alpha2only.add(""); // System.out.println("alpha1only: " + alpha1only); // System.out.println("alpha2only: " + alpha2only); // System.out.println("sync: " + sync); // Initialise states storage states = new IndexedSet<IndexPair>(); explore = new LinkedList<IndexPair>(); // Add initial location addState(0, 0); src = -1; while (!explore.isEmpty()) { // Pick next state to explore // (they are stored in order found so know index is src+1) state = explore.removeFirst(); src++; // Go through asynchronous transitions of PTA 1 for (String a : alpha1only) { for (Transition transition1 : pta1.getTransitionsByAction(state.i1, a)) { // Create new transition transition = par.addTransition(src, a); // Copy guard for (Constraint c : transition1.getGuardConstraints()) transition.addGuardConstraint(c.deepCopy().renameClocks(pta1, par)); // Combine edges for (Edge edge1 : transition1.getEdges()) { prob = edge1.getProbability(); dest = addState(edge1.getDestination(), state.i2); edge = transition.addEdge(prob, dest); // Copy resets for (Map.Entry<Integer, Integer> e : edge1.getResets()) { edge.addReset(PTA.renameClock(pta1, par, e.getKey()), e.getValue()); } } } } // Go through asynchronous transitions of PTA 2 for (String a : alpha2only) { for (Transition transition2 : pta2.getTransitionsByAction(state.i2, a)) { // Create new transition transition = par.addTransition(src, a); // Copy guard for (Constraint c : transition2.getGuardConstraints()) transition.addGuardConstraint(c.deepCopy().renameClocks(pta2, par)); // Combine edges for (Edge edge2 : transition2.getEdges()) { prob = edge2.getProbability(); dest = addState(state.i1, edge2.getDestination()); edge = transition.addEdge(prob, dest); // Copy resets for (Map.Entry<Integer, Integer> e : edge2.getResets()) { edge.addReset(PTA.renameClock(pta2, par, e.getKey()), e.getValue()); } } } } // Go through synchronous transitions for (String a : sync) { for (Transition transition1 : pta1.getTransitionsByAction(state.i1, a)) { for (Transition transition2 : pta2.getTransitionsByAction(state.i2, a)) { // Create new transition transition = par.addTransition(src, a); // Guard is conjunction of guards for (Constraint c : transition1.getGuardConstraints()) transition.addGuardConstraint(c.deepCopy().renameClocks(pta1, par)); for (Constraint c : transition2.getGuardConstraints()) transition.addGuardConstraint(c.deepCopy().renameClocks(pta2, par)); // Combine edges for (Edge edge1 : transition1.getEdges()) { for (Edge edge2 : transition2.getEdges()) { prob = edge1.getProbability() * edge2.getProbability(); dest = addState(edge1.getDestination(), edge2.getDestination()); edge = transition.addEdge(prob, dest); // Reset set is union of reset sets for (Map.Entry<Integer, Integer> e : edge1.getResets()) { edge.addReset(PTA.renameClock(pta1, par, e.getKey()), e.getValue()); } for (Map.Entry<Integer, Integer> e : edge2.getResets()) { edge.addReset(PTA.renameClock(pta2, par, e.getKey()), e.getValue()); } } } } } } } return par; }
/** Build a time bounded reachability query into a PTA; return the new target location set. */ private BitSet buildTimeBoundIntoPta( PTA pta, BitSet targetLocs, int timeBound, boolean timeBoundStrict) { String timerClock = null; int timerClockIndex, numLocs, newTargetLoc; String newTargetLocString; List<Transition> trNewList; Transition trNew; BitSet targetLocsNew; boolean toTarget; int i; // Add a timer clock timerClock = "time"; while (pta.getClockIndex(timerClock) != -1) timerClock += "_"; timerClockIndex = pta.addClock(timerClock); // Add a new target location numLocs = pta.getNumLocations(); newTargetLocString = "target"; while (pta.getLocationIndex(newTargetLocString) != -1) newTargetLocString += "_"; newTargetLoc = pta.addLocation(newTargetLocString); // Go through old (on-target) locations for (i = 0; i < numLocs; i++) { trNewList = new ArrayList<Transition>(); for (Transition tr : pta.getTransitions(i)) { // See if the transition can go to a target location toTarget = false; for (Edge e : tr.getEdges()) { if (targetLocs.get(e.getDestination())) { toTarget = true; break; } } // Copy transition, modify edges going to target and add guard if (toTarget) { trNew = new Transition(tr); for (Edge e : trNew.getEdges()) { if (targetLocs.get(e.getDestination())) { e.setDestination(newTargetLoc); } } if (timeBoundStrict) trNew.addGuardConstraint(Constraint.buildLt(timerClockIndex, timeBound)); else trNew.addGuardConstraint(Constraint.buildLeq(timerClockIndex, timeBound)); trNewList.add(trNew); // Modify guard of copied transition if (timeBoundStrict) tr.addGuardConstraint(Constraint.buildGeq(timerClockIndex, timeBound)); else tr.addGuardConstraint(Constraint.buildGt(timerClockIndex, timeBound)); } } // Add new transitions to PTA for (Transition tr : trNewList) { pta.addTransition(tr); } } // Re-generate set of target locations targetLocsNew = new BitSet(pta.getNumLocations()); targetLocsNew.set(newTargetLoc); return targetLocsNew; }
private static <T> void depthFirstSearch(Graph<T> graph, T n, Set<T> v) { v.add(n); for (Edge<T> e : graph.getEdgesFrom(n)) if (!v.contains(e.getDestination())) depthFirstSearch(graph, e.getDestination(), v); }