Topological Sorting

给定一个有向图,图节点的拓扑排序被定义为:

 

  • 对于每条有向边A--> B,则A必须排在B之前  
  • 拓扑排序的第一个节点可以是任何在图中没有其他节点指向它的节点  

 

找到给定图的任一拓扑排序.

拓扑排序一共有两种解法:

1.是kahn’s algorithm
2.DFS的做法(按结束顺序放入L中)
kahn算法从入度考虑实际是BFS,DFS从出度考虑。复杂度都为O(V+E).
维基百科给出的kahn算法的伪代码为:
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
    remove a node n from S
    add n to tail of L
    for each node m with an edge e from n to m do
        remove edge e from the graph
        if m has no other incoming edges then
            insert m into S
if graph has edges then
    return error (graph has at least one cycle)
else 
    return L (a topologically sorted order)
 
分三步走:1.先找到度为0的节点,加入队列。
              2.处理队列中的节点,拿出一个结点,对每个结点的临接结点的入度都减少
              3.如果结点的临接结点入度减少为0,则加入队列中。(意思是排在前面的都已经处理完了,到了当前节点)
代码如下:
# Definition for a Directed graph node
# class DirectedGraphNode:
#     def __init__(self, x):
#         self.label = x
#         self.neighbors = []

class Solution:
    """
    @param graph: A list of Directed graph node
    @return: A list of graph nodes in topological order.
    """
    def topSort(self, graph):
        if not graph:
            return []
        hashmap = {} #count the in-degree of the node
#这题graph里包含了所有的结点,所以可以这么写,否则还需要存储一个包含所有结点的集合。 for node in graph: for neigh in node.neighbors: if neigh not in hashmap: hashmap[neigh] = 1 else: hashmap[neigh] += 1 from collections import deque queue = deque() for node in graph: if node not in hashmap: queue.append(node) res = [] while queue: cur = queue.popleft() res.append(cur) #when pop, add to the result for neigh in cur.neighbors: #不会有重复出现,因为拓扑排序的设置,入度高的都在后面,不存在前后重复的问题。 hashmap[neigh] -= 1 if hashmap[neigh] == 0: #when the indegree is reduced to 0,means the prior node are all tackled. queue.append(neigh) return res

 

 

转载于:https://www.cnblogs.com/sherylwang/p/5577999.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值