Floyd warshall meio estranho

0 respostas
guisantogui

galera vou colocar o código do floyd warshall que fiz, mas não ta fechando com o meu teste de mesa. Se notarem um erro de cara desculpem a noobice! :wink:

public int valorAresta(int i, int j){
        Nodo n1 = nodos.get(i);
        Nodo n2 = nodos.get(j);
        List&lt;Aresta&gt;<A>&gt; aux = n1.getArestas();
        
        if(temAresta(n1, n2)){
            for(Aresta ares: aux){
                if(ares.getNodo().equals(n2)){
                    return (Integer) ares.getValor();
                }
            }
        }
        return 1000000;
    }
    
    private boolean temAresta(Nodo&lt;E&gt; or, Nodo&lt;E&gt; de){
        if(or.getAdjacentes().contains(de)){
            return true;
        }
        else{
            return false;
        }
    }
    
    public void printMatriz(int[][] mat){
        for(int i = 0;i &lt; mat.length; i++){
            for(int j = 0; j &lt; mat[i].length; j++){
                System.out.println(mat[i][j]);
            }
        }
    }
    
    public int[][] floydwarshall(){
        int [][] fw = new int[nodos.size()][nodos.size()];
        for(int i = 0 ;i &lt; nodos.size(); i++){
            for(int j = 0;j &lt; nodos.size(); j++){
                fw[i][j] = valorAresta(i, j);
            }
        }
        printMatriz(fw);
        System.out.println("----------------------------------");
        for(int k = 0; k &lt; nodos.size();k++){
            for(int i = 0; i &lt; nodos.size();i++){
                 for(int j = 0; j &lt; nodos.size();j++){
                    fw[i][j] = Math.min(fw[i][j], fw[i][k]+fw[k][j]);
                }
            }
        }
        return fw;
    }

Aqui os nodos que eu coloco no grafo e as arestas.

GrafoComAresta&lt;String,Integer&gt; grafo = new GrafoComAresta&lt;String, Integer&gt;();
        
        grafo.addNodo("A");
        grafo.addNodo("B");
        grafo.addNodo("C");
        grafo.addNodo("D");
        grafo.addNodo("E");
        
        grafo.addAresta("A", "B",5);
        grafo.addAresta("C", "B",3);
        grafo.addAresta("E", "A",12);
        grafo.addAresta("D", "B",10);
        grafo.addAresta("E", "C",7);
Criado 23 de junho de 2011
Respostas 0
Participantes 1