Skip to content

Create TravelingSalesManPoblem.java #5855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import org.junit.Test;
import static org.junit.Assert.*;

public class TSPTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use @ParameterizedTest. Also, it is better to test both the path and minimum cost for all cases.


@Test
public void testTSPWithSmallGraph() {
int[][] distanceMatrix = {
{ 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
{ 20, 25, 30, 0 }
};

TSP.initialize(distanceMatrix);

int minCost = TSP.tsp(0, 1);

assertEquals(80, minCost);
}

@Test
public void testOptimalPath() {
int[][] distanceMatrix = {
{ 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
{ 20, 25, 30, 0 }
};

TSP.initialize(distanceMatrix);

TSP.tsp(0, 1);

String expectedPath = "0 → 1 → 3 → 2 → 0";
String actualPath = TSP.printPath(0, 1);

assertEquals(expectedPath, actualPath);
}

@Test
public void testDifferentGraph() {
int[][] distanceMatrix = {
{ 0, 29, 20, 21 },
{ 29, 0, 15, 17 },
{ 20, 15, 0, 28 },
{ 21, 17, 28, 0 }
};

TSP.initialize(distanceMatrix);

int minCost = TSP.tsp(0, 1);

assertEquals(75, minCost);

String expectedPath = "0 → 2 → 1 → 3 → 0";
String actualPath = TSP.printPath(0, 1);

assertEquals(expectedPath, actualPath);
}
}
Loading