当前位置:网站首页>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
边栏推荐
- Rewrite, maplocal and maplocal operations of Charles
- Pod lifecycle in kubernetes
- Tso hardware sharding is a header copy problem
- NPM download speed is slow
- Remember the frequently forgotten problem of continuously reading pictures -%04d
- B站带货当学新东方
- Golang daily question
- Logical backup: mysqldump vs physical backup: xtrabackup
- Minimum cost and maximum flow (template question)
- Summary of message protocol problems
猜你喜欢

Adding subscribers to a list using mailchimp's API V3

VirtualBox virtual machine installation win10 Enterprise Edition

memcached完全剖析–1. memcached的基础

Role of wait function

Microsoft Certification (dynamic 365) test

Power apps Guide

基于C语言实现的足球信息查询系统 课程报告+项目源码+演示PPT+项目截图

Appium introduction and environment installation

Analysis of BBR congestion control state machine

Network flow 24 questions (round table questions)
随机推荐
memcached全面剖析–2. 理解memcached的内存存储
Physical layer introduction
BPF_ PROG_ TYPE_ SOCKET_ Filter function implementation
Static routing experiment
Codeforces Round #720 (Div. 2)
XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
升哲科技 AI 智能防溺水服务上线
架构实战营 第 6 期 毕业总结
TCP_ Nodelay and TCP_ CORK
VirtualBox virtual machine installation win10 Enterprise Edition
AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
Remember the frequently forgotten problem of continuously reading pictures -%04d
VirtualBox虚拟机安装Win10企业版
Simple analysis of WordPress architecture
Station B takes goods to learn from New Oriental
[cloud native learning notes] learn about kubernetes' pod
Wireshark packet capturing skills summarized by myself
Page replacement of virtual memory paging mechanism
大厂出海,败于“姿态”
Curl command