Given an directed graph, a topological order of the graph nodes is defined as follow:
For each directed edge A -> B in graph, A must before B in the order list. The first node in the order can be any node in the graph with no nodes direct to it. Find any topological order for the given graph.
Example
For graph as follow:
The topological order can be:
[0, 1, 2, 3, 4, 5] [0, 2, 3, 1, 5, 4] ...
It is worth noting that if there is a cycle in the graph, then there is no topogical sorting for the graph.
Time: O(N)
Space: O(N)
/**
* Definition for Directed graph.
* class DirectedGraphNode {
* int label;
* ArrayList<DirectedGraphNode> neighbors;
* DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); }
* };
*/
public class Solution {
/**
* @param graph: A list of Directed graph node
* @return: Any topological order for the given graph.
*/
public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
ArrayList<DirectedGraphNode> result = new ArrayList<>();
if (graph == null) {
return result;
}
// init inDegreesMap
Map<DirectedGraphNode, Integer> inDegreesMap = new HashMap<>();
for (DirectedGraphNode node : graph) {
inDegreesMap.put(node, 0);
}
// count indegrees for all nodes
for (DirectedGraphNode node : graph) {
for (DirectedGraphNode neighbor : node.neighbors) {
inDegreesMap.put(neighbor, inDegreesMap.get(neighbor) + 1);
}
}
// Put all initially zero indegree node into a queue
Queue<DirectedGraphNode> zeroInDegreeNodesQueue = new LinkedList<>();
for (Map.Entry<DirectedGraphNode, Integer> entry : inDegreesMap.entrySet()) {
if (entry.getValue() == 0) {
zeroInDegreeNodesQueue.offer(entry.getKey());
}
}
// Remove zero indegree nodes from the graph, and put new zero indegree node into queue.
while (!zeroInDegreeNodesQueue.isEmpty()) {
DirectedGraphNode zeroInDegreeNode = zeroInDegreeNodesQueue.poll();
result.add(zeroInDegreeNode);
for (DirectedGraphNode neighbor : zeroInDegreeNode.neighbors) {
inDegreesMap.put(neighbor, inDegreesMap.get(neighbor) - 1);
if (inDegreesMap.get(neighbor) == 0) {
zeroInDegreeNodesQueue.add(neighbor);
}
}
}
assert result.size() == graph.size();
return result;
}
}
Time: O(N)
Space: O(N)
/**
* Definition for Directed graph.
* class DirectedGraphNode {
* int label;
* ArrayList<DirectedGraphNode> neighbors;
* DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); }
* };
*/
public class Solution {
/**
* @param graph: A list of Directed graph node
* @return: Any topological order for the given graph.
*/
private Map<DirectedGraphNode, Integer> inDegreesMap = new HashMap<>();
public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
ArrayList<DirectedGraphNode> result = new ArrayList<>();
if (graph == null) {
return result;
}
// init inDegreesMap
for (DirectedGraphNode node : graph) {
inDegreesMap.put(node, 0);
}
// count indegrees for all nodes
for (DirectedGraphNode node : graph) {
for (DirectedGraphNode neighbor : node.neighbors) {
inDegreesMap.put(neighbor, inDegreesMap.get(neighbor) + 1);
}
}
// DFS
for (DirectedGraphNode node : graph) {
if (inDegreesMap.get(node) == 0) {
dfs(result, node);
}
}
assert result.size() == graph.size();
return result;
}
private void dfs(ArrayList<DirectedGraphNode> result, DirectedGraphNode zeroInDegreeNode) {
inDegreesMap.put(zeroInDegreeNode, -1); // mark as visited
result.add(zeroInDegreeNode);
for (DirectedGraphNode neighbor : zeroInDegreeNode.neighbors) {
inDegreesMap.put(neighbor, inDegreesMap.get(neighbor) - 1);
if (inDegreesMap.get(neighbor) == 0) {
dfs(result, neighbor);
}
}
}
}