国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Lintcode: Topological Sorting

2019-11-14 23:49:29
字體:
來源:轉載
供稿:網友
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.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  *     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 node12      * @return: Any topological order for the given graph.13      */    14     public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {15         // write your code here16         ArrayList<DirectedGraphNode> res = new ArrayList<DirectedGraphNode>();17         if (graph.size() == 0) return res;18         HashMap<DirectedGraphNode, Set<DirectedGraphNode>> map = new HashMap<DirectedGraphNode, Set<DirectedGraphNode>>();19         for (DirectedGraphNode each : graph) {20             map.put(each, new HashSet<DirectedGraphNode>());21         }22         for (DirectedGraphNode each : graph) {23             for (int i=0; i<each.neighbors.size(); i++) {24                 map.get(each.neighbors.get(i)).add(each);25             }26         }27         while (graph.size() > 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 modifyDFSto find Topological Sorting of a graph. InDFS, 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  *     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 node12      * @return: Any topological order for the given graph.13      */    14     public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {15         // write your code here16         ArrayList<DirectedGraphNode> res= new ArrayList<DirectedGraphNode>();17         if (graph.size() == 0) return res;18         HashMap<DirectedGraphNode, Integer> status = new HashMap<DirectedGraphNode, Integer>();19         for (DirectedGraphNode elem : graph) {20             status.put(elem, 0);21         }22         ArrayList<DirectedGraphNode> templist = new ArrayList<DirectedGraphNode>();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<DirectedGraphNode> graph, HashMap<DirectedGraphNode, Integer> status, ArrayList<DirectedGraphNode> 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<DirectedGraphNode, Integer> status, ArrayList<DirectedGraphNode> 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 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙口市| 大连市| 兰西县| 抚宁县| 云安县| 平和县| 大连市| 常山县| 泸溪县| 隆昌县| 阿城市| 邵武市| 麻城市| 从化市| 深圳市| 左权县| 台湾省| 富源县| 麻江县| 潜山县| 土默特右旗| 丰都县| 武陟县| 五台县| 南城县| 靖安县| 略阳县| 全椒县| 平和县| 虹口区| 南陵县| 淳安县| 垫江县| 巴林左旗| 赤峰市| 齐河县| 麻江县| 皋兰县| 慈利县| 屏山县| 南江县|