DIJKSTRA IN JAVA

Asked

Viewed 188 times

-1

Hello.
I am implementing a graph and as a smaller path algorithm, I am using DIJKSTRA. The graph indicates the relation of genders and their respective subgenera.
The graph is directional, unweighted and has isolated vertices.
Follows code below:


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class TratamentoArquivo {

    public static void main(String[] args) throws IOException {
        String fileName = "C:\\Users\\MATEUS\\Documents\\R\\dados\\generosFinal.csv"; //LOCAL ONDE ESTÁ O ARQUIVO
        BufferedReader in = new BufferedReader(new FileReader(fileName));
        StringBuffer sb = new StringBuffer((int) new File(fileName).length());

        String linha = in.readLine();

        while (linha != null) {
            sb.append(linha + "\n");
            linha = in.readLine();
        }

        in.close();
        String text = sb.toString();
        ArrayList<String> generos = new ArrayList<String>();

        int n = 1228;

        String lines[] = text.split("\n");

        for (int i = 0; i < n; i++) {
            lines[i] = lines[i].replace("{", "");
            lines[i] = lines[i].replace("}", "");

            String temp[] = lines[i].split(";");
            String current = temp[0];
            generos.add(temp[0]);
            lines[i] = lines[i].replace(temp[0], generos.indexOf(current) + "");

            if (temp[1].contains(",")) {
                String temp2[] = temp[1].split(",");
                for (int j = 0; j < temp2.length; j++) {

                    if (!generos.contains(temp2[j])) {
                        generos.add(temp2[j]);
                    }

                    lines[i] = lines[i].replace(temp2[j], generos.indexOf(temp2[j]) + "");
                }
            } else {
                if (!generos.contains(temp[1]) && !temp[1].equals("NULL")) {
                    generos.add(temp[1]);
                    lines[i] = lines[i].replace(temp[1], generos.indexOf(temp[1]) + "");
                } else if (!temp[1].equals("NULL")) {
                    lines[i] = lines[i].replace(temp[1], generos.indexOf(temp[1]) + "");
                }
            }
        }

        System.out.println("Total: " + generos.size());
        int adj[][] = new int[generos.size()][generos.size()];
        for (int i = 0; i < n; i++) {
            System.out.println(lines[i]);
        }

        for (int i = 0; i < n; i++) {
            String temp[] = lines[i].split(";");

            if (temp[1].contains(",")) {
                String temp2[] = temp[1].split(",");
                for (int j = 0; j < temp2.length; j++) {
                    adj[Integer.parseInt(temp[0])][Integer.parseInt(temp2[j])] = 1;

                }
            } else {
                if (!temp[1].equals("NULL")) {
                    adj[Integer.parseInt(temp[0])][Integer.parseInt(temp[1])] = 1;
                }
            }
        }

        //Inicializando o Dijkstra
        //Tentativa 2 DIJKSTRA
        int v = adj.length;
        boolean visitados[] = new boolean[v];
        int distancias[] = new int[v];
        distancias[0] = 0;

        for (int i = 1; i < v; i++) {
            distancias[i] = Integer.MAX_VALUE;

        }

        for (int i = 0; i < v - 1; i++) {
            //Encontrando vertice com menor distância
            int verticeMinimo = encontrarVerticeMinimo(distancias, visitados);
            //System.out.println(verticeMinimo);
            visitados[verticeMinimo] = true;
            //Explorando Vizinhos
            for (int j = 0; j < v; j++) {
                if ((distancias[j] > (distancias[verticeMinimo] + adj[verticeMinimo][j])) && adj[verticeMinimo][j] == 1) {
                    distancias[j] = adj[verticeMinimo][j] + distancias[verticeMinimo];
                }
            }

        }

        //Impressão
        for (int i = 0; i < v; i++) {
            System.out.println(i + " " + distancias[i]);
        }

    }

    public static int encontrarVerticeMinimo(int distancias[], boolean visitados[]) {

        int verticeMinimo = -1;
        for (int i = 0; i < distancias.length; i++) {
            if (!visitados[i] && (verticeMinimo == -1 || distancias[i] < distancias[verticeMinimo])) {
                verticeMinimo = i;
            }
        }
        return verticeMinimo;
    }
}

Despite having followed several tutorials and studied the algorithm, the distance vector is being fed only with infinite results (with the exception of the first vertex).
I made several modifications to the code in order to solve this problem, the only thing that was changed in the result is that, for the non-esolated vertices (that is, they have a subgenus), the value "infinite" was negative.
Link of the file with gender and subgenre information.

1 answer

0

I was able to solve my problem using a "different" form of the Dijkstra algorithm, with the following function:

public static int[] dijkstra2(int adj[][], int origem) {

        int tamanho = adj.length;
        boolean visitados[] = new boolean[tamanho];
        int distancias[] = new int[tamanho];

        for (int i = 0; i < tamanho; i++) {
            distancias[i] = Integer.MAX_VALUE;
        }
        distancias[origem] = 0;

        for (int i = 0; i < tamanho - 1; i++) {
            int menorVertice = encontrarVerticeMinimo(distancias, visitados);
            visitados[menorVertice] = true;
            for (int j = 0; j < tamanho; j++) {
                if (adj[menorVertice][j] != 0 && !visitados[j] && distancias[menorVertice] != Integer.MAX_VALUE) {
                    int novaDistancia = distancias[menorVertice] + adj[menorVertice][j];
                    if (novaDistancia < distancias[j]) {
                        distancias[j] = novaDistancia;
                    }
                }
            }
        }

        return distancias;
    }

The method called encontrarMenorVertice is as follows:

public static int encontrarVerticeMinimo(int distancias[], boolean visitados[]) {

        int verticeMinimo = -1;
        for (int i = 0; i < distancias.length; i++) {
            if (!visitados[i] && (verticeMinimo == -1 || distancias[i] < distancias[verticeMinimo])) {
                verticeMinimo = i;
            }
        }
        return verticeMinimo;
    }

The solution was to use each vertex as the source in each execution of the algorithm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.