-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPathApp.java
More file actions
108 lines (88 loc) · 3.56 KB
/
Copy pathShortestPathApp.java
File metadata and controls
108 lines (88 loc) · 3.56 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
import java.util.*;
class Graph {
private int numVertices;
private LinkedList<int[]>[] adjacencyList;
// Constructor
@SuppressWarnings("unchecked")
Graph(int vertices) {
this.numVertices = vertices;
adjacencyList = new LinkedList[vertices];
for (int i = 0; i < vertices; i++) {
adjacencyList[i] = new LinkedList<>();
}
}
// Add an edge to the graph
void addEdge(int from, int to, int weight) {
adjacencyList[from].add(new int[]{to, weight});
adjacencyList[to].add(new int[]{from, weight}); // Remove this line for directed graph
}
// Dijkstra's algorithm to find shortest paths from source
void findShortestPaths(int source) {
int[] distances = new int[numVertices]; // distance from source
int[] parents = new int[numVertices]; // store paths
boolean[] visited = new boolean[numVertices];
Arrays.fill(distances, Integer.MAX_VALUE);
Arrays.fill(parents, -1);
distances[source] = 0;
// Priority queue for selecting vertex with minimum distance
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
pq.add(new int[]{source, 0});
while (!pq.isEmpty()) {
int[] current = pq.poll();
int u = current[0];
if (visited[u]) continue;
visited[u] = true;
for (int[] neighbor : adjacencyList[u]) {
int v = neighbor[0];
int weight = neighbor[1];
if (!visited[v] && distances[u] + weight < distances[v]) {
distances[v] = distances[u] + weight;
parents[v] = u;
pq.add(new int[]{v, distances[v]});
}
}
}
// Print results
System.out.println("\nVertex\tDistance from Source\tPath");
for (int i = 0; i < numVertices; i++) {
if (i != source) {
System.out.print(i + "\t\t" + distances[i] + "\t\t");
printPath(i, parents);
System.out.println();
}
}
}
// Recursive function to print path from source to target
void printPath(int vertex, int[] parents) {
if (parents[vertex] == -1) {
System.out.print(vertex);
return;
}
printPath(parents[vertex], parents);
System.out.print(" -> " + vertex);
}
}
public class ShortestPathApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("=== Shortest Path Finder using Dijkstra's Algorithm ===");
System.out.print("Enter number of vertices in the graph: ");
int vertices = scanner.nextInt();
Graph graph = new Graph(vertices);
System.out.print("Enter number of edges: ");
int edges = scanner.nextInt();
System.out.println("Enter each edge in the format: from to weight");
for (int i = 0; i < edges; i++) {
System.out.print("Edge " + (i + 1) + ": ");
int from = scanner.nextInt();
int to = scanner.nextInt();
int weight = scanner.nextInt();
graph.addEdge(from, to, weight);
}
System.out.print("Enter the source vertex: ");
int source = scanner.nextInt();
System.out.println("\nCalculating shortest paths...");
graph.findShortestPaths(source);
scanner.close();
}
}