Python算法教程:找出图的连通分量
生活随笔
收集整理的這篇文章主要介紹了
Python算法教程:找出图的连通分量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個圖結構的連通分量是能讓它里面的所有節點彼此到達的最大子圖。
def components(graph):component = []seen = set()for u in graph:if u in seen:continuecurrent = walk(graph, u)seen.update(current)component.append(current)return componentdef walk(graph, start, s=set()):nodes, current = set(), dict()current[start] = Nonenodes.add(start)while nodes:u = nodes.pop()for v in graph[u].difference(current, s):nodes.add(v)current[v] = ureturn currentgraph = {'a': set('bc'),'b': set('d'),'c': set('bd'),'d': set(), } print(components(graph))(最近更新:2019年05月31日)
總結
以上是生活随笔為你收集整理的Python算法教程:找出图的连通分量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 注解和反射实现dao层增删改查
- 下一篇: TSQL索引与视图