-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortestPathOnDAG.java
109 lines (92 loc) · 2.94 KB
/
ShortestPathOnDAG.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
100
101
102
103
104
105
106
107
108
109
package ShortestPath;
import java.util.*;
import java.util.LinkedList;
public class ShortestPathOnDAG {
private static int n;
private static ArrayList<Integer> topologicalOrder;
private static int dist[];
private static int prev[];
static class Graph {
private ArrayList<Edge> adj[];
public Graph(int n) {
adj = new ArrayList[n];
for (int i = 0; i < n; i++)
adj[i] = new ArrayList<>();
}
public void addEdge(int a, int b, int weight) {
adj[a].add(new Edge(b, weight));
}
}
static class Edge {
private int to;
private int weight;
public Edge(int to, int weight) {
this.to = to;
this.weight = weight;
}
}
public static void topologicalSort (Graph graph, int start) {
topologicalOrder = new ArrayList<>();
boolean visited[] = new boolean[n];
dfs(graph, start, visited);
Collections.reverse(topologicalOrder);
}
public static void dfs(Graph graph, int at, boolean visited[]) {
visited[at] = true;
for (Edge edge : graph.adj[at]) {
if (!visited[edge.to])
dfs(graph, edge.to, visited);
}
topologicalOrder.add(at);
}
public static void seekShortestPathOnDAG(Graph graph, int start) {
dist = new int[n];
prev = new int[n];
for (int i = 0; i < n; i++) {
dist[i] = Integer.MAX_VALUE;
prev[i] = -1;
}
dist[start] = 0;
for (int order : topologicalOrder) {
for (Edge edge : graph.adj[order]) {
int newDist = dist[order] + edge.weight;
if (newDist < dist[edge.to]){
dist[edge.to] = newDist;
prev[edge.to] = order;
}
}
}
}
public static void pathReconstruction() {
LinkedList<Integer> path = new LinkedList<>();
int endNode = topologicalOrder.get(n-1);
path.addFirst(endNode);
for (int i = prev[endNode]; i != -1 ; i = prev[i]) {
path.addFirst(i);
}
for (int next : path)
System.out.print(next + " ");
}
// Sample Graph : https://www.youtube.com/watch?v=TXkDpqjDMHA at 4:20
public static void main(String[] args) {
n = 8;
Graph graph = new Graph(n);
graph.addEdge(0, 1, 3);
graph.addEdge(0, 2, 6);
graph.addEdge(1, 2, 4);
graph.addEdge(1, 3, 4);
graph.addEdge(1, 4, 11);
graph.addEdge(2, 3, 8);
graph.addEdge(2, 6, 11);
graph.addEdge(3, 4, -4);
graph.addEdge(3, 5, 5);
graph.addEdge(3, 6, 2);
graph.addEdge(4, 7, 9);
graph.addEdge(5, 7, 1);
graph.addEdge(6, 7, 2);
topologicalSort(graph, 0);
seekShortestPathOnDAG(graph, 0);
System.out.println(dist[7]);
pathReconstruction();
}
}