-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSPNearestNeighbour.java
More file actions
123 lines (112 loc) · 3.79 KB
/
TSPNearestNeighbour.java
File metadata and controls
123 lines (112 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;
import java.io.*;
import java.util.*;
public class TSPNearestNeighbour
{
private int numberOfNodes;
private Stack<Integer> stack;
public TSPNearestNeighbour()
{
stack = new Stack<Integer>();
}
public ArrayList tsp(int adjacencyMatrix[][], int startNode)
{
numberOfNodes = adjacencyMatrix[1].length - 1;
int[] visited = new int[numberOfNodes + 1];
visited[startNode] = 1;
stack.push(startNode);
//visited[1] = 1;
//stack.push(1);
int element, dst = 0, i;
int min = Integer.MAX_VALUE;
boolean minFlag = false;
ArrayList nodeList = new ArrayList();
nodeList.add(startNode);
while (!stack.isEmpty())
{
element = stack.peek();
i = 1;
min = Integer.MAX_VALUE;
while (i <= numberOfNodes)
{
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{
if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
nodeList.add(dst);
//System.out.print(dst + "\t");
minFlag = false;
continue;
}
stack.pop();
}
return nodeList;
}
public static void main(String... arg) throws IOException
{
int number_of_nodes;
try
{
int T = 1; // number of test cases
Scanner fin = new Scanner (new File (arg[0]));
int N = fin.nextInt();
//System.out.println(N);
int[][] d = new int[N + 1][N + 1];
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
d[i][j] = fin.nextInt();
//System.out.print(d[i][j] + " ");
}
//System.out.println();
}
//System.out.println("the citys are visited as follows");
TSPNearestNeighbour tspNearestNeighbour = new TSPNearestNeighbour();
ArrayList<Integer> diffCosts = new ArrayList<Integer>();
fin.close();
for (int start = 1; start <= N; start++) {
ArrayList nodeList = tspNearestNeighbour.tsp(d, start);
int cost = 0;
for (int i = 0; i < nodeList.size(); i++) {
// System.out.print(nodeList.get(i) + " ");
}
for (int i = 0; i < nodeList.size() - 1; i++) {
cost += (int)d[(int)nodeList.get(i)][(int)nodeList.get(i+1)];
}
// System.out.println();
// System.out.println("Cost: " + cost);
diffCosts.add(cost);
}
// calculate average
double average = 0;
for (int i = 0; i < diffCosts.size(); i++) {
average += diffCosts.get(i);
}
average = average / diffCosts.size();
System.out.println("Average: " + average);
double sd = 0;
for (int i=0; i<diffCosts.size();i++)
{
sd = sd + Math.pow(diffCosts.get(i) - average, 2);
}
sd = sd / diffCosts.size();
sd = Math.sqrt(sd);
System.out.println("STD: " + sd);
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input format");
}
}
}