Skip to content

Commit c846643

Browse files
add comments, add DAG shortest path
1 parent cfb79a8 commit c846643

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

HolczerUdemyCourses/src/graphs/shortestpath/Dijkstra.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
import java.util.List;
66
import java.util.PriorityQueue;
77

8+
/* - handles positive edge weights
9+
* - greedy algorithm
10+
* - applications: GPS, navigation, routing information protocol
11+
*/
812
public class Dijkstra {
913

14+
// running time complexity: O(V log V + E)
1015
public void computePaths(Vertex source) {
1116
source.setDistance(0);
1217
PriorityQueue<Vertex> queue = new PriorityQueue<>();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package graphs.shortestpath.dag;
2+
3+
import graphs.shortestpath.Edge;
4+
import graphs.shortestpath.Vertex;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import java.util.Stack;
10+
11+
/* - compute the topological order of a DAG, then iterate through it and relax all edges from the actual vertex
12+
* - handles negative edge weights
13+
* - application: solving Knapsack-problem, seam carving (Avidan-Shamir method)
14+
*/
15+
public class AcyclicShortestPath {
16+
17+
// running time complexity: O(V + E)
18+
public void computePaths(List<Vertex> graph, Vertex source, Vertex target) {
19+
source.setDistance(0);
20+
21+
TopologicalSort topologicalSort = new TopologicalSort();
22+
topologicalSort.sort(graph);
23+
Stack<Vertex> stack = topologicalSort.getTopologicalOrder();
24+
25+
for (Vertex vertex : stack) {
26+
for (Edge edge : vertex.getAdjacencies()) {
27+
Vertex u = edge.getU();
28+
Vertex v = edge.getV();
29+
double tempDistance = u.getDistance() + edge.getWeight();
30+
if (tempDistance < v.getDistance()) {
31+
v.setDistance(tempDistance);
32+
v.setPredecessor(u);
33+
}
34+
}
35+
}
36+
37+
if (target.getDistance() == Double.MAX_VALUE) {
38+
System.out.println("There is no shortest path.");
39+
} else {
40+
System.out.println("Target vertex shortest path: " + target.getDistance());
41+
}
42+
}
43+
44+
public List<Vertex> getShortestPath(Vertex target) {
45+
List<Vertex> shortestPath = new ArrayList<>();
46+
Vertex v = target;
47+
while (v != null) {
48+
shortestPath.add(v);
49+
v = v.getPredecessor();
50+
}
51+
52+
Collections.reverse(shortestPath);
53+
return shortestPath;
54+
}
55+
56+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package graphs.shortestpath.dag;
2+
3+
import graphs.shortestpath.Edge;
4+
import graphs.shortestpath.Vertex;
5+
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.Stack;
9+
10+
public class TopologicalSort {
11+
private Stack<Vertex> stack;
12+
13+
public TopologicalSort() {
14+
this.stack = new Stack<>();
15+
}
16+
17+
public void sort(List<Vertex> graph) {
18+
for (Vertex vertex : graph) {
19+
if (!vertex.isVisited()) {
20+
dfs(vertex);
21+
}
22+
}
23+
}
24+
25+
private void dfs(Vertex vertex) {
26+
vertex.setVisited(true);
27+
28+
for (Edge edge : vertex.getAdjacencies()) {
29+
if (!edge.getV().isVisited()) {
30+
edge.getV().setVisited(true);
31+
dfs(edge.getV());
32+
}
33+
}
34+
35+
stack.push(vertex);
36+
}
37+
38+
public Stack<Vertex> getTopologicalOrder() {
39+
Collections.reverse(stack);
40+
return stack;
41+
}
42+
43+
}

HolczerUdemyCourses/src/graphs/shortestpath/BellmanFord.java renamed to HolczerUdemyCourses/src/graphs/shortestpath/negativeedge/BellmanFord.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
package graphs.shortestpath;
1+
package graphs.shortestpath.negativeedge;
2+
3+
import graphs.shortestpath.Edge;
4+
import graphs.shortestpath.Vertex;
25

36
import java.util.List;
47

8+
/* - handles negative edge weights
9+
* - relaxes all edges at the same time for V - 1 iteration
10+
* - +1 iteration for detecting cycles, if the cost increases in the Vth iteration -> negative cycle
11+
* - applications: arbitrage situation on FOREX, longest path problem, paralell job scheduling problem, critical path method
12+
*/
513
public class BellmanFord {
614
private List<Edge> edges;
715
private List<Vertex> vertices;
@@ -11,6 +19,7 @@ public BellmanFord(List<Edge> edges, List<Vertex> vertices) {
1119
this.vertices = vertices;
1220
}
1321

22+
// running time complexity: O(V * E)
1423
public void computePaths(Vertex source) {
1524
source.setDistance(0);
1625
for (int i = 0; i < vertices.size() - 1; i++) {
@@ -28,6 +37,7 @@ public void computePaths(Vertex source) {
2837
}
2938
}
3039

40+
// cycle detection
3141
for (Edge edge : edges) {
3242
if (edge.getU().getDistance() != Double.MAX_VALUE) {
3343
if (hasCycle(edge)) {

0 commit comments

Comments
 (0)