Advertisement
Guest User

graph

a guest
May 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. public class Graph
  2. {
  3. private Warteschlange w;
  4. private boolean [] [] am;
  5. private int maxKnotenAnzahl;
  6. private Knoten [] knotenliste;
  7. private int anzahl = 0;
  8.  
  9. private int anzahlZusammenhängenderKnoten=0;
  10.  
  11. public Graph(int maxKnotenAnzahl)
  12. {
  13. this.maxKnotenAnzahl = maxKnotenAnzahl;
  14. am = new boolean [maxKnotenAnzahl][maxKnotenAnzahl];
  15. knotenliste = new Knoten[maxKnotenAnzahl];
  16. w = new Warteschlange();
  17. }
  18.  
  19. public void knotenEinfügen(Knoten k)
  20. {
  21. if (anzahl < maxKnotenAnzahl)
  22. {
  23. knotenliste[anzahl] = k;
  24. k.setzeKnotennummer(anzahl);
  25. anzahl = anzahl+1;
  26. }
  27. else
  28. {
  29. System.out.println("Graph ist schon voll");
  30. }
  31.  
  32. }
  33.  
  34. public boolean istGerichtet()
  35. {
  36. for (int i = 0; i < maxKnotenAnzahl; i=i+1)
  37. for (int j = 0; j < maxKnotenAnzahl; j = j+1)
  38. if (am [i][j] != am [j][i])
  39. return true;
  40.  
  41. return false;
  42.  
  43. }
  44. public void kanteEinfügen(int von, int nach)
  45. { //Einfügen einer ungerichteten Kante
  46. am[von][nach] = true;
  47. am[nach][von] = true;
  48. }
  49.  
  50. public int gibKantenanzahl()
  51. {
  52. int zähler = 0;
  53. for (int i=0; i<maxKnotenAnzahl; i=i+1)
  54. {
  55. for (int j=0; j<maxKnotenAnzahl; j=j+1)
  56. {
  57. if (am[i][j] == true)
  58. zähler++;
  59. }
  60. }
  61. System.out.println(" Kantenanzahl = "+ zähler/2);
  62. return zähler/2;
  63. }
  64.  
  65. public void druckeMatrix()
  66. {
  67. String zeile = " ";
  68. for (int i=0; i<maxKnotenAnzahl; i=i+1)
  69. {
  70. if (i<10)
  71. zeile = zeile + " "+i;
  72. else
  73. zeile = zeile + i;
  74. }
  75. System.out.println(zeile);
  76.  
  77. for (int i=0; i<maxKnotenAnzahl; i=i+1)
  78. {
  79. zeile = "";
  80. if (i<10)
  81. zeile = zeile + " "+i;
  82. else
  83. zeile = zeile + i;
  84. for (int j=0; j<maxKnotenAnzahl; j=j+1)
  85. {
  86. if (am[i][j] == false)
  87. zeile = zeile + " .";
  88. else
  89. zeile = zeile + " X";
  90.  
  91. }
  92. System.out.println(zeile);
  93. }
  94. }
  95.  
  96. public int tiefensuche(int startNr)
  97. {
  98. // rekursive Methode aufrufen
  99. return tiefensucheKnoten(startNr);
  100. }
  101.  
  102. public int tiefensucheKnoten(int vindex)
  103. {
  104. int summe=1;
  105. // 0. gib den Knotenindex aus;
  106. System.out.println(" Index: "+ vindex);
  107. // 1. Knoten markieren
  108. knotenliste[vindex].markieren();
  109. //anzahlZusammenhängenderKnoten++;
  110. //System.out.println("Es gibt jetzt " + anzahlZusammenhängenderKnoten + " Knoten");
  111. System.out.println("Es gibt jetzt " + summe + " Knoten");
  112. // 2. in Adjazenzmatrix Nachbarn heraussuchen und unmarkierte
  113. // Nachbarn besuchen
  114. for (int i=0; i<maxKnotenAnzahl; i++)
  115. {
  116. if ((am[vindex][i] == true) && (knotenliste[i].istNichtMarkiert()))
  117. { summe = summe + tiefensucheKnoten(i);
  118. }
  119. }
  120. return summe;
  121. }
  122.  
  123. public void breitensuche(int startNr)
  124. {
  125. // in der Knotenliste unter startNr nachschauen
  126. // und diesen Knoten in Warteschlange w hinten anstellen
  127. w.hintenAnstellen(knotenliste[startNr]);
  128.  
  129. // solange Warteschlange nicht abgearbeitet ist,
  130. // weitermachen d.h.
  131. for (int i=0; i<maxKnotenAnzahl; i++) //Nachbarn von Startknoten einfügen
  132. {
  133. if ((am[startNr][i] == true) && (knotenliste[i].istNichtMarkiert()))
  134. { w.hintenAnstellen(knotenliste[i]);
  135. System.out.println("Es wurde Knoten "+i+" hinten angestellt");
  136. }
  137. }
  138. knotenliste[startNr].markieren();
  139. w.setzeAktuellWeiter();
  140.  
  141. while (w.istAktuellAmEnde() == false)
  142. {
  143. Listenelement le=w.gibAktuell();
  144. Knoten ak = (Knoten) le.gibInhalt();
  145. int vindex = ak.gibKnotennummer();
  146. System.out.println(" Index: "+ vindex);
  147.  
  148. for (int i=0; i<maxKnotenAnzahl; i++) //Nachbarn von Startknoten einfügen
  149. {
  150. if ((am[vindex][i] == true) && (knotenliste[i].istNichtMarkiert()))
  151. { w.hintenAnstellen(knotenliste[i]);
  152. }
  153. }
  154. knotenliste[vindex].markieren();
  155. }
  156.  
  157. }
  158.  
  159. // Nachbarn des aktuellen Knotens finden und die unmarkierten
  160. // Knoten in w hintenAnstellen()
  161.  
  162. // anschließend zum nächsten Knoten in w
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement