-
Notifications
You must be signed in to change notification settings - Fork 0
/
BruteForce.java
99 lines (78 loc) · 3.13 KB
/
BruteForce.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
96
97
98
99
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
public class BruteForce extends Algorithm {
private List<CostPath> paths;
BruteForce(Board b) {
super(b);
paths = new ArrayList<CostPath>();
}
public void printShortestPath()
{
Collections.sort(paths, (Comparator.<CostPath>
comparingInt(path1 -> path1.cost)).thenComparingInt(path2 -> path2.cost));
if (paths.isEmpty())
{
System.out.println("No paths found");
return;
}
System.out.println("Shortest Path: " + paths.get(0).cost + " : " + paths.get(0).path);
System.out.println("Longest Path: " + paths.get(paths.size()-1).cost + " : " + paths.get(paths.size()-1).path);
System.out.println("Number of paths: " + paths.size());
}
public void listPaths()
{
Collections.sort(paths, (Comparator.<CostPath>
comparingInt(path1 -> path1.cost)).thenComparingInt(path2 -> path2.cost));
for (CostPath cp :paths) {
System.out.print("Cost: " + cp.cost);
System.out.print(" Path: ");
for (City c : cp.path) {
System.out.print(c.name +"-");
}
System.out.println();
}
}
public void shortestPath(City start, City end) {
List<City> visited = new ArrayList<>();
visited.add(start);
long startTime = System.nanoTime();
shortestPath(start, end, 0, visited, 0);
long endTime = System.nanoTime();
System.out.println("BruteForce Shortest Path calculation time: " + (endTime-startTime) + "ns");
return;
}
public void shortestPath(City start, City end, int cost, List<City> visited, int level) {
// System.out.println("BruteForce Shortest Path: level " + level);
// Iterate over all cities
for (City c : b.myCities.cities) {
// The cost to go from start to the next step (c)
int nextStep = b.getBoard(start, c);
if (visited.contains(c)) {
// Already visited this city
// This is crucial step, otherwise the recursion never ends
// DO nothing
}
else if (nextStep > 0 && c == end) {
// If the cost != 0 and c is the end, then we are there
// We have reached the end
// Return the cost and the path
CostPath cp = new CostPath(cost + nextStep,new ArrayList<City>(visited));
cp.path.add(end);
paths.add(cp);
//System.out.println("Found path with cost " + cp.cost);
}
else if (nextStep > 0)
{
// There is a route between start and c
// Recurse to find the all paths
List<City>nextVisited = new ArrayList<City>(visited);
nextVisited.add(c);
shortestPath(c, end, cost+nextStep, nextVisited, level + 1);
}
}
// we have tried all cities. Return
return;
}
}