floyd弗洛伊德算法 PPT课件
- 格式:ppt
- 大小:661.50 KB
- 文档页数:14
弗洛伊德算法定义Floyd算法又称为弗洛伊德算法,插点法,是一种用于寻找给定的加权图中顶点间最短路径的算法。
核心思路通过一个图的权值矩阵求出它的每两点间的最短路径矩阵。
从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按一个公式,构造出矩阵D(1);又用同样地公式由D(1)构造出D(2);……;最后又用同样的公式由D(n-1)构造出矩阵D(n)。
矩阵D(n)的i行j列元素便是i号顶点到j号顶点的最短路径长度,称D(n)为图的距离矩阵,同时还可引入一个后继节点矩阵path来记录两点间的最短路径。
采用的是松弛技术,对在i和j之间的所有其他点进行一次松弛。
所以时间复杂度为O(n^3);算法描述a) 初始化:D[u,v]=A[u,v]b) For k:=1 to nFor i:=1 to nFor j:=1 to nIf D[i,j]>D[i,k]+D[k,j] ThenD[i,j]:=D[i,k]+D[k,j];c) 算法结束:D即为所有点对的最短路径矩阵算法过程把图用邻接矩阵G表示出来,如果从Vi到Vj有路可达,则G[i,j]=d,d表示该路的长度;否则G[i,j]=空值。
定义一个矩阵D用来记录所插入点的信息,D[i,j]表示从Vi到Vj需要经过的点,初始化D[i,j]=j。
把各个顶点插入图中,比较插点后的距离与原来的距离,G[i,j] = min( G[i,j], G[i,k]+G[k,j] ),如果G[i,j]的值变小,则D[i,j]=k。
在G中包含有两点之间最短道路的信息,而在D中则包含了最短通路径的信息。
比如,要寻找从V5到V1的路径。
根据D,假如D(5,1)=3则说明从V5到V1经过V3,路径为{V5,V3,V1},如果D(5,3)=3,说明V5与V3直接相连,如果D(3,1)=1,说明V3与V1直接相连。
时间复杂度O(n^3)优缺点分析Floyd算法适用于APSP(All Pairs Shortest Paths),是一种动态规划算法,稠密图效果最佳,边权可正可负。
【图论⼊门】——弗洛伊德算法(Floyd)——注意事项 1174 - CommandosTime Limit: 2 second(s)Memory Limit: 32 MBA group of commandos were assigned a critical task. They are to destroy an enemy head quarter. The enemy head quarter consists of several buildings and the buildings are connected by roads. The commandos must visit each building and place a bomb at the base of each building. They start their mission at the base of a particular building and from there they disseminate to reach each building. The commandos must use the available roads to travel between buildings. Any of them can visit one building after another, but they must all gather at a common place when their task in done. In this problem, you will be given the description of different enemy headquarters. Your job is to determine the minimum time needed to complete the mission. Each commando takes exactly one unit of time to move between buildings. You may assume that the time required to place a bomb is negligible. Each commando can carry unlimited number of bombs and there is an unlimited supply of commando troops for the mission.InputInput starts with an integer T (≤50), denoting the number of test cases.The first line of each case starts with a positive integer N (1 ≤ N ≤ 100), where N denotes the number of buildings in the head quarter. The next line contains a positive integer R, where R is the number of roads connecting two buildings. Each of the next R lines contain two distinct numbers u v (0 ≤ u, v < N), this means there is a road connecting building u to building v. The buildings are numbered from 0 to N-1. The last line of each case contains two integers s d (0 ≤ s, d < N). Where s denotes the building from where the mission startsand d denotes the building where they must meet. You may assume that two buildings will be directly connected by at most one road. The input will be given such that, it will be possible to go from any building to another by using one or more roads.OutputFor each case, print the case number and the minimum time required to complete the mission.Sample Input Output for SampleInput2 43 0 1 2 1 1 3 0 3 2 10 11 0Case 1: 4 Case 2: 1题意:特⼯队去炸碉堡,要求每个都炸掉,⽽且只能⾛给出的道路。
佛洛依德路径平滑算法(floyd) 常见的a*算法的结果是⼀串⽤来表⽰所经过的路径点坐标。
但是这样的路径通常是有“锯齿”的,并不符合现实中的智能表现。
因此,需要进⼀步的进⾏平滑处理,⽐如佛洛依德算法~ 算法原理很简单,分为两步: 1.去掉相邻的共线的点 2.去掉多余的拐弯的点 第⼀步实现起来很简单,只需要遍历⼀下,计算两个向量的⽅向是否相同。
第⼆步的实现稍微⿇烦⼀点,遍历所有的点,去掉两个可以直接通过的点之间的点。
有点绕。
其实是很经典的画直线算法,找两个点作为端点画⼀条线,这条先经过的⽹格如果都是可同⾏的,那么我们就认为在路径中这两个点中间的那些点是多余的。
其实第⼆步就可以完成优化,但是计算量⽐较⼤。
所以先通过第⼀步来减少⼀部分计算量~下⾯是代码:#region floyd//----------------------------------------弗洛伊德路径平滑--------------------------------------//public List<Vector3> Floyd( List<Vector3> path){if (path == null){return path;}int len = path.Count;//去掉同⼀条线上的点。
if (len > 2){Vector3 vector = path[len -1] - path[len - 2];Vector3 tempvector;for (int i = len - 3; i>= 0; i--){tempvector = path[i+1] - path[i];if (Vector3.Cross(vector, tempvector).y == 0f){path.RemoveAt(i+1);}else{vector = tempvector;}}}//去掉⽆⽤拐点len = path.Count;for (int i = len-1; i >= 0; i--){for (int j = 0; j<= i-1; j++){if (CheckCrossNoteWalkable(path[i],path[j])){for (int k = i-1; k>=j; k--){path.RemoveAt(k);}i=j;//len = path.Count;break;}}}return path;}float currentY; // ⽤于检测攀爬与下落⾼度//判断路径上是否有障碍物public bool CheckCrossNoteWalkable(Vector3 p1, Vector3 p2){currentY = p1.y; //记录初始⾼度,⽤于检测是否可通过bool changexz = Mathf.Abs(p2.z - p1.z) > Mathf.Abs(p2.x - p1.x);if (changexz){float temp = p1.x;p1.x = p1.z;p1.z = temp;temp = p2.x;p2.x = p2.z;p2.z = temp;}if (!Checkwalkable(changexz, p1.x, p1.z)){return false;}float stepX = p2.x > p1.x ? Tilesize : (p2.x < p1.x ? -Tilesize : 0);float stepY = p2.y > p1.y ? Tilesize : (p2.y < p1.y ? -Tilesize : 0);float deltay = Tilesize * ( (p2.z - p1.z) / Mathf.Abs(p2.x - p1.x) );float nowX = p1.x + stepX/2;float nowY = p1.z - stepY/2;float CheckY = nowY;while (nowX != p2.x){if(!Checkwalkable(changexz, nowX, CheckY)){return false;}nowY += deltay;if(nowY >= CheckY + stepY){CheckY += stepY;if (!Checkwalkable(changexz, nowX, CheckY)){return false;}}nowX += stepX;}return true;}private bool Checkwalkable(bool changeXZ, float x, float z){int mapx = (MapStartPosition.x < 0F) ? Mathf.FloorToInt(((x + Mathf.Abs(MapStartPosition.x)) / Tilesize)) : Mathf.FloorToInt((x - MapStartPosition.x) / Tilesize);int mapz = (MapStartPosition.y < 0F) ? Mathf.FloorToInt(((z + Mathf.Abs(MapStartPosition.y)) / Tilesize)) : Mathf.FloorToInt((z - MapStartPosition.y) / Tilesize);if (mapx < 0 || mapz < 0 || mapx >= Map.GetLength(0) || mapz >= Map.GetLength(1)){return false;}Node note;if (changeXZ){note = Map[mapz, mapx];}else{note = Map[mapx, mapz];}bool ret = note. walkable && ( (note.yCoord - currentY <= ClimbLimit && note.yCoord >= currentY) || (currentY - note.yCoord <= MaxFalldownHeight && currentY >= note.yCoord) );if (ret){currentY = note.yCoord;}return ret;}#endregion end floyd。
Floyd算法Floyd算法又称为弗洛伊德算法,插点法,是一种用于寻找给定的加权图中顶点间最短路径的算法。
核心思路:通过一个图的权值矩阵求出它的每两点间的最短路径矩阵。
从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按一个公式,构造出矩阵D(1);又用同样地公式由D(1)构造出D(2);……;最后又用同样的公式由D(n-1)构造出矩阵D(n)。
矩阵D(n)的i行j列元素便是i号顶点到j号顶点的最短路径长度,称D(n)为图的距离矩阵,同时还可引入一个后继节点矩阵path 来记录两点间的最短路径。
算法过程:把图用邻接距阵G表示出来,如果从Vi到Vj有路可达,则G[i,j]=d,d表示该路的长度;否则G[i,j]=无穷大。
定义一个距阵D用来记录所插入点的信息,D[i,j]表示从Vi到Vj需要经过的点,初始化D[i,j]=j。
把各个顶点插入图中,比较插点后的距离与原来的距离,G[i,j] = min( G[i,j], G[i,k]+G[k,j] ),如果G[i,j]的值变小,则D[i,j]=k。
在G中包含有两点之间最短道路的信息,而在D中则包含了最短通路径的信息。
比如,要寻找从V5到V1的路径。
根据D,假如D(5,1)=3则说明从V5到V1经过V3,路径为{V5,V3,V1},如果D(5,3)=3,说明V5与V3直接相连,如果D(3,1)=1,说明V3与V1直接相连。
优缺点分析:Floyd算法适用于APSP(All Pairs Shortest Paths),稠密图效果最佳,边权可正可负。
此算法简单有效,由于三重循环结构紧凑,对于稠密图,效率要高于执行|V|次Dijkstra算法。
优点:容易理解,可以算出任意两个节点之间的最短距离,代码编写简单;缺点:时间复杂度比较高,不适合计算大量数据。
Floyd算法的基本思想:(1)利用二维数组A[1..n-1][1..n-1], A[i][j]记录当前vi到vj的最短路径长度,数组A的初值等于图的代权临街矩阵;(2)集合S记录当前允许的中间顶点,初值S=Φ;(3)依次向S中加入v0 ,v1… vn-1,每加入一个顶点,对A[i][j]进行一次修正:设S={v0 ,v1… vk-1},加入vk,则A(k)[i][j] = min{ A(k-1)[i][j],A(k-1)[i][k]+A(k-1)[k][j]}。
Floyd(弗洛伊德)算法(C语⾔)转载:Floyd算法的介绍算法的特点弗洛伊德算法是解决任意两点间的最短路径的⼀种算法,可以正确处理有向图或有向图或负权(但不可存在负权回路)的最短路径问题,同时也被⽤于计算有向图的传递闭包。
算法的思路通过Floyd计算图G=(V,E)中各个顶点的最短路径时,需要引⼊两个矩阵,矩阵S中的元素a[i][j]表⽰顶点i(第i个顶点)到顶点j(第j个顶点)的距离。
矩阵P中的元素b[i][j],表⽰顶点i到顶点j经过了b[i][j]记录的值所表⽰的顶点。
假设图G中顶点个数为N,则需要对矩阵D和矩阵P进⾏N次更新。
初始时,矩阵D中顶点a[i][j]的距离为顶点i到顶点j的权值;如果i和j不相邻,则a[i][j]=∞,矩阵P的值为顶点b[i][j]的j的值。
接下来开始,对矩阵D进⾏N次更新。
第1次更新时,如果”a[i][j]的距离” > “a[i][0]+a[0][j]”(a[i] [0]+a[0][j]表⽰”i与j之间经过第1个顶点的距离”),则更新a[i][j]为”a[i][0]+a[0][j]”,更新b[i][j]=b[i][0]。
同理,第k次更新时,如果”a[i][j]的距离” >“a[i][k-1]+a[k-1][j]”,则更新a[i][j]为”a[i][k-1]+a[k-1][j]”,b[i][j]=b[i][k-1]。
更新N次之后,操作完成!补充:以下⾯图为例⼦,b[i][j]中存储的是Vi~Vj之间的中介点,b[i][j]初始值为j,⽐如V0~V3最短路径是V0-->V2-->V1-->v3,在计算最短路径时转换为V0-->V2的距离加上V2-->V3的最短距离,接下来类似于递归,V2-->V3的最短路径就是以V1为中介点,V2-->V1的距离加上V1-->V3的距离。
因此,b[0][3]=2实例说明将整体分为两个步骤1.计算metrixD矩阵(两顶点之间的最短距离)和P矩阵(两顶点的中介点)#include <stdio.h>#include <stdlib.h>void Create_metrixD_P(int** metrixD, int **P ,int VerNum, int EdgNum){int x, y, Weight, edg_count = 0;int i, j, k;for (i = 0; i < VerNum; ++i) {for (j = 0; j < VerNum; ++j) {metrixD[i][j] = INT_MAX;P[i][j] = j;}}while (edg_count < EdgNum) {scanf("%d%d%d", &x, &y, &Weight);metrixD[x - 1][y - 1] = Weight;edg_count++;}}//Floyd algorithmvoid Floyd(int **metirxD, int **P, int VerNum) {int n, x, y, temp = 0;//The triple loop looks for shortest paths and weightsfor (n = 0; n < VerNum; ++n) {for (x = 0; x < VerNum; ++x) {for (y = 0; y < VerNum; ++y) {//The distance between two vertices is compared to the distance through a vertextemp = (metirxD[x][n] == INT_MAX || metirxD[n][y] == INT_MAX) ? INT_MAX : (metirxD[x][n] + metirxD[n][y]);if (temp < metirxD[x][y]) {//Update matrix informationmetirxD[x][y] = temp;P[x][y] = n;}}}}}void Show_metrixD_P(int** metrixD, int **P, int VerNum){int x, y;printf("metrixD:\n");for (x = 0; x < VerNum; ++x) {for (y = 0; y < VerNum; ++y) {if (metrixD[x][y] == INT_MAX) {printf("∞ ");}else {printf("%d ", metrixD[x][y]);}}printf("\n");}printf("P:\n");for (x = 0; x < VerNum; ++x) {for (y = 0; y < VerNum; ++y) {printf("%d ", P[x][y]);}printf("\n");}}int main(void){int VerNum, EdgNum, i;int** metrixD, ** P;printf("Enter the number of vertices and edges:");scanf("%d%d", &VerNum, &EdgNum);metrixD = (int**)malloc(VerNum * sizeof(int));P = (int**)malloc(VerNum * sizeof(int));for (i = 0; i < VerNum; ++i) {metrixD[i] = (int*)malloc(VerNum * sizeof(int));P[i] = (int*)malloc(VerNum * sizeof(int));}printf("Input vertices and weights:");Create_metrixD_P(metrixD, P, VerNum, EdgNum);Floyd(metrixD, P, VerNum);Show_metrixD_P(metrixD, P, VerNum);for (i = 0; i < VerNum; ++i) {free(metrixD[i]);free(P[i]);}free(metrixD);free(P);return0;}2.输出顶点之间的最短距离与路径#include <stdio.h>#include <stdlib.h>#define VEXNUM 5//Adjacency matrix: shows the distance between verticesint metirxD[VEXNUM][VEXNUM] = {INT_MAX,10, 5, INT_MAX,INT_MAX,INT_MAX,INT_MAX,2, 1, INT_MAX,INT_MAX,3, INT_MAX,9, 2,INT_MAX,INT_MAX,INT_MAX,INT_MAX,4,7, INT_MAX,INT_MAX,5, INT_MAX};//Path: passing vertex between two verticesint P[VEXNUM][VEXNUM] = {0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4};//Floyd algorithmvoid Floyd() {int n, x, y, temp = 0;//The triple loop looks for shortest paths and weightsfor (n = 0; n < VEXNUM; ++n) {for (x = 0; x < VEXNUM; ++x) {for (y = 0; y < VEXNUM; ++y) {//The distance between two vertices is compared to the distance through a vertextemp = (metirxD[x][n] == INT_MAX || metirxD[n][y] == INT_MAX) ? INT_MAX : (metirxD[x][n] + metirxD[n][y]);if (temp < metirxD[x][y]) {//Update matrix informationmetirxD[x][y] = temp;P[x][y] = n;}}}}}void Show_Path() {int x, y, temp = 0;//Output the shortest path between two verticesfor (x = 0; x < VEXNUM - 1; ++x) {for (y = x + 1; y < VEXNUM; ++y) {printf("V%d-->V%d weight:%d path:V%d", x, y, metirxD[x][y], x);temp = P[x][y];while (temp != y) {printf("-->V%d", temp);temp = P[temp][y];}printf("-->V%d", y);printf("\n");}}}int main(void){Floyd();Show_Path();return0;}完整代码#include <stdio.h>#include <stdlib.h>void Create_metrixD_P(int** metrixD, int **P ,int VerNum, int EdgNum){int x, y, Weight, edg_count = 0;int i, j, k;for (i = 0; i < VerNum; ++i) {for (j = 0; j < VerNum; ++j) {metrixD[i][j] = INT_MAX;P[i][j] = j;}}while (edg_count < EdgNum) {scanf("%d%d%d", &x, &y, &Weight);metrixD[x - 1][y - 1] = Weight;edg_count++;}}//Floyd algorithmvoid Floyd(int **metirxD, int **P, int VerNum) {int n, x, y, temp = 0;//The triple loop looks for shortest paths and weightsfor (n = 0; n < VerNum; ++n) {for (x = 0; x < VerNum; ++x) {for (y = 0; y < VerNum; ++y) {//The distance between two vertices is compared to the distance through a vertextemp = (metirxD[x][n] == INT_MAX || metirxD[n][y] == INT_MAX) ? INT_MAX : (metirxD[x][n] + metirxD[n][y]);if (temp < metirxD[x][y]) {//Update matrix informationmetirxD[x][y] = temp;P[x][y] = n;}}}}}void Show_metrixD_P(int** metrixD, int **P, int VerNum){int x, y;printf("metrixD:\n");for (x = 0; x < VerNum; ++x) {for (y = 0; y < VerNum; ++y) {if (metrixD[x][y] == INT_MAX) {printf("∞ ");}else {printf("%d ", metrixD[x][y]);}}printf("\n");}printf("P:\n");for (x = 0; x < VerNum; ++x) {for (y = 0; y < VerNum; ++y) {printf("%d ", P[x][y]);}printf("\n");}}void Show_Path(int **metirxD, int **P, int VerNum) {int x, y, temp = 0;//Output the shortest path between two verticesfor (x = 0; x < VerNum - 1; ++x) {for (y = x + 1; y < VerNum; ++y) {printf("V%d-->V%d weight:%d path:V%d", x, y, metirxD[x][y], x); temp = P[x][y];while (temp != y) {printf("-->V%d", temp);temp = P[temp][y];}printf("-->V%d", y);printf("\n");}}}int main(void){int VerNum, EdgNum, i;int** metrixD, ** P;printf("Enter the number of vertices and edges:");scanf("%d%d", &VerNum, &EdgNum);metrixD = (int**)malloc(VerNum * sizeof(int));P = (int**)malloc(VerNum * sizeof(int));for (i = 0; i < VerNum; ++i) {metrixD[i] = (int*)malloc(VerNum * sizeof(int));P[i] = (int*)malloc(VerNum * sizeof(int));}printf("Input vertices and weights:");Create_metrixD_P(metrixD, P, VerNum, EdgNum);Floyd(metrixD, P, VerNum);Show_metrixD_P(metrixD, P, VerNum);Show_Path(metrixD, P, VerNum);for (i = 0; i < VerNum; ++i) {free(metrixD[i]);free(P[i]);}free(metrixD);free(P);return0;}。