Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. package prim;
  2.  
  3. import java.util.*;
  4.  
  5. public class Prim {
  6.  
  7. PriorityQueue<Solmu> keko;
  8. ArrayList<Solmu>[] vieruslista;
  9. int[] etaisyys;
  10. Solmu[] vanhempi;
  11. int[][] painot;
  12. boolean[] onkoPuussa;
  13. Solmu[] verkko;
  14. int n;
  15. int maara;
  16.  
  17. public Prim(int n) {
  18. this.keko = new PriorityQueue<>();
  19. this.vieruslista = new ArrayList[n+1];
  20. this.etaisyys = new int[n+1];
  21. this.vanhempi = new Solmu[n+1];
  22. this.painot = new int[n+1][n+1];
  23. this.onkoPuussa = new boolean[n+1];
  24. this.verkko = new Solmu[n+1];
  25. this.n = n;
  26. this.maara = 0;
  27.  
  28. for (int i = 1; i <= n; i++) {
  29. vieruslista[i] = new ArrayList();
  30. }
  31.  
  32. onkoPuussa[1] = true;
  33.  
  34. etaisyys[1] = 0;
  35.  
  36. for (int i = 2; i <= n; i++) {
  37. etaisyys[i] = Integer.MAX_VALUE;
  38. }
  39.  
  40. }
  41.  
  42. public class Solmu implements Comparable {
  43.  
  44. public int numero, etaisyys;
  45.  
  46. public Solmu(int solmu) {
  47. this.numero = solmu;
  48. this.etaisyys = Integer.MAX_VALUE;
  49. }
  50.  
  51. @Override
  52. public int compareTo(Object o) {
  53. Solmu toinen = (Solmu) o;
  54. return this.etaisyys - toinen.etaisyys;
  55. }
  56. }
  57.  
  58. public void lisaaTie(int a, int b, int x) {
  59.  
  60. if (painot[a][b] > 0 && painot[a][b] < x) {
  61. return;
  62. }
  63.  
  64. Solmu solmuA = new Solmu(a);
  65. Solmu solmuB = new Solmu(b);
  66.  
  67. painot[a][b] = x;
  68. painot[b][a] = x;
  69.  
  70. vieruslista[a].add(solmuB);
  71. vieruslista[b].add(solmuA);
  72. }
  73.  
  74. public int laske() {
  75.  
  76. Solmu eka = new Solmu(1);
  77. eka.etaisyys = 0;
  78. keko.add(eka);
  79. verkko[1] = eka;
  80.  
  81. for (int i = 2; i <= n; i++) {
  82. Solmu uusi = new Solmu(i);
  83. verkko[i] = uusi;
  84. keko.add(uusi);
  85. }
  86.  
  87. int paino = 0;
  88. Solmu solmu;
  89.  
  90. while (maara <= n) {
  91.  
  92. if (keko.isEmpty()) {
  93. break;
  94. }
  95.  
  96. solmu = keko.poll();
  97.  
  98. if (vanhempi[solmu.numero] != null) {
  99. int u = solmu.numero;
  100. int uv = vanhempi[solmu.numero].numero;
  101.  
  102. if (!onkoPuussa[solmu.numero]) {
  103. maara++;
  104. }
  105.  
  106. onkoPuussa[solmu.numero] = true;
  107. }
  108.  
  109. for (int i = 0; i < vieruslista[solmu.numero].size(); i++) {
  110.  
  111. Solmu v = vieruslista[solmu.numero].get(i);
  112.  
  113. if (!onkoPuussa[v.numero] && painot[solmu.numero][v.numero] < etaisyys[v.numero]) {
  114. vanhempi[v.numero] = solmu;
  115. etaisyys[v.numero] = painot[solmu.numero][v.numero];
  116. v.etaisyys = painot[solmu.numero][v.numero];
  117. keko.add(v);
  118. }
  119. }
  120. }
  121.  
  122. for (int i = 2; i <= n; i++) {
  123. paino = paino + painot[vanhempi[i].numero][i];
  124. }
  125.  
  126. return paino;
  127. }
  128.  
  129. public static void main(String[] args) {
  130.  
  131. HashSet<Integer> solmut = new HashSet<>();
  132.  
  133. int n = 5000;
  134. int m = 100000;
  135.  
  136. Prim k = new Prim(n);
  137.  
  138. Random r = new Random(1337);
  139. for (int i = 1; i <= m; i++) {
  140. int a = r.nextInt(n) + 1;
  141. int b = r.nextInt(n) + 1;
  142. int x = r.nextInt(1000) + 1;
  143. k.lisaaTie(a, b, x);
  144.  
  145. solmut.add(a);
  146. solmut.add(b);
  147. }
  148.  
  149. long alku = System.nanoTime();
  150.  
  151. System.out.println(k.laske());
  152.  
  153. long loppu = System.nanoTime();
  154.  
  155. System.out.println("Aikaa kului " + ((loppu-alku) / 1e9) + " s");
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement