Cartoon: "multi-source" shortest route map

640?wx_fmt=gif

640?wx_fmt=jpeg

640?wx_fmt=jpeg

 

 

----- the next day -----

 

 

640?wx_fmt=jpeg

640?wx_fmt=jpeg

 

640?wx_fmt=jpeg

 

640?wx_fmt=png

640?wx_fmt=jpeg

 

640?wx_fmt=jpeg

 

Small gray roadmap is as follows:

The first step, from the table using Dijkstra's algorithm, is determined from the apex A, the shortest distance to each other vertex:

 

640?wx_fmt=png

 

The second step, to continue using Dijkstra's algorithm, obtained from the vertex B, the shortest distance to each of the other vertices.

The third step, starting from the vertex C, the shortest distance to each vertex.

The fourth step, starting from the vertex D ......

.......

Like this, it has been traversed to the vertex G.

The time complexity of this idea is how much?

If there are n vertices in the graph, the heap without considering optimization, a Dijkstra time complexity is O (n ^ 2). Therefore, to each vertex are calculated again, the overall time complexity of O (n ^ 3).

 

 

640?wx_fmt=jpeg

640?wx_fmt=jpeg

640?wx_fmt=jpeg

 

 

————————————

 

 

640?wx_fmt=jpeg

640?wx_fmt=jpeg

 

640?wx_fmt=jpeg

 

640?wx_fmt=jpeg

640?wx_fmt=jpeg

 

640?wx_fmt=jpeg

 

640?wx_fmt=jpeg

 

640?wx_fmt=jpeg

For a chestnut:

 

640?wx_fmt=png

A vertex C and vertex edge graph is not directly connected to the direct distance between them is infinite.

If B as a "relay top", then the shortest path from A to C is ABC, the shortest distance is 2 + 3 = 5.

640?wx_fmt=jpeg

 

 

Take another chestnut:

 

640?wx_fmt=png

 

Vertex A and vertex C is directly connected to the figure, the distance is 6. However, the presence of a "bypass" path ABC, the distance is 2 + 3 = 5 <6.

Therefore, after the relay vertex B, the shortest distance from A to C may be 5.

 

 

 

640?wx_fmt=jpeg

 

The following detailed steps Floyd algorithm we take a look.

1. To achieve Floyd algorithm, we first need to build a weighted graph adjacency matrix:

640?wx_fmt=png

Among the adjacency matrix, each number represents a direct distance from one vertex to another vertex, the distance relay is not related to any vertex.

2. At this time, assuming that only allows vertex to vertex A as a relay, the distance between the apex of what will become of it?

The distance between B and C was originally infinity, in this case relay A, shorten the distance AB + AC distance = distance

5+2=7。

Updates the corresponding matrix element (orange represents the distance from the apex A to the other temporary vertex):

640?wx_fmt=png

3. Next to the vertices A, B as a relay vertex, then the distance between the apex of what will become of it?

Distance between A and D are originally infinity, B is a relay case, to shorten the distance AB is the distance from the BD = + 5 + 1 = 6.

Distance between A and E originally infinity, B is a relay case, shorten the distance from the BE AB = distance + 5 + 6 = 11.

Updates the corresponding matrix element (orange represents the distance from the apex B to the other temporary vertex):

640?wx_fmt=png

 

 

4. Next to the vertices A, B, C as a relay vertex, then the distance between the apex of what will become of it?

A和F之间的距离原本是无穷大,此时以C为中继,距离缩短为AC距离+CF距离=2+8=10。

更新对应矩阵元素(橙色区域代表顶点C到其他顶点的临时距离):

640?wx_fmt=png

 

.........

.........

以此类推,我们不断引入新的中继顶点,不断刷新矩阵中的临时距离。

最终,当所有顶点都可以作为中继顶点时,我们的距离矩阵更新如下:

 

640?wx_fmt=png

 

此时,矩阵中每一个元素,都对应着某顶点到另一个顶点的最短距离。

640?wx_fmt=jpeg

640?wx_fmt=jpeg

 

为什么这么说呢?让我们回顾一下动态规划的两大要素:

问题的初始状态
问题的状态转移方程式

对于寻找图的所有顶点之间距离的问题,初始状态就是顶点之间的直接距离,也就是邻接矩阵。

