Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4.  
  5. public class Optional {
  6. public int n,k;
  7. List<Character> alphabet=new ArrayList<Character>();
  8. int[] conexe;
  9. public static void main(String[] args)
  10. {
  11. long startTime = System.nanoTime();
  12. Optional work=new Optional();
  13. int check=work.getArguments(args);
  14. if(check == 0) {
  15. System.out.print("Input invalid!");
  16. return;
  17. }
  18. ArrayList<String> wordsArray=work.getWordsArray();
  19. work.displayWordsArray(wordsArray);
  20. boolean[][] adjacentMatrix=work.getAdjacentMatrix(wordsArray);
  21. work.displayAdjacentMatrix(adjacentMatrix);
  22. work.displayMostAndLeastN(wordsArray,adjacentMatrix);
  23. /*adjacentMatrix= new boolean[][]{
  24. {false, true, true, false, false, false},
  25. {true, false, true, false, false, false},
  26. {true, true, false, false, false, false},
  27. {false, false, false, false, true, false},
  28. {false, false, false, false, false, false},
  29. {false, false, false, false, false, false}
  30. };*/ //<- PENTRU TEST BONUS
  31. work.displayComponents(adjacentMatrix);
  32. long endTime = System.nanoTime();
  33. long totalTime = endTime - startTime;
  34. System.out.print("execution time: ");
  35. System.out.println(totalTime);
  36. }
  37. public int getArguments(String[] values)
  38. {
  39. if(values.length <3)
  40. return 0;
  41. try {
  42. n = Integer.parseInt(values[0]);
  43. k = Integer.parseInt(values[1]);
  44. for(int i=2;i<values.length;i++)
  45. {
  46. if(values[i].length()!=1)
  47. return 0;
  48. alphabet.add(values[i].charAt(0));
  49. }
  50. }
  51. catch(Exception e){
  52. return 0;
  53. }
  54. return 1;
  55. }
  56. public ArrayList<String> getWordsArray()
  57. {
  58. ArrayList<String> wordsArray=new ArrayList<String>();
  59. for(int i=0;i<n;i++) {
  60. StringBuilder row = new StringBuilder();
  61. for (int j = 0; j < k; j++) {
  62. int position = (int) (Math.random() * 1_000_000) % alphabet.size();
  63. char character = alphabet.get(position);
  64. row.append(character);
  65. }
  66. wordsArray.add(row.toString());
  67. }
  68. return wordsArray;
  69. }
  70. public void displayWordsArray(ArrayList<String> wordsArray)
  71. {
  72. for (String s : wordsArray)
  73. System.out.print(s+" ");
  74. System.out.println();
  75. }
  76. public boolean[][] getAdjacentMatrix(ArrayList<String> wordsArray)
  77. {
  78. boolean[][] adjacentMatrix=new boolean[n][n];
  79. for(int i=0; i<n; i++)
  80. for(int j=0; j<n; j++)
  81. {
  82. adjacentMatrix[i][j]=false;
  83. if(i==j)
  84. continue;
  85. Word word1= new Word(wordsArray.get(i));
  86. Word word2= new Word(wordsArray.get(j));
  87. if(word1.isAdjacentWith(word2) == 1)
  88. adjacentMatrix[i][j]=true;
  89. }
  90. return adjacentMatrix;
  91.  
  92. }
  93. public void displayAdjacentMatrix( boolean[][] adjacentMatrix)
  94. {
  95. for(boolean[] row: adjacentMatrix)
  96. {
  97. for (boolean elem : row)
  98. System.out.print(elem + " ");
  99. System.out.println();
  100. }
  101. }
  102. public void displayMostAndLeastN(ArrayList<String> wordsArray, boolean[][] adjacentMatrix)
  103. {
  104. int mostIndex=0,leastIndex=0,maxi=0,mini=n;
  105. int[] nrOfN =new int[n];
  106. Arrays.fill(nrOfN,0);
  107. for(int i=0; i<n; i++)
  108. {
  109. int currentN=0;
  110. for(int j=0; j<n; j++)
  111. if(adjacentMatrix[i][j])
  112. currentN++;
  113. if(currentN<=mini)
  114. {
  115. mini=currentN;
  116. leastIndex=i;
  117. }
  118. if(currentN>maxi)
  119. {
  120. maxi=currentN;
  121. mostIndex=i;
  122. }
  123. nrOfN[i]=currentN;
  124. }
  125. System.out.print("Word with most neighbours: ");
  126. System.out.println(wordsArray.get(mostIndex));
  127. System.out.print("Word with least neighbours: ");
  128. System.out.println(wordsArray.get(leastIndex));
  129. boolean sameNrOfN=true;
  130. for(int i=0;i<n;i++)
  131. {
  132. if(nrOfN[i]!=nrOfN[leastIndex])
  133. {
  134. System.out.println("Not all words have the same nr of neighbours");
  135. sameNrOfN=false;
  136. break;
  137. }
  138. }
  139. if(sameNrOfN)
  140. System.out.println("All words have the same nr of neighbours");
  141. }
  142. public void addComp( int node,boolean[][] adjacentMatrix, int comp)
  143. {
  144. if(conexe[node] == 0)
  145. {
  146. System.out.println(node+" "+comp);
  147. conexe[node]=comp;
  148. for(int i=0; i<adjacentMatrix[0].length ; i++)
  149. if(adjacentMatrix[node][i] && conexe[i]==0)
  150. addComp(i,adjacentMatrix,comp);
  151. }
  152. }
  153. public void displayComponents(boolean[][] adjacentMatrix)
  154. {
  155. conexe=new int[n];
  156. Arrays.fill(conexe,0);
  157. int comp=0;
  158. for(int i=0; i<n; i++)
  159. if(conexe[i]==0)
  160. {
  161. comp++;
  162. addComp(i,adjacentMatrix,comp);
  163. }
  164. for(int i=1;i<=comp; i++)
  165. { System.out.println("Componenta "+i);
  166. for(int j=0; j < n; j++)
  167. if(conexe[j]==i)
  168. System.out.print(j+" ");
  169. System.out.println();
  170. }
  171. }
  172.  
  173.  
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement