def _contains_cycle(fgraph, orderings):
 outputs = fgraph.outputs
 assert isinstance(outputs, (tuple, list, deque))
 parent_counts = {}
 node_to_children = {}
 visitable = deque()
 for var in fgraph.variables:
  owner = var.owner
  if owner:
   node_to_children.setdefault(owner, []).append(var)
   parent_counts[var] = 1
  else:
   visitable.append(var)
   parent_counts[var] = 0
 for a_n in fgraph.apply_nodes:
  parents = list(a_n.inputs)
  parents.extend(orderings.get(a_n, []))
  if parents:
   for parent in parents:
    node_to_children.setdefault(parent, []).append(a_n)
   parent_counts[a_n] = len(parents)
  else:
   visitable.append(a_n)
   parent_counts[a_n] = 0
 visited = 0
 while visitable:
  node = visitable.popleft()
  visited += 1
  for client in node_to_children.get(node, []):
   parent_counts[client] -= 1
   if (not parent_counts[client]):
    visitable.append(client)
 return (visited != len(parent_counts))

def _contains_cycle ( fgraph , orderings ) : outputs = fgraph . outputs assert isinstance ( outputs , ( tuple , list , deque ) ) parent_counts = { } node_to_children = { } visitable = deque ( ) for var in fgraph . variables : owner = var . owner if owner : node_to_children . setdefault ( owner , [ ] ) . append ( var ) parent_counts [ var ] = 1 else : visitable . append ( var ) parent_counts [ var ] = 0 for a_n in fgraph . apply_nodes : parents = list ( a_n . inputs ) parents . extend ( orderings . get ( a_n , [ ] ) ) if parents : for parent in parents : node_to_children . setdefault ( parent , [ ] ) . append ( a_n ) parent_counts [ a_n ] = len ( parents ) else : visitable . append ( a_n ) parent_counts [ a_n ] = 0 visited = 0 while visitable : node = visitable . popleft ( ) visited += 1 for client in node_to_children . get ( node , [ ] ) : parent_counts [ client ] -= 1 if ( not parent_counts [ client ] ) : visitable . append ( client ) return ( visited != len ( parent_counts ) )
function to check if the given graph contains a cycle parameters fgraph the functiongraph to check for cycles .