def is_aperiodic(G):
 if (not G.is_directed()):
  raise nx.NetworkXError('is_aperiodic not defined for undirected graphs')
 s = arbitrary_element(G)
 levels = {s: 0}
 this_level = [s]
 g = 0
 l = 1
 while this_level:
  next_level = []
  for u in this_level:
   for v in G[u]:
    if (v in levels):
     g = gcd(g, ((levels[u] - levels[v]) + 1))
    else:
     next_level.append(v)
     levels[v] = l
  this_level = next_level
  l += 1
 if (len(levels) == len(G)):
  return (g == 1)
 else:
  return ((g == 1) and nx.is_aperiodic(G.subgraph((set(G) - set(levels)))))

def is_aperiodic ( G ) : if ( not G . is_directed ( ) ) : raise nx . NetworkXError ( 'is_aperiodic not defined for undirected graphs' ) s = arbitrary_element ( G ) levels = { s : 0 } this_level = [ s ] g = 0 l = 1 while this_level : next_level = [ ] for u in this_level : for v in G [ u ] : if ( v in levels ) : g = gcd ( g , ( ( levels [ u ] - levels [ v ] ) + 1 ) ) else : next_level . append ( v ) levels [ v ] = l this_level = next_level l += 1 if ( len ( levels ) == len ( G ) ) : return ( g == 1 ) else : return ( ( g == 1 ) and nx . is_aperiodic ( G . subgraph ( ( set ( G ) - set ( levels ) ) ) ) )
return true if g is aperiodic .