-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNearestNeighbour.java
95 lines (88 loc) · 2.65 KB
/
NearestNeighbour.java
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
import java.util.ArrayList;
import java.util.List;
public class NearestNeighbour {
Node[] node;
int source,n;
List<Integer> list;
double cost;
boolean rr;
NearestNeighbour(Node[] node,int n,boolean rr,int source){
list = new ArrayList<>();
this.node=node;
this.n = n;
this.source=source;
if(source==-1)
this.source = (int)(Math.random()*n);
this.rr = rr;
cost=0;
node[this.source].visited=true;
list.add(this.source);
solve(this.source);
list.add(this.source);
}
int minimumNeighbour(int source){
if(rr==false) {
int temp = -1;
double dist, min = 1000000;
for (int i = 0; i < n; i++) {
if (i != source && !node[i].visited) {
dist = node[source].distance(node[i]);
if (dist < min) {
min = dist;
temp = i;
}
}
}
return temp;
}
else {
ArrayList<Integer> minList = new ArrayList<>();
for(int k=0;k<5;k++) {
int temp = -1;
double dist, min = 1000000;
for (int i = 0; i < n; i++) {
if (i != source && !node[i].visited) {
dist = node[source].distance(node[i]);
if (dist < min && !minList.contains(i)) {
min = dist;
temp = i;
}
}
}
if(!minList.contains(temp) && temp!=-1)
minList.add(temp);
}
if(minList.size()!=0)
return minList.get((int) (Math.random() * minList.size()));
return -1;
}
}
void solve(int source){
int minNode=minimumNeighbour(source);
if(minNode==-1)
return;
node[minNode].visited=true;
list.add(minNode);
solve(minNode);
}
void printAns(){
System.out.print("Cost:"+String.format("%.2f",cost)+" --- ");
for(int i=0;i<list.size();i++){
System.out.print(list.get(i));
node[list.get(i)].printNode();
if(i!=list.size()-1)
System.out.print(" -> ");
}
System.out.println();
}
public double getCost() {
cost=0;
for(int i=0;i<list.size()-1;i++) {
cost += node[list.get(i)].distance(node[list.get(i + 1)]);
}
return cost;
}
public int getSource() {
return source;
}
}