Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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 }}
Uh oh!
There was an error while loading.
Please reload this page
.
microwind
/
algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
27
Star
174
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
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.