相关疑难解决方法(0)

Python中的邻接列表和邻接矩阵

你好,我理解邻接列表和矩阵的概念,但我很困惑如何在Python中实现它们:

实现以下两个示例的算法实现但不知道从一开始的输入,因为他们在他们的示例中对其进行硬编码:

对于邻接列表:

    a, b, c, d, e, f, g, h = range(8) 
    N = [ 
     {b:2, c:1, d:3, e:9, f:4},    # a 
     {c:4, e:3},                   # b 
     {d:8},                        # c 
     {e:7},                        # d 
     {f:5},                        # e 
     {c:2, g:2, h:2},              # f 
     {f:1, h:6},                   # g 
     {f:9, g:8}                    # h 
   ] 
Run Code Online (Sandbox Code Playgroud)

对于邻接矩阵:

    a, b, c, d, e, f, g, h = range(8) 
    _ = float('inf') 
    #     a b c d e f g h
    W = [[0,2,1,3,9,4,_,_], # a 
        [_,0,4,_,3,_,_,_], # …
Run Code Online (Sandbox Code Playgroud)

python algorithm adjacency-list adjacency-matrix

12
推荐指数
1
解决办法
2万
查看次数