Advertisement
Guest User

codigo

a guest
Dec 14th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.34 KB | None | 0 0
  1. package analise.redes.sociais;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.Formatter;
  6. import java.util.Scanner;
  7. import org.la4j.Matrix;
  8. import org.la4j.decomposition.EigenDecompositor;
  9. import org.la4j.matrix.dense.Basic2DMatrix;
  10.  
  11. public class ANALISEREDESSOCIAIS {
  12.  
  13. private final static String SEPARADOR_DADOS_FICH = ",";
  14.  
  15. private static final Scanner input = new Scanner(System.in);
  16. private static final Formatter output = new Formatter(System.out);
  17.  
  18. public static void main(String[] args) throws FileNotFoundException {
  19. //Contagem do número de elementos
  20. int nElems = contagemEntidades();
  21. //Criação da Matriz de Adjacência
  22. double matrizAdjacencia[][] = new double[nElems][nElems];
  23. //Criação do vetor de Id's
  24. String vetorIds[] = new String[nElems];
  25. double valoresProprios[] = new double[nElems];
  26. double vetorProprio[] = new double[nElems];
  27.  
  28. int op;
  29. do {
  30. op = menu();
  31. switch (op) {
  32. case 1:
  33. System.out.println("");
  34. preenchimentoVetorIds(vetorIds);
  35. preenchimentoMatrizAdjacencia(matrizAdjacencia, vetorIds);
  36. break;
  37. case 2:
  38. System.out.println("");
  39. impressaoMatrizAdjacencia(matrizAdjacencia, nElems);
  40. System.out.println("");
  41. break;
  42. case 3:
  43. System.out.println("");
  44. System.out.println("Digite o id do nó em questão:");
  45. String id = input.nextLine();
  46. System.out.println("");
  47. int indice = indiceDoIdDoNo(vetorIds, id);
  48. int grau = calculoDoGrauDeUmNo(indice, nElems, matrizAdjacencia);
  49. System.out.println("O nó inserido tem grau " + grau);
  50. System.out.println("");
  51. break;
  52. case 4:
  53. System.out.println("");
  54. obterValoresProprios(matrizAdjacencia, nElems, valoresProprios, vetorProprio);
  55. System.out.println("A centralidade tem valor " + descobrirCentralidadeVetor(vetorProprio));
  56. System.out.println("");
  57. break;
  58. case 5:
  59. System.out.println("");
  60. System.out.println("O grau médio é " + grauMedio(nElems, matrizAdjacencia));
  61. System.out.println("");
  62. break;
  63. case 6:
  64. System.out.println("");
  65. float densidade = densidadeRede(nElems, matrizAdjacencia);
  66. System.out.println("A densidade da rede tem valor " + densidade);
  67. System.out.println("");
  68. break;
  69. case 7:
  70. System.out.println("");
  71. int comprimento = pedirComprimento();
  72. System.out.print("Deseja saber quantas ligações de comprimento " + comprimento + " existem entre que nós? (Digite os seus Id's)");
  73. System.out.println("");
  74. System.out.print("1º Nó: ");
  75. String no1 = input.nextLine();
  76. System.out.print("2º Nó: ");
  77. String no2 = input.nextLine();
  78. System.out.println("");
  79. break;
  80. case 0:
  81. System.out.println("");
  82. System.out.println("Já fez todas as gravações necessárias? Confirma terminar (s/n)?");
  83. char resp = (input.next()).charAt(0);
  84. if ((resp != 's') && (resp != 'S')) {
  85. op = 1;
  86. }
  87. break;
  88. default:
  89. System.out.println("");
  90. System.out.println("Opção incorreta. Repita");
  91. System.out.println("");
  92. break;
  93. }
  94. } while (op != 0);
  95.  
  96. }
  97.  
  98. public static int menu() {
  99. int op;
  100. String menu = " MENU:"
  101. + "\n"
  102. + "\n 1 - Carregar informação dos ficheiros de entrada"
  103. + "\n 2 - Impressão da matriz de adjacências (com relações)"
  104. + "\n 3 - Cálculo do grau de 1 nó"
  105. + "\n 4 - Centralidade do vetor próprio"
  106. + "\n 5 - Cálculo do grau médio"
  107. + "\n 6 - Cálculo da densidade da rede"
  108. + "\n 7 - Cálculo de uma potência da matriz de adjacências"
  109. + "\n 0 - Terminar"
  110. + "\n"
  111. + "\n Digite a sua opção: ";
  112. output.format("%s", menu);
  113. op = input.nextInt();
  114. input.nextLine();
  115. return op;
  116. }
  117.  
  118. public static int contagemEntidades() throws FileNotFoundException {
  119. int contagem = 0;
  120. Scanner inputFile = new Scanner(new File("rs_media_nos.csv"));
  121. while (inputFile.hasNextLine()) {
  122. String linha = inputFile.nextLine();
  123. if ((linha.trim()).length() > 0) {
  124. contagem = contagem + 1;
  125. }
  126. }
  127. return (contagem - 1);
  128. }
  129.  
  130. public static void preenchimentoVetorIds(String vetorIds[]) throws FileNotFoundException {
  131. Scanner inputFile = new Scanner(new File("rs_media_nos.csv"));
  132. int i = 0;
  133. inputFile.nextLine();
  134. while (inputFile.hasNextLine()) {
  135. String linha = inputFile.nextLine();
  136. if (linha.trim().length() > 0) {
  137. String[] temp = linha.split(SEPARADOR_DADOS_FICH);
  138. vetorIds[i] = temp[0];
  139. i++;
  140. }
  141. }
  142. }
  143.  
  144. public static void impressaoMatrizAdjacencia(double matrizAdjacencia[][], int nElems) {
  145. for (int i = 0; i < nElems; i++) {
  146. for (int j = 0; j < nElems; j++) {
  147. output.format("%5s", matrizAdjacencia[i][j]);
  148. }
  149. output.format("%n%n", "");
  150. }
  151. }
  152.  
  153. public static int procurarPosicaoVetor(String vetor[], String elemento) {
  154. for (int i = 0; i < vetor.length; i++) {
  155. if (elemento.equals(vetor[i])) {
  156. return i;
  157. }
  158. }
  159. return 0;
  160. }
  161.  
  162. public static void preenchimentoMatrizAdjacencia(double matrizAdjacencia[][], String vetorIds[]) throws FileNotFoundException {
  163. Scanner inputFile = new Scanner(new File("rs_media_ramos.csv"));
  164. int pos1, pos2;
  165. inputFile.nextLine();
  166. while (inputFile.hasNextLine()) {
  167. String linha = inputFile.nextLine();
  168. if (linha.trim().length() > 0) {
  169. String[] temp = linha.split(SEPARADOR_DADOS_FICH);
  170. pos1 = procurarPosicaoVetor(vetorIds, temp[0].trim());
  171. pos2 = procurarPosicaoVetor(vetorIds, temp[1].trim());
  172. if (!(pos1 == pos2)) {
  173. matrizAdjacencia[pos1][pos2] = 1;
  174. matrizAdjacencia[pos2][pos1] = 1;
  175. }
  176. }
  177. }
  178. }
  179.  
  180. public static int indiceDoIdDoNo(String vetorIds[], String no) {
  181. for (int i = 0; i < vetorIds.length; i++) {
  182. if (vetorIds[i].equals(no)) {
  183. return i;
  184. }
  185. }
  186. return 0;
  187. }
  188.  
  189. public static int calculoDoGrauDeUmNo(int no, int nElems, double matrizAdjacencia[][]) {
  190. int grau = 0;
  191. for (int i = 0; i < nElems; i++) {
  192. if (matrizAdjacencia[no][i] != 0) {
  193. grau = grau + 1;
  194. }
  195. }
  196. return grau;
  197. }
  198.  
  199. public static void obterValoresProprios(double matrizAdjacencia[][], int nElems, double valoresProprios[], double vetorProprio[]) {
  200. Matrix a = new Basic2DMatrix(matrizAdjacencia);
  201. EigenDecompositor eigenD = new EigenDecompositor(a);
  202. Matrix[] mattD = eigenD.decompose();
  203. double matA[][] = mattD[0].toDenseMatrix().toArray();
  204. double matB[][] = mattD[1].toDenseMatrix().toArray();
  205. for (int i = 0; i < nElems; i++) {
  206. for (int j = 0; j < nElems; j++) {
  207. if (i == j) {
  208. valoresProprios[i] = matB[i][j];
  209. }
  210. }
  211. }
  212. int indice = verIndiceDoMaiorElementoNumVetor(valoresProprios);
  213. for (int c = 0; c < nElems; c++) {
  214. vetorProprio[c] = matA[c][indice];
  215. }
  216. }
  217.  
  218. public static int verIndiceDoMaiorElementoNumVetor(double vetor[]) {
  219. double valor = vetor[0];
  220. int indice = 0;
  221. for (int i = 1; i < vetor.length; i++) {
  222. if (vetor[i] > valor) {
  223. valor = vetor[i];
  224. indice = i;
  225. }
  226. }
  227. return indice;
  228. }
  229.  
  230. public static double descobrirCentralidadeVetor(double vetorProprio[]) {
  231. int indice = verIndiceDoMaiorElementoNumVetor(vetorProprio);
  232. double centralidade = vetorProprio[indice];
  233. return centralidade;
  234. }
  235.  
  236. public static int grauMedio(int nElems, double matrizAdjacencia[][]) {
  237. int grauMedio = 0;
  238. for (int j = 0; j < nElems; j++) {
  239. grauMedio = grauMedio + calculoDoGrauDeUmNo(j, nElems, matrizAdjacencia);
  240. }
  241. return grauMedio / nElems;
  242. }
  243.  
  244. public static float densidadeRede(int nElem, double matrizAdjacencia[][]) {
  245. float maxRamos = (nElem * nElem - nElem) / 2;
  246. float numRamos = numRamos(matrizAdjacencia, nElem);
  247. return numRamos / maxRamos;
  248. }
  249.  
  250. public static float numRamos(double matrizAdjacencia[][], int nElem) {
  251. int numRamos = 0;
  252. for (int i = 0; i < nElem - 1; i++) {
  253. for (int j = i + 1; j < nElem; j++) {
  254. if (matrizAdjacencia[i][j] == 1) {
  255. numRamos++;
  256. }
  257. }
  258. }
  259. return numRamos;
  260. }
  261.  
  262. public static int pedirComprimento() {
  263. int comprimento;
  264. do {
  265. System.out.print("Digite o comprimento das ligações desejado: ");
  266. comprimento = input.nextInt();
  267. System.out.println("");
  268. input.nextLine();
  269. } while (comprimento <= 0);
  270. return comprimento;
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement