Advertisement
dudarib

Untitled

May 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package edu.ufp.inf.aed2.aulas;
  7.  
  8. import edu.princeton.cs.algs4.Stack;
  9. import static edu.princeton.cs.algs4.StdOut.print;
  10.  
  11. /**
  12. *
  13. * @author dudar
  14. */
  15. public class Aed2_Aula7_Graph {
  16.  
  17. /**
  18. * @param args the command line arguments
  19. */
  20. public static void main(String[] args) {
  21. /*
  22. Graph g = new Graph(8);
  23. g.addEdge(0, 1);
  24. g.addEdge(0, 3);
  25. g.addEdge(0, 7);
  26. g.addEdge(1, 2);
  27. g.addEdge(1, 6);
  28. g.addEdge(2, 3);
  29. g.addEdge(2, 5);
  30. g.addEdge(3, 4);
  31. g.addEdge(4, 5);
  32. g.addEdge(4, 7);
  33. g.addEdge(5, 6);
  34. g.addEdge(6, 7);
  35. System.out.println(g);
  36.  
  37. Bipartite_AED2 bipartite = new Bipartite_AED2(g);
  38. System.out.println("Bipartido: "+ bipartite.isBipartite());
  39. System.out.println("0 e 3 sao adjacentes?: "+ g.areAdjacent(0, 3));
  40. // resolver a matriz de adjacencias para o exercicio de cima
  41.  
  42. */
  43.  
  44. /*
  45. // exercicio de dfs iterativo
  46. Graph g = new Graph(6);
  47. g.addEdge(0, 1);
  48. g.addEdge(0, 2);
  49. g.addEdge(0, 3);
  50. g.addEdge(1, 4);
  51. g.addEdge(3, 5);
  52. g.addEdge(1, 2);
  53. client_dfs_iterativo(g, 0);
  54. */
  55. Graph g = new Graph(10);
  56.  
  57. g.addEdge(0, 1);
  58. g.addEdge(0, 2);
  59. g.addEdge(0, 3);
  60. g.addEdge(1, 4);
  61. g.addEdge(2, 5);
  62. g.addEdge(3, 5);
  63. g.addEdge(3, 6);
  64. g.addEdge(4, 9);
  65. g.addEdge(5, 7);
  66. g.addEdge(5, 8);
  67. g.addEdge(6, 8);
  68.  
  69. kNeighbours(g, 0 , 2);
  70. g.isComplete();
  71.  
  72. }
  73.  
  74. public static void client_dfs_iterativo(Graph g, int s) {
  75. boolean[] marked = new boolean[g.V()];
  76. // for(int i=0; i<g.V();i++){
  77. //print(marked[i]);
  78. //}
  79. Stack<Integer> stack = new Stack<>();
  80. stack.push(s);
  81.  
  82. while (!stack.isEmpty()) {
  83. s = stack.pop();
  84.  
  85. if (!marked[s]) {
  86. print(s);
  87. System.out.print(" ");
  88. marked[s] = true;
  89. for (Integer v : g.adj(s)) {
  90. if (!marked[v]) {
  91. stack.push(v);
  92. }
  93. }
  94. }
  95. }
  96. }
  97.  
  98. public static void kNeighbours(Graph g, int s, int k) {
  99.  
  100. BreadthFirstPaths bfs = new BreadthFirstPaths(g, s);
  101. for (int i = 0; i < g.V(); i++) {
  102. if (i != s && bfs.distTo(i) <= k) {
  103. System.out.println(i);
  104.  
  105. }
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement