示例#1
0
        public String encontrarCaminho(String orig, String dest)
        {
            Vertice         origV = encontrarVertice(orig);
            Vertice         destV = encontrarVertice(dest);
            Lista <Vertice> fila  = new Lista <Vertice>();

            fila.insereNoFim(origV);

            NohLista <Vertice> percorrer = vertices.getInicio();

            do
            {
                if (percorrer.getData().Equals(origV))
                {
                    percorrer.getData().setDistParcial(0);
                    percorrer.getData().setState(false);
                }
                else
                {
                    percorrer.getData().setDistParcial(infinito);
                    percorrer.getData().setState(false);
                }
                percorrer = percorrer.getNext();
            } while (percorrer != null);

            encontrarCaminho(origV, destV, fila);


            origV = encontrarVertice(orig);
            destV = encontrarVertice(dest);
            Vertice percVertices = destV;
            String  caminhoFinal = percVertices.getName() + "(" + percVertices.getDistParcial().ToString() + "km)" + "\n";

            do
            {
                percVertices = percVertices.getAntecessor();
                caminhoFinal = percVertices.getName() + "(" + percVertices.getDistParcial().ToString() + "km)" + "  ->  " + caminhoFinal;
            } while (percVertices != origV);

            return(caminhoFinal);
        }
示例#2
0
        private void encontrarCaminho(Vertice percorrer, Vertice dest, Lista <Vertice> filaVisitar)
        {
            NohLista <Aresta> arestaAux = percorrer.getArestas().getInicio();
            Vertice           verticeDaAresta;

            //Laço para verificar se a distância parcial até aquele nó é menor que a já existente:
            do
            {
                int d = percorrer.getDistParcial() + arestaAux.getData().getDistance();
                verticeDaAresta = arestaAux.getData().getDestiny();

                if (d < arestaAux.getData().getDestiny().getDistParcial())
                {
                    verticeDaAresta.setDistParcial(d);
                    verticeDaAresta.setAntecessor(percorrer);
                    verticeDaAresta.setState(false);
                    filaVisitar.insereNoFim(verticeDaAresta);
                }

                arestaAux = arestaAux.getNext();
            } while (arestaAux != null);

            percorrer.setState(true);

            do
            {
                if (!filaVisitar.getInicio().getData().getState())
                {
                    encontrarCaminho(filaVisitar.getInicio().getData(), dest, filaVisitar);
                }
                else
                {
                    filaVisitar.remove(filaVisitar.getInicio().getData());
                }
            } while (filaVisitar.getInicio() != null);
        }
示例#3
0
 public void setArestas(Lista <Aresta> list)
 {
     this.arestas = list;
 }
示例#4
0
 public Vertice(String name)
 {
     this.name  = name;
     this.state = false;
     arestas    = new Lista <Aresta>();
 }
示例#5
0
 public Vertice()
 {
     arestas = new Lista <Aresta>();
 }
示例#6
0
 public Grafo()
 {
     vertices = new Lista <Vertice>();
 }