Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package analise.redes.sociais;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Formatter;
- import java.util.Scanner;
- import org.la4j.Matrix;
- import org.la4j.decomposition.EigenDecompositor;
- import org.la4j.matrix.dense.Basic2DMatrix;
- public class ANALISEREDESSOCIAIS {
- private final static String SEPARADOR_DADOS_FICH = ",";
- private static final Scanner input = new Scanner(System.in);
- private static final Formatter output = new Formatter(System.out);
- public static void main(String[] args) throws FileNotFoundException {
- //Contagem do número de elementos
- int nElems = contagemEntidades();
- //Criação da Matriz de Adjacência
- double matrizAdjacencia[][] = new double[nElems][nElems];
- //Criação do vetor de Id's
- String vetorIds[] = new String[nElems];
- double valoresProprios[] = new double[nElems];
- double vetorProprio[] = new double[nElems];
- int op;
- do {
- op = menu();
- switch (op) {
- case 1:
- System.out.println("");
- preenchimentoVetorIds(vetorIds);
- preenchimentoMatrizAdjacencia(matrizAdjacencia, vetorIds);
- break;
- case 2:
- System.out.println("");
- impressaoMatrizAdjacencia(matrizAdjacencia, nElems);
- System.out.println("");
- break;
- case 3:
- System.out.println("");
- System.out.println("Digite o id do nó em questão:");
- String id = input.nextLine();
- System.out.println("");
- int indice = indiceDoIdDoNo(vetorIds, id);
- int grau = calculoDoGrauDeUmNo(indice, nElems, matrizAdjacencia);
- System.out.println("O nó inserido tem grau " + grau);
- System.out.println("");
- break;
- case 4:
- System.out.println("");
- obterValoresProprios(matrizAdjacencia, nElems, valoresProprios, vetorProprio);
- System.out.println("A centralidade tem valor " + descobrirCentralidadeVetor(vetorProprio));
- System.out.println("");
- break;
- case 5:
- System.out.println("");
- System.out.println("O grau médio é " + grauMedio(nElems, matrizAdjacencia));
- System.out.println("");
- break;
- case 6:
- System.out.println("");
- float densidade = densidadeRede(nElems, matrizAdjacencia);
- System.out.println("A densidade da rede tem valor " + densidade);
- System.out.println("");
- break;
- case 7:
- System.out.println("");
- int comprimento = pedirComprimento();
- System.out.print("Deseja saber quantas ligações de comprimento " + comprimento + " existem entre que nós? (Digite os seus Id's)");
- System.out.println("");
- System.out.print("1º Nó: ");
- String no1 = input.nextLine();
- System.out.print("2º Nó: ");
- String no2 = input.nextLine();
- System.out.println("");
- break;
- case 0:
- System.out.println("");
- System.out.println("Já fez todas as gravações necessárias? Confirma terminar (s/n)?");
- char resp = (input.next()).charAt(0);
- if ((resp != 's') && (resp != 'S')) {
- op = 1;
- }
- break;
- default:
- System.out.println("");
- System.out.println("Opção incorreta. Repita");
- System.out.println("");
- break;
- }
- } while (op != 0);
- }
- public static int menu() {
- int op;
- String menu = " MENU:"
- + "\n"
- + "\n 1 - Carregar informação dos ficheiros de entrada"
- + "\n 2 - Impressão da matriz de adjacências (com relações)"
- + "\n 3 - Cálculo do grau de 1 nó"
- + "\n 4 - Centralidade do vetor próprio"
- + "\n 5 - Cálculo do grau médio"
- + "\n 6 - Cálculo da densidade da rede"
- + "\n 7 - Cálculo de uma potência da matriz de adjacências"
- + "\n 0 - Terminar"
- + "\n"
- + "\n Digite a sua opção: ";
- output.format("%s", menu);
- op = input.nextInt();
- input.nextLine();
- return op;
- }
- public static int contagemEntidades() throws FileNotFoundException {
- int contagem = 0;
- Scanner inputFile = new Scanner(new File("rs_media_nos.csv"));
- while (inputFile.hasNextLine()) {
- String linha = inputFile.nextLine();
- if ((linha.trim()).length() > 0) {
- contagem = contagem + 1;
- }
- }
- return (contagem - 1);
- }
- public static void preenchimentoVetorIds(String vetorIds[]) throws FileNotFoundException {
- Scanner inputFile = new Scanner(new File("rs_media_nos.csv"));
- int i = 0;
- inputFile.nextLine();
- while (inputFile.hasNextLine()) {
- String linha = inputFile.nextLine();
- if (linha.trim().length() > 0) {
- String[] temp = linha.split(SEPARADOR_DADOS_FICH);
- vetorIds[i] = temp[0];
- i++;
- }
- }
- }
- public static void impressaoMatrizAdjacencia(double matrizAdjacencia[][], int nElems) {
- for (int i = 0; i < nElems; i++) {
- for (int j = 0; j < nElems; j++) {
- output.format("%5s", matrizAdjacencia[i][j]);
- }
- output.format("%n%n", "");
- }
- }
- public static int procurarPosicaoVetor(String vetor[], String elemento) {
- for (int i = 0; i < vetor.length; i++) {
- if (elemento.equals(vetor[i])) {
- return i;
- }
- }
- return 0;
- }
- public static void preenchimentoMatrizAdjacencia(double matrizAdjacencia[][], String vetorIds[]) throws FileNotFoundException {
- Scanner inputFile = new Scanner(new File("rs_media_ramos.csv"));
- int pos1, pos2;
- inputFile.nextLine();
- while (inputFile.hasNextLine()) {
- String linha = inputFile.nextLine();
- if (linha.trim().length() > 0) {
- String[] temp = linha.split(SEPARADOR_DADOS_FICH);
- pos1 = procurarPosicaoVetor(vetorIds, temp[0].trim());
- pos2 = procurarPosicaoVetor(vetorIds, temp[1].trim());
- if (!(pos1 == pos2)) {
- matrizAdjacencia[pos1][pos2] = 1;
- matrizAdjacencia[pos2][pos1] = 1;
- }
- }
- }
- }
- public static int indiceDoIdDoNo(String vetorIds[], String no) {
- for (int i = 0; i < vetorIds.length; i++) {
- if (vetorIds[i].equals(no)) {
- return i;
- }
- }
- return 0;
- }
- public static int calculoDoGrauDeUmNo(int no, int nElems, double matrizAdjacencia[][]) {
- int grau = 0;
- for (int i = 0; i < nElems; i++) {
- if (matrizAdjacencia[no][i] != 0) {
- grau = grau + 1;
- }
- }
- return grau;
- }
- public static void obterValoresProprios(double matrizAdjacencia[][], int nElems, double valoresProprios[], double vetorProprio[]) {
- Matrix a = new Basic2DMatrix(matrizAdjacencia);
- EigenDecompositor eigenD = new EigenDecompositor(a);
- Matrix[] mattD = eigenD.decompose();
- double matA[][] = mattD[0].toDenseMatrix().toArray();
- double matB[][] = mattD[1].toDenseMatrix().toArray();
- for (int i = 0; i < nElems; i++) {
- for (int j = 0; j < nElems; j++) {
- if (i == j) {
- valoresProprios[i] = matB[i][j];
- }
- }
- }
- int indice = verIndiceDoMaiorElementoNumVetor(valoresProprios);
- for (int c = 0; c < nElems; c++) {
- vetorProprio[c] = matA[c][indice];
- }
- }
- public static int verIndiceDoMaiorElementoNumVetor(double vetor[]) {
- double valor = vetor[0];
- int indice = 0;
- for (int i = 1; i < vetor.length; i++) {
- if (vetor[i] > valor) {
- valor = vetor[i];
- indice = i;
- }
- }
- return indice;
- }
- public static double descobrirCentralidadeVetor(double vetorProprio[]) {
- int indice = verIndiceDoMaiorElementoNumVetor(vetorProprio);
- double centralidade = vetorProprio[indice];
- return centralidade;
- }
- public static int grauMedio(int nElems, double matrizAdjacencia[][]) {
- int grauMedio = 0;
- for (int j = 0; j < nElems; j++) {
- grauMedio = grauMedio + calculoDoGrauDeUmNo(j, nElems, matrizAdjacencia);
- }
- return grauMedio / nElems;
- }
- public static float densidadeRede(int nElem, double matrizAdjacencia[][]) {
- float maxRamos = (nElem * nElem - nElem) / 2;
- float numRamos = numRamos(matrizAdjacencia, nElem);
- return numRamos / maxRamos;
- }
- public static float numRamos(double matrizAdjacencia[][], int nElem) {
- int numRamos = 0;
- for (int i = 0; i < nElem - 1; i++) {
- for (int j = i + 1; j < nElem; j++) {
- if (matrizAdjacencia[i][j] == 1) {
- numRamos++;
- }
- }
- }
- return numRamos;
- }
- public static int pedirComprimento() {
- int comprimento;
- do {
- System.out.print("Digite o comprimento das ligações desejado: ");
- comprimento = input.nextInt();
- System.out.println("");
- input.nextLine();
- } while (comprimento <= 0);
- return comprimento;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement