当前位置:网站首页>图的邻接表存储 数组实现
图的邻接表存储 数组实现
2022-06-24 19:19:00 【浪了来来啊】
idx = 0
# 第idx条边的起点,w是终点,w是权重,first[i]=j:是第i顶点的最新加入的边的编号idx(j),pre[i]=j是第i条边的前一条边的是j,且i、j都是同一个顶点的边
if __name__ == '__main__':
n, m = map(int, input().split(' '))
# 这三个数组要比边数多一
u = v = w = [-1] * (m + 1)
print(id(u) == id(v))
# first与per的大小要比顶点书大1
pre = first = [-1 for i in range(0, n + 1)]
# 第idx条边的起点,w是终点,w是权重,first[i]=j:是第i顶点的最新加入的边的编号idx(j),pre[i]=j是第i条边的前一条边的是j,且i、j都是同一个顶点的边
# 读入边
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
# 对每个顶点的指出边进行遍历
for i in range(1, n + 1):
k = first[i]
while (k != -1):
print(f'{u[k]} -> {v[k]}:{w[k]}')
k = next(k)
输入:
- 4 5
- 1 4 9
- 4 3 8
- 1 2 5
- 2 4 6
- 1 3 7
输出:
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
原因:
u,v,w在地址中代表的同一列表
print(id(u) == id(v))结构为True,所以在u[i],v[i],w[i]的值相同。导致pre[i] = first[u[i]]时越界
解决办法:
u = [-1] * (m + 1)
v = [-1] * (m + 1)
w = [-1] * (m + 1)
pre = [-1 for i in range(0, m + 1)]
# first的大小要比顶点书大1
first = [-1 for i in range(0, n + 1)]完整代码:
if __name__ == '__main__':
n, m = map(int, input().split(' '))
# 这四个数组要比边数多一
u = [-1] * (m + 1)
v = [-1] * (m + 1)
w = [-1] * (m + 1)
pre = [-1 for i in range(0, m + 1)]
# first的大小要比顶点书大1
first = [-1 for i in range(0, n + 1)]
# 第idx条边的起点,w是终点,w是权重,first[i]=j:是第i顶点的最新加入的边的编号idx(j),pre[i]=j是第i条边的前一条边的是j,且i、j都是同一个顶点的边
# 读入边
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
# 对每个顶点的指出边进行遍历
for i in range(1, n + 1):
k = first[i]
while (k != -1):
print(f'{u[k]} -> {v[k]}:{w[k]}')
k = pre[k]
思路来源:(58条消息) 图还可以这样存——邻接表的数组实现_不可数的爱的博客-CSDN博客
另一种实现方式:数组模拟邻接表 - AcWing
边栏推荐
- 2021-09-30
- Requests requests for web page garbled code resolution
- (to be optimized and modified) vivado DDR4 SDRAM (MIG) (2.2) IP core learning record
- Talking about the range of data that MySQL update will lock
- [cloud native learning notes] learn about kubernetes configuration list yaml file
- Auto. JS to realize automatic unlocking screen
- Power apps Guide
- 123. 买卖股票的最佳时机 III
- Codeforces Round #720 (Div. 2)
- Packaging_ Conversion between basic type and string type
猜你喜欢

Alibaba cloud lightweight servers open designated ports

After a few years in the testing industry, do you still know a little?

大厂出海,败于“姿态”

Web automation: summary of special scenario processing methods

基于STM32的物联网下智能化养鱼鱼缸控制控制系统

The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich

CondaValueError: The target prefix is the base prefix. Aborting.

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

JMeter basic learning records

OSI notes sorting
随机推荐
Return of missing persons
Summary of message protocol problems
Shell script
Learn to use a new technology quickly
go_ keyword
Microsoft Certification (dynamic 365) test
NPM download speed is slow
[cloud native learning notes] kubernetes practice command
Minimum cost and maximum flow (template question)
A/B测试助力游戏业务增长
Apple mobile phone can see some fun ways to install IPA package
DHCP operation
data link layer
去掉录屏提醒(七牛云demo)
memcached完全剖析–1. memcached的基础
An example illustrates restful API
Static routing job supplement
Concepts of kubernetes components
Realization of truth table assignment by discrete mathematical programming
JMeter implementation specifies concurrent loop testing