完成实验4报告
This commit is contained in:
parent
938778bfa9
commit
7261be4d46
BIN
Assignments/第4章_21281280_柯劲帆_物联网.pdf
Normal file
BIN
Assignments/第4章_21281280_柯劲帆_物联网.pdf
Normal file
Binary file not shown.
BIN
Assignments/第6章_21281280_柯劲帆_物联网.pdf
Normal file
BIN
Assignments/第6章_21281280_柯劲帆_物联网.pdf
Normal file
Binary file not shown.
110
Labs/Lab4/source/实验4_21281280_柯劲帆_物联网.md
Normal file
110
Labs/Lab4/source/实验4_21281280_柯劲帆_物联网.md
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
<h1><center>实验报告</center></h1>
|
||||||
|
|
||||||
|
<div style="text-align: center;">
|
||||||
|
<div><span style="display: inline-block; width: 65px; text-align: center;">课程名称</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 320px; font-weight: bold; text-align: left;">计算机网络原理</span></div>
|
||||||
|
<div><span style="display: inline-block; width: 65px; text-align: center;">实验题目</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 320px; font-weight: bold; text-align: left;">编程实现路由算法</span></div>
|
||||||
|
<div><span style="display: inline-block; width: 65px; text-align: center;">学号</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 320px; font-weight: bold; text-align: left;">21281280</span></div>
|
||||||
|
<div><span style="display: inline-block; width: 65px; text-align: center;">姓名</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 320px; font-weight: bold; text-align: left;">柯劲帆</span></div>
|
||||||
|
<div><span style="display: inline-block; width: 65px; text-align: center;">班级</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 320px; font-weight: bold; text-align: left;">物联网2101班</span></div>
|
||||||
|
<div><span style="display: inline-block; width: 65px; text-align: center;">指导老师</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 320px; font-weight: bold; text-align: left;">常晓琳</span></div>
|
||||||
|
<div><span style="display: inline-block; width: 65px; text-align: center;">报告日期</span><span style="display: inline-block; width: 25px;">:</span><span style="display: inline-block; width: 320px; font-weight: bold; text-align: left;">2024年6月9日</span></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# 目录
|
||||||
|
|
||||||
|
[TOC]
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 1. 实验目的
|
||||||
|
|
||||||
|
运用各种编程语言实现基于 Dijkstra 算法的路由软件。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 2. 实验环境
|
||||||
|
|
||||||
|
- **OS**:WSL2 (内核5.15.146.1-microsoft-standard-WSL2)
|
||||||
|
|
||||||
|
- **Python**:version 3.11.4
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 3. 实验过程
|
||||||
|
|
||||||
|
## 3.1. 编写代码
|
||||||
|
|
||||||
|
```python
|
||||||
|
INF = float('inf')
|
||||||
|
|
||||||
|
def Dijkstra(G, origin):
|
||||||
|
node_num = len(G)
|
||||||
|
vis = [False] * node_num
|
||||||
|
dis = [G[origin][node] for node in range(node_num)]
|
||||||
|
paths = [[]] * node_num
|
||||||
|
paths[origin].append(origin)
|
||||||
|
vis[origin] = True
|
||||||
|
last_point = origin
|
||||||
|
for i in range(node_num - 1):
|
||||||
|
min_dis = INF
|
||||||
|
for j in range(node_num):
|
||||||
|
if not vis[j] and dis[j] < min_dis:
|
||||||
|
min_dis = dis[j]
|
||||||
|
last_point = j
|
||||||
|
vis[last_point] = True
|
||||||
|
if i == 0:
|
||||||
|
paths[last_point] = paths[origin] + [last_point]
|
||||||
|
for k in range(node_num):
|
||||||
|
if G[last_point][k] < INF and dis[k] > dis[last_point] + G[last_point][k]:
|
||||||
|
dis[k] = dis[last_point] + G[last_point][k]
|
||||||
|
paths[k] = paths[last_point] + [k]
|
||||||
|
|
||||||
|
return dis, paths
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
G = [[0, 1, 12, INF, INF, INF],
|
||||||
|
[INF, 0, 9, 3, INF, INF],
|
||||||
|
[INF, INF, 0, INF, 5, INF],
|
||||||
|
[INF, INF, 4, 0, 13, 15],
|
||||||
|
[INF, INF, INF, INF, 0, 4],
|
||||||
|
[INF, INF, INF, INF, INF, 0]]
|
||||||
|
origin = 0
|
||||||
|
dis, paths = Dijkstra(G, origin)
|
||||||
|
for i in range(len(G)):
|
||||||
|
print(f"{origin} to {i}", "path:", paths[i], f"distance: {dis[i]}")
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3.2. 运行代码
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ python main.py
|
||||||
|
0 to 0 path: [0] distance: 0
|
||||||
|
0 to 1 path: [0, 1] distance: 1
|
||||||
|
0 to 2 path: [0, 1, 3, 2] distance: 8
|
||||||
|
0 to 3 path: [0, 1, 3] distance: 4
|
||||||
|
0 to 4 path: [0, 1, 3, 2, 4] distance: 13
|
||||||
|
0 to 5 path: [0, 1, 3, 2, 4, 5] distance: 17
|
||||||
|
```
|
||||||
|
|
||||||
|
代码能够正确求出最短路径和路径长度。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 4. 总结和感想
|
||||||
|
|
||||||
|
在本次实验中,我通过编程实现了 Dijkstra 算法,用于计算加权图中各节点之间的最短路径。在整个实验过程中,我深入理解了 Dijkstra 算法的核心思想及其在路由算法中的应用。
|
||||||
|
|
||||||
|
通过实现 Dijkstra 算法,我对其逐步选取未访问节点中距离最短的节点,并更新周围节点的最短路径的过程有了更深的理解。Dijkstra 算法在处理图中非负权重路径问题时非常高效,能够在多种网络环境下应用。
|
||||||
|
|
||||||
|
在实现过程中,我使用 Python 编写了 Dijkstra 算法,并成功运行程序验证了结果的正确性。代码清晰地展示了算法从初始节点出发,通过不断更新距离数组和路径数组来寻找最短路径的过程。
|
||||||
|
|
||||||
|
通过本次实验,我深刻体会到 Dijkstra 算法在计算机网络中的重要性。路由算法是网络通信的核心之一,能够高效找到最短路径对于提升网络性能至关重要。
|
||||||
|
|
||||||
|
在实验过程中,我意识到代码的优化和效率提升的重要性。尽管 Dijkstra 算法在小规模网络中表现良好,但在大规模网络中,如何进一步优化算法和提高效率是一个值得深入研究的课题。
|
||||||
|
|
||||||
|
本次实验不仅增强了我对算法的理解,也提升了我的编程能力。在未来的学习和工作中,我会继续通过实践和动手编程来巩固理论知识,提升实际解决问题的能力。
|
BIN
Labs/Lab4/实验4_21281280_柯劲帆_物联网.pdf
Normal file
BIN
Labs/Lab4/实验4_21281280_柯劲帆_物联网.pdf
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user