单源最短路径——Dijkstra

本文介绍了Dijkstra算法在图论中的应用,用于寻找给定源点到所有其他顶点的最短路径。提供的C++代码展示了如何使用vis数组标记已访问顶点,通过d[i]存储源点到顶点i的距离,并利用w[u][v]表示边的权值。在求得最短路径的同时,代码还记录了路径上的父节点以便回溯整个路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简述

单源最短路径是图论的一种典型应用,给定n个顶点(包括一个源点)以及一些顶点间的权值,求各个顶点到源点之间的最小的权值和,即最短路。

c++代码

用vis标记访问过的顶点,d[u]表示顶点u到源点之间的距离,w[u][v]表示连接顶点 u 和 v 的边的权值。

void Dijkstra() {
	memset(vis, 0, sizeof(vis));
	d[0] = 0;
	for(int i = 1; i < n; i++) {
		d[i] = INF;
	}
	int t = n;
	while(t--) {
		int x, m = INF;
		for(int i = 0; i < n; i++) {
			if(!vis[i] && d[i] < m) {
				x = i;	m = d[i];
			}
		}
		vis[x] = 1;
		for(int i = 0; i < n; i++) {
			d[i] = min(d[i], d[x] + w[x][i]);
		}
	}
}

当需要输出整条路径时,只需要在最后更新d[i]的时候顺带记录父节点。代码如下:

void Dijkstra() {
	memset(vis, 0, sizeof(vis));
	d[0] = 0;
	for(int i = 1; i < n; i++) {
		d[i] = INF;
	}
	int t = n;
	while(t--) {
		int x, m = INF;
		for(int i = 0; i < n; i++) {
			if(!vis[i] && d[i] < m) {
				x = i;	m = d[i];
			}
		}
		vis[x] = 1;
		for (int i = 0; i < n; i++) {
			if (d[i] > d[x] + w[x][i]) {
				d[i] = d[x] + w[x][i];
				fa[i] = x;
			}
		}
	}
}
离字典,将起始节点的距离设为0,其他节点的距离设为无穷大 distances = {node: sys.maxsize for node in graph} distances[start] = 0 # 初始化已访问节点的集合和未访以下是使用问节点D的集ijkstra合 visited = set() unvisited算法求解最短路径的Python = set(graph) while unvisited: # 代码示例: ```python class D选择当前ijkstra距: def __init__(self, graph离最小的节点 , start, current goal): self.graph = graph # 邻接表_node = min(unvisited, key=lambda self node: distances[node]) # 更新.start = start当前节点的 # 起邻居节点点 self.goal =的距离 goal # 终点 for neighbor in graph self.open[current_node]: _list = {} if neighbor in # open 表 self.closed_list unvisited: new_distance = distances[current_node] + = {} graph[current_node][neighbor # closed 表 self.open_list[start] if new_distance] = < distances[neighbor]: 0.0 # 将 distances[neighbor] = new_distance # 将当前起点放入 open_list 中 self.parent = {节点标记start:为已访 None} 问,并从未访问集合中移除 visited.add # 存储节点的父子关系。键为(current_node) 子节点, unvisited值为父.remove(current_node) return节点。方便做最 distances def print后_path(dist路径的ances,回 start溯 self.min, end): _dis = None # 根 # 最短路径的长度 def shortest_path据距离字典和终点节点(self): while True: ,逆向 if self打印路径.open_list is path = [end None: ] print('搜索 current_node =失败 end while current_node !=, 结束!') break distance start: , min_node = for neighbor in graph min(zip[current_node]: if(self.open_list distances[current.values(), self_node] ==.open_list.keys distances[neighbor())) #] + graph 取出距[neighbor][current_node]: 离最小的节点 self path.open_list.pop.append(min_node)(neighbor) current_node = neighbor break path.reverse() # 将其从 open_list 中去除 self print.closed("_list[minShortest_node] = path from", distance # 将节点加入 closed start, "to", end,_list ":", "->".join(path)) # 示例 中 if min_node == self.goal: # 如果节点为图的邻接矩阵终点 self.min_dis = distance 表示 graph shortest = { _path = [ 'Aself.goal]': {'B': # 5, 'C 记录从': 终1}, 点回溯的路径 'B
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值