当前位置:网站首页>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
边栏推荐
- Appium introduction and environment installation
- Auto. JS to realize automatic unlocking screen
- 188. 买卖股票的最佳时机 IV
- Wireshark packet capturing skills summarized by myself
- CondaValueError: The target prefix is the base prefix. Aborting.
- 推荐模型之多任务模型:ESMM、MMOE
- CondaValueError: The target prefix is the base prefix. Aborting.
- Distributed basic concepts
- Splicing audio files with ffmpeg-4.3
- 188. the best time to buy and sell stocks IV
猜你喜欢
JMeter implementation specifies concurrent loop testing
[cloud native learning notes] kubernetes Foundation
DHCP operation
Concepts of kubernetes components
Realization of truth table assignment by discrete mathematical programming
Static routing experiment
memcached全面剖析–5. memcached的应用和兼容程序
Common data model (updating)
Role of wait function
Codeforces Round #720 (Div. 2)
随机推荐
Intelligent fish tank control system based on STM32 under Internet of things
Shell script
Golang reflection operation collation
Network layer
BBR bandwidth per second conversion logic
Jar package operation
66 pitfalls in go programming language: pitfalls and common errors of golang developers
Address mapping of virtual memory paging mechanism
一文理解OpenStack网络
XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
Analyse complète Memcached – 2. Comprendre le stockage de mémoire pour Memcached
[cloud native learning notes] learn about kubernetes configuration list yaml file
[cloud native learning notes] deploy applications through yaml files
GDB debugging
how to install clustershell
188. the best time to buy and sell stocks IV
Network flow 24 questions (round table questions)
Slider控制Animator动画播放进度
[cloud native learning notes] kubernetes Foundation
Pattern recognition - 9 Decision tree