I know there are two ways to represent my graph: one is using a matrix, and the other one is using a list.
If I use a matrix, I have to flip all the bits in the matrix.
G = {"A":["B", "C","D"],"B":["C", "E"], "C":["D", "E"],"D":[],"E":["D"] } res ={} for k in G.keys(): for val in G[k]: if val not in res.keys(): res[val] = [k] else: res[val].append(k) print(res)