forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopologicalSort.java
More file actions
160 lines (140 loc) · 4.23 KB
/
Copy pathTopologicalSort.java
File metadata and controls
160 lines (140 loc) · 4.23 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.thealgorithms.sorts;
import java.util.*;
/**
* The Topological Sorting algorithm linearly orders a DAG or Directed Acyclic Graph into
* a linked list. A Directed Graph is proven to be acyclic when a DFS or Depth First Search is
* performed, yielding no back-edges.
*
* https://en.wikipedia.org/wiki/Topological_sorting
*
* @author Jonathan Taylor (https://github.com/Jtmonument)
* Based on Introduction to Algorithms 3rd Edition
*/
public class TopologicalSort {
/*
* Enum to represent the colors for the depth first search
* */
private enum Color {
WHITE, GRAY, BLACK
}
/*
* Class to represent vertices
* */
private static class Vertex {
/*
* Name of vertex
* */
public final String label;
/*
* Weight of vertex
* (more accurately defined as the time that a vertex has begun a visit in DFS)
* */
public int weight;
/*
* The time that the vertex has finished a visit in DFS
* */
public int finished;
/*
* π parent of the vertex
* */
public Vertex predecessor;
/*
* Represents the category of visit in DFS
* */
public Color color = Color.WHITE;
/*
* The array of names of descendant vertices
* */
public final ArrayList<String> next = new ArrayList<>();
public Vertex(String label) {
this.label = label;
}
}
/*
* Graph class uses the adjacency list representation
* */
static class Graph {
/*
* Adjacency list representation
* */
private final HashMap<String, Vertex> adj = new LinkedHashMap<>();
/*
* Function to add an edge to the graph
* */
public void addEdge(String label, String... next) {
adj.put(label, new Vertex(label));
if (!next[0].isEmpty())
Collections.addAll(adj.get(label).next, next);
}
}
static class BackEdgeException extends RuntimeException {
public BackEdgeException(String backEdge) {
super("This graph contains a cycle. No linear ordering is possible. " + backEdge);
}
}
/*
* Time variable in DFS
* */
private static int time;
/*
* Depth First Search
*
* DFS(G)
* for each vertex u ∈ G.V
* u.color = WHITE
* u.π = NIL
* time = 0
* for each vertex u ∈ G.V
* if u.color == WHITE
* DFS-VISIT(G, u)
*
* Performed in Θ(V + E) time
* */
public static LinkedList<String> sort(Graph graph) {
LinkedList<String> list = new LinkedList<>();
graph.adj.forEach((name, vertex) -> {
if (vertex.color == Color.WHITE) {
list.addFirst(sort(graph, vertex, list));
}
});
return list;
}
/*
* Depth First Search Visit
*
* DFS-Visit(G, u)
* time = time + 1
* u.d = time
* u.color = GRAY
* for each v ∈ G.Adj[u]
* if v.color == WHITE
* v.π = u
* DFS-Visit(G, u)
* u.color = BLACK
* time = time + 1
* u.f = time
* */
private static String sort(Graph graph, Vertex u, LinkedList<String> list) {
time++;
u.weight = time;
u.color = Color.GRAY;
graph.adj.get(u.label).next.forEach(label -> {
if (graph.adj.get(label).color == Color.WHITE) {
graph.adj.get(label).predecessor = u;
list.addFirst(sort(graph, graph.adj.get(label), list));
} else if (graph.adj.get(label).color == Color.GRAY) {
/*
* A back edge exists if an edge (u, v) connects a vertex u to its ancestor vertex v
* in a depth first tree. If v.d ≤ u.d < u.f ≤ v.f
*
* In many cases, we will not know u.f, but v.color denotes the type of edge
* */
throw new BackEdgeException("Back edge: " + u.label + " -> " + label);
}
});
u.color = Color.BLACK;
time++;
u.finished = time;
return u.label;
}
}