0

I am working on TSP which takes large number of city like 100 ,500 etc. I have wrote a code using greedy algorithm and works fine using command line arguments . But I need to take input from a file which has the below given format.

  1. First line is the number of cities

  2. Second line is 'euclidean' or'not euclidean'

  3. Now from 3rd line we have co-ordinate of n cities (float)

  4. And after cordinates we have nxn distance matrix for each city.

Something like that lets take number of city be 5

5

euclidean

1.3 4.2

1.6 -3.5

1.4 1.5

6.4 3.6

4 2.4

now a 5x5 cost matrix.

How do i store all the input in array ? (n,euclidean/non-euclidean,cordinates,matrix) After taking the input i need to work on the matrix itself.

user2714823
  • 607
  • 5
  • 15
  • 29

1 Answers1

1

I wouldn't store it all in one single array. First of all, worry about reading the number of cities. After you know that, you can allocate 2 arrays: one of them holds a structure with the coordinates for each city, and the other is a 2D array where you store the costs.

This assumes that you look at the cities and count them: in your example, the city with coordinates 1.3 4.2 will be city 0 (stored in position 0 of the array); the city with 1.6 -3.5 will be on position 1, etc. So, basically you'll be doing:

  • Read number of cities, x;
  • Read whether it's euclidean or not, store that in some variable;
  • Allocate an array cities of x elements, and another bi-dimensional array costs of x by x;
  • Read each city's coordinates, and store them in cities[i] (cities shall be an array of a structure with 2 floats to store the coordinates);
  • For each line i (i starting on 0) and for each column j in that line (also starting on 0), set cost[i][j] to whatever value is in the input.

Here's the code that implements this approach:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 32

struct city {
    float c1;
    float c2;
};

int main(void) {
    int citiesNo, i, j;
    struct city *cities;
    float **cost;
    char line[BUFSIZE], euclidean[BUFSIZE];
    fgets(line, BUFSIZE, stdin);
    citiesNo = atoi(line);
    fgets(line, BUFSIZE, stdin);
    strcpy(euclidean, line);

    cities = malloc(sizeof(struct city)*citiesNo);
    cost = malloc(sizeof(float *)*citiesNo);

    for (i = 0; i < citiesNo; i++)
        cost[i] = malloc(sizeof(float)*citiesNo);

    /* Read coordinates */
    for (i = 0; i < citiesNo; i++)
        scanf("%f %f", &(cities[i].c1), &(cities[i].c2));

    /* Read costs */
    for (i = 0; i < citiesNo; i++)
        for (j = 0; j < citiesNo; j++)
            scanf("%f", &(cost[i][j]));

    /* Everything is stored now... */

    return 0;
}

fgets was used in the beginning because scanf("%d", &citiesNo) would leave a newline in the buffer, and the subsequent fgets() call would return an empty line. If you prefer, you can replace both fgets() by scanf(), I just didn't read the euclidean string with scanf because it makes no buffer size checking. If your input is always well formed, this is not a problem.

After this code runs, you have a string with "euclidean" or "not euclidean" stored in the variable euclidean.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70