/// <summary>
            ///
            /// </summary>
            public void Path()
            {
                int startTree = 0;

                vertexList[startTree].isInTree = true;
                nTree = 1;
                for (int j = 0; j <= nVerts; j++)
                {
                    int tempDist = adjMat[startTree, j];
                    sPath[j] = new DistOriginal(startTree, tempDist);
                }
                while (nTree < nVerts)
                {
                    int indexMin = GetMin();
                    int minDist  = sPath[indexMin].distance;
                    currentVert    = indexMin;
                    startToCurrent = sPath[indexMin].distance;
                    vertexList[currentVert].isInTree = true;
                    nTree++;
                    AdjustShortPath();
                }
                DisplayPaths();
                nTree = 0;
                for (int j = 0; j <= nVerts - 1; j++)
                {
                    vertexList[j].isInTree = false;
                }
            }
        public List <Caminho> BuscarCaminhos(int idCidadeOrigem, int idCidadeDestino)
        {
            for (int j = 0; j < QtdCidades; j++)
            {
                vertices[j].FoiVisitado = false;
            }
            vertices[idCidadeOrigem].FoiVisitado = true;

            for (int i = 0; i < QtdCidades; i++)
            {
                int distanciaTemporaria = matrizAdjacenciaComPeso[idCidadeOrigem, i];
                percursos[i] = new DistOriginal(idCidadeOrigem, distanciaTemporaria);
            }

            for (int i = 0; i < QtdCidades; i++)
            {
                int indiceDoMenor = ObterIndiceDoMenor();
                vertices[indiceDoMenor].FoiVisitado = true;

                verticeAtual     = indiceDoMenor;
                doInicioAteAtual = percursos[indiceDoMenor].distancia;

                AjustarMenorCaminho();
            }
            return(SalvarResultados(idCidadeOrigem, idCidadeDestino));
        }
Exemple #3
0
    public void Path(string start, string end)
    {
        int startTree = FindVertexListPos(start);
        int endtree   = FindVertexListPos(end);
        int minDist;

        Debug.Log(startTree);
        if (startTree != infinity)
        {
            vertexList[startTree].isInTree = true;
            nTree = 1;
            for (int j = 0; j <= nVerts - 1; j++)
            {
                int tempDist = adjMat[startTree, j];
                sPath[j] = new DistOriginal(startTree, tempDist);
            }
            while (nTree < nVerts)
            {
                int indexMin = GetMin();
                minDist        = sPath[indexMin].distance;
                currentVert    = indexMin;
                startToCurrent = sPath[indexMin].distance;
                vertexList[currentVert].isInTree = true;
                nTree++;
                AdjustShortPath();
            }
            DisplayPaths();
            nTree = 0;
            for (int j = 0; j <= nVerts - 1; j++)
            {
                vertexList[j].isInTree = false;
            }

            List <int> mylist  = new List <int>();
            int        current = startTree;
            mylist.Add(current);
            minDist = 0;
            int shortestdisVertexFromCurrent = current;

            while (current != endtree)
            {
                for (int i = 0; i < nVerts; i++)
                {
                    if (adjMat[current, nVerts] < minDist)
                    {
                        shortestdisVertexFromCurrent = nVerts;
                    }
                }
            }
        }
        else
        {
            Debug.Log("Could not find vertex in graph ");
        }
    }
Exemple #4
0
    public string Caminho(int inicioDoPercurso, int finalDoPercurso, ListBox lista)
    {
        for (int j = 0; j < numVerts; j++)
        {
            vertices[j].foiVisitado = false;
        }

        vertices[inicioDoPercurso].foiVisitado = true;
        for (int j = 0; j < numVerts; j++)
        {
            // anotamos no vetor percurso a distância entre o inicioDoPercurso e cada vértice
            // se não há ligação direta, o valor da distância será infinity
            int tempDist = adjMatrix[inicioDoPercurso, j];
            percurso[j] = new DistOriginal(inicioDoPercurso, tempDist);
        }

        for (int nTree = 0; nTree < numVerts; nTree++)
        {
            // Procuramos a saída não visitada do vértice inicioDoPercurso com a menor distância
            int indiceDoMenor = ObterMenor();

            // e anotamos essa menor distância
            int distanciaMinima = percurso[indiceDoMenor].distancia;


            // o vértice com a menor distância passa a ser o vértice atual
            // para compararmos com a distância calculada em AjustarMenorCaminho()
            verticeAtual     = indiceDoMenor;
            doInicioAteAtual = percurso[indiceDoMenor].distancia;

            // visitamos o vértice com a menor distância desde o inicioDoPercurso
            vertices[verticeAtual].foiVisitado = true;
            AjustarMenorCaminho(lista);
        }

        return(ExibirPercursos(inicioDoPercurso, finalDoPercurso, lista));
    }