Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following class to solve the traveling salesman problem. TSP -n: int//

ID: 3573983 • Letter: W

Question

Write the following class to solve the traveling salesman problem. TSP -n: int//number of cities +x: int[]//x-coordinates of cities +y: int []//y-coordinates of cities +path: int[]//current (random) path +bestPath: int[]//best path found thus far +dist: float//distance of current path +bestDist: float//distance of best path +Tsp() + setN(int n_in): void +getN (): int +nextRandPath(): void +getPathDistance(): float +display(): void +randperrm(dim: int, arr: int[]): void -getDistance (xl: float, yl: float, x2: float, y2: float): float -swap(a: int *, b:int *): void

Explanation / Answer

class Tsp

{

private int n;

public int x[],y[] ,path,bestPath;

public float dist,bestDist;

public Tsp()

{

//constructor

//initialise members

}

public void setN(int n_in)

{

n=n_in;

}

public int getN()

{

return n;

}

public void nextRandPath()

{

//generate random tours and save the shortest one(path is generated and comparision are made on path with previous values)

//to generate random paths use randPerm function

//finally display best path(bestPath variable is initialised with final path variable value)

}

public void getPathDistance()

{

float s=0;

//select each consecutive point from path

//perform getDistance in each case

//store retun value in float variable k;

//tillthe end of loop run

s=s+k;

//finally dist=s;

}

public void display()

{

cout<<"best path"<<bestPath;

cout<"Minimum Cost"<<bestDist;

}

public void randperm(int dim,int arr[])

{

//from the co-ordinates given in the form of array 'x' and 'y' we need to generate paths and save each time in 'path' variable.

}

private float getDistance(float x1,float y1,float x2,float y2)

{

float dis;

float s1=x1-x2;

s1=pow(s1,2);

float s2=y1-y2;

s2=pow(s2,2)

dis=s1+s2;

dis=pow(dis,0.5);

return dis;

}

private void swap(int *a,int *b)

{

int c;

c=*a;

*a=*b;

*b=c;

}

}