【图论】深入理解Dijsktra算法
1. 介绍
Dijsktra算法是大牛Dijsktra于1956年提出,用来解决有向图单源最短路径问题;但是不能解决负权的有向图,若要解决负权图则需要用到Bellman-Ford算法。Dijsktra算法思想:在DFS遍历图的过程中,每一次取出离源点的最近距离的点,将该点标记为已访问,松弛与该点相邻的结点。
有向图记为\(G=(n, m)\),其中,\(n\)为顶点数,\(m\)为边数;且\(e[a,b]\)表示从结点\(a\)到结点\(b\)的边。\(d[i]\)记录源点到结点i的距离,\(U\)为未访问的结点集合,\(V\)为已访问的结点集合。Dijsktra算法具体步骤如下:
- 从集合\(U\)中寻找离源点最近的结点\(u\),并将结点\(u\)标记为已访问(从集合\(U\)中移到集合\(V\)中)
\]
- 松弛与结点\(u\)相邻的未访问结点,更新d数组
\]
- 重复上述操作\(n\)次,即访问了所有结点,集合\(U\)为空
Dijsktra算法的Java实现
/**
* Dijkstra's Algorithm for finding the shortest path
*
* @param adjMatrix adjacency matrix representation of the graph
* @param source the source vertex
* @param dest the destination vertex
* @return the cost for the shortest path
*/
public static int dijkstra(int[][] adjMatrix, int source, int dest) {
int numVertex = adjMatrix.length, minVertex = source;
// `d` marks the cost for the shortest path, `visit` marks whether has been visited or not
int[] d = new int[numVertex], visit = new int[numVertex];
Arrays.fill(d, Integer.MAX_VALUE);
d[source] = 0;
for (int cnt = 1; cnt <= numVertex; cnt++) {
int lowCost = Integer.MAX_VALUE;
// find the min-vertex which is the nearest among the unvisited vertices
for (int i = 0; i < numVertex; i++) {
if (visit[i] == 0 && d[i] < lowCost) {
lowCost = d[i];
minVertex = i;
}
}
visit[minVertex] = 1;
if (minVertex == dest) return d[dest];
// relax the minVertex's adjacency vertices
for (int i = 0; i < numVertex; i++) {
if (visit[i] == 0 && adjMatrix[minVertex][i] != Integer.MAX_VALUE) {
d[i] = Math.min(d[i], d[minVertex] + adjMatrix[minVertex][i]);
}
}
}
return d[dest];
}
复杂度分析
- 时间复杂度:重复操作(即最外层for循环)n次,找出minNode操作、松弛操作需遍历所有结点,因此复杂度为\(O(n^2)\).
- 空间复杂度:开辟两个长度为n的数组d与visit,因此复杂度为\(T(n)\).
2. 优化
从上述Java实现中,我们发现:(里层for循环)寻找距离源点最近的未访问结点\(u\),通过遍历整个数组来实现的,缺点是重复访问已经访问过的结点,浪费了时间。首先,我们来看看堆的性质。
堆
堆是一种完全二叉树(complete binary tree);若其高度为h,则1~h-1层都是满的。如果从左至右从上至下从1开始给结点编号,堆满足:
- 结点\(i\)的父结点编号为\(i/2\).
- 结点\(i\)的左右孩子结点编号分别为\(2*i\), \(2*i+1\).
如果结点\(i\)的关键值小于父结点的关键值,则需要进行上浮操作(move up);如果结点\(i\)的关键值大于父结点的,则需要下沉操作(move down)。为了保持堆的整体有序性,通常下沉操作从根结点开始。
小顶堆优化Dijsktra算法
我们可以用小顶堆来代替d数组,堆顶对应于结点\(u\);取出堆顶,然后删除,如此堆中结点都是未访问的。同时为了记录数组\(d[i]\)中索引\(i\)值,我们让每个堆结点挂两个值——顶点、源点到该顶点的距离。算法伪代码如下:
Insert(vertex 0, 0) // 插入源点
FOR i from 1 to n-1: // 初始化堆
Insert(vertex i, infinity)
FOR k from 1 to n:
(i, d) := DeleteMin()
FOR all edges ij:
IF d + edge(i,j) < j’s key
DecreaseKey(vertex j, d + edge(i,j))
- Insert(vertex i, d)指在堆中插入堆结点(i, d)。
- DeleteMin()指取出堆顶并删除,时间复杂度为\(O(\log n)\)。
- DecreaseKey(vertex j, d + edge(i,j))是松弛操作,更新结点(vertex j, d + edge(i,j)),需要进行上浮,时间复杂度为\(O(\log n)\)。
我们需要n次DeleteMin,m次DecreaseKey,优化版的算法时间复杂度为\(O((n+m)\log n)\).
代码实现
邻接表
每一个邻接表的表项包含两个部分:头结点、表结点,整个邻接表可以用一个头结点数组表示。下面给出其Java实现
public class AdjList {
private int V = 0;
private HNode[] adjList =null; //邻接表
/*表结点*/
class ArcNode {
int adjvex, weight;
ArcNode next;
public ArcNode(int adjvex, int weight) {
this.adjvex = adjvex;
this.weight = weight;
next = null;
}
}
/*头结点*/
class HNode {
int vertex;
ArcNode firstArc;
public HNode(int vertex) {
this.vertex = vertex;
firstArc = null;
}
}
/*构造函数*/
public AdjList(int V) {
this.V = V;
adjList = new HNode[V+1];
for(int i = 1; i <= V; i++) {
adjList[i] = new HNode(i);
}
}
/*添加边*/
public void addEdge(int start, int end, int weight) {
ArcNode arc = new ArcNode(end, weight);
ArcNode temp = adjList[start].firstArc;
adjList[start].firstArc = arc;
arc.next = temp;
}
public int getV() {
return V;
}
public HNode[] getAdjList() {
return adjList;
}
}
小顶堆
public class Heap {
public int size = 0 ;
public Node[] h = null; //堆结点
/*记录Node中vertex对应堆的位置*/
public int[] index = null;
/*堆结点:
* 存储结点+源点到该结点的距离
*/
public class Node {
int vertex, weight;
public Node(int vertex, int weight) {
this.vertex = vertex;
this.weight = weight;
}
}
public Heap(int maximum) {
h = new Node[maximum];
index = new int[maximum];
}
/*上浮*/
public void moveUp(int pos) {
Node temp = h[pos];
for (; pos > 1 && h[pos/2].weight > temp.weight; pos/=2) {
h[pos] = h[pos/2];
index[h[pos].vertex] = pos; //更新位置
}
h[pos] = temp;
index[h[pos].vertex] = pos;
}
/*下沉*/
public void moveDown( ) {
Node root = h[1];
int pos = 1, child = 1;
for(; pos <= size; pos = child) {
child = 2*pos;
if(child < size && h[child+1].weight < h[child].weight)
child++;
if(h[child].weight < root.weight) {
h[pos] = h[child];
index[h[pos].vertex] = pos;
} else {
break;
}
}
h[pos] = root;
index[h[pos].vertex] = pos;
}
/*插入操作*/
public void insert(int v, int w) {
h[++size] = new Node(v, w);
moveUp(size);
}
/*删除堆顶元素*/
public Node deleteMin( ) {
Node result = h[1];
h[1] = h[size--];
moveDown();
return result;
}
}
优化算法
public class ShortestPath {
private static final int inf = 0xffffff;
public static void dijkstra(AdjList al) {
int V = al.getV();
Heap heap = new Heap(V+1);
heap.insert(1, 0);
for(int i = 2; i <= V; i++) {
heap.insert(i, inf);
}
for(int k =1; k <= V; k++) {
Heap.Node min = heap.deleteMin();
if(min.vertex == V) {
System.out.println(min.weight);
break;
}
AdjList.ArcNode arc = al.getAdjList()[min.vertex].firstArc;
while(arc != null) {
if((min.weight+ arc.weight) < heap.h[heap.index[arc.adjvex]].weight) {
heap.h[heap.index[arc.adjvex]].weight = min.weight+ arc.weight;
heap.moveUp(heap.index[arc.adjvex]);
}
arc = arc.next;
}
}
}
/*main方法用于测试*/
public static void main(String[] args) {
AdjList al = new AdjList(5);
al.addEdge(1, 2, 20);
al.addEdge(2, 3, 30);
al.addEdge(3, 4, 20);
al.addEdge(4, 5, 20);
al.addEdge(1, 5, 100);
dijkstra(al);
}
}
3. 参考资料#
[1] FRWMM, ALGORITHMS - DIJKSTRA WITH HEAPS.
【图论】深入理解Dijsktra算法的更多相关文章
- 深入理解KMP算法
前言:本人最近在看<大话数据结构>字符串模式匹配算法的内容,但是看得很迷糊,这本书中这块的内容感觉基本是严蔚敏<数据结构>的一个翻版,此书中给出的代码实现确实非常精炼,但是个人 ...
- KMP算法详解 --- 彻头彻尾理解KMP算法
前言 之前对kmp算法虽然了解它的原理,即求出P0···Pi的最大相同前后缀长度k. 但是问题在于如何求出这个最大前后缀长度呢? 我觉得网上很多帖子都说的不是很清楚,总感觉没有把那层纸戳破, 后来翻看 ...
- 一步一步理解Paxos算法
一步一步理解Paxos算法 背景 Paxos 算法是Lamport于1990年提出的一种基于消息传递的一致性算法.由于算法难以理解起初并没有引起人们的重视,使Lamport在八年后重新发表到 TOCS ...
- Dijsktra算法C++实现
Dijsktra算法解决了有向图G=(V,E)上带权的单源最短路径问题.但要求所有边的权值非负. 思想:Dijkstra算法中设置了一顶点集合S,从源点s到集合中的顶点的最终最短路径的权值均已确定.算 ...
- 简单的理解deflate算法
简单的理解deflate算法 最近做压缩算法. 用到了deflate压缩算法, 找了很多资料, 这篇文章算是讲的比较易懂的, 这篇文章不长,但却浅显易懂, 基本上涵盖了我想要知道的所有要点. 翻译 ...
- Hdu-2112 HDU Today (单源多点最短路——Dijsktra算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给你N个公交车站,起点,终点,各站之间的距离,求起点到终点之间的最短距离.(起点终点相 ...
- 理解 KMP 算法
KMP(The Knuth-Morris-Pratt Algorithm)算法用于字符串匹配,从字符串中找出给定的子字符串.但它并不是很好理解和掌握.而理解它概念中的部分匹配表,是理解 KMP 算法的 ...
- 理解DeepBox算法
理解DeepBox算法 基本情况 论文发表在ICCV2015,作者是Berkeley的博士生Weicheng Kuo: @inproceedings{KuoICCV15DeepBox, Author ...
- 如何感性地理解EM算法?
https://www.jianshu.com/p/1121509ac1dc 如果使用基于最大似然估计的模型,模型中存在隐变量,就要用EM算法做参数估计.个人认为,理解EM算法背后的idea,远比看懂 ...
随机推荐
- 中国大学MOOC-陈越、何钦铭-数据结构-2016秋期末考试
判断题: 1-1 N2logN和NlogN2具有相同的增长速度. (2分) 1-2 对一棵平衡二叉树,所有非叶结点的平衡因子都是0,当且仅当该树是完全二叉树.(2分) 1-3 无向连通图所有顶点的度之 ...
- 解决Ubuntu Server 12.04 在Hyper-v 2012 R2中不能使用动态内存的问题
前言 全新Hyper-v 2012 R2终于开始支持在Linux的VPS中使用动态内存,可以大大优化服务器的资源分配,小弟我兴奋不已,于是抽空时间赶紧升级到 2012 R2,好好整理一番内存分配,不过 ...
- 基于SignalR的web端即时通讯 - ChatJS
先看下效果. ChatJS 是基于SignalR实现的Web端IM,界面风格模仿的是“脸书”,可以很方便的集成到已有的产品中. 项目官网:http://chatjs.net/ github地址:htt ...
- [Asp.net 开发系列之SignalR篇]专题一:Asp.net SignalR快速入门
一.前言 之前半年时间感觉自己有点浮躁,导致停顿了半年多的时间没有更新博客,今天重新开始记录博文,希望自己可以找回初心,继续沉淀.由于最近做的项目中用到SignalR技术,所以打算总结下Asp.net ...
- QQ揭秘:如何实现托盘闪动消息提醒?【低调赠送:QQ高仿版GG 4.1 最新源码】
当QQ收到好友的消息时,托盘的图标会变成好友的头像,并闪动起来,点击托盘,就会弹出与好友的聊天框,随即,托盘恢复成QQ的图标,不再闪动.当然,如果还有其它的好友的消息没有提取,托盘的图标会变成另一个好 ...
- Linux grep命令和正则表达式
介绍 grep是一个功能强大的文本搜索命令,可以用它来搜索某个文件中是否包含指定的搜索内容,它可以利用正则表达式来做复杂的筛选操作,它还可以为其它命令传输给管道的筛选,比如我们常用到的分析单个进程的操 ...
- GLFW初体验
GLFW - 很遗憾,没有找到FW的确切含义,Wiki上没有,GLFW主页也没有.猜测F表示for,W表示Window GLFW是干啥用的? 一个轻量级的,开源的,跨平台的library.支持Open ...
- [备忘]删除SQL Server中无登录名的用户
这个问题通常会在还原虚拟主机的备份SQL文件后发生,原先在虚拟主机上的用户会被还原到本地,但是本地没有权限对其进行操作. SELECT N'ALTER AUTHORIZATION ON SCHEMA: ...
- Qt5.3编译错误——call of overloaded ‘max(int int)’is ambiguous
错误描述: 今天在使用Qt写一个C++函数模板的测试程序的时候,编译的时候,编译的时候出现如下错误: 错误描述为:在main函数中,进行函数max()重载时,出现(ambiguous)含糊的,不明确的 ...
- Java中main()的args的知识点浅谈
我们先来了解下Java中main()方法的默认定义格式: public static void main(String[] args){ }1.main方法是程序执行的入口,除了args这个形参变量可 ...