Exemplo n.º 1
0
def has_cycle(G):
    """Decides whether the directed graph has a cycle."""
    try:
        consume(topological_sort(G))
    except nx.NetworkXUnfeasible:
        return True
    else:
        return False
Exemplo n.º 2
0
def has_cycle(G):
    """Decides whether the directed graph has a cycle."""
    try:
        consume(topological_sort(G))
    except nx.NetworkXUnfeasible:
        return True
    else:
        return False
Exemplo n.º 3
0
    def test_topological_sort2(self):
        DG = nx.DiGraph({1: [2], 2: [3], 3: [4],
                         4: [5], 5: [1], 11: [12],
                         12: [13], 13: [14], 14: [15]})
        pytest.raises(nx.NetworkXUnfeasible, consume, nx.topological_sort(DG))

        assert not nx.is_directed_acyclic_graph(DG)

        DG.remove_edge(1, 2)
        consume(nx.topological_sort(DG))
        assert nx.is_directed_acyclic_graph(DG)
Exemplo n.º 4
0
    def test_topological_sort2(self):
        DG = nx.DiGraph({1: [2], 2: [3], 3: [4],
                         4: [5], 5: [1], 11: [12],
                         12: [13], 13: [14], 14: [15]})
        assert_raises(nx.NetworkXUnfeasible, consume, nx.topological_sort(DG))

        assert_false(nx.is_directed_acyclic_graph(DG))

        DG.remove_edge(1, 2)
        consume(nx.topological_sort(DG))
        assert_true(nx.is_directed_acyclic_graph(DG))
Exemplo n.º 5
0
def is_directed_acyclic_graph(G):
    """Return True if the graph `G` is a directed acyclic graph (DAG) or
    False if not.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    bool
        True if `G` is a DAG, False otherwise
    """
    if not G.is_directed():
        return False
    try:
        consume(topological_sort(G))
        return True
    except nx.NetworkXUnfeasible:
        return False
Exemplo n.º 6
0
def is_directed_acyclic_graph(G):
    """Return True if the graph `G` is a directed acyclic graph (DAG) or
    False if not.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    bool
        True if `G` is a DAG, False otherwise
    """
    if not G.is_directed():
        return False
    try:
        consume(topological_sort(G))
        return True
    except nx.NetworkXUnfeasible:
        return False