当前位置:网站首页>Finding all paths between two points in a directed graph
Finding all paths between two points in a directed graph
2022-07-23 22:53:00 【Sign today】
Take this picture as an example :
Solve all paths between two points in a directed graph
It mainly adopts the idea of depth traversal :
# Enter the number of vertices
vertex_num = int(input())
# Enter the number of edges
edge_num = int(input())
# Enter the starting point
start = int(input())
# Enter the destination
end = int(input())
# Initialize adjacency matrix
graph = [[0 for _ in range(vertex_num)] for _ in range(vertex_num)]
# initialization visit Array -- Used in dfs Record the accessed vertex information in
visit = [0 for _ in range(vertex_num)]
# Store every possible path
path = []
# Store final path collection
result_path = []
# Create diagrams
for i in range(edge_num):
# Input path (0 1 Express : 0 You can go to 1)
a_list = input().split(" ")
graph[int(a_list[0])][int(a_list[1])] = 1
def dfs(start):
# Express start Point has been visited
visit[start] = 1
# The visited points are put into path list
path.append(start)
# If the current visited point is the destination , Join in result_path
if start == end:
result_path.append(path[:])
else: # Recursive traversal
i = 0
while i < vertex_num:
if visit[i] == 0 and i != start and graph[start][i] != 0:
dfs(i)
i = i + 1
# Clear one by one after recursion path In the point , Prepare for the next path
path.remove(path[-1])
# Reset the point to 0, I.e. no access status
visit[start] = 0
dfs(start)
print(result_path)
Input :
5
6
0
2
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Output :
[[0, 1, 2], [0, 2]]
边栏推荐
- Drools (1): introduction to drools
- 视频号加强打击低俗内容:对违背公序良俗的内容必须赶尽杀绝
- What else do entrepreneurs need besides money? Exclusive interview with Mingyue Lake venture capital institutions
- El select drop-down box multi selection remote search anti display
- [golang learning notes] concurrency basis
- ES6 use of arrow function
- [golang learning notes] is parameter transfer in go language value transfer or reference transfer
- openEuler 资源利用率提升之道 01:概论
- 达梦数据库tools包中的工具(操作达梦数据库)
- 【Matplotlib绘图】
猜你喜欢

Build your own target detection environment, model configuration, data configuration mmdetection

Matlab wavelet toolbox import signal error (doesn't contain one dimensional single)

作为开发,你不得不知道的三个性能测试工具|Jmeter、Apipost、JMH使用指南

Multithreading problem: why should we not use multithreading to read and write the same socket connection?

Diabetes genetic risk testing challenge baseline

1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮

除了钱,创业者还需要什么?专访明月湖创赛创投机构
![[golang learning notes] is parameter transfer in go language value transfer or reference transfer](/img/79/57b28d0e6501132c347f7e7c0516b3.png)
[golang learning notes] is parameter transfer in go language value transfer or reference transfer

Rails搭配OSS最佳实践

1000 okaleido tiger launched binance NFT, triggering a rush to buy
随机推荐
TAP 系列文章4 | 基于 Backstage 的 TAP 开发者门户
ES6箭头函数的使用
dried food! Implicit sparse regularization effect in neural networks
Array -- 27. remove elements
Tap series article 7 | easy to manage pipeline configuration
TAP 系列文章9 | 应用开发加速器
Video Number strengthens the fight against vulgar content: the content that violates public order and good customs must be eliminated
openEuler 资源利用率提升之道 01:概论
Tap series article 6 | application model of tap
D2admin framework is basically used
Mongodb - Introduction to the usage of logical operators not, and, or, nor in query statements
vip股票账户在手机开通安全吗?
13. Roman to integer
[Matplotlib drawing]
(CVPR-2022)BiCnet
Zhongbang technology devotes itself to another work -- gyro maker OA system
Diabetes genetic risk testing challenge baseline
ES6 use of arrow function
Relevant interfaces of [asp.net core] option mode
记忆化搜索 - DP