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

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

Topological Sorting is very important, almost every company has a question related to it.

Goal: To find an proper order making the front nodes are independent from the rest.

Degree: In-degree: how many edges points to this node. Out-Degree: how may edges points out from this node. In Topological Sorting, we only care about in-degree.

Step:

1) Summarize in-degree: keep a hashmap to add the node which indegree is larger than 1.

2) Put all the zero in-degree to a queue. (We can loop the list find the nodes that is not in the hashmap.)

3) Poll those node out, all this nodes neighbors' in-degree minus 1. If then the in-degree equals to 0, put it into the queue. 

If all the nodes come out, this graph could do a Topological Sorting. Because if there is cycle dependency, it cannot do Topological Sorting. 

BSF doesn't need to hashMap to keep which node has been visit, because we use in-dgree to track. Only the in-degree is 0, it will be put in the queue. This make sure there are no duplicates. 

/** * Definition for Directed graph. * class DirectedGraphNode { *     int label; *     ArrayList
neighbors; * DirectedGraphNode(int x) { label = x; neighbors = new ArrayList
(); } * }; */public class Solution { /** * @param graph: A list of Directed graph node * @return: Any topological order for the given graph. */ public ArrayList
topSort(ArrayList
graph) { // write your code here ArrayList
rst = new ArrayList
(); if (graph == null) { return rst; } HashMap
degrees = initalizeDegreeMap(graph); Queue
q = initializeQueue(graph, degrees); while (!q.isEmpty()) { DirectedGraphNode node = q.poll(); rst.add(node); for (DirectedGraphNode neighbor : node.neighbors) { if (degrees.containsKey(neighbor)) { int inDegree = degrees.get(neighbor) - 1; if (inDegree == 0) { q.offer(neighbor); } degrees.put(neighbor, inDegree); } } } return rst; } private HashMap
initalizeDegreeMap(ArrayList
graph) { HashMap
degrees = new HashMap<>(); for (DirectedGraphNode node : graph) { for (DirectedGraphNode neighbor : node.neighbors) { if (!degrees.containsKey(neighbor)) { degrees.put(neighbor, 1); } else { degrees.put(neighbor, degrees.get(neighbor) + 1); } } } return degrees; } private Queue
initializeQueue(ArrayList
graph, HashMap
degrees) { Queue
q = new LinkedList
(); for (DirectedGraphNode node : graph) { if (!degrees.containsKey(node)) { q.offer(node); } } return q; }}

 

转载于:https://www.cnblogs.com/codingEskimo/p/7006909.html

你可能感兴趣的文章
Linux下WebSphere安装
查看>>
django实现文件下载
查看>>
编译 Clozure CL 的 Mac IDE 版,超级简单
查看>>
Windows 下 gcc + golang 编译 git2go
查看>>
@Transactional数据事务控制
查看>>
juniper交换机ex2200配置(生产环境)
查看>>
SecureCRT 生成公钥KEY登录
查看>>
AOP原理
查看>>
ubuntu maven 安装配置
查看>>
医学教育网批量资源下载程序之——获取下载列表
查看>>
#CCNA#笔记第二弹
查看>>
控制面板打不开
查看>>
JVM 之 ParNew 和 CMS 日志分析
查看>>
开发者分享 | 从零开始开发一个即时通讯项目
查看>>
var 是 Java 开发的好朋友啊!
查看>>
提高 网站 百度权重
查看>>
最牛逼的 HTML 和 CSS 代码的背后
查看>>
apache 配置虚拟目录
查看>>
Hibernate、Mybait,Mysql、Postgresql适用场景
查看>>
WordPress表结构说明(转)
查看>>