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

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

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
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 ArrayList
res = new ArrayList
();16 if (graph.size()==0) return res;17 18 //Construct hash map.19 Map
> map = new HashMap
>();20 for (DirectedGraphNode node: graph){21 Set
set = new HashSet
();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

Solution 2 (DFS):

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 ArrayList
res = new ArrayList
();16 if (graph.size()==0) return res;17 Map
status = new HashMap
();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
status, ArrayList
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
status, ArrayList
graph, ArrayList
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 }

 

转载于:https://www.cnblogs.com/lishiblog/p/4187867.html

你可能感兴趣的文章
(转载)深入理解WeakHashmap
查看>>
JAVA中的数组
查看>>
爬虫—使用Requests
查看>>
scrollIntoView()窗口滚动
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
使用ansible远程管理集群
查看>>
读jQuery源码释疑笔记3
查看>>
手把手教你jmeter压测--适合入门
查看>>
Sequelize+MySQL存储emoji表情
查看>>
RabbitMQ学习之Publish/Subscribe(3)
查看>>
[SCOI2010]生成字符串
查看>>
JLOI2015 城池攻占
查看>>
在 Azure 虚拟机上快速搭建 MongoDB 集群
查看>>
跑步运动软件调研
查看>>
搭建ntp时间服务器 ntp - (Network Time Protocol)
查看>>
35. Search Insert Position
查看>>
awk使用
查看>>
ASP.NET Razor 视图引擎编程参考
查看>>
Vue 基础篇
查看>>
malloc_free_new_delete
查看>>