forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDagDynamicProgramming.java
More file actions
125 lines (98 loc) · 2.67 KB
/
DagDynamicProgramming.java
File metadata and controls
125 lines (98 loc) · 2.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
package com.williamfiset.algorithms.dp;
import java.util.*;
/**
* Dynamic Programming on Directed Acyclic Graphs (DAG).
*
* <p>
* This implementation demonstrates how to apply dynamic programming on a DAG
* using a topological ordering (Kahn's algorithm).
*
* <p>
* Example use-case: counting the number of ways to reach each node from a
* source.
*
* <p>
* Time Complexity: O(V + E)
* Space Complexity: O(V + E)
*/
public class DagDynamicProgramming {
// Minimal edge representation (only what is needed)
public static class Edge {
int to;
public Edge(int to) {
this.to = to;
}
}
/**
* Performs topological sorting using Kahn's algorithm.
*/
public static int[] kahnTopoSort(Map<Integer, List<Edge>> graph, int numNodes) {
int[] indegree = new int[numNodes];
// Compute indegree
for (int u = 0; u < numNodes; u++) {
for (Edge edge : graph.getOrDefault(u, Collections.emptyList())) {
indegree[edge.to]++;
}
}
Queue<Integer> q = new ArrayDeque<>();
for (int i = 0; i < numNodes; i++) {
if (indegree[i] == 0)
q.add(i);
}
int[] topo = new int[numNodes];
int index = 0;
while (!q.isEmpty()) {
int u = q.poll();
topo[index++] = u;
for (Edge edge : graph.getOrDefault(u, Collections.emptyList())) {
if (--indegree[edge.to] == 0) {
q.add(edge.to);
}
}
}
// Cycle detection
if (index != numNodes)
return new int[0];
return topo;
}
/**
* Counts number of ways to reach each node from a source in a DAG.
*/
public static long[] countWaysDAG(
Map<Integer, List<Edge>> graph, int source, int numNodes) {
int[] topo = kahnTopoSort(graph, numNodes);
if (topo.length == 0)
return null;
long[] dp = new long[numNodes];
dp[source] = 1;
for (int u : topo) {
if (dp[u] == 0L)
continue;
for (Edge edge : graph.getOrDefault(u, Collections.emptyList())) {
dp[edge.to] += dp[u];
}
}
return dp;
}
public static void main(String[] args) {
final int N = 6;
Map<Integer, List<Edge>> graph = new HashMap<>();
for (int i = 0; i < N; i++) {
graph.put(i, new ArrayList<>());
}
// Example DAG
graph.get(0).add(new Edge(1));
graph.get(0).add(new Edge(2));
graph.get(1).add(new Edge(3));
graph.get(2).add(new Edge(3));
graph.get(3).add(new Edge(4));
int source = 0;
long[] dp = countWaysDAG(graph, source, N);
if (dp == null) {
System.out.println("Graph contains a cycle!");
return;
}
System.out.println("Ways from source:");
System.out.println(Arrays.toString(dp));
}
}