簡體   English   中英

如何使用Python檢測有向圖中的循環?

[英]How to detect a cycle in a directed graph with Python?

我有一些輸入,如: [('A', 'B'),('C', 'D'),('D', 'C'),('C', 'D')] 我想在這個 edgeList 表示的有向圖中尋找循環是否存在。

我讀了一個討論: https : //www.geeksforgeeks.org/detect-cycle-in-a-graph/ ,但是當情況是這樣時它有一些錯誤:

g = Graph(3)
g.addEdge('A', 'B')
g.addEdge('B', 'C')
g.addEdge('C', 'A')

它的結果是“圖沒有循環”。 這顯然是錯誤的。 你能幫我解決這個問題嗎?

使用networkx庫,我們可以使用simple_cycles函數查找有向圖的所有簡單循環。

示例代碼:

import networkx as nx

edges = [('A', 'B'),('C', 'D'),('D', 'C'),('C', 'D')]

G = nx.DiGraph(edges)

for cycle in nx.simple_cycles(G):
    print(cycle)

G = nx.DiGraph()

G.add_edge('A', 'B')
G.add_edge('B', 'C')
G.add_edge('C', 'A')

for cycle in nx.simple_cycles(G):
    print(cycle)

輸出:

['D', 'C']
['B', 'C', 'A']

問題是 [1] 中給出的示例: https ://www.geeksforgeeks.org/detect-cycle-in-a-graph/ 僅適用於整數,因為它們使用range()函數來創建節點列表,看線

for node in range(self.V):

這使得假設不僅所有節點都是整數,而且它們將是一個連續的集合,即[0,1,2,3]是可以的,但[0,3,10]不是。

如果您想使用任何節點,可以通過將上面給出的行與

for node in self.graph.keys():

這將遍歷所有節點而不是一系列數字:)

我自己的實現(非遞歸所以沒有循環長度限制):

from collections import defaultdict


def has_cycle(graph):
    try:
        next(_iter_cycles(graph))
    except StopIteration:
        return False
    return True


def _iter_cycles(edges):
    """Iterate over simple cycles in the directed graph."""
    if isinstance(edges, dict):
        graph = edges
    else:
        graph = defaultdict(set)
        for x, y in edges:
            graph[x].add(y)
    SEP = object()
    checked_nodes = set()  # already checked nodes
    for start_node in graph:
        if start_node in checked_nodes:
            continue
        nodes_left = [start_node]
        path = []         # current path from start_node
        node_idx = {}     # {node: path.index(node)}
        while nodes_left:
            node = nodes_left.pop()
            if node is SEP:
                checked_node = path.pop()
                del node_idx[checked_node]
                checked_nodes.add(checked_node)
                continue
            if node in checked_nodes:
                continue
            if node in node_idx:
                cycle_path = path[node_idx[node]:]
                cycle_path.append(node)
                yield cycle_path
                continue
            next_nodes = graph.get(node)
            if not next_nodes:
                checked_nodes.add(node)
                continue
            node_idx[node] = len(path)
            path.append(node)
            nodes_left.append(SEP)
            nodes_left.extend(next_nodes)


assert not has_cycle({0: [1, 2], 1: [3, 4], 5: [6, 7]})
assert has_cycle([(0, 1), (1, 0), (1, 2), (2, 1)])


def assert_cycles(graph, expected):
    detected = sorted(_iter_cycles(graph))
    if detected != expected:
        raise Exception('expected cycles:\n{}\ndetected cycles:\n{}'.format(expected, detected))


assert_cycles([('A', 'B'),('C', 'D'),('D', 'C'),('C', 'D')], [['C', 'D', 'C']])
assert_cycles([('A', 'B'),('B', 'A'),('B', 'C'),('C', 'B')], [['A', 'B', 'A'], ['B', 'C', 'B']])

assert_cycles({1: [2, 3], 2: [3, 4]}, [])
assert_cycles([(1, 2), (1, 3), (2, 3), (2, 4)], [])

assert_cycles({1: [2, 4], 2: [3, 4], 3: [1]}, [[1, 2, 3, 1]])
assert_cycles([(1, 2), (1, 4), (2, 3), (2, 4), (3, 1)], [[1, 2, 3, 1]])

assert_cycles({0: [1, 2], 2: [3], 3: [4], 4: [2]}, [[2, 3, 4, 2]])
assert_cycles([(0, 1), (0, 2), (2, 3), (3, 4), (4, 2)], [[2, 3, 4, 2]])

assert_cycles({1: [2], 3: [4], 4: [5], 5: [3]}, [[3, 4, 5, 3]])
assert_cycles([(1, 2), (3, 4), (4, 5), (5, 3)], [[3, 4, 5, 3]])

assert_cycles({0: [], 1: []}, [])
assert_cycles([], [])

assert_cycles({0: [1, 2], 1: [3, 4], 5: [6, 7]}, [])
assert_cycles([(0, 1), (0, 2), (1, 3), (1, 4), (5, 6), (5, 7)], [])

assert_cycles({0: [1], 1: [0, 2], 2: [1]}, [[0, 1, 0], [1, 2, 1]])
assert_cycles([(0, 1), (1, 0), (1, 2), (2, 1)], [[0, 1, 0], [1, 2, 1]])

編輯:

我發現雖然has_cycle似乎是正確的,但_iter_cycles沒有遍歷所有周期!

_iter_cycles未找到所有循環的示例:

assert_cycles([
        (0, 1), (1, 2), (2, 0),  # Cycle 0-1-2
        (0, 2), (2, 0),          # Cycle 0-2
        (0, 1), (1, 4), (4, 0),  # Cycle 0-1-4
    ],
    [
        [0, 1, 2, 0],  # Not found (in Python 3.7)!
        [0, 1, 4, 0],
        [0, 2, 0],
    ]
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM