Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
microwind
algorithms
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithms
/
data-structures
/
graph
/
graph_path.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
57 lines (47 loc) · 1.88 KB
main
Breadcrumbs
algorithms
/
data-structures
/
graph
/
graph_path.py
Copy path
Top
File metadata and controls
Code
Blame
57 lines (47 loc) · 1.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# -*- encoding: UTF-8-*-
"""
下面是一个使用Python和networkx库的示例代码,在一个城市地图上找到最短路径。
应用场景:城市导航 - 寻找最短路径
示例城市地图
城市中的地点:A、B、C、D、E、F
每条边代表两地点之间的道路,边的权重表示道路的距离(单位:公里)
"""
import networkx as nx
# 创建一个有向加权图
city_map = nx.DiGraph()
# 添加边(地点之间的道路及其距离)
city_map.add_weighted_edges_from([
('A', 'B', 5),
('A', 'C', 2),
('B', 'C', 1),
('B', 'D', 3),
('C', 'D', 8),
('C', 'E', 10),
('D', 'E', 4),
('D', 'F', 6),
('E', 'F', 2)
])
# 使用Dijkstra算法计算从A到F的最短路径
shortest_path = nx.dijkstra_path(city_map, 'A', 'F')
shortest_distance = nx.dijkstra_path_length(city_map, 'A', 'F')
# 安装依赖库networkx
# $ pip install --upgrade pip --user
# $ pip install networkx
# 输出最短路径及其总距离
print(f"从A到F的最短路径是: {shortest_path}")
print(f"总距离是: {shortest_distance} 公里")
"""
打印结果:
jarry@jarrys-MacBook-Pro graph % python graph_path.py
从A到F的最短路径是: ['A', 'B', 'D', 'F']
总距离是: 14 公里
"""
"""
代码解析:
创建图:我们使用networkx.DiGraph()创建一个有向图,因为道路的方向可能影响路径选择。
添加边和权重:add_weighted_edges_from()函数将每个地点对(如('A', 'B', 5))及其对应的距离(权重)添加到图中。
Dijkstra算法:nx.dijkstra_path()计算从A到F的最短路径,nx.dijkstra_path_length()则计算该路径的总距离。
实际应用场景:
这里展示了在城市导航中如何使用图来表示道路网络,并通过Dijkstra算法计算出两地点之间的最短路径。这在GPS导航系统、地图应用或物流配送优化中非常有用。
"""
You can’t perform that action at this time.