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.NoteYou can assume that there is at least one topological order in the graph.ExampleFor graph as follow: The topological order can be:[0, 1, 2, 3, 4, 5]or[0, 2, 3, 1, 5, 4]or....ChallengeCan you do it in both BFS and DFS?
这道题参考了网上一些很好的思路:
method1: Record the pre nodes of every node, then find out a node without pre node in each iteration and delete this node from unvisited set, add this node to result.
1 /** 2 * Definition for Directed graph. 3 * class DirectedGraphNode { 4 * int label; 5 * ArrayListneighbors; 6 * DirectedGraphNode(int x) { label = x; neighbors = new ArrayList (); } 7 * }; 8 */ 9 public class Solution {10 /**11 * @param graph: A list of Directed graph node12 * @return: Any topological order for the given graph.13 */ 14 public ArrayList topSort(ArrayList graph) {15 // write your code here16 ArrayList res = new ArrayList ();17 if (graph.size() == 0) return res;18 HashMap > map = new HashMap >();19 for (DirectedGraphNode each : graph) {20 map.put(each, new HashSet ());21 }22 for (DirectedGraphNode each : graph) {23 for (int i=0; i 0) {28 int index = 0;29 while (index < graph.size()) {30 DirectedGraphNode cur = graph.get(index);31 if (map.get(cur).size() == 0) {32 //add the node to our result33 //remove the node from the graph34 res.add(cur);35 graph.remove(index);36 for (DirectedGraphNode elem : graph) {37 if (map.get(elem).contains(cur)) {38 map.get(elem).remove(cur);39 }40 }41 }42 else index++;43 }44 }45 return res;46 }47 }
method2: DFS: use a recursive method, randomly pick up an unmakred node, before adding it into result list, recursively visite all its neighbors and add its neighbors into list first. In this way, we guarantee that all the nodes belong to some node's post nodes will be added to the result list first.
To be more specific, we can modify to find Topological Sorting of a graph. In , we start from a vertex, we first print it and then recursively call DFS for its adjacent vertices. In topological sorting, we don’t print the vertex immediately, we first recursively call topological sorting for all its adjacent vertices, then print the current vertex. In this way, we ensure a node's neighbor nodes are always added before the node itself.
1 /** 2 * Definition for Directed graph. 3 * class DirectedGraphNode { 4 * int label; 5 * ArrayListneighbors; 6 * DirectedGraphNode(int x) { label = x; neighbors = new ArrayList (); } 7 * }; 8 */ 9 public class Solution {10 /**11 * @param graph: A list of Directed graph node12 * @return: Any topological order for the given graph.13 */ 14 public ArrayList topSort(ArrayList graph) {15 // write your code here16 ArrayList res= new ArrayList ();17 if (graph.size() == 0) return res;18 HashMap status = new HashMap ();19 for (DirectedGraphNode elem : graph) {20 status.put(elem, 0);21 }22 ArrayList templist = new ArrayList ();23 templist.add(null);24 while (hasUnvisited(graph, status, templist)) {25 DirectedGraphNode cur = templist.get(0);26 templist.set(0, null);27 search(cur, status, res);28 }29 return res;30 }31 32 public boolean hasUnvisited(ArrayList graph, HashMap status, ArrayList templist) {33 for (DirectedGraphNode elem : graph) {34 if (status.get(elem) == 0) {35 templist.set(0, elem);36 return true;37 }38 }39 return false;40 }41 42 public void search(DirectedGraphNode cur, HashMap status, ArrayList res) {43 if (status.get(cur) == 1) System.out.println("not a DAG");44 if (status.get(cur) == 2) return;45 status.put(cur, 1);46 for (DirectedGraphNode neigh : cur.neighbors) {47 search(neigh, status, res);48 }49 status.put(cur, 2);50 res.add(0, cur);51 }52 }