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.
Notice
You can assume that there is at least one topological order in the graph.
Clarification
Learn more about representation of graphs
Example
For graph as follow:
The topological order can be:
[0, 1, 2, 3, 4, 5]
[0, 2, 3, 1, 5, 4]
...
思路
和course schedule II是一题。
BFS:
Kahn's algorithm
One of these algorithms, first described by Kahn (1962), works by choosing vertices in the same order as the eventual topological sort. First, find a list of "start nodes" which have no incoming edges and insert them into a set S; at least one such node must exist in a non-empty acyclic graph. Then:
L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S add n to tail of L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into S if graph has edges then return error (graph has at least one cycle) else return L (a topologically sorted order)
If the graph is a DAG, a solution will be contained in the list L (the solution is not necessarily unique). Otherwise, the graph must have at least one cycle and therefore a topological sorting is impossible.
Reflecting the non-uniqueness of the resulting sort, the structure S can be simply a set or a queue or a stack. Depending on the order that nodes n are removed from set S, a different solution is created. A variation of Kahn's algorithm that breaks ties lexicographically forms a key component of the Coffman–Graham algorithm for parallel scheduling and layered graph drawing.
Code
/** * 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) { // write your code here ArrayList<DirectedGraphNode> result = new ArrayList<>(); HashMap<DirectedGraphNode, Integer> inDegree = new HashMap<>(); for (DirectedGraphNode vertex : graph) { for (DirectedGraphNode neighbor : vertex.neighbors) { if (!inDegree.containsKey(neighbor)) { inDegree.put(neighbor, 1); } else { inDegree.put(neighbor, inDegree.get(neighbor) + 1); } } } Queue<DirectedGraphNode> queue = new LinkedList<>(); for (DirectedGraphNode vertex : graph) { if (!inDegree.containsKey(vertex)) { queue.offer(vertex); } } int count = 0; while (!queue.isEmpty()) { DirectedGraphNode vertex = queue.poll(); result.add(vertex); count++; for (DirectedGraphNode neighbor : vertex.neighbors) { inDegree.put(neighbor, inDegree.get(neighbor) - 1); if (inDegree.get(neighbor) == 0) { queue.offer(neighbor); } } } if (count == graph.size()) { return result; } return new ArrayList<DirectedGraphNode>(0); } }