def invert_graph(graph):
 inverted = {}
 for key in graph:
  for value in graph[key]:
   inverted.setdefault(value, set()).add(key)
 return inverted

def invert_graph ( graph ) : inverted = { } for key in graph : for value in graph [ key ] : inverted . setdefault ( value , set ( ) ) . add ( key ) return inverted
inverts a directed graph .