当前位置:网站首页>Implementation of adjacency table storage array of graph

Implementation of adjacency table storage array of graph

2022-06-24 21:34:00 Come on, come on

idx = 0
#  The first idx The starting point of the edge ,w It's the end ,w Weight. ,first[i]=j: It's No i The number of the newly added edge of the vertex idx(j),pre[i]=j It's No i The front edge of the side is j, And i、j Edges that are all the same vertex 

if __name__ == '__main__':
    n, m = map(int, input().split(' '))

    #  These three arrays are more than the number of sides 
    u = v = w = [-1] * (m + 1)
    print(id(u) == id(v))

    # first And per Is larger than the vertex book 1
    pre = first = [-1 for i in range(0, n + 1)]

    #  The first idx The starting point of the edge ,w It's the end ,w Weight. ,first[i]=j: It's No i The number of the newly added edge of the vertex idx(j),pre[i]=j It's No i The front edge of the side is j, And i、j Edges that are all the same vertex 
    #  Read in 
    for i in range(1, int(m + 1)):
        temp = input().split(' ')
        u[i] = int(temp[0])

        v[i] = int(temp[1])
        w[i] = int(temp[2])
        print(u[i])
        pre[i] = first[u[i]]

        first[u[i]] = i

#  Traverse the indicated edge of each vertex 
for i in range(1, n + 1):
    k = first[i]
    while (k != -1):
        print(f'{u[k]} -> {v[k]}:{w[k]}')
        k = next(k)

  Input :

  1. 4 5
  2. 1 4 9
  3. 4 3 8
  4. 1 2 5
  5. 2 4 6
  6. 1 3 7

Output :

Traceback (most recent call last):
  File "E:\Program\PYTHON\pythonProject3\main.py", line 23, in <module>
    pre[i] = first[u[i]]
IndexError: list index out of range

reason :

 u,v,w The same list represented in the address

print(id(u) == id(v))

The structure is True, So in u[i],v[i],w[i] The value of is the same . Lead to pre[i] = first[u[i]] Time goes beyond the border

terms of settlement :

    u = [-1] * (m + 1)
    v = [-1] * (m + 1)
    w = [-1] * (m + 1)
    pre = [-1 for i in range(0, m + 1)]

    # first Is larger than the vertex book 1
    first = [-1 for i in range(0, n + 1)]

Complete code :


if __name__ == '__main__':
    n, m = map(int, input().split(' '))
    #  These four arrays are more than the number of sides 
    u = [-1] * (m + 1)
    v = [-1] * (m + 1)
    w = [-1] * (m + 1)
    pre = [-1 for i in range(0, m + 1)]

    # first Is larger than the vertex book 1
    first = [-1 for i in range(0, n + 1)]
    #  The first idx The starting point of the edge ,w It's the end ,w Weight. ,first[i]=j: It's No i The number of the newly added edge of the vertex idx(j),pre[i]=j It's No i The front edge of the side is j, And i、j Edges that are all the same vertex 

    #  Read in 
    for i in range(1, int(m + 1)):
        temp = input().split(' ')
        u[i] = int(temp[0])
        v[i] = int(temp[1])
        w[i] = int(temp[2])
        pre[i] = first[u[i]]
        first[u[i]] = i

    #  Traverse the indicated edge of each vertex 
    for i in range(1, n + 1):
        k = first[i]
        while (k != -1):
            print(f'{u[k]} -> {v[k]}:{w[k]}')
            k = pre[k]

Source of ideas :(58 Bar message ) Pictures can also be saved like this —— Array implementation of adjacency table _ Countless love blogs -CSDN Blog

Another way to do it : Array simulation adjacency table - AcWing

原网站

版权声明
本文为[Come on, come on]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241442110879.html