Floyd(弗洛伊德)算法(C语言)
转载:https://blog.csdn.net/qq_35644234/article/details/60875818
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 = ;
int i, j, k; for (i = ; i < VerNum; ++i) {
for (j = ; 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 - ][y - ] = Weight;
edg_count++;
}
} //Floyd algorithm
void Floyd(int **metirxD, int **P, int VerNum) {
int n, x, y, temp = ;
//The triple loop looks for shortest paths and weights
for (n = ; n < VerNum; ++n) {
for (x = ; x < VerNum; ++x) {
for (y = ; y < VerNum; ++y) {
//The distance between two vertices is compared to the distance through a vertex
temp = (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 information
metirxD[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 = ; x < VerNum; ++x) {
for (y = ; y < VerNum; ++y) {
if (metrixD[x][y] == INT_MAX) {
printf("∞ ");
}
else {
printf("%d ", metrixD[x][y]);
}
}
printf("\n");
}
printf("P:\n");
for (x = ; x < VerNum; ++x) {
for (y = ; 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 = ; 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 = ; i < VerNum; ++i) {
free(metrixD[i]);
free(P[i]);
}
free(metrixD);
free(P); return ;
}
2.输出顶点之间的最短距离与路径
#include <stdio.h>
#include <stdlib.h> #define VEXNUM 5 //Adjacency matrix: shows the distance between vertices
int metirxD[VEXNUM][VEXNUM] = {
INT_MAX,, , INT_MAX,INT_MAX,
INT_MAX,INT_MAX,, , INT_MAX,
INT_MAX,, INT_MAX,, ,
INT_MAX,INT_MAX,INT_MAX,INT_MAX,,
, INT_MAX,INT_MAX,, INT_MAX
}; //Path: passing vertex between two vertices
int P[VEXNUM][VEXNUM] = {
,,,,,
,,,,,
,,,,,
,,,,,
,,,,
}; //Floyd algorithm
void Floyd() {
int n, x, y, temp = ;
//The triple loop looks for shortest paths and weights
for (n = ; n < VEXNUM; ++n) {
for (x = ; x < VEXNUM; ++x) {
for (y = ; y < VEXNUM; ++y) {
//The distance between two vertices is compared to the distance through a vertex
temp = (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 information
metirxD[x][y] = temp;
P[x][y] = n;
}
}
}
}
} void Show_Path() {
int x, y, temp = ;
//Output the shortest path between two vertices
for (x = ; x < VEXNUM - ; ++x) {
for (y = x + ; 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(); return ;
}
完整代码
#include <stdio.h>
#include <stdlib.h> void Create_metrixD_P(int** metrixD, int **P ,int VerNum, int EdgNum)
{
int x, y, Weight, edg_count = ;
int i, j, k; for (i = ; i < VerNum; ++i) {
for (j = ; 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 - ][y - ] = Weight;
edg_count++;
}
} //Floyd algorithm
void Floyd(int **metirxD, int **P, int VerNum) {
int n, x, y, temp = ;
//The triple loop looks for shortest paths and weights
for (n = ; n < VerNum; ++n) {
for (x = ; x < VerNum; ++x) {
for (y = ; y < VerNum; ++y) {
//The distance between two vertices is compared to the distance through a vertex
temp = (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 information
metirxD[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 = ; x < VerNum; ++x) {
for (y = ; y < VerNum; ++y) {
if (metrixD[x][y] == INT_MAX) {
printf("∞ ");
}
else {
printf("%d ", metrixD[x][y]);
}
}
printf("\n");
}
printf("P:\n");
for (x = ; x < VerNum; ++x) {
for (y = ; y < VerNum; ++y) {
printf("%d ", P[x][y]);
}
printf("\n");
}
} void Show_Path(int **metirxD, int **P, int VerNum) {
int x, y, temp = ;
//Output the shortest path between two vertices
for (x = ; x < VerNum - ; ++x) {
for (y = x + ; 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 = ; 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 = ; i < VerNum; ++i) {
free(metrixD[i]);
free(P[i]);
}
free(metrixD);
free(P); return ;
}
Floyd(弗洛伊德)算法(C语言)的更多相关文章
- 经典问题----最短路径(Floyd弗洛伊德算法)(HDU2066)
问题简介: 给定T条路,S个起点,D个终点,求最短的起点到终点的距离. 思路简介: 弗洛伊德算法即先以a作为中转点,再以a.b作为中转点,直到所有的点都做过中转点,求得所有点到其他点的最短路径,Flo ...
- Floyd弗洛伊德算法
先看懂如何使用 用Java实现一个地铁票价计算程序 String station = "A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A1 ...
- 数据结构C语言版 弗洛伊德算法实现
/* 数据结构C语言版 弗洛伊德算法 P191 编译环境:Dev-C++ 4.9.9.2 */ #include <stdio.h>#include <limits.h> # ...
- Floyd算法(弗洛伊德算法)
算法描述: Floyd算法又称为弗洛伊德算法,插点法,是一种用于寻找给定的加权图中顶点间最短路径的算法.从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按 ...
- 弗洛伊德算法(Floyd算法)
原博来自http://www.cnblogs.com/skywang12345/ 弗洛伊德算法介绍 和Dijkstra算法一样,弗洛伊德(Floyd)算法也是一种用于寻找给定的加权图中顶点间最短路径的 ...
- 弗洛伊德算法(Floyd )
package com.rao.graph; /** * @author Srao * @className Floyd * @date 2019/12/11 18:43 * @package com ...
- [从今天开始修炼数据结构]图的最短路径 —— 迪杰斯特拉算法和弗洛伊德算法的详解与Java实现
在网图和非网图中,最短路径的含义不同.非网图中边上没有权值,所谓的最短路径,其实就是两顶点之间经过的边数最少的路径:而对于网图来说,最短路径,是指两顶点之间经过的边上权值之和最少的路径,我们称路径上第 ...
- Floyd最短路径算法
看完这篇文章写的小程序,Floyd最短路径算法,求从一个点到另一个点的最短距离,中间可以经过其他任意个点.三个for循环,从i到j依次经过k的最短距离,最外层for循环是经过点K,内部两个循环是从i( ...
- 【转】位置式、增量式PID算法C语言实现
位置式.增量式PID算法C语言实现 芯片:STM32F107VC 编译器:KEIL4 作者:SY 日期:2017-9-21 15:29:19 概述 PID 算法是一种工控领域常见的控制算法,用于闭环反 ...
随机推荐
- 触发器insert
USE [stalentzx]GO/****** Object: Trigger [dbo].[GZ_HISTORY_INSERT] Script Date: 2019/12/24 13:11:40 ...
- 使用distinct消除重复记录的同时又能选取多个字段值
需求是:我要消除name字段值重复的记录,同时又要得到id字段的值,其中id是自增字段. select distinct name from t1 能消除重复记录,但只能取一个字段,现在要同时取id, ...
- height设置百分比的条件
很多时候我们在给height设置百分比的时候不起作用, 这时候就要来谈谈什么情况下才起作用了 1)所有父级元素必须有高度: 2)必须是块级元素,行内元素不起作用: 3)ie9 以下 使用 positi ...
- [转载]Android: 如何实现ScrollView中含有ListView?
原文地址:Android: 如何实现ScrollView中含有ListView?作者:mailofzxf ListView本身就含有ScrollView,因此把ListView放到ScrollView ...
- BigDecimal保留两位小数,或指定精确的位数
public static void main(String[] args) { BigDecimal t1 = new BigDecimal("3.15289"); BigDec ...
- oracle各服务说明及cmd启动启动命令
成功安装Oracle 11g后,共有7个服务,一.这七个服务的含义分别为:1. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写入服务,VSS(Volume Sh ...
- sqlserver 查询当前阻塞进程 并杀掉
select * from master.dbo.sysprocesses where DB_NAME(dbid)=’test’ and spid<>@@SPID 看看阻塞的进程 然后ki ...
- 利用Javascript解决HTML大数据列表引起的网页加载慢/卡死问题。
在一些网页应用中,有时会碰到一个超级巨大的列表,成千上万行,这时大部份浏览器解析起来就非常痛苦了(有可能直接卡死). 也许你们会说可以分页或动态加载啊?但是有可能需求不允许分页,动态加载?网络的延迟也 ...
- 服务器上安装搭建node环境
一.版本 : node v4.4.3 npm v2.15.1 pm2 v1.1.3 express v4.13.4 二.node安装与环境配置 新建node安装目录,与node项目目录 ...
- 在window 10查看一下指定命令行工具所在的位置
很久之前安装过node,现在想要给node升级,通过where命令查看一下node的安装位置