def serialize_with_set(n):
    name, children = n
output = [name]
done = {name}
candidates = deque(children)
while candidates:
    cname, cchildren = candidates.popleft()
if cname not in done:
    output.append(cname)
done.add(cname)
candidates.extend(cchildren)
return output
Serializing a directed, weighted graph