求parent或child

V = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
E = [ [1,2], [1,3],  [2,4],  [2,5],  [3,6],  [3,7],  [4,8], [4,9], [5,10], [5,11], [6,12], [6,13], [7,14], [7,15] ]  # edge:[父,子]

parents = lambda vertices: reduce(lambda x, edge: x+[edge[0]] if edge[1] in vertices else x, E, [])
childs = lambda vertices: reduce(lambda x, edge: x+[edge[1]] if edge[0] in vertices else x, E, [])

print(childs(parents([9,11])))

你可能感兴趣的:(求parent或child)