forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopoSortKahn.java
More file actions
112 lines (95 loc) · 3.29 KB
/
Copy pathTopoSortKahn.java
File metadata and controls
112 lines (95 loc) · 3.29 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
package Misc;
import java.util.*;
class jobGraph2 {
ArrayList<jobNode2> nodes;
// ArrayList<Integer> jobs;
HashMap<Integer, jobNode2> graph = new HashMap<>();
// A graph with nodes as jobs and edges as dependencies
jobGraph2(ArrayList<Integer> jobs) {
// this.jobs=jobs;
this.nodes = new ArrayList<>();
for (int job : jobs) this.addNode(job);
}
void addDep(int job, int dep) {
jobNode2 j = this.getNode(job);
jobNode2 depNode = this.getNode(dep);
j.deps.add(depNode);
depNode.prereqCount++;
}
void addNode(int job) {
this.graph.put(job, new jobNode2(job));
nodes.add(graph.get(job));
}
jobNode2 getNode(int job) {
if (!graph.containsKey(job)) addNode(job);
return graph.get(job);
}
}
// Nodes of a job-graph consisting of the job and its number of pre-requisites
class jobNode2 {
int job, prereqCount;
ArrayList<jobNode2> deps;
jobNode2(int job) {
this.job = job;
this.deps = new ArrayList<>();
this.prereqCount = 0;
}
}
public class TopoSortKahn {
// Gives the topological ordering given a set of jobs and their prerequisites
public static ArrayList<Integer> topoSort(ArrayList<Integer> jobs, ArrayList<Integer[]> deps) {
jobGraph2 graph = createJobGraph(jobs, deps);
return getOrderedJobs(graph);
}
// Forms the job graph using given list of ordered pairs of dependencies and job list
public static jobGraph2 createJobGraph(ArrayList<Integer> jobs, ArrayList<Integer[]> deps) {
jobGraph2 j = new jobGraph2(jobs);
for (Integer[] t : deps) j.addDep(t[0], t[1]);
return j;
}
// Builds the Topological sort using Kahn's Algorithm
public static ArrayList<Integer> getOrderedJobs(jobGraph2 graph) {
ArrayList<Integer> orderedJobs = new ArrayList<>();
ArrayList<jobNode2> nodesWithNoPrereqs = new ArrayList<>();
for (jobNode2 x : graph.nodes) {
if (x.prereqCount == 0) nodesWithNoPrereqs.add(x);
}
while (nodesWithNoPrereqs.size() != 0) {
jobNode2 temp = nodesWithNoPrereqs.remove(nodesWithNoPrereqs.size() - 1);
orderedJobs.add(temp.job);
removeDeps(temp, nodesWithNoPrereqs);
}
boolean graphHasEdges = false;
for (jobNode2 t : graph.nodes) {
if (t.prereqCount != 0) {
graphHasEdges = true;
break;
}
}
return graphHasEdges ? new ArrayList<>() : orderedJobs;
}
// Removes Dependencies of node currently being explored
public static void removeDeps(jobNode2 node, ArrayList<jobNode2> nodesWithNoprereqs) {
while (node.deps.size() != 0) {
jobNode2 dep = node.deps.remove(node.deps.size() - 1);
dep.prereqCount--;
if (dep.prereqCount == 0) nodesWithNoprereqs.add(dep);
}
}
public static void main(String[] args) {
// Sample Testcase
ArrayList<Integer> jobs = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
ArrayList<Integer[]> deps = new ArrayList<>();
deps.add(
new Integer[] {
2, 3
}); // (2, 3) indicates that job 2 has to be completed before starting job 3
deps.add(new Integer[] {2, 5});
deps.add(new Integer[] {1, 3});
deps.add(new Integer[] {3, 4});
deps.add(new Integer[] {4, 6});
deps.add(new Integer[] {5, 6});
deps.add(new Integer[] {6, 7});
assert topoSort(jobs, deps).equals(Arrays.asList(2, 5, 1, 3, 4, 6, 7));
}
}