而问题的状态转移方程式又是什么呢?

假设新引入的中继顶点是n,那么:

顶点i 到 顶点j 的新距离 = Min(顶点i 到 顶点j 的旧距离,顶点i 到 顶点n 的距离+顶点n 到 顶点j 的距离)

 

640?wx_fmt=jpeg

final static int INF = Integer.MAX_VALUE;	
public static void floyd(int[][] matrix){	
    //循环更新矩阵的值	
    for(int k=0; k<matrix.length; k++){	
        for(int i=0; i<matrix.length; i++){	
            for(int j=0; j<matrix.length; j++){	
                if(matrix[i][k] == INF || matrix[k][j] == INF) {	
                    continue;	
                }	
                matrix[i][j] = Math.min(matrix[i][j], matrix[i][k] + matrix[k][j]);	
            }	
        }	
    }	
    // 打印floyd最短路径的结果	
    System.out.printf("最短路径矩阵: \n");	
    for (int i = 0; i < matrix.length; i++) {	
        for (int j = 0; j < matrix.length; j++)	
            System.out.printf("%3d  ", matrix[i][j]);	
        System.out.printf("\n");	
    }	
}	
public static void main(String[] args) {	
    int[][] matrix = {	
            {0, 5, 2, INF, INF, INF, INF},	
            {5, 0, INF, 1, 6, INF, INF},	
            {2, INF, 0, 6, INF, 8, INF},	
            {INF, 1, 6, 0, 1, 2, INF},	
            {INF, 6, INF, 1, 0, INF, 7},	
            {INF, INF, 8, 2, INF, 0, 3},	
            {INF, INF, INF, INF, 7, 3, 0}	
    };	
    floyd(matrix);	
}

640?wx_fmt=jpeg

 

640?wx_fmt=jpeg

如何在短时间内成为Python工程师?

https://edu.csdn.net/topic/python115?utm_source=cxrs_bw

 

文章来自小灰,小灰的《漫画算法》上市后销量疯涨,出版社加印了好几次!占据各大电商图书板块第一位!

640?wx_fmt=png

扫码查看详情

640?wx_fmt=png

小灰把两年多以来积累的漫画作品进行了筛选和优化,并加上了一些更为基础和系统的入门章节,最终完成了本书的六大篇章:

 

第一章 算法概述

介绍了算法和数据结构的相关概念,告诉大家算法是什么,数据结构又是什么,它们有哪些用途,如何分析时间复杂度,如何分析空间复杂度。

第二章 数据结构基础

介绍了最基本的数据结构,包括数组、链表、栈、队列、哈希表的概念和读写操作。

第三章 树

介绍了树和二叉树的概念、二叉树的各种遍历方式、二叉堆和优先队列的应用。

第四章 排序算法

介绍了几种典型的排序算法,包括冒泡排序、快速排序、堆排序、计数排序、桶排序。

第五章 面试中的算法

介绍了10余道职场上流行的算法面试题及详细的解题思路。例如怎样判断链表有环、怎样计算大整数相加等。

第六章 算法的实际应用

Some applications of the algorithm in the workplace, such as the use LRU algorithm to eliminate cold data, using statistical algorithms to Bitmap user characteristics and so on.

 

This book is full-color printing system, each chapter of the book, each section, each word, each chart, each line of code, have been a little gray and editors of well-polished, and strive to use the most straightforward way to speak to understand the knowledge, say thorough.


Click to read the scan code or original purchase

640?wx_fmt=png

Add below the staff of micro-channel can also receive coupons oh

 

640?wx_fmt=gif

Code book store is a store CSDN is dedicating to our customers, provide a lot of technical books here, in addition to books we also provide lifestyle-related products, such as headsets, keyboards, etc., or you if there is a demand can also contact the code book store customer service or leave a message you need the product number in public, we try to meet the needs of everyone Oh.

As the code book store operations staff, invite you into our " CSDN codebook welfare group ", the group will from time to time give you books to books, coupons , etc., there are books recommended or logistics information is also available in the counseling group ~ at present the group is full of 100 people, and needs to sweep under the group, please add micro letter Fanger Wei code, pull you into the group oh ~

640?wx_fmt=png

 

 

640?wx_fmt=gif

 

Guess you like

Origin blog.csdn.net/csdnsevenn/article/details/93984784