Beispiel #1
0
  /**
   * Accepts two vertices and a weight, and adds the edge ({one, two}, weight) iff no Edge relating
   * one and two exists in the Graph.
   *
   * @param one The first Vertex of the Edge
   * @param two The second Vertex of the Edge
   * @param weight The weight of the Edge
   * @return true iff no Edge already exists in the Graph
   */
  public boolean addEdge(Vertex one, Vertex two, double weight) {
    if (one.equals(two)) {
      return false;
    }

    // ensures the Edge is not in the Graph
    Edge e = new Edge(one, two, weight);
    if (edges.containsKey(e.hashCode())) {
      return false;
    }

    // and that the Edge isn't already incident to one of the vertices
    else if (one.containsNeighbor(e) || two.containsNeighbor(e)) {
      return false;
    }

    edges.put(e.hashCode(), e);
    one.addNeighbor(e);
    two.addNeighbor(e);
    return true;
  }