迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学家狄克斯特拉于1959年提出的,因此又叫狄克斯特拉算法。
是从一个顶点到其余各顶点的最短路径算法,解决的是有权图中最短路径问题。
迪杰斯特拉算法主要特点是从起始点开始,采用贪心算法的策略,每次遍历到始点距离最近且未访问过的顶点的邻接节点,直到扩展到终点为止。
迪杰斯特拉算法采用贪心算法的策略,将所有顶点分为已标记点和未标记点两个集合,从起始点开始,不断在未标记点中寻找距离起始点路径最短的顶点,并将其标记,直到所有顶点都被标记为止。需要注意的一点是该方法不能处理带有负权边的图。
问题描述:
在有向图 G=(V,E) 中,假设每条边 E[i] 的长度为 w[i],找到由顶点 V0 到其余各点的最短值。
算法思想:
按路径长度递增次序产生算法:
把顶点集合V分成两组:
(1)S:已求出的顶点的集合(初始时只含有源点V0)
(2)V-S=T:尚未确定的顶点集合
将T中顶点按递增的次序加入到S中,保证:
(1)从源点V0到S中其他各顶点的长度都不大于从V0到T中任何顶点的最短路径长度
(2)每个顶点对应一个距离值
S中顶点:从V0到此顶点的长度
T中顶点:从V0到此顶点的只包括S中顶点作中间顶点的最短路径长度
依据:可以证明V0到T中顶点Vk的,或是从V0到Vk的直接路径的权值;或是从V0经S中顶点到Vk的路径权值之和
(反证法可证)
求最短路径步骤
算法步骤如下:
G={V,E}
- 初始时令 S={V0},T=V-S={其余顶点},T中顶点对应的距离值
若存在,d(V0,Vi)为弧上的权值
若不存在,d(V0,Vi)为∞ - 从T中选取一个与S中顶点有关联边且权值最小的顶点W,加入到S中。
- 对其余T中顶点的距离值进行修改:若加进W作中间顶点,从V0到Vi的距离值缩短,则修改此距离值。
重复上述步骤2、3,直到S [1] 中包含所有顶点,即W=Vi为止。
大学经典教材<<数据结构>>(C语言版 严蔚敏 吴为民 编著) 中该算法的实现
/*
测试数据 教科书 P189 G6 的邻接矩阵 其中 数字 1000000 代表无穷大
6
1000000 1000000 10 100000 30 100
1000000 1000000 5 1000000 1000000 1000000
1000000 1000000 1000000 50 1000000 1000000
1000000 1000000 1000000 1000000 1000000 10
1000000 1000000 1000000 20 1000000 60
1000000 1000000 1000000 1000000 1000000 1000000
结果:
D[0] D[1] D[2] D[3] D[4] D[5]
0 1000000 10 50 30 60
*/
#include <iostream>
#include <cstdio>
#define MAX 1000000
using namespace std;
int arcs[10][10];//邻接矩阵
int D[10];//保存最短路径长度
int p[10][10];//路径
int final[10];//若final[i] = 1则说明 顶点vi已在集合S中
int n = 0;//顶点个数
int v0 = 0;//源点
int v,w;
void ShortestPath_DIJ()
{
for (v = 0; v < n; v++) //循环 初始化
{
final[v] = 0; D[v] = arcs[v0][v];
for (w = 0; w < n; w++) p[v][w] = 0;//设空路径
if (D[v] < MAX) {p[v][v0] = 1; p[v][v] = 1;}
}
D[v0] = 0; final[v0]=1; //初始化 v0顶点属于集合S
//开始主循环 每次求得v0到某个顶点v的最短路径 并加v到集合S中
for (int i = 1; i < n; i++)
{
int min = MAX;
for (w = 0; w < n; w++)
{
//我认为的核心过程--选点
if (!final[w]) //如果w顶点在V-S中
{
//这个过程最终选出的点 应该是选出当前V-S中与S有关联边
//且权值最小的顶点 书上描述为 当前离V0最近的点
if (D[w] < min) {v = w; min = D[w];}
}
}
final[v] = 1; //选出该点后加入到合集S中
for (w = 0; w < n; w++)//更新当前最短路径和距离
{
/*在此循环中 v为当前刚选入集合S中的点
则以点V为中间点 考察 d0v+dvw 是否小于 D[w] 如果小于 则更新
比如加进点 3 则若要考察 D[5] 是否要更新 就 判断 d(v0-v3) + d(v3-v5) 的和是否小于D[5]
*/
if (!final[w] && (min+arcs[v][w]<D[w]))
{
D[w] = min + arcs[v][w];
// p[w] = p[v];
p[w][w] = 1; //p[w] = p[v] + [w]
}
}
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arcs[i][j];
}
}
ShortestPath_DIJ();
for (int i = 0; i < n; i++) printf("D[%d] = %d\n",i,D[i]);
return 0;
}
ccf 行车路线题目
解题思路:
使用邻接表存储边,基于迪杰斯特拉算法计算最短的疲劳值。使用一个长度为n的数组记录,某结点之前走的小道的长度。
在Dijstra中比较更新最短路时,如果迭代到小路时,比较路的长度为((选取结点的最短路)-选取结点之前走的小路的平方+(选取结点之前走的小路长度+本结点选择的小路长度)的平方),如果比较路小于本结点当前最短路,则替换当前最短路,本结点之前走的小路长度记为(选取结点之前走的小路+本次走的小路长度),如果大于等于,不修改,继续迭代;如果迭代到大路,比较路的长度为(选取结点的最短路+本结点选择的大路),如果比较路小于本结点当前最短路,则替换当前最短路,且本结点之前走的最短路记为0,如果大于等于,不修改,继续迭代。直至n结点确定最短路,程序结束,输入n结点的最短路数值。
package exercise;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Route {
public static int n, m;
public static boolean[] mFinalVex; // 判断当前节点是否使用
public static double[] mShortPath; // 存储到达当前路径的最短距离
public static List<LinkedList<EDGE>> list; // 类似于邻接矩阵 存储图
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("路口的数量:");
n = input.nextInt();
System.out.print("道路的数量:");
m = input.nextInt();
list = new ArrayList<LinkedList<EDGE>>(n);
for (int i = 0; i < n; i++) {
list.add(new LinkedList<EDGE>());
}
int type;
int start;
int end;
double length;
for (int i = 0; i < m; i++) {
type = input.nextInt();
start = input.nextInt();
end = input.nextInt();
length = input.nextInt();
list.get(start - 1).add(new EDGE(type, start - 1, end - 1, length));
list.get(end - 1).add(new EDGE(type, end - 1, start - 1, length));
}
mDijkstra();
System.out.println("最优路线下小明的疲劳度:"+(long) mShortPath[n - 1]);
input.close();
}
private static void mDijkstra() {
double[] trail = new double[n]; // 存储到达当前节点连续走的小路长度
mFinalVex = new boolean[n];
mShortPath = new double[n];
int index = -1; // 表示当前选择并处理某个节点
EDGE tmp;
Arrays.fill(mFinalVex, false);
Arrays.fill(mShortPath, Integer.MAX_VALUE); // 初始化都是无穷大
Arrays.fill(trail, 0);
mShortPath[0] = 0;
while (!mFinalVex[n - 1]) { // 当未搜索到最后一点的时候
index = min(mShortPath);
if (index == -1) {
break;
}
// 加入之后开始遍历更新存储长度的数组
LinkedList<EDGE> edges = list.get(index);
Iterator<EDGE> it = edges.iterator();
while (it.hasNext()) {
tmp = it.next();
int j = tmp.end;
if (mFinalVex[j]) { // 如果已经被用过了
continue;
}
if (tmp.type == 1) {
double eee = trail[index] + tmp.length;
double sum = mShortPath[index] - (int) Math.pow(trail[index], 2) + (int) Math.pow(eee, 2);
if (sum < mShortPath[j]) {
mShortPath[j] = sum;
trail[j] = eee;
}
} else {
double sum = mShortPath[index] + tmp.length;
if (sum < mShortPath[j]) {
mShortPath[j] = sum;
trail[j] = 0;
}
}
}
}
}
private static int min(double[] arr) {
int index = -1;
for (int i = 0; i < n; i++) {
if (!mFinalVex[i]) {
index = i;
break;
}
}
if (index == -1) {
return index;
}
for (int i = 0; i < n; i++) {
if (!mFinalVex[i] && arr[index] > arr[i]) {
index = i;
}
}
mFinalVex[index] = true; // 表明将当前节点加入到已用节点里面
return index;
}
}
class EDGE {
public int type;
public int start;
public int end;
public double length;
public EDGE(int type, int start, int end, double length) {
super();
this.type = type;
this.start = start;
this.end = end;
this.length = length;
}
}
输出结果: