博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lintcode: Topological Sorting
阅读量:4582 次
发布时间:2019-06-09

本文共 4293 字,大约阅读时间需要 14 分钟。

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
neighbors; 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  *     ArrayList
neighbors; 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 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/4431722.html

你可能感兴趣的文章
阿里云 Redis 服务遇到的问题
查看>>
Jwt Token 安全策略使用 ECDSA 椭圆曲线加密算法签名/验证
查看>>
Window2008通过web.config进行限制ip访问
查看>>
浅析门户网站体育赛事CDN加速解决方案
查看>>
启动/关闭xp_cmdshell
查看>>
[PY3]——内置数据结构(8)——解构与封装
查看>>
进程、单线程和多线程
查看>>
python入门(3)python的解释器
查看>>
maven入门(1-3)构建简单的maven项目
查看>>
git 清除本地无效的分支
查看>>
poj1001--Exponentiation
查看>>
Python基础(迭代)
查看>>
使用 PHP 获得网页内容 GET方式
查看>>
TJU Problem 2857 Digit Sorting
查看>>
C# 修饰符
查看>>
java中使用session的一些细节
查看>>
浏览器输入服务器端口号来访问html网页
查看>>
hdu 6435 CSGO(最大曼哈顿距离)
查看>>
logback框架之——日志分割所带来的潜在问题
查看>>
链路追踪工具之Zipkin学习小记
查看>>