-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransportationCode.java
More file actions
292 lines (230 loc) · 8.67 KB
/
Copy pathTransportationCode.java
File metadata and controls
292 lines (230 loc) · 8.67 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.Queue;
public class TransportationCode extends JFrame {
private Graph graph;
private JComboBox<String> sourceBox;
private JTextArea outputArea;
public TransportationCode() {
graph = new Graph();
setTitle("Transportation Network Application");
setSize(900, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(9, 1, 10, 10));
JButton addCityBtn = new JButton("Add City");
JButton addRoadBtn = new JButton("Add Road");
JButton shortestPathBtn = new JButton("Minimum Route");
JButton traversalBtn = new JButton("Traversal Order");
JButton reachabilityBtn = new JButton("Reachability Check");
JButton mstBtn = new JButton("Minimum Cost Network");
JButton resetBtn = new JButton("Reset Graph");
sourceBox = new JComboBox<>();
controlPanel.add(addCityBtn);
controlPanel.add(addRoadBtn);
controlPanel.add(new JLabel("Select Source City:"));
controlPanel.add(sourceBox);
controlPanel.add(shortestPathBtn);
controlPanel.add(traversalBtn);
controlPanel.add(reachabilityBtn);
controlPanel.add(mstBtn);
controlPanel.add(resetBtn);
outputArea = new JTextArea();
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
add(controlPanel, BorderLayout.WEST);
add(scrollPane, BorderLayout.CENTER);
addCityBtn.addActionListener(e -> addCity());
addRoadBtn.addActionListener(e -> addRoad());
shortestPathBtn.addActionListener(e -> showShortestPath());
traversalBtn.addActionListener(e -> showTraversal());
reachabilityBtn.addActionListener(e -> checkReachability());
mstBtn.addActionListener(e -> showMST());
resetBtn.addActionListener(e -> resetGraph());
setVisible(true);
}
private void addCity() {
String city = JOptionPane.showInputDialog(this, "Enter city name:");
if (city != null && !city.trim().isEmpty()) {
city = city.trim();
graph.addCity(city);
sourceBox.addItem(city);
outputArea.append("City Added: " + city + "\n");
}
}
private void addRoad() {
java.util.List<String> cities = graph.getCities();
if (cities.size() < 2) {
JOptionPane.showMessageDialog(this, "Add at least 2 cities first.");
return;
}
String from = (String) JOptionPane.showInputDialog(
this,
"Select From City",
"Add Road",
JOptionPane.PLAIN_MESSAGE,
null,
cities.toArray(),
null);
String to = (String) JOptionPane.showInputDialog(
this,
"Select To City",
"Add Road",
JOptionPane.PLAIN_MESSAGE,
null,
cities.toArray(),
null);
String distanceText = JOptionPane.showInputDialog(this, "Enter Distance:");
try {
int distance = Integer.parseInt(distanceText);
if (from != null && to != null && !from.equals(to)) {
graph.addRoad(from, to, distance);
outputArea.append("Road Added: " + from + " -> " + to + " = " + distance + "\n");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid Distance");
}
}
private void showShortestPath() {
String source = (String) sourceBox.getSelectedItem();
if (source == null) return;
Map<String, Integer> result = graph.dijkstra(source);
outputArea.setText("Minimum Route from " + source + "\n\n");
for (String city : result.keySet()) {
if (result.get(city) == Integer.MAX_VALUE) {
outputArea.append(source + " -> " + city + " = Not Reachable\n");
} else {
outputArea.append(source + " -> " + city + " = " + result.get(city) + "\n");
}
}
}
private void showTraversal() {
String source = (String) sourceBox.getSelectedItem();
if (source == null) return;
java.util.List<String> order = graph.bfs(source);
outputArea.setText("Traversal Order (BFS):\n\n");
outputArea.append(String.join(" -> ", order));
}
private void checkReachability() {
String source = (String) sourceBox.getSelectedItem();
if (source == null) return;
java.util.List<String> visited = graph.bfs(source);
if (visited.size() == graph.getCities().size()) {
outputArea.setText("All cities are reachable from " + source);
} else {
outputArea.setText("Some cities are unreachable from " + source);
}
}
private void showMST() {
outputArea.setText(graph.minimumSpanningTree());
}
private void resetGraph() {
graph = new Graph();
sourceBox.removeAllItems();
outputArea.setText("");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TransportationCode();
}
});
}
}
class Graph {
private Map<String, Map<String, Integer>> adj;
public Graph() {
adj = new HashMap<>();
}
public void addCity(String city) {
adj.putIfAbsent(city, new HashMap<String, Integer>());
}
public void addRoad(String from, String to, int distance) {
adj.get(from).put(to, distance);
adj.get(to).put(from, distance);
}
public java.util.List<String> getCities() {
return new ArrayList<String>(adj.keySet());
}
public Map<String, Integer> dijkstra(String source) {
Map<String, Integer> dist = new HashMap<String, Integer>();
for (String city : adj.keySet()) {
dist.put(city, Integer.MAX_VALUE);
}
dist.put(source, 0);
PriorityQueue<String> pq = new PriorityQueue<String>(
Comparator.comparingInt(dist::get)
);
pq.add(source);
while (!pq.isEmpty()) {
String current = pq.poll();
for (String neighbor : adj.get(current).keySet()) {
int newDistance = dist.get(current) + adj.get(current).get(neighbor);
if (newDistance < dist.get(neighbor)) {
dist.put(neighbor, newDistance);
pq.add(neighbor);
}
}
}
return dist;
}
public java.util.List<String> bfs(String source) {
java.util.List<String> order = new ArrayList<String>();
Set<String> visited = new HashSet<String>();
Queue<String> queue = new LinkedList<String>();
queue.add(source);
visited.add(source);
while (!queue.isEmpty()) {
String current = queue.poll();
order.add(current);
for (String neighbor : adj.get(current).keySet()) {
if (!visited.contains(neighbor)) {
visited.add(neighbor);
queue.add(neighbor);
}
}
}
return order;
}
public String minimumSpanningTree() {
if (adj.isEmpty()) {
return "Graph is Empty";
}
String start = getCities().get(0);
Set<String> visited = new HashSet<String>();
visited.add(start);
StringBuilder result = new StringBuilder();
result.append("Minimum Cost Network (Prim's Algorithm)\n\n");
int totalCost = 0;
while (visited.size() < adj.size()) {
String minFrom = "";
String minTo = "";
int minCost = Integer.MAX_VALUE;
for (String from : visited) {
for (String to : adj.get(from).keySet()) {
int cost = adj.get(from).get(to);
if (!visited.contains(to) && cost < minCost) {
minCost = cost;
minFrom = from;
minTo = to;
}
}
}
if (minTo.equals("")) {
break;
}
visited.add(minTo);
totalCost += minCost;
result.append(minFrom)
.append(" - ")
.append(minTo)
.append(" = ")
.append(minCost)
.append("\n");
}
result.append("\nTotal Cost = ").append(totalCost);
return result.toString();
}
}