Advertisement
Sylv3rWolf

Untitled

Nov 3rd, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 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 grafy;
  7.  
  8. /**
  9. *
  10. * @author Stud
  11. */
  12. public class TGraph extends AGraph {
  13.  
  14. int[][] matrix;
  15.  
  16. public TGraph(int vertexCount) {
  17. super(vertexCount);
  18. matrix = new int [100][100];
  19.  
  20. }
  21.  
  22. @Override
  23. public void writeMatrix() {
  24. for (int i = 0; i < size; i++) {
  25.  
  26. for (int j = 0; j < size; j++) {
  27. System.out.printf(matrix[i][j] + " ");
  28. }
  29. System.out.println("");
  30. }
  31. }
  32.  
  33. @Override
  34. public boolean check(int i, int j) throws IllegalArgumentException {
  35. if (i < 0 || i >= size) {
  36. throw new IllegalArgumentException("złe dane");
  37. }
  38. if (j < 0 || j >= size) {
  39. throw new IllegalArgumentException("złe dane");
  40. } else {
  41. if (matrix[i][j] == 1) {
  42. return true;
  43. } else {
  44. return false;
  45. }
  46. }
  47.  
  48. }
  49.  
  50. @Override
  51. public void connect(int i, int j) throws IllegalArgumentException {
  52. if (i < 0 || i >= size) {
  53. throw new IllegalArgumentException("złe dane");
  54. }
  55. if (j < 0 || j >= size) {
  56. throw new IllegalArgumentException("złe dane");
  57. } else {
  58. matrix[i][j] = 1;
  59. }
  60. }
  61.  
  62. @Override
  63. public void writeList() {
  64. for (int i = 0; i < size; i++) {
  65. for (int j = 0; j < size; j++) {
  66. if (check(i, j)) {
  67. System.out.println("(" + i + "," + j + ")");
  68. }
  69. }
  70.  
  71. }
  72.  
  73. }
  74.  
  75. public void czyMaSasiada() {
  76. int s = 0;
  77. for (int i = 0; i < size; i++) {
  78. for (int j = 0; j < size; j++) {
  79. if (check(i, j)) {
  80. break;
  81. } else {
  82. s += 1;
  83.  
  84. }
  85. }
  86. if (s == size) {
  87. System.out.println(i + " Nie mam sasiada");
  88. }
  89. s=0;
  90.  
  91. }
  92.  
  93. }
  94.  
  95. public void klika()
  96. {
  97. for (int i = 0; i < size; i++) {
  98. for (int j = 0; j < size; j++) {
  99. if (matrix[i][j] == 0 && i!=j){
  100. System.out.println("Graf nie jest klika");
  101. return;
  102. } else { System.out.println("Graf jest kliką");
  103. return; }
  104.  
  105. }
  106. }
  107.  
  108. }
  109. public void transpozycja() {
  110. System.out.println("Macierz po transpozycji: ");
  111. for (int i = 0; i < size; i++) {
  112.  
  113. for (int j = 0; j < size; j++) {
  114. System.out.printf(matrix[j][i] + " ");
  115. }
  116. System.out.println("");
  117. }
  118. }
  119.  
  120. public static void main(String[] args) {
  121.  
  122.  
  123. TGraph graf = new TGraph(5);
  124.  
  125. graf.connect(1,4);
  126. graf.connect(1,2);
  127. graf.connect(2,3);
  128. graf.connect(1,1);
  129. graf.connect(3,4);
  130. graf.connect(2,1);
  131. graf.connect(4,2);
  132.  
  133. graf.writeMatrix();
  134.  
  135. System.out.println("Czy krawędź "+graf.check(1,4));
  136.  
  137. System.out.println(" ");
  138.  
  139. graf.writeList();
  140.  
  141. graf.klika();
  142.  
  143. graf.transpozycja();
  144.  
  145.  
  146. }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement