当前位置:网站首页>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 :
- 4 5
- 1 4 9
- 4 3 8
- 1 2 5
- 2 4 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
边栏推荐
- 基于C语言实现的足球信息查询系统 课程报告+项目源码+演示PPT+项目截图
- Docking of arkit and character creator animation curves
- Oauth2.0 introduction
- Golang daily question
- Am, FM, PM modulation technology
- Physical layer introduction
- The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich
- Auto. JS to realize automatic unlocking screen
- 关于Unity中的transform.InverseTransformPoint, transform.InverseTransofrmDirection
- memcached全面剖析–2. 理解memcached的内存存储
猜你喜欢

Microsoft Certification (dynamic 365) test

Limit summary (under update)

Big factories go out to sea and lose "posture"

123. the best time to buy and sell shares III

Concepts of kubernetes components

Basic database syntax learning

EditText controls the soft keyboard to search

VirtualBox虚拟机安装Win10企业版

Analysis of BBR congestion control state machine

Memcached full profiling – 1 Fundamentals of memcached
随机推荐
Tso hardware sharding is a header copy problem
Remove the screen recording reminder (seven cattle cloud demo)
regular expression
Analysis of BBR congestion control state machine
go_ keyword
Docking of arkit and character creator animation curves
BBR bandwidth per second conversion logic
Memcached comprehensive analysis – 5 Memcached applications and compatible programs
Return of missing persons
Go coding specification
Three more days
Notes_ Vlan
CondaValueError: The target prefix is the base prefix. Aborting.
Apple mobile phone can see some fun ways to install IPA package
Microsoft Certification (dynamic 365) test
VirtualBox virtual machine installation win10 Enterprise Edition
Time standard and format
Learn to use a new technology quickly
Mysql优化查询速度
188. the best time to buy and sell stocks IV