基本概念
步骤分析
图的输入 | | n1 | n2 | n3 | n4 | n5 | n6 | | —- | —- | —- | —- | —- | —- | —- | | n1 | 0 | 2 | 1 | 5 | inf | inf | | n2 | 2 | 0 | 2 | 3 | inf | inf | | n3 | 1 | 2 | 0 | 3 | 1 | inf | | n4 | 5 | 3 | 3 | 0 | 1 | 5 | | n5 | inf | inf | 1 | 1 | 0 | 2 | | n6 | inf | inf | inf | 5 | 2 | 0 |
迭代过程
- 定义如下记号
| N’ | D(2),P(2) | D(3),P(3) | D(4),P(4) | D(5),P(5) | D(6),P(6) |
| —- | —- | —- | —- | —- | —- |
| 1 | [D(2) Vs D(1)+C(1,2)]
[2 = 0 + 2]
D(2),P(2) = 2,1 | [D(3) Vs D(1)+C(1,3)]
[1 = 0 + 1]
D(3),P(3) = 1,1(MIN) | [D(4) Vs D(1)+C(1,4)]
[5 = 0 + 5]
D(4),P(4) = 5,1 | [D(5) Vs D(1)+C(1,5)]
[inf = 0 + inf]
D(5),P(5) = inf | [D(6) Vs D(1)+C(1,6)]
[inf = 0 + inf]
D(6),P(6) = inf |
| 1,3 | [D(2) Vs D(3)+C(3,2)]
[2 < 1 + 2]
D(2),P(2) = 保留2,1 | | [D(4) Vs D(3)+C(3,4)]
[5 > 1 + 3]
D(4),P(4) = 更新4,3 | [D(5) Vs D(3)+C(3,5)]
[inf < 1 + 1]
D(5),P(5) = 更新2,3(MIN) | [D(6) Vs D(3)+C(3,6)]
[inf = inf]
D(6),P(6) = inf |
| 1,3,5 | [D(2) Vs D(5)+C(5,2)]
[2 < 2 + inf]
D(2),P(2) = 保留2,1(MIN) | | [D(4) Vs D(5)+C(5,4)]
[4 > 2 + 1]
D(4),P(4) = 更新3,5 | | [D(6) Vs D(5)+C(5,6)]
[inf > 2 + 2]
D(6),P(6) = 更新4,5 |
| 1,3,5,2 | | | [D(4) Vs D(2)+C(2,4)]
[3 < 2 + 3]
D(4),P(4) = 保留3,5(MIN) | | [D(6) Vs D(2)+C(2,6)]
[4 < 2 + inf]
D(6),P(6) = 保留4,5 |
| 1,3,5,2,4 | | | | | [D(6) Vs D(4)+C(4,6)]
[4 < 3 + 5]
D(6),P(6) = 保留4,5(MIN) |
| 1,3,5,2,4,6 | | | | | |
C++实现
/*
* Copyright (c) 2020 - ~, Wang Xi
*
* DIJKSTRA ALGORITHM
* Used in C++
*
* Change Logs:
* Date Author Notes Mail
* 2021-04-17 WangXi first version WangXi_chn@foxmail.com
*/
#include <iostream>
#include <string>
using namespace std;
// Record the calculate result and be used in iteration
struct Result
{
string path;
int value;
bool visit;
};
class Graph
{
public:
int _inf_ = 10000;
int vexnum = 6;
int soucenode = 1;
int arc[6][6] = {
{0, 2, 1, 5, _inf_, _inf_},
{2, 0, 2, 3, _inf_, _inf_},
{1, 2, 0, 3, 1, _inf_},
{5, 3, 3, 0, 1, 5},
{_inf_, _inf_, 1, 1, 0, 2},
{_inf_, _inf_, _inf_, 5, 2, 0}
};;
Result result[6];
Graph(void);
void GraphInit();
string GraphPrint();
string PathPrint();
void Dijkstra();
};
Graph::Graph(void)
{
this->GraphInit();
cout << this->GraphPrint() << endl;
this->Dijkstra();
cout << this->PathPrint() << endl;
}
void Graph::GraphInit() {
for (int i = 0; i < this->vexnum; i++)
{
this->result[i].path = "";
this->result[i].value = 0;
this->result[i].visit = false;
}
}
string Graph::GraphPrint()
{
string result = "图的邻接矩阵为:\r\n";
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] == this->_inf_)
result = result + "∞" + "\t";
else
result = result + std::to_string(arc[count_row][count_col]) + "\t";
++count_col;
}
++count_row;
result += "\r\n";
}
result += "\r\n";
return result;
}
string Graph::PathPrint()
{
string str;
str = "以D" + std::to_string(this->soucenode) + "为起点的最短路径为:\r\n";
for (int i = 0; i != this->vexnum; i++)
{
if (result[i].value != _inf_)
str += result[i].path + "=" + std::to_string(result[i].value) + "\r\n";
else
{
str += result[i].path + "是无最短路径的" + "\r\n";
}
}
return str;
}
void Graph::Dijkstra()
{
/* Config result array */
int i;
for (i = 0; i < this->vexnum; i++)
{
/* Set up default path */
this->result[i].path = "D" + std::to_string(this->soucenode) + "-->D" + std::to_string(i + 1);
result[i].value = arc[this->soucenode - 1][i];
}
/* Set up start to start node value */
result[this->soucenode - 1].value = 0;
result[this->soucenode - 1].visit = true;
int count = 1;
/* Calculate the rest node */
while (count != this->vexnum)
{
//temp: the index of node has min length
//min: the min path's length value
int temp = 0;
int min = _inf_;
for (i = 0; i < this->vexnum; i++)
{
if (!result[i].visit && result[i].value < min)
{
min = result[i].value;
temp = i;
}
}
/* Mark the node had been choosen */
result[temp].visit = true;
++count;
for (i = 0; i < this->vexnum; i++)
{
if (!result[i].visit && arc[temp][i] != _inf_ && (result[temp].value + arc[temp][i]) < result[i].value)
{
/* Find the better path, updata the result array */
result[i].value = result[temp].value + arc[temp][i];
result[i].path = result[temp].path + "-->D" + to_string(i + 1);
}
}
}
}
int main()
{
Graph graph;
return 0;
}
/************************ (C) COPYRIGHT 2021 WANGXI **************END OF FILE****/