/** * @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()); }
/** * 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; }
/** * 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()); }