Search in width and depth in a graph [closed]

closed . This question needs details or to be clearer and is not currently accepting answers.

want to improve this question? Add details and make it clearer what problem is being solved by editing this post .

Closed 3 years ago .

improve this question

I need to implement a system of visits, and the search in width (graphs).

How to proceed? [EDICT] I would host to know the best way to make this algorithm work for graph as well, as it is currently only working specifically for directionless trees. I would like someone to show me the most "correct" way to implement a method for width search, and a method that demonstrates which nodes have been traversed.

I did it:

public class GrafoMatriz {
    int[][] grafo;
    int[] visitas;
    int nVertices;
    int[] profundidade;

    public void getMatriz(String file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = "";

        line = br.readLine();
        nVertices = Integer.parseInt(line);
        grafo = new int[nVertices][nVertices];
        visitas = new int[nVertices];

        line = br.readLine();

        while (line != null) {
            String[] pares = line.split(" ");
            grafo[Integer.parseInt(pares[0])][Integer.parseInt(pares[1])] = 1;
            // matriz[Integer.parseInt(pares[1])][Integer.parseInt(pares[0])] =
            // 1;
            line = br.readLine();

        }
        profundidade = new int[nVertices];
        br.close();
        // return matriz;
    }

    public void imprimeMatriz() {
        for (int i = 0; i < nVertices; i++) {
            for (int j = 0; j < nVertices; j++) {
                System.out.print(grafo[i][j]);
            }
            System.out.println();
        }
        System.out.println("\n--------------------\n");
    }

    public void zeraMatriz() {
        for (int i = 0; i < grafo.length; i++) {
            for (int j = 0; j < grafo[i].length; j++) {
                grafo[i][j] = 0;
            }
        }
    }

    public void geraListaVisitas(int no1, int no2) {
        zeraLista();
        eAlcancavel(no1, no2);
        imprimeLista();

    }

    public void eAlcancavel(int no1, int no2) {
        visitas[no1] = 1;
        for (int i = 0; i < visitas.length; i++) {
            if (grafo[no1][i] == 1) {
                eAlcancavel(i, no2);
            }
        }
    }

    public void imprimeLista() {
        for (int i = 0; i < visitas.length; i++) {
            System.out.print(visitas[i] + " ");

        }
    }

    public void zeraLista() {
        for (int i = 0; i < visitas.length; i++) {
            visitas[i] = 0;
        }
    }

    public void profundidade(int x){
        profundidade[x]++;
    }
}
 1
Author: JoubertJoe .Vieira Lellis, 2017-09-11