web-dev-qa-db-ja.com

Pythonで有向グラフのサイクルを検出する方法は?

[('A', 'B'),('C', 'D'),('D', 'C'),('C', 'D')]のような入力があります。この辺のリストで表される有向グラフにサイクルが存在するかどうかを調べたい。

私はディスカッションを読みます: https://www.geeksforgeeks.org/detect-cycle-in-a-graph/ ですが、次の場合にはいくつかのエラーがあります:

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

その結果は「グラフに周期がない」です。これは明らかに間違っています。この問題の解決を手伝ってくれませんか?

3
Abigtree

私自身の実装(再帰的ではないため、サイクル長の制限なし):

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_cyclesnotallサイクル繰り返します!

_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],
    ]
)
0