DIRECTED ACYCLIC GRAPHS
----------------------------------------------------------------------
# CYCLE DETECTION FOR UNDIRECTED GRAPHS

from graphtheory.algorithms.acyclic import AcyclicGraphDFS
from graphtheory.algorithms.acyclic import is_acyclic

# G is given.
assert is_acyclic(G)     # simple test
algorithm = AcyclicGraphDFS(G)
algorithm.run()
print ( algorithm.parent )   # a DFS tree or a forest as a dict
----------------------------------------------------------------------
# DAG DETECTION

# G is given.
assert G.is_directed()
assert is_acyclic(G)
----------------------------------------------------------------------
# TOPOLOGICAL SORTING FOR DAGS

from graphtheory.algorithms.topsort import TopologicalSortQueue
from graphtheory.algorithms.topsort import TopologicalSortSet
from graphtheory.algorithms.topsort import TopologicalSortList
from graphtheory.algorithms.topsort import TopologicalSortDFS

# G is a DAG.
#algorithm = TopologicalSortQueue(G)
#algorithm = TopologicalSortSet(G)
#algorithm = TopologicalSortList(G)
algorithm = TopologicalSortDFS(G)
algorithm.run()
print ( algorithm.sorted_nodes )   # list of nodes sorted
----------------------------------------------------------------------
EOF
