Doubt in the printing of results

Asked

Viewed 71 times

1

The exercise asks me to print, in a file, all possible paths between n cities, where the end is always the starting point and each city is represented as coordinates (x,y). That first part I managed to do, resulting in the following file:

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)

Where 24 is the amount of possibilities.

After this first part of the exercise, I need to calculate the shortest possible path, that is, with the shortest distance, through the Euclidean distance formula, and print these paths, and I must do this for each different starting point (different initial cities). For this, I implemented the following functions:

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]);
            printf("Distancia: %f\n", Distancia(C[i], C[0]));
        }
        else
        {
            total = total + Distancia(C[i], C[i+1]);
            printf("Distancia: %f\n", Distancia(C[i], C[i+1]));
        }
    }
    return printf("Distancia Total: %f\n\n", total);
}

I ask for the function Distancias print the results on the screen to help me initially, because in fact, you need to print them in another file. After these two functions, I used a permutation function (used in another exercise) to print all possible paths, shown above. The function is as follows:

void Troca(int *x, int *y)
{
    int aux;
    aux = *x;
    *x = *y;
    *y = aux;
}

void Permuta(FILE *saida, Cidade *C, int *sequencia, int inicio, int 
termino)
{
    int i, j;
    float distancias[TotalViagens(termino)];
    if(inicio == termino)
    {
        for(i = 0; i < termino; i++)
        {
            fprintf(saida, "(%d,%d)\t", C[sequencia[i]].x, 
C[sequencia[i]].y);
            distancias[i] = Distancias(C, termino);
        }
        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));
        }
    }
}

Within that same function, I created the variable distancias to store the distance values of all possible trips (whereas the vector size TotalViagens(termino) is calculated by this function). So I put this vector distancias in the loop that goes from 0 the total number of cities which, in the function, is called termino. I got a little lost in the logic of the function, but I put it in this loop for the same reason that the loop print all possible paths, therefore, I imagine that, soon after printing a path, the variable distancias[i] will receive the value returned by the function Distancias. The result I have is the calculation of the same distance every time (96 times for the total of 4 cities and 24 possible paths). The way out is this::

Distancia: 6.708204
Distancia: 3.162278
Distancia: 3.162278
Distancia: 10.000000
.
.
.

That is, the same result is printed 96 times.

I would like to know why the total number of distances is multiplied by the number of cities (24x4) and the calculated possibilities are always the same.

  • Try looking over BFS (Breadth-first search), or search wide in English. I think it solves your problem without too much headache.

  • I did a little research on and I saw that BFS has to do with graphs, right? But in this implementation that I did, I didn’t implement anything from graph, just coordinate structure (x,y). My question is more about logic and why I’m printing all these 96 times

No answers

Browser other questions tagged

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