-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTravelDepthFirst.java
More file actions
201 lines (167 loc) · 6.27 KB
/
TravelDepthFirst.java
File metadata and controls
201 lines (167 loc) · 6.27 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package travelingsalesman;
import java.util.LinkedList;
public class TravelDepthFirst {
private class Path {
private LinkedList<Integer> list;
private int cost;
//Constructor of path class
private Path()
{
list = new LinkedList<>();
cost = 0;
}
//Copy constructor
private Path(Path other)
{
list = new LinkedList<>();
for (int i = 0; i < other.list.size(); i++) {
list.addLast(other.list.get(i));
}
cost = other.cost;
}
//Method adds vertex to path
private void add(int vertex, int weight)
{
list.addLast(vertex);
cost += weight;
}
//Method finds last vertex of path
private int last()
{
return list.getLast();
}
//Method finds cost of path
private int cost()
{
return cost;
}
//Method finds length of path
private int size()
{
return list.size();
}
//Method decides whether path contains a given vertex
private boolean contains(int vertex)
{
for (int i = 0; i < list.size(); i ++)
{
if (list.get(i) == vertex)
return true;
}
return false;
}
//Method displays path and its cost
private void display()
{
for (int i = 0; i < list.size(); i++)
System.out.print(list.get(i) + " ");
System.out.println(": " + cost);
}
}
private int size;
private int[][] matrix;
private int initial;
//Constructor of Travel class
public TravelDepthFirst(int vertices, int[][] edges)
{
size = vertices;
matrix = new int[size][size]; //initialize matrix to 0s
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
matrix[i][j] = 0;
for (int i = 0; i < edges.length; i++) //initialize matrix with
{ //all edges and their neighbors
int u = edges[i][0];
int v = edges[i][1];
int weight = edges[i][2];
matrix[u][v] = weight;
matrix[v][u] = weight;
}
initial = edges[0][0];
}
//Method finds shortest cycle
public void solve()
{
Path shortestPath = null;
int minimumCost = Integer.MAX_VALUE;
LinkedList<Path> openList = new LinkedList<>();
LinkedList<Path> closedList = new LinkedList<>();
int numberOfCycles = 0;
Path path = new Path();
path.add(initial, 0); //add verticy to path
openList.addFirst(path);
while(!openList.isEmpty())
{
path = openList.removeFirst();
closedList.addFirst(path);
if (complete(path)) //if path is complete cycle
{
if (path.cost() < minimumCost) //if minimum is lowest
{
minimumCost = path.cost();
shortestPath = path;
}
numberOfCycles++; //cylce has been created
path.display();
}
else
{
LinkedList<Path> children = generate(path); //new path based off
//previous path
for (int i = 0; i < children.size(); i ++) {
openList.addFirst(children.get(i));
}
}
}
if (shortestPath == null)
System.out.println("no solution");
else
{
System.out.println("Solution: ");
shortestPath.display();
System.out.println("Number of states created: " + closedList.size());
System.out.println("Number of cylces created: " + numberOfCycles);
}
}
//Method generates children of path
private LinkedList<Path> generate(Path path)
{
LinkedList<Path> children = new LinkedList<>();
int lastVertex = path.last();
for (int i = 0; i < size; i++)
{
if (matrix[lastVertex][i] != 0) //if matrix has neighbor
{
if (i == initial) //if verticy is same as initial
{
if (path.size() == size - 1)
{
Path child = new Path(path);
child.add(i, matrix[lastVertex][i]);
children.addLast(child);
}
}
else
{
if (!path.contains(i)) //if verticy is not already used in path
{
Path child = new Path(path);
child.add(i, matrix[lastVertex][i]);
children.addLast(child);
}
}
}
}
return children;
}
//Method decides whether a path is complete
boolean complete(Path path)
{
return path.size() == size;
}
}