def child_max(dependencies, dependents, scores):
 result = dict()
 num_needed = dict(((k, len(v)) for (k, v) in dependencies.items()))
 current = set((k for (k, v) in num_needed.items() if (v == 0)))
 while current:
  key = current.pop()
  score = scores[key]
  children = dependencies[key]
  if children:
   score += max((result[child] for child in children))
  result[key] = score
  for parent in dependents[key]:
   num_needed[parent] -= 1
   if (num_needed[parent] == 0):
    current.add(parent)
 return result

def child_max ( dependencies , dependents , scores ) : result = dict ( ) num_needed = dict ( ( ( k , len ( v ) ) for ( k , v ) in dependencies . items ( ) ) ) current = set ( ( k for ( k , v ) in num_needed . items ( ) if ( v == 0 ) ) ) while current : key = current . pop ( ) score = scores [ key ] children = dependencies [ key ] if children : score += max ( ( result [ child ] for child in children ) ) result [ key ] = score for parent in dependents [ key ] : num_needed [ parent ] -= 1 if ( num_needed [ parent ] == 0 ) : current . add ( parent ) return result
maximum-ish of scores of children this takes a dictionary of scores per key and returns a new set of scores per key that is the maximum of the scores of all children of that node plus its own score .