-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedy.java
More file actions
206 lines (166 loc) · 5.08 KB
/
Greedy.java
File metadata and controls
206 lines (166 loc) · 5.08 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
202
203
204
205
206
package greedytester;
import java.util.LinkedList;
public class Greedy {
private class Path
{
private LinkedList<Integer> list;
private int cost;
//Constructor of path class
private Path()
{
list = new LinkedList<Integer>();
cost = 0;
}
//Copy constructor
private Path(Path other)
{
list = new LinkedList<Integer>();
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;
}
private int size()
{
return list.size();
}
//Method decides whether pathcontains 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 Greedy class
public Greedy(int vertices, int[][] edges)
{
size = vertices;
matrix = new int[size][size];
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++)
{
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> list = new LinkedList<>();
Path path = new Path();
path.add(initial, 0);
list.addFirst(path);
while (!list.isEmpty())
{
path = list.removeFirst();
if (complete(path))
{
if (path.cost() < minimumCost)
{
minimumCost = path.cost();
shortestPath = path;
}
}
else
{
LinkedList<Path> children = generate(path);
if (children != null)
{
int best = selectBest(children);
list.addFirst(children.get(best));
}
}
}
if (shortestPath == null)
System.out.println("no solution");
else
shortestPath.display();
}
//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 (i == initial)
{
if (path.size() == size)
{
Path child = new Path(path);
child.add(i, matrix[lastVertex][i]);
children.addLast(child);
}
}
else
{
if (!path.contains(i))
{
Path child = new Path(path);
child.add(i, matrix[lastVertex][i]);
children.addLast(child);
}
}
}
}
return children;
}
//Method locates the path with minimum cost in a list of paths
private int selectBest(LinkedList<Path> list)
{
int minValue = list.get(0).cost;
int minIndex = 0;
for (int i = 0; i < list.size(); i++)
{
int value = list.get(i).cost;
if (value < minValue)
{
minValue = value;
minIndex = i;
}
}
return minIndex;
}
//Method decides whether a path is complete
boolean complete(Path path)
{
return path.size() == size + 1;
}
}