Quantcast
Channel: 未分类 –懒得折腾
Viewing all articles
Browse latest Browse all 756

Design and Analysis of Algorithm 2 Homework 5

$
0
0

Question 1

In this assignment you will implement one or more algorithms for the traveling salesman problem, such as the dynamic programming algorithm covered in the video lectures. Here is a data file describing a TSP instance. The first line indicates the number of cities. Each city is a point in the plane, and each subsequent line indicates the x- and y-coordinates of a single city.The distance between two cities is defined as the Euclidean distance — that is, two cities at locations (x,y) and (z,w)have distance (x−z)2+(y−w)2−−−−−−−−−−−−−−−√ between them.

In the box below, type in the minimum cost of a traveling salesman tour for this instance, rounded down to the nearest integer.

OPTIONAL: If you want bigger data sets to play with, check out the TSP instances from around the world here. The smallest data set (Western Sahara) has 29 cities, and most of the data sets are much bigger than that. What’s the largest of these data sets that you’re able to solve — using dynamic programming or, if you like, a completely different method?

 

25
20833.3333 17100.0000
20900.0000 17066.6667
21300.0000 13016.6667
21600.0000 14150.0000
21600.0000 14966.6667
21600.0000 16500.0000
22183.3333 13133.3333
22583.3333 14300.0000
22683.3333 12716.6667
23616.6667 15866.6667
23700.0000 15933.3333
23883.3333 14533.3333
24166.6667 13250.0000
25149.1667 12365.8333
26133.3333 14500.0000
26150.0000 10550.0000
26283.3333 12766.6667
26433.3333 13433.3333
26550.0000 13850.0000
26733.3333 11683.3333
27026.1111 13051.9444
27096.1111 13415.8333
27153.6111 13203.3333
27166.6667 9833.3333
27233.3333 10450.0000

// MP 5 Scan
// Given a list (lst) of length n
// Output its prefix sum = {lst[0], lst[0] + lst[1], lst[0] + lst[1] + ... + lst[n-1]}
// Due Tuesday, January 22, 2013 at 11:59 p.m. PST

//#include    <wb.h>
#include <stdio.h>   /* required for file operations */
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define BLOCK_SIZE 512 //@@ You can change this
#define MAX_COST 1000000000

int BitCount(unsigned int u)
{
     unsigned int uCount;

     uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111);
     return ((uCount + (uCount >> 3)) & 030707070707) % 63;
}

int main(int argc, char ** argv) {
    int nodeElements; // number of elements in the list
	//int n;
    int  i=0;
    double atof ( const char * str ); 
    char line[100];  /* declare a char array */

    FILE *file;  /* declare a FILE pointer  */
    file = fopen("tsp.txt", "r");  /* open a text file for reading */
	char * pch;
	int k;
	float * X;
	float * Y;
    while(fgets(line, sizeof line, file)!=NULL) {       /* keep looping until NULL pointer... */
		pch = strtok (line," ");
		k = 0;
		if (i == 0) {
			while (pch != NULL){
				if (k == 0) {
					nodeElements = atoi(pch);
					X = (float *) malloc (nodeElements * sizeof(float));
					Y = (float *) malloc (nodeElements * sizeof(float));
				}
				pch = strtok (NULL, " ");
				k ++;
			}
		} else {
			float m, n;
			m = n = -1;
			while (pch != NULL){
				if (k == 0) {
					m = atof(pch);
				}
				if (k == 1) {
					n = atof(pch);
					X[i - 1] = m;
					Y[i - 1] = n;
				}			
				pch = strtok (NULL, " ");
				k ++;
			}
		}
        i++;
    }

	float * D = (float *) malloc (nodeElements * nodeElements * sizeof(float));
	for (int m = 0; m < nodeElements; m++) {
		for (int n = 0; n < nodeElements; n++) {
			float dx = X[m] - X[n];
			float dy = Y[m] - Y[n];
			int index = m * nodeElements + n;
			D[index] = sqrt(dx*dx + dy*dy);
		}
	}

	int j; //,min,temp;
	int * P = (int *) malloc ((nodeElements + 1) * sizeof(int));
	int b = 1;
	for (i = 0; i <= nodeElements; i++) {
		P[i] = b;
		b = b * 2;
	}
	b = P[nodeElements];
	float * A = (float *) malloc (nodeElements * b * sizeof(float));
	for(i = 0; i < b; i++)
		for(j = 0; j < nodeElements; j++)
		{
			int index = j*b + i;
			if ((j == 0) && (i == 1)) {
				A[index] = 0;
			} else if ((j == 0)&& (i != 1)) {
				A[index] = MAX_COST;
			} else {
				A[index] = -1;				
			}
		}

	int * bitPositions = (int *) malloc ((nodeElements) * sizeof(int));
	for (int m = 2; m <= nodeElements; m++) {
		printf("m: %d\n", m); 
		for (unsigned int ii = 0; ii < b; ii++) {
			if ((ii % 2 == 1) && (BitCount(ii) == m)) {
				int bitCount = 0;
				for (unsigned int k = 0; k <= nodeElements; k++) {
					if (ii & P[k]) {
						bitPositions[bitCount] = k;
						bitCount++;
					} 
				}			
				for (unsigned int k1 = 0; k1 < m; k1++) {
					j = bitPositions[k1];
					if (j == 0)
						continue;
					float min =  MAX_COST;
					for (unsigned int k2 = 0; k2 < m; k2++) {
						if (k1 == k2)
							continue;
						int kk = bitPositions[k2];						
						float result = A[(ii - P[j]) + kk*b] + D[kk * nodeElements + j];
						if (result > MAX_COST) 
							result = MAX_COST;
						if (result < min) {
							min = result;
						}
					}
					A[ii + b * j] = min;
				}
			}
		}
	}
	free(bitPositions);
	float finalResult = MAX_COST;
	for (int j=1; j<nodeElements; j++) {
		float result = A[j*b + b-1] + D[j * nodeElements + 0];
		if (result < finalResult) {
			finalResult = result;
		}
	}
	printf ("%f.3", finalResult);

	free(X);
	free(Y);
	free(D);
	free(A);
	//free(M);
	free(P);	
    return 0;
}




Viewing all articles
Browse latest Browse all 756

Trending Articles