/** * Pone una arista en el grafo que incidirá en los vértices arista.origen() y arista.destino(). Si * ya hay una, la sustituye. * * @param arista Arista que se va a añadir al grafo. */ public void ponerArista(Arista arista) throws IllegalArgumentException { if (!(this.existeIdVertice(arista.origen()) && this.existeIdVertice(arista.destino()))) { throw new IllegalArgumentException("Error al poner una arista: algún vértice no existe."); } if (arista.origen() == arista.destino()) { throw new IllegalArgumentException( "Error al poner una arista: deben ser identificadores diferentes."); } this.ponerArista(arista.origen(), arista.destino(), arista); }
/** * Devuelve cierto si las aristas conectan todos los vértices del grafo. Falso en caso contrario. * * @param aristas Las aristas. * @return Cierto si las aristas conectan todos los vértices del grafo. Falso en caso contrario. */ public boolean conectaTodosVertices(ArrayList<Arista> aristas) throws IllegalArgumentException { ArrayList<Vertice> V = new ArrayList<>(); int visitados = 0; for (int i = 0; i < aristas.size() && visitados < this.nVertices; i++) { Arista a = aristas.get(i); if (a == null) { throw new IllegalArgumentException( "Error al comprobar arista: referencia nula a la arista."); } int idOrigen = a.origen(); int idDestino = a.destino(); if (idOrigen == idDestino) { throw new IllegalArgumentException( "Error al comprobar arista: la arista tiene el mismo origen y destino."); } if (!(this.existeIdVertice(idOrigen) && this.existeIdVertice(idDestino))) { throw new IllegalArgumentException("Error al comprobar arista: algún vértice no existe."); } Vertice v1 = this.vertice(idOrigen); Vertice v2 = this.vertice(idDestino); if (!V.contains(v1)) { V.add(v1); visitados++; } if (!V.contains(v2)) { V.add(v2); visitados++; } } return visitados == this.nVertices; }