Dijkstra演算法C++實現
阿新 • • 發佈:2019-01-06
- 以下程式碼為Willam編寫的Dijkstra最短路徑演算法的C++實現。輸入為有向圖,輸出最短路徑。
- Dijkstra.h檔案的程式碼
/************************************************************/
/* 程式作者:Willam */
/* 程式完成時間:2017/3/8 */
/* 有任何問題請聯絡:[email protected] */
/************************************************************/
//@儘量寫出完美的程式
#pragma once
//#pragma once是一個比較常用的C/C++雜注,
//只要在標頭檔案的最開始加入這條雜注,
//就能夠保證標頭檔案只被編譯一次。
#include<iostream>
#include<string>
using namespace std;
/*
本程式是使用Dijkstra演算法實現求解最短路徑的問題
採用的鄰接矩陣來儲存圖
*/
//記錄起點到每個頂點的最短路徑的資訊
struct Dis {
string path;
int value;
bool visit;
Dis() {
visit = false ;
value = 0;
path = "";
}
};
class Graph_DG {
private:
int vexnum; //圖的頂點個數
int edge; //圖的邊數
int **arc; //鄰接矩陣
Dis * dis; //記錄各個頂點最短路徑的資訊
public:
//建構函式
Graph_DG(int vexnum, int edge);
//解構函式
~Graph_DG();
// 判斷我們每次輸入的的邊的資訊是否合法
//頂點從1開始編號
bool check_edge_value(int start, int end, int weight);
//建立圖
void createGraph();
//列印鄰接矩陣
void print();
//求最短路徑
void Dijkstra(int begin);
//列印最短路徑
void print_path(int);
};
- Dijkstra.cpp檔案的程式碼
#include"Dijkstra.h"
//建構函式
Graph_DG::Graph_DG(int vexnum, int edge) {
//初始化頂點數和邊數
this->vexnum = vexnum;
this->edge = edge;
//為鄰接矩陣開闢空間和賦初值
arc = new int*[this->vexnum];
dis = new Dis[this->vexnum];
for (int i = 0; i < this->vexnum; i++) {
arc[i] = new int[this->vexnum];
for (int k = 0; k < this->vexnum; k++) {
//鄰接矩陣初始化為無窮大
arc[i][k] = INT_MAX;
}
}
}
//解構函式
Graph_DG::~Graph_DG() {
delete[] dis;
for (int i = 0; i < this->vexnum; i++) {
delete this->arc[i];
}
delete arc;
}
// 判斷我們每次輸入的的邊的資訊是否合法
//頂點從1開始編號
bool Graph_DG::check_edge_value(int start, int end, int weight) {
if (start<1 || end<1 || start>vexnum || end>vexnum || weight < 0) {
return false;
}
return true;
}
void Graph_DG::createGraph() {
cout << "請輸入每條邊的起點和終點(頂點編號從1開始)以及其權重" << endl;
int start;
int end;
int weight;
int count = 0;
while (count != this->edge) {
cin >> start >> end >> weight;
//首先判斷邊的資訊是否合法
while (!this->check_edge_value(start, end, weight)) {
cout << "輸入的邊的資訊不合法,請重新輸入" << endl;
cin >> start >> end >> weight;
}
//對鄰接矩陣對應上的點賦值
arc[start - 1][end - 1] = weight;
//無向圖新增上這行程式碼
//arc[end - 1][start - 1] = weight;
++count;
}
}
void Graph_DG::print() {
cout << "圖的鄰接矩陣為:" << endl;
int count_row = 0; //列印行的標籤
int count_col = 0; //列印列的標籤
//開始列印
while (count_row != this->vexnum) {
count_col = 0;
while (count_col != this->vexnum) {
if (arc[count_row][count_col] == INT_MAX)
cout << "∞" << " ";
else
cout << arc[count_row][count_col] << " ";
++count_col;
}
cout << endl;
++count_row;
}
}
void Graph_DG::Dijkstra(int begin){
//首先初始化我們的dis陣列
int i;
for (i = 0; i < this->vexnum; i++) {
//設定當前的路徑
dis[i].path = "v" + to_string(begin) + "-->v" + to_string(i + 1);
dis[i].value = arc[begin - 1][i];
}
//設定起點的到起點的路徑為0
dis[begin - 1].value = 0;
dis[begin - 1].visit = true;
int count = 1;
//計算剩餘的頂點的最短路徑(剩餘this->vexnum-1個頂點)
while (count != this->vexnum) {
//temp用於儲存當前dis陣列中最小的那個下標
//min記錄的當前的最小值
int temp=0;
int min = INT_MAX;
for (i = 0; i < this->vexnum; i++) {
if (!dis[i].visit && dis[i].value<min) {
min = dis[i].value;
temp = i;
}
}
//cout << temp + 1 << " "<<min << endl;
//把temp對應的頂點加入到已經找到的最短路徑的集合中
dis[temp].visit = true;
++count;
for (i = 0; i < this->vexnum; i++) {
//注意這裡的條件arc[temp][i]!=INT_MAX必須加,不然會出現溢位,從而造成程式異常
if (!dis[i].visit && arc[temp][i]!=INT_MAX && (dis[temp].value + arc[temp][i]) < dis[i].value) {
//如果新得到的邊可以影響其他為訪問的頂點,那就就更新它的最短路徑和長度
dis[i].value = dis[temp].value + arc[temp][i];
dis[i].path = dis[temp].path + "-->v" + to_string(i + 1);
}
}
}
}
void Graph_DG::print_path(int begin) {
string str;
str = "v" + to_string(begin);
cout << "以"<<str<<"為起點的圖的最短路徑為:" << endl;
for (int i = 0; i != this->vexnum; i++) {
if(dis[i].value!=INT_MAX)
cout << dis[i].path << "=" << dis[i].value << endl;
else {
cout << dis[i].path << "是無最短路徑的" << endl;
}
}
}
- main.cpp檔案的程式碼
#include"Dijkstra.h"
//檢驗輸入邊數和頂點數的值是否有效,可以自己推算為啥:
//頂點數和邊數的關係是:((Vexnum*(Vexnum - 1)) / 2) < edge
bool check(int Vexnum, int edge) {
if (Vexnum <= 0 || edge <= 0 || ((Vexnum*(Vexnum - 1)) / 2) < edge)
return false;
return true;
}
int main() {
int vexnum; int edge;
cout << "輸入圖的頂點個數和邊的條數:" << endl;
cin >> vexnum >> edge;
while (!check(vexnum, edge)) {
cout << "輸入的數值不合法,請重新輸入" << endl;
cin >> vexnum >> edge;
}
Graph_DG graph(vexnum, edge);
graph.createGraph();
graph.print();
graph.Dijkstra(1);
graph.print_path(1);
system("pause");
return 0;
}
輸入:
6 8
1 3 10
1 5 30
1 6 100
2 3 5
3 4 50
4 6 10
5 6 60
5 4 20
輸出: