private void checkExcepction(Collection<Vertex> v, Collection<Edge> e) {
    for (Edge edge : e) {
      // No edge from or to vertex labeled A if there is no vertex with
      // label A
      if (!v.contains(edge.getSource()) || !v.contains(edge.getDestination())) {
        throw new IllegalArgumentException();
      }

      // Edge weights should not be negative
      if (edge.getWeight() < 0) {
        throw new IllegalArgumentException();
      }

      // Collection of edges should not has the same directed edge more
      // than once
      // with a different weight
      for (Edge checkEdges : e) {
        if (checkEdges.getSource().equals(edge.getSource())
            && checkEdges.getDestination().equals(edge.getDestination())
            && checkEdges.getWeight() != edge.getWeight()) {
          throw new IllegalArgumentException();
        }
      }
    }
  }
 @Override
 public Vertex<V> opposite(Vertex<V> v, Edge<E, V> e) {
   Vertex<V> vt;
   if (e.getSource().equals(v)) vt = (Vertex<V>) e.getDestination();
   else if (e.getDestination().equals(v)) vt = (Vertex<V>) e.getSource();
   else vt = null;
   return vt;
 }
 @SuppressWarnings("unchecked")
 @Override
 public boolean areAdjacent(Vertex<V> v, Vertex<V> w) {
   boolean b = false;
   Edge<E, V> eg = (Edge<E, V>) edges.getHeader();
   while (eg != null && !b) {
     if ((eg.getDestination().equals(v) || eg.getSource().equals(v))
         && (eg.getDestination().equals(w) || eg.getSource().equals(w))) b = true;
     try {
       eg = (Edge<E, V>) edges.next((Pointer<E>) eg.getPosition()).element();
     } catch (Exception exc) {
       eg = null;
     }
   }
   return b;
 }
Beispiel #4
0
 public Node whatNodeIGetTo(Node from, Transition with) {
   for (Edge e : myGraph.edgeSet()) {
     if (e.getSource().equals(from) && e.getTransition().equals(with)) {
       return e.getDest();
     }
   }
   return null;
 }
Beispiel #5
0
 private Graph buildGraph(List<Edge> edges) {
   Graph toReturn = new Graph();
   for (Edge e : edges) {
     toReturn.addNode(e.getSource());
     toReturn.addNode(e.getDest());
     toReturn.addEdge(e);
   }
   return toReturn;
 }
Beispiel #6
0
 public List<Graph> getAllEdgeCoverage() {
   List<Edge> toVisit = new ArrayList<>(myGraph.edgeSet());
   List<Edge> left = new ArrayList<>(myGraph.edgeSet());
   List<Graph> toReturn = new ArrayList<>();
   for (Edge e : toVisit) {
     if (left.contains(e)) {
       if (getInitialState().equals(e.getSource())) {
         List<Edge> toAdd = new ArrayList<>();
         toAdd.add(e);
         toReturn.add(buildGraph(toAdd));
       } else {
         List<Edge> path =
             BellmanFordShortestPath.findPathBetween(myGraph, getInitialState(), e.getSource());
         path.add(e);
         left.remove(e);
         left.removeAll(path);
         toReturn.add(buildGraph(path));
       }
     }
   }
   return toReturn;
 }
  /**
   * Creates a MyGraph object with the given collection of vertices and the given collection of
   * edges.
   *
   * @param v a collection of the vertices in this graph
   * @param e a collection of the edges in this graph
   */
  public MyGraph(Collection<Vertex> v, Collection<Edge> e) {
    // collection passed in should not be null
    if (v == null || e == null) {
      throw new IllegalArgumentException();
    }

    adj = new HashMap<Vertex, ArrayList<Edge>>();
    totalEdges = new ArrayList<Edge>();
    totalVertices = new ArrayList<Vertex>();
    checkExcepction(v, e);

    // loop through a collection
    // for (iterable_type iterable_element : collection)
    // Add edges and vertex to the map
    for (Edge edge : e) {
      if (!totalEdges.contains(edge)) {
        totalEdges.add(edge);
      }
      // if this vertex is not in key groups of the HashMap, add it to the group
      if (!adj.containsKey(edge.getSource())) {
        adj.put(edge.getSource(), new ArrayList<Edge>());
      }
      // add edge to the ArrayList, which is the "value" in key-value pairs in HashMap, adj.
      adj.get(edge.getSource()).add(edge);
    }

    for (Vertex vertex : v) {
      if (!totalVertices.contains(vertex)) {
        totalVertices.add(vertex);
      }

      if (!adj.containsKey(
          vertex)) { // add this vertex as a key in adj even if there might not be edge extending
                     // from it,
        // namely even if we end up with empty ArrayList as value of this key
        adj.put(vertex, new ArrayList<Edge>());
      }
    }
  }
 @SuppressWarnings("unchecked")
 @Override
 public void reverseDirection() {
   Edge<E, V> h = (Edge<E, V>) edges.getHeader();
   while (h != null) {
     Vertex<V> temp = h.getSource();
     h.setSource(h.getDestination());
     h.setDestination(temp);
     try {
       h = (Edge<E, V>) edges.next((Pointer<E>) h.getPosition()).element();
     } catch (Exception exc) {
       h = null;
     }
   }
 }
 @SuppressWarnings("unchecked")
 public String printEdges(String type) {
   String s = "";
   Edge<E, V> eg = (Edge<E, V>) edges.getHeader();
   while (eg != null) {
     if (eg.getType().equals(type))
       s += eg.getSource().getInfo() + " -> " + eg.getDestination().getInfo() + "\n";
     try {
       eg = (Edge<E, V>) edges.next((Pointer<E>) eg.getPosition()).element();
     } catch (Exception exc) {
       eg = null;
     }
   }
   return s;
 }
  @SuppressWarnings("unchecked")
  @Override
  public Iterator<Vertex<V>> adjacentVertices(Vertex<V> v) {
    List<Vertex<V>> coll = new ArrayList<Vertex<V>>();
    Edge<E, V> head_edg = (Edge<E, V>) edges.getHeader();
    while (head_edg != null) {
      if (head_edg.getSource().equals(v)) coll.add((Vertex<V>) head_edg.getDestination());
      try {
        head_edg = (Edge<E, V>) edges.next((Pointer<E>) head_edg.getPosition()).element();
      } catch (Exception exc) {
        head_edg = null;
      }
    }

    return coll.iterator();
  }
Beispiel #11
0
 public void addEdge(Edge toAdd) {
   myGraph.addEdge(toAdd.getSource(), toAdd.getDest(), toAdd);
 }