Calculate the shortest possible distance

Asked

Viewed 848 times

3

In an exercise, I need to print all possible paths between cities, and cities are represented by coordinates x and y and that the last city is the city of departure. Remembering that should be found the smallest paths for each different initial city. That part of the exercise was done. For example, for four cities, I have the following exit:

24
(1,10)     (4,4)     (5,1)     (2,0)     (1,10)
(1,10)     (4,4)     (2,0)     (5,1)     (1,10)
(1,10)     (5,1)     (4,4)     (2,0)     (1,10)
(1,10)     (5,1)     (2,0)     (4,4)     (1,10)
(1,10)     (2,0)     (5,1)     (4,4)     (1,10)
(1,10)     (2,0)     (4,4)     (5,1)     (1,10)
(4,4)     (1,10)     (5,1)     (2,0)     (4,4)
(4,4)     (1,10)     (2,0)     (5,1)     (4,4)
(4,4)     (5,1)     (1,10)     (2,0)     (4,4)
(4,4)     (5,1)     (2,0)     (1,10)     (4,4)
(4,4)     (2,0)     (5,1)     (1,10)     (4,4)
(4,4)     (2,0)     (1,10)     (5,1)     (4,4)
(5,1)     (4,4)     (1,10)     (2,0)     (5,1)
(5,1)     (4,4)     (2,0)     (1,10)     (5,1)
(5,1)     (1,10)     (4,4)     (2,0)     (5,1)
(5,1)     (1,10)     (2,0)     (4,4)     (5,1) 
(5,1)     (2,0)     (1,10)     (4,4)     (5,1)
(5,1)     (2,0)     (4,4)     (1,10)     (5,1)
(2,0)     (4,4)     (5,1)     (1,10)     (2,0)
(2,0)     (4,4)     (1,10)     (5,1)     (2,0)
(2,0)     (5,1)     (4,4)     (1,10)     (2,0)
(2,0)     (5,1)     (1,10)     (4,4)     (2,0)
(2,0)     (1,10)     (5,1)     (4,4)     (2,0)
(2,0)     (1,10)     (4,4)     (5,1)     (2,0)

24 being the total possibilities.

After this part, I need to indicate the best path, that is, the path with the shortest distance, using the formula of Euclidean distance between two points. I created two functions to perform this calculation, as can be seen below:

float Distancia(Cidade cA, Cidade cB)
{
    int distanciaX = pow(cA.x - cB.x, 2);
    int distanciaY = pow(cA.y - cB.y, 2);
    float distancia = sqrt(distanciaX + distanciaY);
    return distancia;
}

float Distancias(Cidade *C, int numeroCidades)
{
    int i;
    float total = 0;
    for(i = 0; i < numeroCidades; i++)
    {
        if(i == numeroCidades - 1)
        {
            total = total + Distancia(C[i], C[0]);
        }
        else
        {
            total = total + Distancia(C[i], C[i+1]);
        }
    }
    return total;
}

To find the shortest distance, I implemented the following function:

float MenorDistancia(Cidade *C, int numeroCidade)
{
    int i;
    float distancias[numeroCidade];
    float menor = distancias[0];
    for(i = 0; i < numeroCidade; i++)
    {
        distancias[i] = Distancias(C, numeroCidade);
        if(distancias[i] < menor)
            menor = distancias[i];
    }
    return menor;
}

But now I am in doubt on how to implement these functions in the operation of the program as a whole. The first question I have is whether or not my duties are correct. If they are, I would like to know if I can use them right away in the function that creates these pathway possibilities, which is the next function:

void Permuta(FILE *saida, Cidade *C, int *sequencia, int inicio, int 
termino)
{
    int i, j;
    if(inicio == termino)
    {
        for(i = 0; i < termino; i++)
        {
            fprintf(saida, "(%d,%d)     ", C[sequencia[i]].x, 
C[sequencia[i]].y);
        }
        fprintf(saida, "(%d,%d)\n", C[sequencia[0]].x, C[sequencia[0]].y);
    }
    else
    {  
        for(j = inicio; j < termino; j++)
        {
            Troca((sequencia+inicio), (sequencia+j));
            Permuta(saida, C, sequencia, inicio+1, termino);
            Troca((sequencia+inicio), (sequencia+j));
        }
    }
}

I was wondering if you could use the functions in the same loop that prints the paths and/or if there’s a better way to do it.

  • 2

    You know there’s an exponential number of possible paths, right? That’s ignoring loops, which then goes to infinity...

  • 2

    What I understood from the problem is that the number of possible paths is the factorial of the number of cities. Correct? The exercise asks you to find these possible paths and their shortest distances from 4 to 14 cities. For 14 cities, I know that there are many possibilities. I used the example of 4 for being smaller.

  • Okay, it’s factorial, but o(n!) ~~= o(n^n), That’s why I’m talking about exponential. On calculating permutation, I am more in favour of going through the city graph in depth and, when arriving at the destination, printing the path list. I find it more direct than calculating permutation.

  • 1

    All possible paths for 14 cities are 14! = 87178291200. Such solutions quickly become unviable, and so it is as if artificial intelligence algorithms are being used as simulated annealing among others

  • Yes. It really gets unfeasible. But I think the real intention of the exercise is to show these kinds of possibilities.

No answers

Browser other questions tagged

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