Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void addEdge(E e, V v1, V v2) {
- if (!vertexToNeighbours.containsKey(v1)) {
- throw new IllegalArgumentException("Graph doesn't contain vertex " + v1);
- }
- if (!vertexToNeighbours.containsKey(v2)) {
- throw new IllegalArgumentException("Graph doesn't contain vertex " + v2);
- }
- UnorderedPair<V> endpoints = new UnorderedPair<>(v1, v2);
- if (edgeToEndpoints.containsValue(endpoints)) {
- throw new IllegalArgumentException("Graph already contains directed"
- + " edge from vertex " + v1 + " to vertex " + v2 + ".");
- } else if (edgeToEndpoints.containsKey(e)) {
- throw new IllegalArgumentException("Graph already containts edge "
- + e + "connecting these guys: " + edgeToEndpoints.get(e));
- } else {
- edgeToEndpoints.put(e, endpoints);
- vertexToNeighbours.get(v1).addNeighbour(v2);
- vertexToNeighbours.get(v2).addNeighbour(v1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement