参考书: 算法(第四版)

  1. 优秀的算法因为能解决实际问题而变得尤为重要
  2. 高效算法的代码也可以很简单
  3. 理解某个实现的性能特点是一项有趣而令人满足的挑战
  4. 在解决同一个问题的多种算法之间进行选择时,科学方法时一种重要的工具
  5. 迭代式改进能让算法的效率越来越高

3 图

图模型:

  • 无向图
  • 有向图
  • 加权图
  • 加权有向图

3.1 无向图

3.1.0 相关术语介绍

  • 自环:一条连接一个顶点和其自身的边
  • 平行边:链接同一对顶点的两条边
  • 多重图:含有平行边的图
  • 简单图:没有平行边或环的图
  • 路径:由边顺序连接的一系列顶点
  • 简单路径:一条没有重复顶点的路径
  • 简单环:不含有重复顶点和边的环
  • 路径或者环的的长度 = 边数

如果从任意一个顶点都存在一条路径到达另一个任意顶点————称为连通图

是无环连通图,互不相连的树组成的集合称为森林

连通图的生成树是它的一副子图

生成树森林是所有连通子图的生成树的集合
image.png

一个V个节点的图G满足五个条件,可以称为一棵

  1. G由 V-1 条边,且无环
  2. G由 V-1 条边,且连通的
  3. G连通图,但删除任意一条边不再相通
  4. G无环图,但添加任意一条边产生环
  5. G的任意一对顶点之间仅存在一条简单路径

3.1.1 图的表示方式

邻接矩阵 边的数组 邻接表
使用一个V*V的布尔矩阵,当有相连的边时,对应单元为true(空间代价太高!) 存储边集 以一个顶点为索引的列表数组,其中每个元素都是和该顶点相邻的顶点列表

image.png

3.1.2 邻接表

将每个顶点的所有相邻顶点都保存在该顶点对应的元素所指向的一张链表中

每条边都会出现两次

  • 使用的空间和 V+E 成正比
  • 添加 1条边的所需时间为常数
  • 遍历顶点v的所有相邻顶点的时间与度数成正比
  1. public class Graph {
  2. private final int V;//顶点数目
  3. private int E;//边数目
  4. private Bag<Integer>[] adj;//邻接表
  5. public Graph(int V) {
  6. this.V = V;
  7. adj = (Bag<Integer>[])new Bag[V];
  8. for(int v=0; v<V; v++) {
  9. adj[v] = new Bag<Integer>();
  10. }
  11. }
  12. /**
  13. * compute V's degree
  14. */
  15. public static int degree(Graph G, int v) {
  16. int degree = 0;
  17. for(int w:G.adj(v)) degree++;
  18. return degree;
  19. }
  20. /**
  21. * cal max(degree(G,v))
  22. */
  23. public static int maxDegree(Graph G) {
  24. int max = 0;
  25. for(int v=0; v<G.V(); v++) {
  26. if(degree(G, v) > max)
  27. max = degree(G, v);
  28. }
  29. return max;
  30. }
  31. /**
  32. * 所有节点平均度数
  33. */
  34. public static double avgDegree(Graph G) {
  35. return 2*G.E()/G.V();
  36. }
  37. /**
  38. * 计算环的个数
  39. */
  40. public static int numberOfSelfLoops(Graph G) {
  41. int cnt = 0;
  42. for(int v=0; v<G.V(); v++) {
  43. for(int w : G.adj(v)) {
  44. if(w == v) cnt++;
  45. }
  46. }
  47. return cnt/2; //每条边标记过2次
  48. }
  49. /**
  50. * return the num of vetexs
  51. */
  52. public int V() {
  53. return V;
  54. }
  55. /**
  56. * return the num of edges
  57. */
  58. public int E() {
  59. return E;
  60. }
  61. /**
  62. * add a edge bwteen v and w
  63. */
  64. public void addEdge(int v, int w) {
  65. adj[v].add(w);
  66. adj[w].add(v);
  67. E++;
  68. }
  69. /**
  70. * all vertexs(array) adjoin v
  71. */
  72. public Iterable<Integer> adj(int v){
  73. return adj[v];
  74. }
  75. }

image.png

3.1.3 深度优先搜索—-DFS

  1. public class DFS {
  2. private boolean[] marked;
  3. private int count;
  4. public DFS(Graph G, int s) {
  5. marked = new boolean[G.V()];
  6. dfs(G, s);
  7. }
  8. private void dfs(Graph G, int v) {
  9. marked[v] = true;
  10. count++;
  11. for(int w:G.adj(v)) {
  12. if(!marked[w]) dfs(G, w);//若当前顶点没有走过,go ahead!
  13. }
  14. }
  15. public boolean marked(int w) {
  16. return marked[w];
  17. }
  18. public int count() {
  19. return count;
  20. }
  21. }

DFS只需要一个递归方法来遍历所有节点,在访问一个顶点时:

  • 标记当前节点:已访问
  • 递归的访问该节点所有没有被标记过的邻接点

通常,理解一个算法算好的方法就是跟踪它的行为

我们要注意两点:

  1. 算法遍历边和访问顶点的顺序和图的表示是有关的,因为DFS只会访问当前节点所有的相邻极点
  2. 每条边都会被访问两次。并且第二次访问时总发现当前节点已经标记过了

3.1.4 寻找路径

我们利用DFS来建立一个寻找路径的算法

  1. public class DFSpaths {
  2. private boolean[] marked; // 顶点标记数组
  3. private int[] edgeTo;
  4. private final int s; // 起点【source】
  5. public DFSpaths(Graph G, int s) {
  6. // TODO Auto-generated constructor stub
  7. marked = new boolean[G.V()];
  8. edgeTo = new int[G.V()];
  9. this.s = s;
  10. dfs(G, s);
  11. }
  12. private void dfs(Graph G, int v) {
  13. marked[v] = true;
  14. for (int w : G.adj(v)) {
  15. if (!marked[w]) { // if Vertex w isn't marked
  16. /**
  17. * 到达顶点w的是顶点v
  18. */
  19. edgeTo[w] = v;
  20. dfs(G, w);
  21. }
  22. }
  23. }
  24. public boolean hasPathTo(int v) {
  25. return marked[v];
  26. }
  27. public Iterable<Integer> pathTo(int v) {
  28. /**
  29. * 如果v没被标记,说明没有顶点能够到顶点v,返回
  30. */
  31. if (!hasPathTo(v))
  32. return null;
  33. Stack<Integer> path = new Stack<Integer>();
  34. for (int x = v; x != s; x = edgeTo[x]) { // 从终点向起点走,利用栈保存
  35. path.push(x);
  36. }
  37. path.push(s);
  38. return path;
  39. }
  40. }

edgeTo[] 数组会记住每个顶点到起点的路径,从而形成一个以起点s 为根的含有所有与s相连的路径的树

image.png

【将DFS的轨迹想象成一个树的构成】

3.1.5 广度优先搜索—BFS

在BFS中,我们希望按照与起点的顺序来遍历所有顶点,看起来很容易实现————使用队列

广度优先搜索的真正目标:————为了找到最短路径

3.1.5.1 实现

算法流程简述:

  1. 先将起点 s 加入队列
  2. 反复执行下列行为,直至队列空:
    1. 取队列的下一个顶点,并标记它
    2. 将当前顶点的所有(未标记的)邻点都放入队列

BFS算法并不是递归的,它最终也能形成一棵树

  1. public class BFS {
  2. private boolean[] marked;
  3. private int[] edgeTo;
  4. private final int s; //source
  5. public BFS(Graph G, int s) {
  6. marked = new boolean[G.V()];
  7. edgeTo = new int[G.V()];
  8. this.s = s;
  9. bfs(G, s);
  10. }
  11. private void bfs(Graph G, int v) {
  12. Queue<Integer> queue = new Queue<Integer>();//利用队列结构来实现bfs
  13. marked[v] = true;
  14. queue.enqueue(v);
  15. while(!queue.isEmpty()) {
  16. int t = queue.dequeue();
  17. for(int w : G.adj(t)) {
  18. if(!marked[w]) {
  19. edgeTo[w] = t; //到达w的顶点为t
  20. marked[w] = true; //标记已经访问过的顶点
  21. queue.enqueue(w);
  22. }
  23. }
  24. }
  25. }
  26. public boolean hasPathTo(int v) {
  27. return marked[v];
  28. }
  29. public Iterable<Integer> pathTo(int v) {
  30. /**
  31. * 如果v没被标记,说明没有顶点能够到顶点v,返回
  32. */
  33. if (!hasPathTo(v))
  34. return null;
  35. Stack<Integer> path = new Stack<Integer>();
  36. for (int x = v; x != s; x = edgeTo[x]) { // 从终点向起点走,利用栈保存
  37. path.push(x);
  38. }
  39. path.push(s);
  40. return path;
  41. }
  42. }

同样的,使用BFS算法遍历时,每条边都会被访问两次,且第二次访问时这条边已经被标记过了

3.1.6 DFS和BFS

这两个算法的不同之处仅仅在于从数据结构中获取下一个顶点的规则

  • 对于BFS来说,是最早加入的顶点
  • 对于DFS来说,是最晚加入的顶点(递归的过程,最早加入的最后才会返回)

image.png

image.png

3.1.7 连通分量

过程

  1. 从0 号顶点开始 dfs 遍历
  2. 如果遇到没有标记的顶点,重复进行遍历(遍历的路径上的所有顶点都是一个连通分量
  1. public class CC {
  2. private boolean[] marked;
  3. private int[] id;
  4. private int count;
  5. public CC(Graph G) {
  6. marked = new boolean[G.V()];
  7. id = new int[G.V()];
  8. for (int x = 0; x < G.V(); x++) {
  9. /**
  10. * 从第一个顶点开始,如果位被标记,则从该节点dfs遍历(一个新的连通分量)
  11. */
  12. if (!marked[x]) {
  13. dfs(G, x);
  14. }
  15. count++; // 下一个连通分量
  16. }
  17. }
  18. private void dfs(Graph G, int v) {
  19. marked[v] = true;
  20. id[v] = count;
  21. for (int w : G.adj(v)) {
  22. if (!marked[w]) {
  23. /**
  24. * 从起点v开始的所有遍历过的路径上的所有顶点都是一个连通分量
  25. */
  26. id[w] = count;
  27. dfs(G, w);
  28. }
  29. }
  30. }
  31. public boolean connected(int v, int w) {
  32. return id[v] == id[w];
  33. }
  34. public int id(int v) {
  35. return id[v];
  36. }
  37. public int count() {
  38. return count;
  39. }
  40. }

3.1.8 使用DFS的应用

3.1.8.1 判断是否有环

  1. public class Cycle {
  2. private boolean[] marked;
  3. private boolean hasCycle;
  4. public Cycle(Graph G) {
  5. marked = new boolean[G.V()];
  6. for(int s=0; s<G.V(); s++) {
  7. if(!marked[s]) {
  8. dfs(G, s, s);
  9. }
  10. }
  11. }
  12. private void dfs(Graph G, int v, int u) {
  13. marked[v] = true;
  14. for(int w:G.adj(v)) {
  15. if(!marked[w]) {
  16. dfs(G, w, v);
  17. }
  18. else if( v == u) {
  19. /**
  20. * dfs的参数为当前顶点和已经标记的相邻顶点,如果两个顶点相等,说明遇到了环
  21. */
  22. hasCycle = true;
  23. break;
  24. }
  25. }
  26. }
  27. public boolean haCycle() {
  28. return hasCycle;
  29. }
  30. }

3.1.8.2 是否为二分图(双色问题)

  1. public class TwoColor {
  2. private boolean[] marked;
  3. private boolean[] color;
  4. private boolean isTwocolor = true;
  5. public TwoColor(Graph G) {
  6. marked = new boolean[G.V()];
  7. color = new boolean[G.V()];;
  8. for (int s = 0; s < G.V(); s++) {
  9. if (!marked[s]) {
  10. dfs(G, s);
  11. }
  12. }
  13. }
  14. private void dfs(Graph G, int v) {
  15. marked[v] = true;
  16. for (int w : G.adj(v)) {
  17. if (!marked[v]) {
  18. color[w] = !color[v];//在遍历下一个顶点之前做动作
  19. dfs(G, w);
  20. } else if (color[v] == color[w]) // 如果两个相邻顶点颜色一样,则不是二分图
  21. isTwocolor = false;
  22. }
  23. }
  24. public boolean isTwoColor() {
  25. return isTwocolor;
  26. }
  27. }

3.2. 有向图

image.png

3.2.1 基本API实现

  1. public class Digraph {
  2. private final int V;//顶点数目
  3. private int E;//边数目
  4. private Bag<Integer>[] adj;//邻接表
  5. public Digraph(int V) {
  6. this.V = V;
  7. adj = (Bag<Integer>[])new Bag[V];
  8. for(int v=0; v<V; v++) {
  9. adj[v] = new Bag<Integer>();
  10. }
  11. }
  12. /**
  13. * return the num of vetexs
  14. */
  15. public int V() {
  16. return V;
  17. }
  18. /**
  19. * return the num of edges
  20. */
  21. public int E() {
  22. return E;
  23. }
  24. /**
  25. * add an edge bwteen v and w
  26. * notice that in the Digraph, every edges have a direction
  27. */
  28. public void addEdge(int v, int w) {
  29. adj[v].add(w);//因为是有向的,所以只需要添加一条边
  30. E++;
  31. }
  32. /**
  33. * all vertexs(array) adjoin v
  34. */
  35. public Iterable<Integer> adj(int v){
  36. return adj[v];
  37. }
  38. /**
  39. * 构造有向图的反向图
  40. */
  41. public Digraph reverse() {
  42. Digraph R = new Digraph(V);
  43. for(int v=0; v<V; v++) {
  44. for(int w:adj(v)) {
  45. R.addEdge(w, v);
  46. }
  47. }
  48. return R;
  49. }
  50. }

3.2.2 DFS、BFS

本质上和无向图的DFS、BFS实现是一样的(同样的思想)

3.2.3 多点可达性

给定一副有向图和顶点的集合,回答:

  • 是否存在一条从集合中的任意顶点到达给定顶点v的有向路径

实际应用:内存管理系统(java垃圾回收机制)

image.png
image.png

3.2.4 调度问题

一种应用广泛的模型是给定一组任务并安排执行顺序

最重要的一种限制条件————优先级限制

image.png

3.2.5 拓扑排序

优先级限制下的调度问题 等价于 拓扑排序问题

【拓扑排序】:给定一副有向图,将所有的顶点排序,使得所有的有向边均从排在前面的元素指向排在后面的元素

image.png

拓扑排序(或者说优先级限制问题)是针对有向无环图【DAG】的,因此是不允许有环的

拓扑排序就是dfs逆后序的顶点排序

3.2.6 有向图寻找环

寻找环的基本思想还是利用DFS算法

但是我们增加了一个boolean类型的数组onStack[]来保存递归调用期间栈上的所有顶点

如果找到一条边v->w,并且w已在栈中(也就意味着之前已经标记过的顶点w在之后又被找到,也就是一条环路)

则找到了一个有向环

  1. public void dfs(Digraph G, int s) {
  2. marked[s] = true;
  3. onStack[s] = true;
  4. for(int w:G.adj(s)) {
  5. if(hasCycle()) return;
  6. else if(!marked[w]) {
  7. edgeTo[w] = s;
  8. dfs(G, w);
  9. }
  10. else if(onStack[w]) {//有环路,则记录环路路径
  11. cycle = new Stack<Integer>();
  12. cycle.push(w);
  13. for(int x=edgeTo[w]; x != w; x = edgeTo[x]) {
  14. cycle.push(x);
  15. }
  16. }
  17. onStack[s] = false; //若当前搜索路径没有找到环,需要将栈数组复原,以便下一条路径的搜索
  18. }
  19. }

3.2.7 强连通性

如果一副有向图中的任意两个顶点都是强连通的,则称这幅图是强连通的

(难以理解,跳过,后续深入)

3.2.8 顶点对的可达性

只需利用DFS进行可达性的判定,但无法使用于大型图网络(构造图的代价太高)

  1. public class TransitiveClosure {
  2. private DirectedDFS[] all;
  3. public TransitiveClosure(Digraph G) {
  4. all = new DirectedDFS[G.V()];//有向图DFS
  5. for(int v=0; v<G.V(); v++) {
  6. /**
  7. * 构造了一个二维数组,相当于矩阵表示图关系
  8. * 每个节点DFS一次,若v和w相连,那么marked[w]一定为true
  9. */
  10. all[v] = new DirectedDFS(G, v);
  11. }
  12. }
  13. public boolean reachable(int v, int w) {
  14. return all[v].marked(w);
  15. }
  16. }

3.3. 最小生成树

给定一副加权无向图,找到它的一棵最小生成树

图的生成树是它的一棵含有其所有顶点无环连通子图

最小生成树【MST】是一棵权值之和最小(成本最小)的生成树。

image.png

image.png

3.3.1 一些约定

  • 只考虑连通图
  • 边的权重不一定表示距离
  • 边的权重可能是0或者负数
  • 所有边的权重各不相同

3.3.2 原理

image.png

image.png

反证法

3.3.3 切分定理下的贪心算法

切分定理是解决最小生成树问题的所有算法的基础

这些都是贪心算法的特殊情况:使用切分定理找到最小生成树的一条边,不断重复直到找到所有边

image.png

一棵【tree】的特点:V个顶点V-1条边

3.3.4 Prim算法

数据结构

  • 顶点:marked[]数组
  • 边:Edge对象
  • 横切边:最小优先队列MinPQ

3.3.4.1 延时实现

每当我们向树中添加了一条边之后,也向树中添加了一个顶点

将连接这个顶点和其他不再树中顶点的边加入优先队列

两端顶点都在树中的边已经失效了(防止环的出现)

延时的实现会将失效的边留在优先队列里
image.png

在添加了V个顶点之后(V-1条边),最小生成树就完成了

(优先队列中余下的项都是无效的,不需要再检查它们)

image.png

  1. public class LazyPrimMST {
  2. private boolean[] marked;//顶点
  3. private Queue<Edge> mst;//边
  4. private MinPQ<Edge> pq;//横切边(包括最小的边)
  5. public LazyPrimMST(EdgeWeightedGraph G) {
  6. marked = new boolean[G.V()];
  7. mst = new Queue<Edge>();
  8. pq = new MinPQ<Edge>();
  9. visit(G, 0);
  10. while(!pq.isEmpty()) {
  11. Edge e = pq.delMin(); //找到最小横切边
  12. int v = e.either();
  13. int w = e.other(v);
  14. /**
  15. * 忽略无效边(形成环的边)
  16. */
  17. if(marked[v] && marked[w]) continue;
  18. mst.enqueue(e);//最小生成树加入边
  19. /**
  20. * 访问未标记的顶点,将边放入优先队列中
  21. */
  22. if(!marked[v]) visit(G, v);
  23. if(!marked[w]) visit(G, w);
  24. }
  25. }
  26. /**
  27. * 将顶点v连接的边加入优先队列中
  28. */
  29. private void visit(EdgeWeightedGraph G, int v) {
  30. marked[v] = true;
  31. for(Edge w:G.adj(v)) {
  32. if(!marked[w.other(v)]) { //如果当前边没有被添加过【两个顶点未被全部标记】
  33. pq.insert(w);
  34. }
  35. }
  36. }
  37. public Iterable<Edge> mst(){
  38. return mst;
  39. }
  40. /**
  41. * 延迟计算权重
  42. */
  43. public double weight() {
  44. double w = 0;
  45. for(int i=0; i<marked.length-1; i++) {
  46. Edge e = mst.dequeue();
  47. w += e.weight();
  48. mst.enqueue(e);
  49. }
  50. return w;
  51. }
  52. }

3.3.4.2 即时实现

基本思路是:

  • 一个根节点出发,每一个找到一条与当前树相连(且不在树中)的权值最小的边(也就是最短的横切边)
  • 将该边加入树中
  • 反复执行该过程,直到N个顶点全部被标记完成

顶点索引的edgeTo[]和distTo[]数组

它们具有如下性质:

  1. 如果顶点v不在树中,但是至少一条边和树相连,那么edgeTo[v]是与v相连的最短的边,distTo[v]为该边的权重
  2. 所有的这类顶点v都保存在最小优先队列中

算法处理的过程:

  • 首先从优先队列中取出权值最小的边
  • 检查每条边v—w,如果w已经标记过了,则什么也不做
  • 否则, 如果当前边已经存在与优先队列中并且小于当前队列中的数值,更新它的值(说明到w点还有距离更短的边)
  • 如果不在优先队列中,添加它
    image.png

【思考】

为什么每次都能找到与当前树相连的权重最小的边呢?————优先队列

  1. public class PrimMST {
  2. private Edge[] edgeTo; // 距离当前树权重最小的边
  3. private double[] distTo;// weight[w] = edgeTo[w].weight()
  4. private boolean[] marked;// 如果v在树种则为true
  5. private IndexMinPQ<Double> pq;// 有效边的横切边
  6. public PrimMST(EdgeWeightedGraph G) {
  7. edgeTo = new Edge[G.V()];
  8. distTo = new double[G.V()];
  9. marked = new boolean[G.V()];
  10. pq = new IndexMinPQ<Double>(G.V());
  11. for (int v = 0; v < G.V(); v++) {
  12. distTo[v] = Double.POSITIVE_INFINITY;
  13. }
  14. distTo[0] = 0.0;
  15. pq.insert(0, 0.0); // 初始化最小优先队列
  16. while (!pq.isEmpty()) { // 将与树相连的最小权重边加入树中
  17. visit(G, pq.delMin());
  18. }
  19. }
  20. /**
  21. * 将顶点加到树中,更新数据
  22. */
  23. private void visit(EdgeWeightedGraph G, int v) {
  24. marked[v] = true;
  25. for (Edge e : G.adj(v)) {
  26. if (marked[e.other(v)])
  27. continue; // 一条边的两个顶点都被标记过,则什么也不做
  28. if (e.weight() < distTo[e.other(v)]) {//如果当前边小于优先队列中到这个点的距离
  29. edgeTo[e.other(v)] = e;
  30. distTo[e.other(v)] = e.weight();
  31. if (pq.contains(e.other(v))) {//若不再优先队列中,添加进去
  32. pq.changeKey(e.other(v), distTo[e.other(v)]);
  33. } else {//否则,更新它
  34. pq.insert(e.other(v), e.weight());
  35. }
  36. }
  37. }
  38. }
  39. }

image.png

3.3.5 Kruskal算法

将边按照权重排序

依次选择权重最小的边加入(前提:不形成环)

选择的边由森林逐渐形成树

Prim是由一条边一条边地构成一棵树,从一个根节点开始,每一步不断添加一条边

Kruskal是由不同的边,它的边会连接成森林,最终连成一棵树

  1. public class KruskalMST {
  2. private Queue<Edge> mst;
  3. public KruskalMST(EdgeWeightedGraph G) {
  4. mst = new Queue<Edge>();
  5. MinPQ<Edge> pq = new MinPQ<Edge>((Comparator<Edge>) G.edges());
  6. UF uf = new UF(G.V());
  7. while (mst.size() < G.V() - 1 && !pq.isEmpty()) {
  8. Edge e = pq.delMin(); // 找到权重最小的边
  9. int v = e.either();
  10. int w = e.other(v);
  11. if (uf.connected(v, w))
  12. continue; // 如果在找到一条边的两个端点已经在一个连通分量上,说明这条边的连接必定会形成环
  13. uf.union(v, w);
  14. mst.enqueue(e);
  15. }
  16. }
  17. }

image.png

Kruskal算法比Prim算法要慢一些,两个算法都需要对每条边进行优先队列操作

Kruskal算法还需要进行依次union操作(连接两个点形成一个连通分量)

3.4. 最短路径

3.4.1 定义

从顶点s到顶点t的最短路径是所有从s到t的路径中权重最小

3.4.2 性质

  • 路径是有向的
  • 并不是所有顶点都是可达的
  • 负权重会使问题变复杂
  • 最短路径一般都是简单图
  • 最短路径不一定是唯一的
  • 可能存在平行边和自环

3.4.3 最短路径树

最短路径问题,最后会给出一个根节点为起点s的最短路径树,它包含了s到所有可达顶点的最短路径
image.png

3.4.4 有向加权边API

  1. public class DirectedEdge {
  2. private final int v; // 顶点之一
  3. private final int w; // 另一个顶点
  4. private final double weight; // 权重
  5. public DirectedEdge(int v, int w, double weight) {
  6. this.v = v;
  7. this.w = w;
  8. this.weight = weight;
  9. }
  10. public double weight() {
  11. return weight;
  12. }
  13. public int from() {
  14. return v;
  15. }
  16. public int to() {
  17. return w;
  18. }
  19. }

3.4.5 有向加权图API

  1. public class EdgeWeightedDigraph {
  2. private final int V;// 顶点总数
  3. private int E;// 边总数
  4. private Bag<DirectedEdge>[] adj;// 邻接表
  5. public EdgeWeightedDigraph(int V) {
  6. this.V = V;
  7. adj = (Bag<DirectedEdge>[]) new Bag[V];
  8. for (int v = 0; v < V; v++) {
  9. adj[v] = new Bag<DirectedEdge>();
  10. }
  11. }
  12. public int V() {
  13. return V;
  14. }
  15. public int E() {
  16. return E;
  17. }
  18. public void addEdge(DirectedEdge e) {
  19. adj[e.from()].add(e);// 有向图,只需要加入一条边
  20. E++;
  21. }
  22. public Iterable<DirectedEdge> adj(int v) {
  23. return adj[v];
  24. }
  25. /**
  26. * 返回加权有向图的所有边
  27. */
  28. public Iterable<DirectedEdge> edges() {
  29. Bag<DirectedEdge> b = new Bag<DirectedEdge>();
  30. for (int v = 0; v < V; v++) {
  31. for (DirectedEdge e : adj[v]) {
  32. b.add(e);
  33. }
  34. }
  35. return b;
  36. }
  37. }

不同于无向图,在加权有向图中,每条边在邻接表中仅出现一次

3.4.6 边的松弛【relax】

我们的最短路径算法的实现都是基于一个叫做松弛【relax】的操作进行的

在遇到新的边v——>w时,需要进行松弛操作:

  • 如果dist[w] <= dist[v] + e.weight():
    则说明到s到w的路径和不大于从s到v再从v到w的路径和,那么v——>w这条边就失效了(不选择这条边
  • 如果dist[w] > dist[v] + e.weight():
    更新dist[w]的值
  1. /*
  2. 边的松弛
  3. */
  4. public void relax(DirectedEdge e){
  5. int v = e.from();
  6. int w = e.to();
  7. if(dist[v] + e.weight() < dist[w]){
  8. dist[w] = dist[v] + e.weight(); //更新成更小的权重和
  9. edgeTo[w] = e; //更新到w的边,之前的边会被替换为新边e
  10. }
  11. }

image.png

放松一条边就类似于:

将橡皮筋转移到一条更短的路径上,从而放松了较长边的压力

3.4.7 顶点的松弛

对于每一个顶点,我们会遍历所有顶点的邻边,并对所有邻接边进行松弛操作

因为,在最短路径中,每一个顶点有且只有一条边连接到它(前提:从源点可达)

image.png

  1. /*
  2. 顶点的松弛
  3. */
  4. public void relax(EdgeWeightedDigraph G, int v){
  5. for(DirectedEdge e : G.adj[v]){
  6. int w = e.to();
  7. if(dist[w] > dist[v] + e.weight()){
  8. dist[w] = dist[v] + e.weight(); //更新成更小的权重和
  9. edgeTo[w] = e; //更新到w的边,之前的边会被替换为新边e
  10. }
  11. }
  12. }

3.4.9 Dijkstra算法

回忆一下Prim算法:每一次向树中添加一条边

Dijkstra算法也是采用了类似的方法

  • 首先将dist[s]初始化为0
  • dist[]中其他的元素初始化为正无穷(即不可达)
  • 然后将dist[]中最小的非树顶点进行松弛操作,并加入树中
  • 如此这般直到所有顶点都在树中,或者非树顶点为正无穷(即完成算法,或者某些顶点不可达)

image.png

另外,dijkstra算法在对边的松弛时,需要处理两种情况:

  1. 如果边的to()不在优先队列中,添加进pq
  2. 如果已在优先队列中,需要降低优先级

3.4.9.1 实现

  1. public class DijkstraSP {
  2. private DirectedEdge[] edgeTo;
  3. private double[] distTo;
  4. private IndexMinPQ<Double> pq;
  5. public DijkstraSP(EdgeWeightedDigraph G, int s) {
  6. edgeTo = new DirectedEdge[G.V()];
  7. distTo = new double[G.V()];
  8. pq = new IndexMinPQ<Double>(G.V());
  9. //将所有边到起点的距离初始化为正无穷
  10. for (int v = 0; v < G.V(); v++) {
  11. distTo[v] = Double.POSITIVE_INFINITY;
  12. }
  13. distTo[s] = 0.0;
  14. pq.insert(s, 0.0);
  15. while(!pq.isEmpty()) {
  16. relax(G, s);//对所有顶点进行松弛操作
  17. }
  18. }
  19. /**
  20. * 顶点的松弛操作
  21. * 1. 如果边的to()不在优先队列中,添加进pq
  22. * 2. 如果已在优先队列中,需要降低优先级
  23. */
  24. private void relax(EdgeWeightedDigraph G, int v) {
  25. for(DirectedEdge e : G.adj(v)){
  26. int w = e.to();
  27. if(distTo[w] > distTo[v] + e.weight()){
  28. distTo[w] = distTo[v] + e.weight(); //更新成更小的权重和
  29. edgeTo[w] = e; //更新到w的边,之前的边会被替换为新边e
  30. if(pq.contains(w)) pq.change(w, e.weight());
  31. else pq.insert(w, e.weight());
  32. }
  33. }
  34. }
  35. }

dijkstra算法是每一次往树中添加一条边,该边是由树中的顶点指向非树中的顶点w,且是到s最近的点

该算法可以找到给定两点的最短路径

3.4.9.2 时间复杂度

在一副有V个顶点和E条边的加权有向图,时间与ElogV成正比(最坏情况下)

但是,对于有负权重的加权有向图中的最短路径问题,dijkstra算法无法完成这类问题

【问题】:(未答)

为什么dijkstra算法无法解决带负权重问题呢?

image.png

3.4.10 无环加权有向图的最短路径算法

特点:

  • 能够在线性时间解决单点最短路径
  • 能够处理负权重的边
  • 能够解决相关的问题,例如找出最长的路径

只要将顶点的放松和拓扑排序结合起来,马上就能够得到一种解决无环加权有向图中的最短路径的算法

3.4.11 解决负权重的最短路径算法

Bellman-ford算法

时间复杂度算法 第四版(三) - 图27#card=math&code=O%28VE%29&id=ilkEE)

(后续跟进)

image.png