Yen 的k_shortest paths 算法的C++实现

博客详细介绍了Yen的k_shortest_paths算法,并提供了C++实现。作者分享了对算法的理解,虽然可能不完全准确,但整体数据结构和逻辑正确。文章中指出,算法有待改进的地方是未使用堆优化,作者计划后续改进。
摘要由CSDN通过智能技术生成

具体介绍见:https://en.wikipedia.org/wiki/Yen%27s_algorithm

还有具体步骤见:https://blog.csdn.net/sharpdew/article/details/446510?tdsourcetag=s_pctim_aiomsg

我也不知道我对这个算法理解是否完全正确,但是大体的数据结构和逻辑是正确的,希望大家指点,后期我更理解了,会做相应修改。

在这个算法里需要改进的地方是:我没有用堆去插入,这个等有时间再做 

#include<vector>
#include<string>
#include<math.h>
#include<algorithm>
#include <iostream>
#include<stack>
using namespace std;
static const unsigned int INF(std::numeric_limits<int>::max());
static const unsigned undefined = INF;

class K_Shortest_Path {
public:
	vector<vector<unsigned int>> run(
		const unsigned int kPath,					// K Path
		const vector<vector<unsigned int>>& NW,		// network
		const unsigned int src,						// source node
		const unsigned int dst);					// destination node
};


//
//结构体用于保存两点之间的最短路径和长度
//
class DijPath
{
public:
	vector<unsigned int> onePath;
	int cost;

	bool operator <(const DijPath &n2);

	//判断两条路径是否相等
	bool operator ==(const DijPath &n2);
};


bool DijPath::operator <(const DijPath &n2)
{
	return cost < n2.cost;
}
//判断两条路径是否相等
bool DijPath::operator ==(const DijPath &n2)
{
	if (onePath.size() == n2.onePath.size())
	{
		for (unsigned int i = 0; i < onePath.size(); i++)
		{
			if (onePath[i] != n2.onePath[i])
				return false;
		}

		return true;
	}

	return false;
}


//
//最短路径算法,返回一个DijPath结构体
//
DijPath dijkstra(
	const vector<vector<unsigned int>> &NW,
	const int src,
	const int dst
)
{
	//图中节点个数
	unsigned int sizeNW = NW.size(); 

	//知道每一个节点都被访问过结束
	vector<bool> visited(sizeNW); 

	//到达dst顶点的最短路径的前一个顶点
	vector<unsigned int> prev(sizeNW);    

	//下一个距离当前访问过的最小的一个点
	int minPos = 0; 

	//用于记录每个顶点到源节点的距离,如果最终len[dst]=INF,
	//说明src和dst不可到达,讲cost设置为INF,用于ksp做判断舍弃这条路径
	vector<unsigned int> len(sizeNW);

	for (unsigned int i = 0; i < NW.size(); i++) //初始化
	{
		visited[i] = false;  //一开始均被访问
		len[i] = NW[src][i];
		prev[i] = INF;
	}

	//初始节点被设置为访问过
	visited[src] = true;

	for (unsigned int i = 0; i < sizeNW; ++i)  
	{
		unsigned int min = INF;      //记录访问过的节点到没访问过的节点的最小路径长度

		for (unsigned int j = 0; j < sizeNW; ++j)
		{
			if (!visited[j] && min > len[j])
			{
				minPos = j;   //记录找到了下一个节点
				min = len[j];
			}
		}

		visited[minPos] = true;

		for (unsigned int j = 0; j < sizeNW; ++j)
		{
			//如果j节点没有被访问过,且通过j节点发现到其他节点更短的len[j]值
			if (!visited[j] && len[j] > (len[minPos] + NW[minPos][j]))
			{
				prev[j] = minPos;
				len[j] = len[minPos] + NW[minPos][j];
			}
		}
	}

	
	unsigned int beforeVertex = dst;
	//通过一个栈将prev[]中的节点给倒过去,实现正序排列
	stack<unsigned int> st;
	while (prev[beforeVertex] != IN
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值