A node is added when its all sources are traversed
/** * Definition for Directed graph. * class DirectedGraphNode { * int label; * ArrayList<DirectedGraphNode> neighbors; * DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); } * }; */publicclassSolution {/* * @param graph: A list of Directed graph node * @return: Any topological order for the given graph. */publicArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {Map<DirectedGraphNode,Integer> map =newHashMap<>();// Count the flow in number of each node for (DirectedGraphNode node: graph){for (DirectedGraphNode neighbor:node.neighbors){map.put(neighbor,map.getOrDefault(neighbor,0) +1); } }ArrayList<DirectedGraphNode> ans =newArrayList<>();Queue<DirectedGraphNode> queue =newLinkedList<>();// Find the heads of graphfor (DirectedGraphNode node: graph){if (!map.containsKey(node)){queue.add(node);ans.add(node); } }// start to put node to answhile(!queue.isEmpty()){DirectedGraphNode curNode =queue.poll();for (DirectedGraphNode node:curNode.neighbors){map.put(node ,map.get(node) -1);if (map.get(node) ==0){queue.add(node);ans.add(node); } } }return ans; }}
DFS
Use map to record the flowing in times of each node
Find out the initial node of graph and put them into stack
A boolean array to record "traversed"
Use dfs and a stack to push leaf node first, parent node last
publicArrayList<DirectedGraphNode>topSort(ArrayList<DirectedGraphNode> graph) {Map<DirectedGraphNode,Integer> map =newHashMap<>();// Count the flow in number of each node for (DirectedGraphNode node: graph){for (DirectedGraphNode neighbor:node.neighbors){map.put(neighbor,map.getOrDefault(neighbor,0) +1); } }ArrayList<DirectedGraphNode> ans =newArrayList<>();Stack<DirectedGraphNode> stack =newStack<>();boolean[] traversed =newboolean[graph.size()];for (DirectedGraphNode node: graph){if (!map.containsKey(node)){dfs(stack, traversed, node); } }while(!stack.isEmpty()){ans.add(stack.pop()); }return ans; }privatevoiddfs(Stack<DirectedGraphNode> stack,boolean[] traversed,DirectedGraphNode node){ traversed[node.label] =true;for (DirectedGraphNode neighbor:node.neighbors){if (traversed[neighbor.label] ==false){dfs(stack, traversed, neighbor); } }stack.push(node); }
210 Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
from collections import defaultdictclassSolution:deffindOrder(self,numCourses:int,prerequisites: List[List[int]]) -> List[int]: graph =defaultdict(set) degree =defaultdict(int)for c1, c2 in prerequisites: graph[c2].add(c1) degree[c1]+=1 q = [] ans = []for i inrange(numCourses):if i notin degree: q.append(i)while q: c = q.pop(0) ans.append(c)for nei in graph[c]: degree[nei]-=1if degree[nei]==0: q.append(nei)return ans iflen(ans)== numCourses else []