# Path: pyalgs/data_structures/commons/stack.py
# class Stack(object):
#     """ Stack interface which provides the API for stack data structure
#     """
# 
#     __metaclass__ = ABCMeta
# 
#     @abstractmethod
#     def push(self, item):
#         pass
# 
#     @abstractmethod
#     def pop(self):
#         pass
# 
#     @abstractmethod
#     def is_empty(self):
#         pass
# 
#     @abstractmethod
#     def size(self):
#         pass
# 
#     @abstractmethod
#     def iterate(self):
#         pass
# 
#     @staticmethod
#     def create():
#         return LinkedListStack()
# 
# Path: pyalgs/data_structures/graphs/graph.py
# class DirectedEdgeWeightedGraph(object):
#     adjList = None
#     V = 0
# 
#     def __init__(self, vertex_count):
#         self.V = vertex_count
#         self.adjList = [None] * vertex_count
#         for v in range(vertex_count):
#             self.adjList[v] = Bag()
# 
#     def add_edge(self, edge):
#         v = edge.start()
#         self.adjList[v].add(edge)
# 
#     def adj(self, v):
#         return self.adjList[v].iterate()
# 
#     def vertex_count(self):
#         return self.V
# 
#     def edges(self):
#         for v in range(self.V):
#             for e in self.adj(v):
#                 yield e
# 
#     def to_graph(self):
#         g = Graph()
# 
#         for e in self.edges():
#             g.add_edge(e.start(), e.end())
#         return g
# 
#     def to_digraph(self):
#         g = Digraph(self.V)
# 
#         for e in self.edges():
#             g.add_edge(e.start(), e.end())
# 
#         return g
# 
# class Digraph(object):
#     V = 0
#     adjList = None
# 
#     def __init__(self, V):
#         self.V = V
#         self.adjList = [None] * V
#         for v in range(V):
#             self.adjList[v] = Bag()
# 
#     def vertex_count(self):
#         return self.V
# 
#     def adj(self, v):
#         return self.adjList[v].iterate()
# 
#     def add_edge(self, v, w):
#         self.adjList[v].add(w)
# 
#     def reverse(self):
#         g = Digraph(self.V)
#         for v in range(self.V):
#             for w in self.adjList[v].iterate():
#                 g.add_edge(w, v)
# 
#         return g


from pyalgs.data_structures.commons.stack import Stack
from pyalgs.data_structures.graphs.graph import DirectedEdgeWeightedGraph, Digraph
    cycle = None
    edgeTo = None

    def __init__(self, G):
        if isinstance(G, DirectedEdgeWeightedGraph):
            G = G.to_digraph()

        if not isinstance(G, Digraph):
            raise ValueError('Graph must be unweighted digraph')

        vertex_count = G.vertex_count()
        self.marked = [False] * vertex_count
        self.onStack = [False] * vertex_count
        self.edgeTo = [-1] * vertex_count

        for v in range(vertex_count):
            if not self.marked[v]:
                self.dfs(G, v)

    def dfs(self, G, v):
        self.marked[v] = True
        self.onStack[v] = True

        for w in G.adj(v):
            if not self.marked[w]:
                self.edgeTo[w] = v
                self.dfs(G, w)
            elif self.cycle is not None:
                break
            elif self.onStack[w]:

# Path: pyalgs/data_structures/commons/stack.py
# class Stack(object):
#     """ Stack interface which provides the API for stack data structure
#     """
# 
#     __metaclass__ = ABCMeta
# 
#     @abstractmethod
#     def push(self, item):
#         pass
# 
#     @abstractmethod
#     def pop(self):
#         pass
# 
#     @abstractmethod
#     def is_empty(self):
#         pass
# 
#     @abstractmethod
#     def size(self):
#         pass
# 
#     @abstractmethod
#     def iterate(self):
#         pass
# 
#     @staticmethod
#     def create():
#         return LinkedListStack()
# 
# Path: pyalgs/data_structures/graphs/graph.py
# class DirectedEdgeWeightedGraph(object):
#     adjList = None
#     V = 0
# 
#     def __init__(self, vertex_count):
#         self.V = vertex_count
#         self.adjList = [None] * vertex_count
#         for v in range(vertex_count):
#             self.adjList[v] = Bag()
# 
#     def add_edge(self, edge):
#         v = edge.start()
#         self.adjList[v].add(edge)
# 
#     def adj(self, v):
#         return self.adjList[v].iterate()
# 
#     def vertex_count(self):
#         return self.V
# 
#     def edges(self):
#         for v in range(self.V):
#             for e in self.adj(v):
#                 yield e
# 
#     def to_graph(self):
#         g = Graph()
# 
#         for e in self.edges():
#             g.add_edge(e.start(), e.end())
#         return g
# 
#     def to_digraph(self):
#         g = Digraph(self.V)
# 
#         for e in self.edges():
#             g.add_edge(e.start(), e.end())
# 
#         return g
# 
# class Digraph(object):
#     V = 0
#     adjList = None
# 
#     def __init__(self, V):
#         self.V = V
#         self.adjList = [None] * V
#         for v in range(V):
#             self.adjList[v] = Bag()
# 
#     def vertex_count(self):
#         return self.V
# 
#     def adj(self, v):
#         return self.adjList[v].iterate()
# 
#     def add_edge(self, v, w):
#         self.adjList[v].add(w)
# 
#     def reverse(self):
#         g = Digraph(self.V)
#         for v in range(self.V):
#             for w in self.adjList[v].iterate():
#                 g.add_edge(w, v)
# 
#         return g


# Path: pyalgs/algorithms/graphs/directed_cycle.py
from pyalgs.data_structures.commons.stack import Stack
from pyalgs.data_structures.graphs.graph import DirectedEdgeWeightedGraph, Digraph

    cycle = None
    edgeTo = None

    def __init__(self, G):
        if isinstance(G, DirectedEdgeWeightedGraph):
            G = G.to_digraph()

        if not isinstance(G, Digraph):
            raise ValueError('Graph must be unweighted digraph')

        vertex_count = G.vertex_count()
        self.marked = [False] * vertex_count
        self.onStack = [False] * vertex_count
        self.edgeTo = [-1] * vertex_count

        for v in range(vertex_count):
            if not self.marked[v]:
                self.dfs(G, v)

    def dfs(self, G, v):
        self.marked[v] = True
        self.onStack[v] = True

        for w in G.adj(v):
            if not self.marked[w]:
                self.edgeTo[w] = v
                self.dfs(G, w)
            elif self.cycle is not None:
                break
            elif self.onStack[w]:

                self.cycle = Stack.create()