Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package adda2;
- import java.io.File;
- import java.util.Iterator;
- import java.util.function.Predicate;
- import org.jgrapht.*;
- import org.jgrapht.graph.SimpleWeightedGraph;
- import org.jgrapht.io.DOTExporter;
- import org.jgrapht.io.ExportException;
- import org.jgrapht.io.StringComponentNameProvider;
- import us.lsi.grafos.datos.Carretera;
- import us.lsi.grafos.datos.Ciudad;
- import us.lsi.graphs.GraphsReader;
- import us.lsi.graphs.views.SubGraphView;
- public class ejer1 {
- public static void main(String[] args) throws ExportException {
- SimpleWeightedGraph<Ciudad,Carretera> grafitoAndalucia =
- GraphsReader.newGraph("TestAlumnos/PI3Ej1DatosEntrada_andalucia.txt",
- Ciudad::ofFormat,
- Carretera::ofFormat,
- ()->new SimpleWeightedGraph<Ciudad, Carretera>(Ciudad::of,Carretera::of),
- Carretera::getKm);
- SimpleWeightedGraph<Ciudad,Carretera> grafitoCasstilla =
- GraphsReader.newGraph("TestAlumnos/PI3Ej1DatosEntrada_castillalamancha.txt",
- Ciudad::ofFormat,
- Carretera::ofFormat,
- ()->new SimpleWeightedGraph<Ciudad, Carretera>(Ciudad::of,Carretera::of),
- Carretera::getKm);
- System.out.println("Ejercicio 1 a:");
- String letra = "e";
- Predicate<Ciudad> p1 = x->x.getNombre().contains(letra);
- Predicate<Carretera> p2 = x->x.getKm() <= 100.0;
- SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> ResultadoAndalucia =
- SubGraphView.of(grafitoAndalucia, p1, p2);
- System.out.println(ResultadoAndalucia);
- SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> ResultadoCastilla =
- SubGraphView.of(grafitoCasstilla, p1, p2);
- System.out.println(ResultadoCastilla);
- DOTExporter<Ciudad, Carretera> de = new DOTExporter<Ciudad, Carretera>(
- new StringComponentNameProvider<>(),
- v -> v.getNombre(),
- e -> String.format("%.2f",e.getKm()));
- de.exportGraph(ResultadoAndalucia, new File("TestAlumnos/exportsP31aANDALUCIA1a_1.gv"));
- DOTExporter<Ciudad, Carretera> de1 = new DOTExporter<Ciudad, Carretera>(
- new StringComponentNameProvider<>(),
- v -> v.getNombre(),
- e -> String.format("%.2f",e.getKm()));
- de1.exportGraph(ResultadoCastilla, new File("TestAlumnos/exportsP31aCASTILLA1a_1.gv"));
- System.out.println("");
- Predicate<Ciudad> p3 = x->x.getHabitantes() <= 500000;
- Predicate<Carretera> p4 = x-> (x.getSource().toString().length() >= 8
- || x.getTarget().toString().length() >= 8)
- && x.getKm() >= 150;
- SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> ResultadoAndalucia2 =
- SubGraphView.of(grafitoAndalucia, p3, p4);
- System.out.println(ResultadoAndalucia2);
- SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> ResultadoCastilla2 =
- SubGraphView.of(grafitoCasstilla, p3, p4);
- System.out.println(ResultadoCastilla2);
- DOTExporter<Ciudad, Carretera> de3 = new DOTExporter<Ciudad, Carretera>(
- new StringComponentNameProvider<>(),
- v -> v.getNombre(),
- e -> String.format("%.2f",e.getKm()));
- de3.exportGraph(ResultadoAndalucia2, new File("TestAlumnos/exportsP31aANDALUCIA1a_2.gv"));
- DOTExporter<Ciudad, Carretera> de4 = new DOTExporter<Ciudad, Carretera>(
- new StringComponentNameProvider<>(),
- v -> v.getNombre(),
- e -> String.format("%.2f",e.getKm()));
- de4.exportGraph(ResultadoCastilla2, new File("TestAlumnos/exportsP31aCASTILLA1a_2.gv"));
- System.out.println("Ejercicio 1 b:");
- SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> grafAndCompleto
- = SubGraphView.of(grafitoAndalucia, grafitoAndalucia.vertexSet());
- Iterator<Ciudad> ite = grafitoAndalucia.vertexSet().iterator();
- while(ite.hasNext()) {
- Ciudad C1 = ite.next();
- Iterator<Ciudad> ite2 = grafitoAndalucia.vertexSet().iterator();
- while(ite2.hasNext()) {
- Ciudad C2 = ite2.next();
- Carretera posibilidad1 = Carretera.ofVertex(C1, C2);
- Carretera posibilidad2 = Carretera.ofVertex(C2, C1);
- Iterator<Carretera> carresAnda = grafitoAndalucia.edgeSet().iterator();
- while(carresAnda.hasNext()) {
- if (C1.equals(C2) == false && carresAnda.next().equals(posibilidad1) == false
- && carresAnda.next().equals(posibilidad2) == false) {
- posibilidad1.setKm(1000.0);
- grafAndCompleto.addEdge(C1, C2, posibilidad1);
- }
- }
- }
- }
- System.out.println(grafAndCompleto);
- SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> grafCasCompleto
- = SubGraphView.of(grafitoCasstilla, grafitoCasstilla.vertexSet());
- Iterator<Ciudad> ite1 = grafitoCasstilla.vertexSet().iterator();
- while(ite1.hasNext()) {
- Ciudad C1 = ite.next();
- Iterator<Ciudad> ite2 = grafitoAndalucia.vertexSet().iterator();
- while(ite2.hasNext()) {
- Ciudad C2 = ite2.next();
- Carretera posibilidad1 = Carretera.ofVertex(C1, C2);
- Carretera posibilidad2 = Carretera.ofVertex(C2, C1);
- Iterator<Carretera> carresCast = grafCasCompleto.edgeSet().iterator();
- while(carresCast.hasNext()) {
- if (C1.equals(C2) == false && carresCast.next().equals(posibilidad1) == false
- && carresCast.next().equals(posibilidad2) == false) {
- posibilidad1.setKm(1000.0);
- grafCasCompleto.addEdge(C1, C2, posibilidad1);
- }
- }
- }
- }
- System.out.println(grafCasCompleto);
- DOTExporter<Ciudad, Carretera> de5 = new DOTExporter<Ciudad, Carretera>(
- new StringComponentNameProvider<>(),
- v -> v.getNombre(),
- e -> String.format("%.2f",e.getKm()));
- de5.exportGraph(grafAndCompleto, new File("TestAlumnos/exportsP31aANDALUCIA1b.gv"));
- DOTExporter<Ciudad, Carretera> de6 = new DOTExporter<Ciudad, Carretera>(
- new StringComponentNameProvider<>(),
- v -> v.getNombre(),
- e -> String.format("%.2f",e.getKm()));
- de6.exportGraph(grafCasCompleto, new File("TestAlumnos/exportsP31aCASTILLA1b.gv"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement