zoukankan      html  css  js  c++  java
  • LintCode-Topological Sorting

    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.
    Note
    You can assume that there is at least one topological order in the graph.
    Example

    For graph as follow: 

    The topological order can be:

    [0, 1, 2, 3, 4, 5]

    or

    [0, 2, 3, 1, 5, 4]

    or

    ....

    Challenge

    Can you do it in both BFS and DFS?

    Analysis:

    A basica method is recording the pre nodes of every node, then find out a node without pre node in each iteration and delete this node from unvisited set.

    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.

    Solution 1:

     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         ArrayList<DirectedGraphNode> res = new ArrayList<DirectedGraphNode>();
    16         if (graph.size()==0) return res;
    17 
    18         //Construct hash map.
    19         Map<DirectedGraphNode, Set<DirectedGraphNode>> map = new HashMap<DirectedGraphNode, Set<DirectedGraphNode>>();
    20         for (DirectedGraphNode node: graph){
    21             Set<DirectedGraphNode> set = new HashSet<DirectedGraphNode>();
    22             map.put(node,set);
    23         }
    24         for (DirectedGraphNode node : graph)
    25             for (DirectedGraphNode temp: node.neighbors)
    26                 map.get(temp).add(node);                
    27 
    28         //Construct topological order sequence.
    29         int len = graph.size();
    30         while (graph.size()>0) {
    31             int index = 0;
    32             while (index<graph.size()){
    33                 DirectedGraphNode node = graph.get(index);
    34                 if (map.get(node).size()==0){
    35                     graph.remove(node);
    36                     res.add(node);
    37                     for (DirectedGraphNode temp: graph)
    38                         if (map.get(temp).contains(node))
    39                             map.get(temp).remove(node);
    40                 } else index++;
    41             }
    42         }
    43         return res;
    44     
    45     
    46         
    47 
    48     }
    49 }

    Solution 2 (DFS):

     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         ArrayList<DirectedGraphNode> res = new ArrayList<DirectedGraphNode>();
    16         if (graph.size()==0) return res;
    17         Map<DirectedGraphNode,Integer> status = new HashMap<DirectedGraphNode,Integer>();
    18         for (DirectedGraphNode node: graph)
    19             status.put(node,0);
    20 
    21         while (hasUnsorted(status,graph)){
    22             DirectedGraphNode node = null;
    23             for (DirectedGraphNode temp : graph)
    24                 if (status.get(temp)==0) node = temp;
    25             search(status, graph, res, node);
    26         }
    27 
    28         return res;
    29             
    30     }
    31 
    32     public boolean hasUnsorted(Map<DirectedGraphNode,Integer> status, ArrayList<DirectedGraphNode> graph){
    33         for (DirectedGraphNode node : graph)
    34             if (status.get(node)==0) return true;
    35 
    36         return false;
    37     }
    38 
    39     public void search(Map<DirectedGraphNode,Integer> status, ArrayList<DirectedGraphNode> graph, ArrayList<DirectedGraphNode> res, DirectedGraphNode node){
    40         if (status.get(node)==1) System.out.println("It is not a DAG");
    41         if (status.get(node)==2) return;
    42         status.put(node,1);
    43         for (DirectedGraphNode next : node.neighbors)
    44             search(status,graph,res,next);
    45         status.put(node,2);
    46         res.add(0,node);
    47     }
    48         
    49 }
  • 相关阅读:
    浅析电商防止恶意下单
    Dubbo与Hadoop RPC的区别
    中秋福利 | 10本技术图书(编程语言、数据分析等)免费送
    IOS渠道追踪方式
    数据迁移的应用场景与解决方案Hamal
    JVM锁实现探究2:synchronized深探
    网页设计简史看设计&代码“隔膜”
    知物由学 | 你真的了解网络安全吗?
    一行代码搞定Dubbo接口调用
    测试周期内测试进度报告规范
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4187867.html
Copyright © 2011-2022 走看看