How to reverse a graph in linear time?

前端 未结 4 1843
天涯浪人
天涯浪人 2021-02-08 06:46

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.

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-08 07:00

    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)
    

提交回复
热议问题