Пример #1
0
  /**
   * @param e The Edge to look up
   * @return true iff this Graph contains the Edge e
   */
  public boolean containsEdge(Edge e) {
    if (e.getOne() == null || e.getTwo() == null) {
      return false;
    }

    return this.edges.containsKey(e.hashCode());
  }
Пример #2
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;
  }
Пример #3
0
  /**
   * This method adds an existing edge, which contains vertex one, two and weight.
   *
   * @param e
   * @return true if no edge relating to one and two exists in the Graph
   */
  public boolean addEdge(Edge e) {
    if (e.getOne().equals(e.getTwo())) {
      return false;
    }

    // ensures the Edge is not in the Graph

    if (edges.containsKey(e.hashCode())) {
      return false;
    }

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

    edges.put(e.hashCode(), e);
    e.getOne().addNeighbor(e);
    e.getTwo().addNeighbor(e);
    return true;
  }
Пример #4
0
 /**
  * This method removes the specified Edge from the Graph, including as each vertex's incidence
  * neighborhood.
  *
  * @param e The Edge to remove from the Graph
  * @return Edge The Edge removed from the Graph
  */
 public Edge removeEdge(Edge e) {
   e.getOne().removeNeighbor(e);
   e.getTwo().removeNeighbor(e);
   return this.edges.remove(e.hashCode());
 }