-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPathSolver.java
More file actions
162 lines (128 loc) · 4.7 KB
/
Copy pathShortestPathSolver.java
File metadata and controls
162 lines (128 loc) · 4.7 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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ShortestPathSolver {
static class Edge {
int to;
int weight;
Edge(int to, int weight) {
this.to = to;
this.weight = weight;
}
}
static class Node implements Comparable<Node> {
int vertex;
int distance;
Node(int vertex, int distance) {
this.vertex = vertex;
this.distance = distance;
}
@Override
public int compareTo(Node other) {
return Integer.compare(this.distance, other.distance);
}
}
static class Result {
int distance;
List<Integer> path;
Result(int distance, List<Integer> path) {
this.distance = distance;
this.path = path;
}
}
public static Map<Integer, List<Edge>> loadGraph(String filename) throws IOException {
String text = Files.readString(Paths.get(filename));
Pattern pattern = Pattern.compile("\\{(\\d+),\\s*(\\d+),\\s*(\\d+)\\}");
Matcher matcher = pattern.matcher(text);
Map<Integer, List<Edge>> graph = new HashMap<>();
while (matcher.find()) {
int u = Integer.parseInt(matcher.group(1));
int v = Integer.parseInt(matcher.group(2));
int w = Integer.parseInt(matcher.group(3));
graph.putIfAbsent(u, new ArrayList<>());
graph.putIfAbsent(v, new ArrayList<>());
// Undirected graph
graph.get(u).add(new Edge(v, w));
graph.get(v).add(new Edge(u, w));
}
return graph;
}
public static Result dijkstra(Map<Integer, List<Edge>> graph, int start, int end) {
PriorityQueue<Node> pq = new PriorityQueue<>();
Map<Integer, Integer> dist = new HashMap<>();
Map<Integer, Integer> parent = new HashMap<>();
Set<Integer> visited = new HashSet<>();
pq.add(new Node(start, 0));
dist.put(start, 0);
parent.put(start, null);
while (!pq.isEmpty()) {
Node current = pq.poll();
int u = current.vertex;
if (visited.contains(u)) {
continue;
}
visited.add(u);
if (u == end) {
break;
}
List<Edge> neighbors = graph.getOrDefault(u, new ArrayList<>());
for (Edge edge : neighbors) {
int v = edge.to;
int newDist = dist.get(u) + edge.weight;
if (!dist.containsKey(v) || newDist < dist.get(v)) {
dist.put(v, newDist);
parent.put(v, u);
pq.add(new Node(v, newDist));
}
}
}
if (!dist.containsKey(end)) {
return new Result(Integer.MAX_VALUE, new ArrayList<>());
}
List<Integer> path = new ArrayList<>();
Integer current = end;
while (current != null) {
path.add(current);
current = parent.get(current);
}
Collections.reverse(path);
return new Result(dist.get(end), path);
}
public static void main(String[] args) {
String filename = "graph.txt";
try {
Map<Integer, List<Edge>> graph = loadGraph(filename);
Scanner scanner = new Scanner(System.in);
System.out.println("Graph loaded.");
System.out.println("Enter start and end vertices to find the shortest path.");
System.out.println("Type -1 at any prompt to quit.\n");
while (true) {
System.out.print("Start vertex: ");
int start = scanner.nextInt();
if (start == -1) {
break;
}
System.out.print("End vertex: ");
int end = scanner.nextInt();
if (end == -1) {
break;
}
Result result = dijkstra(graph, start, end);
if (result.distance == Integer.MAX_VALUE) {
System.out.println("No path found from " + start + " to " + end + ".\n");
} else {
System.out.println("Shortest path length: " + result.distance);
System.out.println("Path: " + result.path + "\n");
}
}
scanner.close();
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
} catch (InputMismatchException e) {
System.out.println("Please enter valid integers.");
}
}
}