[关闭]
@w1024020103 2017-07-08T19:11:07.000000Z 字数 2379 阅读 550

Topological Sorting

LintCode


image_1bkgtq23gu9o1e9e13cb1lc36p9.png-99.1kB
image_1bkgtql7q18v1ioitch1a4p1bk3m.png-99.4kB
该视频帮助理解 :
Topological Sort

Topological sorting被老师称为“非常重要”,据说每家公司面试都会考到。Topological order的定义是:
1、图上u->v则排序后u点一定在v点前面
2、排序后的开头的点必须是没有指向该点的edge.(indegree == 0)

In this problem, we use a HashMap to map all nodes to their indegree. By traversing each node's neighbors and put them in the map, those nodes with indegree 0 have been left. We offer these nodes with 0 indegree to the queue and add them to the result.Then we start with these nodes to sort in topological order. We poll one node each time, meaning we traversed it and delete it from the graph. Meanwhile we minus the indegree of its neighbors by one, meaning we delete the edges between this node to its neighbors. We'll then check if any of its neighbors have an indegree of 0, if so we add it to the result and poll it to the queue. We'll later poll it out from the queue and check its neighbors.

This is BFS method, the only difference is that we'll need to check the indegree when we offer a node to the queue.

  1. /**
  2. * Definition for Directed graph.
  3. * class DirectedGraphNode {
  4. * int label;
  5. * ArrayList<DirectedGraphNode> neighbors;
  6. * DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); }
  7. * };
  8. */
  9. public class Solution {
  10. /**
  11. * @param graph: A list of Directed graph node
  12. * @return: Any topological order for the given graph.
  13. */
  14. public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
  15. // write your code here
  16. ArrayList<DirectedGraphNode> result = new ArrayList<>();
  17. if (graph == null || graph.size() == 0) {
  18. return result;
  19. }
  20. Map<DirectedGraphNode, Integer> indegree = new HashMap<DirectedGraphNode, Integer>();
  21. Queue<DirectedGraphNode> queue = new LinkedList<DirectedGraphNode>();
  22. //mapping node to its indegree to the HashMap, however these nodes
  23. //have to be directed to by one other node, nodes whose indegree == 0
  24. //would not be mapped.
  25. for (DirectedGraphNode DAGNode : graph){
  26. for (DirectedGraphNode nei : DAGNode.neighbors){
  27. if(indegree.containsKey(nei)){
  28. indegree.put(nei, indegree.get(nei) + 1);
  29. } else {
  30. indegree.put(nei, 1);
  31. }
  32. }
  33. }
  34. //find all nodes with indegree == 0. They should be at starting positon in the result
  35. for (DirectedGraphNode GraphNode : graph) {
  36. if (!indegree.containsKey(GraphNode)){
  37. queue.offer(GraphNode);
  38. result.add(GraphNode);
  39. }
  40. }
  41. //everytime we poll out a node from the queue, it means we delete it from the
  42. //graph, we will minus its neighbors indegree by one, this is the same meaning
  43. //as we delete the edge from the node to its neighbors.
  44. while (!queue.isEmpty()) {
  45. DirectedGraphNode temp = queue.poll();
  46. for (DirectedGraphNode neighbor : temp.neighbors){
  47. indegree.put(neighbor, indegree.get(neighbor) - 1);
  48. if (indegree.get(neighbor) == 0){
  49. result.add(neighbor);
  50. queue.offer(neighbor);
  51. }
  52. }
  53. }
  54. return result;
  55. }
  56. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注