Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. package adda2;
  2.  
  3. import java.io.File;
  4. import java.util.Iterator;
  5. import java.util.function.Predicate;
  6.  
  7. import org.jgrapht.*;
  8. import org.jgrapht.graph.SimpleWeightedGraph;
  9. import org.jgrapht.io.DOTExporter;
  10. import org.jgrapht.io.ExportException;
  11. import org.jgrapht.io.StringComponentNameProvider;
  12.  
  13. import us.lsi.grafos.datos.Carretera;
  14. import us.lsi.grafos.datos.Ciudad;
  15. import us.lsi.graphs.GraphsReader;
  16. import us.lsi.graphs.views.SubGraphView;
  17.  
  18. public class ejer1 {
  19.  
  20. public static void main(String[] args) throws ExportException {
  21. SimpleWeightedGraph<Ciudad,Carretera> grafitoAndalucia =
  22. GraphsReader.newGraph("TestAlumnos/PI3Ej1DatosEntrada_andalucia.txt",
  23. Ciudad::ofFormat,
  24. Carretera::ofFormat,
  25. ()->new SimpleWeightedGraph<Ciudad, Carretera>(Ciudad::of,Carretera::of),
  26. Carretera::getKm);
  27.  
  28. SimpleWeightedGraph<Ciudad,Carretera> grafitoCasstilla =
  29. GraphsReader.newGraph("TestAlumnos/PI3Ej1DatosEntrada_castillalamancha.txt",
  30. Ciudad::ofFormat,
  31. Carretera::ofFormat,
  32. ()->new SimpleWeightedGraph<Ciudad, Carretera>(Ciudad::of,Carretera::of),
  33. Carretera::getKm);
  34.  
  35.  
  36. System.out.println("Ejercicio 1 a:");
  37.  
  38. String letra = "e";
  39.  
  40. Predicate<Ciudad> p1 = x->x.getNombre().contains(letra);
  41.  
  42. Predicate<Carretera> p2 = x->x.getKm() <= 100.0;
  43.  
  44. SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> ResultadoAndalucia =
  45. SubGraphView.of(grafitoAndalucia, p1, p2);
  46.  
  47. System.out.println(ResultadoAndalucia);
  48.  
  49. SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> ResultadoCastilla =
  50. SubGraphView.of(grafitoCasstilla, p1, p2);
  51.  
  52. System.out.println(ResultadoCastilla);
  53.  
  54.  
  55. DOTExporter<Ciudad, Carretera> de = new DOTExporter<Ciudad, Carretera>(
  56. new StringComponentNameProvider<>(),
  57. v -> v.getNombre(),
  58. e -> String.format("%.2f",e.getKm()));
  59. de.exportGraph(ResultadoAndalucia, new File("TestAlumnos/exportsP31aANDALUCIA1a_1.gv"));
  60.  
  61. DOTExporter<Ciudad, Carretera> de1 = new DOTExporter<Ciudad, Carretera>(
  62. new StringComponentNameProvider<>(),
  63. v -> v.getNombre(),
  64. e -> String.format("%.2f",e.getKm()));
  65. de1.exportGraph(ResultadoCastilla, new File("TestAlumnos/exportsP31aCASTILLA1a_1.gv"));
  66.  
  67.  
  68.  
  69. System.out.println("");
  70. Predicate<Ciudad> p3 = x->x.getHabitantes() <= 500000;
  71.  
  72. Predicate<Carretera> p4 = x-> (x.getSource().toString().length() >= 8
  73. || x.getTarget().toString().length() >= 8)
  74. && x.getKm() >= 150;
  75.  
  76.  
  77.  
  78. SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> ResultadoAndalucia2 =
  79. SubGraphView.of(grafitoAndalucia, p3, p4);
  80.  
  81. System.out.println(ResultadoAndalucia2);
  82.  
  83. SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> ResultadoCastilla2 =
  84. SubGraphView.of(grafitoCasstilla, p3, p4);
  85.  
  86. System.out.println(ResultadoCastilla2);
  87.  
  88.  
  89. DOTExporter<Ciudad, Carretera> de3 = new DOTExporter<Ciudad, Carretera>(
  90. new StringComponentNameProvider<>(),
  91. v -> v.getNombre(),
  92. e -> String.format("%.2f",e.getKm()));
  93. de3.exportGraph(ResultadoAndalucia2, new File("TestAlumnos/exportsP31aANDALUCIA1a_2.gv"));
  94.  
  95. DOTExporter<Ciudad, Carretera> de4 = new DOTExporter<Ciudad, Carretera>(
  96. new StringComponentNameProvider<>(),
  97. v -> v.getNombre(),
  98. e -> String.format("%.2f",e.getKm()));
  99. de4.exportGraph(ResultadoCastilla2, new File("TestAlumnos/exportsP31aCASTILLA1a_2.gv"));
  100.  
  101.  
  102.  
  103.  
  104.  
  105. System.out.println("Ejercicio 1 b:");
  106.  
  107. SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> grafAndCompleto
  108. = SubGraphView.of(grafitoAndalucia, grafitoAndalucia.vertexSet());
  109. Iterator<Ciudad> ite = grafitoAndalucia.vertexSet().iterator();
  110. while(ite.hasNext()) {
  111. Ciudad C1 = ite.next();
  112. Iterator<Ciudad> ite2 = grafitoAndalucia.vertexSet().iterator();
  113. while(ite2.hasNext()) {
  114. Ciudad C2 = ite2.next();
  115. Carretera posibilidad1 = Carretera.ofVertex(C1, C2);
  116. Carretera posibilidad2 = Carretera.ofVertex(C2, C1);
  117. Iterator<Carretera> carresAnda = grafitoAndalucia.edgeSet().iterator();
  118. while(carresAnda.hasNext()) {
  119. if (C1.equals(C2) == false && carresAnda.next().equals(posibilidad1) == false
  120. && carresAnda.next().equals(posibilidad2) == false) {
  121. posibilidad1.setKm(1000.0);
  122. grafAndCompleto.addEdge(C1, C2, posibilidad1);
  123. }
  124. }
  125.  
  126.  
  127. }
  128. }
  129.  
  130.  
  131.  
  132. System.out.println(grafAndCompleto);
  133.  
  134.  
  135. SubGraphView<Ciudad, Carretera, Graph<Ciudad,Carretera>> grafCasCompleto
  136. = SubGraphView.of(grafitoCasstilla, grafitoCasstilla.vertexSet());
  137. Iterator<Ciudad> ite1 = grafitoCasstilla.vertexSet().iterator();
  138. while(ite1.hasNext()) {
  139. Ciudad C1 = ite.next();
  140. Iterator<Ciudad> ite2 = grafitoAndalucia.vertexSet().iterator();
  141. while(ite2.hasNext()) {
  142. Ciudad C2 = ite2.next();
  143. Carretera posibilidad1 = Carretera.ofVertex(C1, C2);
  144. Carretera posibilidad2 = Carretera.ofVertex(C2, C1);
  145. Iterator<Carretera> carresCast = grafCasCompleto.edgeSet().iterator();
  146. while(carresCast.hasNext()) {
  147. if (C1.equals(C2) == false && carresCast.next().equals(posibilidad1) == false
  148. && carresCast.next().equals(posibilidad2) == false) {
  149. posibilidad1.setKm(1000.0);
  150. grafCasCompleto.addEdge(C1, C2, posibilidad1);
  151. }
  152. }
  153.  
  154.  
  155. }
  156. }
  157.  
  158.  
  159. System.out.println(grafCasCompleto);
  160.  
  161.  
  162. DOTExporter<Ciudad, Carretera> de5 = new DOTExporter<Ciudad, Carretera>(
  163. new StringComponentNameProvider<>(),
  164. v -> v.getNombre(),
  165. e -> String.format("%.2f",e.getKm()));
  166. de5.exportGraph(grafAndCompleto, new File("TestAlumnos/exportsP31aANDALUCIA1b.gv"));
  167.  
  168. DOTExporter<Ciudad, Carretera> de6 = new DOTExporter<Ciudad, Carretera>(
  169. new StringComponentNameProvider<>(),
  170. v -> v.getNombre(),
  171. e -> String.format("%.2f",e.getKm()));
  172. de6.exportGraph(grafCasCompleto, new File("TestAlumnos/exportsP31aCASTILLA1b.gv"));
  173.  
  174. }
  175.  
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement