forked from RuselleDaanoy/CampusRoute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.java
More file actions
133 lines (116 loc) · 6.03 KB
/
Dijkstra.java
File metadata and controls
133 lines (116 loc) · 6.03 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
import java.util.*;
public class Dijkstra {
private static final int INFINITY = Integer.MAX_VALUE;
private String results;
private String verticalResults;
int[][] graph;
String[][] direction1;
int zoneNumber;
public String getResults() {
return results;
}
public String getVerticalResults() {
return verticalResults;
}
public Dijkstra(int[][] graph, String[][] direction, int zoneNumber) {
this.graph = graph;
this.direction1 = direction;
this.zoneNumber = zoneNumber;
int destinationNode = zoneNumber; // Change this to your desired destination node
Result result = findShortestPath(graph, direction1, 0, destinationNode);
StringBuilder builder = new StringBuilder();
builder.append("Shortest Path from Zone 1 to Zone " + (destinationNode + 1) + ": \n > " + formatPath(result.path) + "\n");
builder.append("Total Distance: \n > " + result.distance + " units \n");
builder.append("Directions: \n > " + formatDirectionsHorizontal(result.directions) + "\n");
this.results = builder.toString();
StringBuilder verticalBuilder = new StringBuilder();
verticalBuilder.append("Shortest Path from Zone 1 to Zone " + (destinationNode + 1) + ":\n\n");
verticalBuilder.append(formatVerticalResults(result.path, result.directions, result.distance));
this.verticalResults = verticalBuilder.toString();
}
private String formatPath(List<Integer> path) {
StringBuilder formattedPath = new StringBuilder();
for (int i = 0; i < path.size(); i++) {
formattedPath.append(path.get(i) + 1); // Increment node name by 1
if (i < path.size() - 1) {
formattedPath.append(" -> ");
}
}
return formattedPath.toString();
}
private String formatDirectionsHorizontal(List<String> directions) {
StringBuilder formattedDirections = new StringBuilder();
for (int i = 0; i < directions.size(); i++) {
formattedDirections.append(directions.get(i));
if (i < directions.size() - 1) {
formattedDirections.append(", ");
}
}
return formattedDirections.toString();
}
private String formatVerticalResults(List<Integer> path, List<String> directions, int distance) {
StringBuilder verticalResults = new StringBuilder();
for (int i = 0; i < path.size(); i++) {
verticalResults.append("Zone ").append(path.get(i) + 1).append(" > ");
if (i < directions.size()) {
verticalResults.append(directions.get(i)).append("\n");
}
}
verticalResults.append("\nTotal Distance: ").append(distance).append(" units\n");
return verticalResults.toString();
}
// represents the result of the shortest path calculation, including the path (list of nodes), directions (list of strings), and the distance (total distance).
private static class Result {
List<Integer> path;
List<String> directions;
int distance;
Result(List<Integer> path, List<String> directions, int distance) {
this.path = path;
this.directions = directions;
this.distance = distance;
}
}
private static Result findShortestPath(int[][] graph, String[][] directions, int source, int destination) {
int n = graph.length; //calculate the number on node(zone) by getting the length of graph @Floor Directory Class
int[] distances = new int[n]; //store the distance from the starting node(kiosk) to another node
boolean[] visited = new boolean[n]; //keep track the nodes if visited
Arrays.fill(distances, INFINITY); //maximum integer value ng distances
distances[source] = 0; //kiosk as starting point
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.comparingInt(i -> distances[i]));
pq.offer(source); // a prio queue based on distance. kapag mas shorter shorter yung distance mas prio siya
while (!pq.isEmpty()) { //return the node with shortest distance
int u = pq.poll();
if (u == destination) {
break; // Stop early if destination is reached
}
visited[u] = true; // magmamark as visited para maavoid yung revisiting ng nodes
for (int v = 0; v < n; v++) {
if (graph[u][v] != 0 && !visited[v]) { // check if there is an edge between nodes
int alt = distances[u] + graph[u][v]; // add or calculate the distance
if (alt < distances[v]) { //chcheck kung shorter ba yung current distance
distances[v] = alt; // then update
pq.offer(v); // another prio q kung may shorter path pa
}
}
}
}
List<Integer> path = new ArrayList<>(); //store nodes na may shortest path
List<String> pathDirections = new ArrayList<>(); //store directional instruction
path.add(destination); //
pathDirections.add("You're right there!");
int current = destination;
while (current != source) {
for (int prev = 0; prev < n; prev++) {
if (graph[current][prev] != 0 && distances[current] == distances[prev] + graph[prev][current]) {
path.add(prev);
pathDirections.add(directions[prev][current]);
current = prev;
break;
}
}
}
Collections.reverse(path);
Collections.reverse(pathDirections);
return new Result(path, pathDirections, distances[destination]);
}
}