Created
March 20, 2019 15:57
-
-
Save rodrigovilar/a42d4b9ee27fa212375c41e06ec82090 to your computer and use it in GitHub Desktop.
Grafos - Aula 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Aresta { | |
private Vertice origem; | |
private Vertice destino; | |
private int distancia; | |
public Vertice getOrigem() { | |
return origem; | |
} | |
public void setOrigem(Vertice origem) { | |
this.origem = origem; | |
} | |
public Vertice getDestino() { | |
return destino; | |
} | |
public void setDestino(Vertice destino) { | |
this.destino = destino; | |
} | |
public int getDistancia() { | |
return distancia; | |
} | |
public void setDistancia(int distancia) { | |
this.distancia = distancia; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
public interface Grafo { | |
Vertice addVertice(String nome); | |
void addAresta(Vertice v1, Vertice v2, int distancia); | |
List<Vertice> getVertices(); | |
int getDistanciaDireta(Vertice v1, Vertice v2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class GrafoComListaDeAdjacencias implements Grafo { | |
private List<Vertice> vertices = new ArrayList<>(); | |
private List<VerticeInterno> verticesInternos = new ArrayList<>(); | |
@Override | |
public Vertice addVertice(String nome) { | |
Vertice vertice = new Vertice(); | |
vertice.setNome(nome); | |
vertices.add(vertice); | |
VerticeInterno verticeInterno = new VerticeInterno(); | |
verticeInterno.setVertice(vertice); | |
verticesInternos.add(verticeInterno); | |
return vertice; | |
} | |
private VerticeInterno getVerticeInterno(Vertice v) { | |
for (VerticeInterno verticeInterno : verticesInternos) { | |
if (verticeInterno.getVertice().equals(v)) { | |
return verticeInterno; | |
} | |
} | |
throw new RuntimeException("Vertice interno não encontrado"); | |
} | |
@Override | |
public void addAresta(Vertice v1, Vertice v2, int distancia) { | |
Aresta ida = new Aresta(); | |
ida.setOrigem(v1); | |
ida.setDestino(v2); | |
ida.setDistancia(distancia); | |
VerticeInterno verticeInternoV1 = getVerticeInterno(v1); | |
verticeInternoV1.getArestas().add(ida); | |
Aresta volta = new Aresta(); | |
volta.setOrigem(v2); | |
volta.setDestino(v1); | |
volta.setDistancia(distancia); | |
VerticeInterno verticeInternoV2 = getVerticeInterno(v2); | |
verticeInternoV2.getArestas().add(volta); | |
} | |
@Override | |
public List<Vertice> getVertices() { | |
return vertices; | |
} | |
@Override | |
public int getDistanciaDireta(Vertice v1, Vertice v2) { | |
VerticeInterno verticeInternoV1 = getVerticeInterno(v1); | |
for (Aresta aresta : verticeInternoV1.getArestas()) { | |
if (aresta.getDestino().equals(v2)) { | |
return aresta.getDistancia(); | |
} | |
} | |
return -1; | |
} | |
} | |
class VerticeInterno { | |
private Vertice vertice; | |
private List<Aresta> arestas = new ArrayList<>(); | |
public Vertice getVertice() { | |
return vertice; | |
} | |
public void setVertice(Vertice vertice) { | |
this.vertice = vertice; | |
} | |
public List<Aresta> getArestas() { | |
return arestas; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.*; | |
import java.util.List; | |
import org.junit.Test; | |
public class GrafoTest { | |
@Test | |
public void test() { | |
Grafo grafo = new GrafoComListaDeAdjacencias(); | |
Vertice mamanguape = grafo.addVertice("Mamanguape"); | |
Vertice rioTinto = grafo.addVertice("Rio Tinto"); | |
Vertice marcacao = grafo.addVertice("Marcacao"); | |
Vertice baiaDaTraicao = grafo.addVertice("Baía da traição"); | |
Vertice sape = grafo.addVertice("Sapé"); | |
Vertice capim = grafo.addVertice("Capim"); | |
Vertice santaRita = grafo.addVertice("Santa Rita"); | |
Vertice cuiteDeMamanguape = grafo.addVertice("Cuité de Mamanguape"); | |
Vertice jacarau = grafo.addVertice("Jacaraú"); | |
Vertice itapororoca = grafo.addVertice("Itapororoca"); | |
grafo.addAresta(mamanguape, rioTinto, 8); | |
grafo.addAresta(rioTinto, marcacao, 9); | |
grafo.addAresta(marcacao, baiaDaTraicao, 14); | |
grafo.addAresta(mamanguape, jacarau, 36); | |
grafo.addAresta(mamanguape, itapororoca, 14); | |
grafo.addAresta(itapororoca, cuiteDeMamanguape, 10); | |
grafo.addAresta(cuiteDeMamanguape, capim, 11); | |
grafo.addAresta(capim, sape, 24); | |
grafo.addAresta(capim, mamanguape, 12); | |
grafo.addAresta(mamanguape, santaRita, 45); | |
grafo.addAresta(sape, santaRita, 39); | |
List<Vertice> vertices = grafo.getVertices(); | |
assertTrue(vertices.contains(mamanguape)); | |
assertTrue(vertices.contains(rioTinto)); | |
assertTrue(vertices.contains(marcacao)); | |
assertTrue(vertices.contains(baiaDaTraicao)); | |
assertTrue(vertices.contains(sape)); | |
assertTrue(vertices.contains(capim)); | |
assertTrue(vertices.contains(santaRita)); | |
assertTrue(vertices.contains(cuiteDeMamanguape)); | |
assertTrue(vertices.contains(jacarau)); | |
assertTrue(vertices.contains(itapororoca)); | |
assertEquals(10, vertices.size()); | |
assertEquals(8, grafo.getDistanciaDireta(mamanguape, rioTinto)); | |
assertEquals(9, grafo.getDistanciaDireta(rioTinto, marcacao)); | |
assertEquals(14, grafo.getDistanciaDireta(marcacao, baiaDaTraicao)); | |
assertEquals(36, grafo.getDistanciaDireta(mamanguape, jacarau)); | |
assertEquals(14, grafo.getDistanciaDireta(mamanguape, itapororoca)); | |
assertEquals(10, grafo.getDistanciaDireta(itapororoca, cuiteDeMamanguape)); | |
assertEquals(11, grafo.getDistanciaDireta(cuiteDeMamanguape, capim)); | |
assertEquals(24, grafo.getDistanciaDireta(capim, sape)); | |
assertEquals(12, grafo.getDistanciaDireta(capim, mamanguape)); | |
assertEquals(45, grafo.getDistanciaDireta(mamanguape, santaRita)); | |
assertEquals(39, grafo.getDistanciaDireta(sape, santaRita)); | |
assertEquals(-1, grafo.getDistanciaDireta(mamanguape, baiaDaTraicao)); | |
assertEquals(-1, grafo.getDistanciaDireta(mamanguape, sape)); | |
assertEquals(-1, grafo.getDistanciaDireta(mamanguape, marcacao)); | |
assertEquals(-1, grafo.getDistanciaDireta(rioTinto, itapororoca)); | |
assertEquals(-1, grafo.getDistanciaDireta(sape, baiaDaTraicao)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Vertice { | |
private String nome; | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment