C++实现Dijkstra算法的示例代码

 更新时间:2022年07月15日 16:50:06   作者:Hornswoggle  
迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶点的最短路径算法。本文将用C++实现Dijkstra算法,需要的可以参考一下

一、算法原理

链接: Dijkstra算法及其C++实现参考这篇文章

二、具体代码

1.graph类

graph类用于邻接表建立和保存有向图。

graph.h:

#ifndef GRAPH_H
#define GRAPH_H

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>

using namespace std;

// 定义顶点
typedef struct EdgeNode {
	int adjvex;	// 顶点下标
	struct  EdgeNode *next;	// 下一条边的指针
	double cost;	// 当前边的代价

	EdgeNode();
	~EdgeNode();
} EdgeNode;


// 定义顶点表
typedef struct VexList
{
	string Vexs;  //用来存储顶点信息
	EdgeNode *firstedge;  //用来存储当前顶点的下一个顶点

	VexList();
	~VexList();
} VertexList;



// 定义图
typedef class GraphList {
public:
	GraphList();
	~GraphList();

	void PrintGraph();	// 打印图
	void CreateGraph();	// 构建图

	vector<VexList> VexList;
	int Vertexs, Edges;

} GraphList;

typedef GraphList* GraphListPtr;


#endif

graph.cpp

#include <graph.h>

EdgeNode::EdgeNode() {
	cost = 0;
	next = nullptr;
}
EdgeNode::~EdgeNode() {
	//cout << "delete Node" << endl;
}

VexList::VexList() {
	firstedge = nullptr;
}
VexList::~VexList() {
	//cout << "delete VexList" << endl;
}

GraphList::GraphList() {
	VexList.clear();
}

GraphList::~GraphList() {
	//cout << "delete GraphList" << endl;
}

void GraphList::PrintGraph() {
	cout << "所建立的地图如以下所示:" << endl;
	for (int i = 0; i< Vertexs; i++) {
		cout << VexList[i].Vexs;             //先输出顶点信息
		EdgeNode * e = VexList[i].firstedge;
		while (e) {                           //然后就开始遍历输出每个边表所存储的邻接点的下标
			if (e->cost == -1) {
				cout << "---->" << e->adjvex;
			}
			else {
				cout << "-- " << e->cost << " -->" << e->adjvex;
			}
			e = e->next;
		}
		cout << endl;
	}
}

void GraphList::CreateGraph() {
	EdgeNode *e = new EdgeNode();
	cout << "请输入顶点数和边数:" << endl;
	cin >> Vertexs >> Edges;
	cout << "请输入顶点的信息:" << endl;

	for (int i = 0; i <Vertexs; ++i) {
		VertexList tmp;
		cin >> tmp.Vexs;
		tmp.firstedge = NULL;
		VexList.push_back(tmp);
	}

	for (int k = 0; k < Edges; ++k) {
		int i, j;	//(Vi,Vj)
		double cost;
		cout << "请输入边(Vi,Vj)与 cost:" << endl;
		cin >> i >> j >> cost;
		if (VexList[i].firstedge == NULL) {//当前顶点i后面没有顶点
			e = new EdgeNode;
			e->adjvex = j;
			e->cost = cost;
			e->next = NULL;
			VexList[i].firstedge = e;
		}
		else {  //当前i后面有顶点
			EdgeNode *p = VexList[i].firstedge;
			while (p->next) {
				p = p->next;
			}
			e = new EdgeNode;
			e->adjvex = j;
			e->cost = cost;
			e->next = NULL;
			p->next = e;
		}
	}
}

2.PathFinder类

PathFinder类用于搜索最短路径

pathFinder.h

#ifndef PATH_FINDER_H
#define PATH_FINDER_H

#include <iostream>
#include <graph.h>
#include <queue>

enum State{OPEN = 0, CLOSED, UNFIND};
// 定义dijkstra求解器
class DijNode {

public:
	DijNode();
	DijNode(double _val);
	~DijNode() {};
	double getCost() { return m_cost; }
	State getState() { return m_state; }
	void setCost(double _val) { m_cost = _val; }
	void setState(State _state) { m_state = _state; }
	int getIndex() { return m_index; }
	void setIndex(int _idx) { m_index = _idx; }
	void setPred(DijNode* _ptr) { preNode = _ptr; }
	DijNode* getPred() { return preNode; }

	VertexList Vertex;
private:
	int m_index;
	double m_cost;	// 起点到当前点的代价
	State m_state;
	DijNode* preNode;	// 保存父节点
};

typedef DijNode* DijNodePtr;

// 构造优先队列用的
struct cmp {
	bool operator() (DijNodePtr &a, DijNodePtr &b) {
		return a->getCost() > b->getCost();
	}
};

class PathFinder {
public:
	priority_queue<DijNodePtr, vector<DijNodePtr>, cmp > openList;//用优先队列做openList,队首元素为最小值
	vector<DijNodePtr> m_path;	// 存放最终路径
	PathFinder() {
		openList.empty();
		m_path.clear();
	}
	~PathFinder() {};

	void StoreGraph(GraphListPtr _graph);
	void Search(int start, int end);
	void retrievePath(DijNodePtr _ptr);

	vector<DijNodePtr> NodeList;

private:
	GraphListPtr m_graph;
	/*vector<DijNodePtr> NodeList;*/
};

typedef PathFinder* PathFinderPtr;
#endif

PathFinder.cpp

#include <PathFinder.h>

DijNode::DijNode() {
	m_cost = -1;	// -1表示未被探索过,距离为无穷,非负数表示已经被探索过
	m_index = -1;
	m_state = UNFIND;	// OPEN表示openlist, CLOSED表示在closeList中,UNFIND表示未探索过
	preNode = nullptr;
}

DijNode::DijNode(double _val) {
	m_cost = _val;	// -1表示未被探索过,非负数表示已经被探索过
	m_index = -1;
	m_state = UNFIND;	// OPEN表示openlist, CLOSED表示在closeList中,UNFIND表示未探索过
	preNode = nullptr;
}

void PathFinder::StoreGraph(GraphListPtr _graph) {
	for (int i = 0; i < _graph->VexList.size(); ++i) {
		DijNodePtr node = new DijNode();
		node->Vertex = _graph->VexList[i];
		node->setIndex(i);
		NodeList.push_back(node);
	}
}

void PathFinder::Search(int start, int end) {
	// 搜索起点
	DijNodePtr m_start = NodeList[start];
	m_start->setCost(0);
	m_start->setIndex(start);
	m_start->setState(OPEN);
	openList.push(m_start);

	int count = 0;
	while (!openList.empty()) {

		
		// 弹出openList中的队首元素
		DijNodePtr cur = openList.top();
		cur->setState(CLOSED);	// 加入closelist中
		openList.pop();

		// 遍历队首元素所有的边
		EdgeNode *e = cur->Vertex.firstedge;
		while (e != nullptr) {
			int _index = e->adjvex;
			double _cost = e->cost;
			
			//cout << "_cost = " << _cost << endl;
			// 如果节点在close list中,直接跳过
			if (NodeList[_index]->getState() == CLOSED) {
				continue;
			}

			if (NodeList[_index]->getCost() == -1) {
				NodeList[_index]->setCost(cur->getCost() + _cost);	// 更新代价
				NodeList[_index]->setPred(cur);		// 更新父节点
				NodeList[_index]->setState(OPEN);	// 加入open list中
				openList.push(NodeList[_index]);
			}
			else if (cur->getCost() + _cost < NodeList[_index]->getCost()) {
				// 如果从当前节点到第_index个节点的距离更短,更新距离和父节点
				NodeList[_index]->setCost(cur->getCost() + _cost);	// 更新代价
				NodeList[_index]->setPred(cur);		// 更新父节点
				NodeList[_index]->setState(OPEN);	// 加入open list中
			}

			e = e->next;
		}
	}

	cout << "最短距离为:" << NodeList[end]->getCost() << endl;
	retrievePath(NodeList[end]);

}

void PathFinder::retrievePath(DijNodePtr ptr) {
	while (ptr != nullptr) {
		m_path.push_back(ptr);
		ptr = ptr->getPred();
	}
	reverse(m_path.begin(),m_path.end());
}

3. main.cpp

主函数

#include <graph.h>
#include <PathFinder.h>


int main() {
	cout << "构建地图" << endl;
	GraphListPtr graph = new GraphList();
	graph->CreateGraph();

	cout << "\n \n";
	graph->PrintGraph();

	PathFinderPtr _solver = new PathFinder();
	_solver->StoreGraph(graph);

	cout << "\n \n";

	int start, end;
	cout << "输入起点" << endl;
	cin >> start;

	cout << "输入终点" << endl;
	cin >> end;

	cout << "\n \n";

	_solver->Search(start, end);
	cout << "最短路径为:";
	 
	for (int i = 0; i < _solver->m_path.size(); ++i) {
		 cout << _solver->m_path[i]->Vertex.Vexs ;
		 if (i < _solver->m_path.size() - 1)
			 cout << "-->";
	}
	cout << endl;

	system("pause");
	return 0;
}

三、示例

以上就是C++实现Dijkstra算法的示例代码的详细内容,更多关于C++ Dijkstra算法的资料请关注脚本之家其它相关文章!

相关文章

  • 可能是你看过最全的十大排序算法详解(完整版代码)

    可能是你看过最全的十大排序算法详解(完整版代码)

    排序算法是程序中常用的算法,下面这篇文章主要给大家介绍了关于十大排序算法的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • C语言实现页面置换算法(FIFO、LRU)

    C语言实现页面置换算法(FIFO、LRU)

    这篇文章主要介绍了通过C语言实现的两种页面置换算法:先进先出(FIFO)页面置换算法和最近最久未使用(LRU)页面置换算法。文中的代码具有一定的学习或工作价值,快来跟随小编学习一下吧
    2021-12-12
  • C++实现猜数字游戏

    C++实现猜数字游戏

    这篇文章主要为大家详细介绍了C++实现猜数字游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • Visual Studio 2022最新版安装教程(图文详解)

    Visual Studio 2022最新版安装教程(图文详解)

    本文主要介绍了Visual Studio 2022最新版安装教程,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • C++遍历文件夹目录的方法

    C++遍历文件夹目录的方法

    这篇文章主要介绍了C++遍历文件夹目录的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Visual Studio2022报错无法打开源文件 "openssl/conf.h"解决方法

    Visual Studio2022报错无法打开源文件 "openssl/conf.h"解决方法

    这篇文章主要介绍了Visual Studio2022报错无法打开源文件"openssl/conf.h"解决方式,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • C++实现LeetCode(86.划分链表)

    C++实现LeetCode(86.划分链表)

    这篇文章主要介绍了C++实现LeetCode(86.划分链表),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C++ 高精度乘法运算的实现

    C++ 高精度乘法运算的实现

    本文主要介绍了C++ 高精度乘法运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • 详解C++之类和对象(1)

    详解C++之类和对象(1)

    类是创建对象的模板,一个类可以创建多个对象,每个对象都是类类型的一个变量;创建对象的过程也叫类的实例化。每个对象都是类的一个具体实例(Instance),拥有类的成员变量和成员函数
    2021-11-11
  • C++学习小结之语句

    C++学习小结之语句

    本文给大家汇总介绍了下C++中比较基础的知识--语句,常用的语句都有详细介绍和附上了相关示例,十分实用,有需要的小伙伴可以参考下
    2015-07-07

最新评论