floyd算法学习笔记
算法思路
路径矩阵
时间复杂度与空间复杂度
时间复杂度:因为核心算法是采用松弛法的三个for循环,因此时间复杂度为O(n^3)
空间复杂度:整个算法空间消耗是一个n*n的矩阵,因此其空间复杂度为O(n^2)
C++代码
// floyd.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include"iostream"
#include"fstream"
#define maxlen 20
#define maximum 100
using namespace std;
typedef struct graph
{
int vertex;
int edge;
int matrix[maxlen][maxlen];
};
int _tmain(int argc, _TCHAR* argv[])
{
ofstream outwrite;
outwrite.open("h.txt",ios::app|ios::out);
outwrite<<"welcome to the graph world!\n";
outwrite<<"the initial matrix is:\n";
int vertexnumber;
int edgenumber;
int beginning,ending,weight;
int mindistance[maxlen][maxlen];
int interval[maxlen][maxlen];
graph floydgraph;
cout<<"welcome to the graph world!"<<endl;
cout<<"input the number of the vertex: ";
cin>>vertexnumber;
cout<<"input the number of the edge: ";
cin>>edgenumber;
for (int i = 0; i < vertexnumber; i++)
{
for (int j = 0; j < vertexnumber; j++)
{
floydgraph.matrix[i][j]=maximum;
}
}
for (int i = 0; i <edgenumber; i++)
{
cout<<"please input the beginning index: ";
cin>>beginning;
cout<<"please input the ending index: ";
cin>>ending;
cout<<"please input the distance of the two dot: ";
cin>>weight;
floydgraph.matrix[beginning][ending]=weight;
}
for (int i = 0; i <vertexnumber; i++)
{
for (int j = 0; j < vertexnumber; j++)
{
mindistance[i][j]=floydgraph.matrix[i][j];
outwrite<<floydgraph.matrix[i][j]<<"\t";
interval[i][j]=-1;
}
outwrite<<"\n";
}
for (int k = 0; k <vertexnumber; k++)
{
for (int i = 0; i < vertexnumber; i++)
{
for (int j = 0; j < vertexnumber; j++)
{
if(mindistance[i][j]>mindistance[i][k]+mindistance[k][j])
{
mindistance[i][j]=mindistance[i][k]+mindistance[k][j];
interval[i][j]=k;
}
}
}
}
outwrite<<"\n"<<"after the floyd transition, the matrix is: "<<"\n";
for (int i = 0; i < vertexnumber; i++)
{
for (int j = 0; j < vertexnumber; j++)
{
cout<<"the mindistance between "<<i<<" and "<<j <<" is: ";
cout<<mindistance[i][j]<<endl;
cout<<"the two points pass through the point: "<<interval[i][j];
cout<<endl;
outwrite<<mindistance[i][j]<<"\t";
}
outwrite<<"\n";
}
outwrite<<"\n";
outwrite<<"the points between the beginning point and the ending point is:"<<"\n";
for (int i = 0; i < vertexnumber; i++)
{
for (int j = 0; j < vertexnumber; j++)
{
outwrite<<interval[i][j]<<"\t";
}
outwrite<<"\n";
}
outwrite.close();
getchar();
getchar();
getchar();
return 0;
}
floyd算法学习笔记的更多相关文章
- Johnson算法学习笔记
\(Johnson\)算法学习笔记. 在最短路的学习中,我们曾学习了三种最短路的算法,\(Bellman-Ford\)算法及其队列优化\(SPFA\)算法,\(Dijkstra\)算法.这些算法可以快 ...
- Johnson 全源最短路径算法学习笔记
Johnson 全源最短路径算法学习笔记 如果你希望得到带互动的极简文字体验,请点这里 我们来学习johnson Johnson 算法是一种在边加权有向图中找到所有顶点对之间最短路径的方法.它允许一些 ...
- C / C++算法学习笔记(8)-SHELL排序
原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...
- Manacher算法学习笔记 | LeetCode#5
Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...
- 某科学的PID算法学习笔记
最近,在某社团的要求下,自学了PID算法.学完后,深切地感受到PID算法之强大.PID算法应用广泛,比如加热器.平衡车.无人机等等,是自动控制理论中比较容易理解但十分重要的算法. 下面是博主学习过程中 ...
- 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法
图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...
- 算法学习笔记——sort 和 qsort 提供的快速排序
这里存放的是笔者在学习算法和数据结构时相关的学习笔记,记录了笔者通过网络和书籍资料中学习到的知识点和技巧,在供自己学习和反思的同时为有需要的人提供一定的思路和帮助. 从排序开始 基本的排序算法包括冒泡 ...
- R语言实现关联规则与推荐算法(学习笔记)
R语言实现关联规则 笔者前言:以前在网上遇到很多很好的关联规则的案例,最近看到一个更好的,于是便学习一下,写个学习笔记. 1 1 0 0 2 1 1 0 0 3 1 1 0 1 4 0 0 0 0 5 ...
- 二次剩余Cipolla算法学习笔记
对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...
随机推荐
- ASP.NET 5运行时升级到Beta5
在Visual Studio 2015 RTM和Windows 10正式发布之前,微软把开源.NET升级到了beta5,带来了一些增强和改变.和Visual Studio 2015 RC一起安装的AS ...
- 认识ASP.NET 5项目结构和项目文件xproj
ASP.NET 5 在项目结构上做了很大的改变,我们以前熟悉的目录结构与项目文件内容都不太一样了,本篇文章带大家了解 ASP.NET 5 到底跟以前有哪些不一样的地方. 我们先用 Visual Stu ...
- MySql 死锁时的一种解决办法
转自:http://blog.csdn.net/mchdba/article/details/38313881 之前也遇到一次,今天又遇到了这个问题,所以这次必须解决,网上找到这篇文章帮了大忙,方便以 ...
- 【腾讯Bugly干货分享】移动客户端中高效使用SQLite
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57b57f2a0703f7d31b9a3932 作者:赵丰 导语 iOS 程序能 ...
- MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN
在Membership系列的最后一篇引入了ASP.NET Identity,看到大家对它还是挺感兴趣的,于是来一篇详解登录原理的文章.本文会涉及到Claims-based(基于声明)的认证,我们会详细 ...
- ASP.NET MVC 5 Web编程3 -- Controller的应用及扩展
Controller基础 一. 访问修饰符 1.1 类的访问修饰符 Controller类的访问修饰符必须是public,url才能被拦截. internal能编译通过,但无法拦截url请求.priv ...
- ABP教程-通过ABPboilerplate模版创建项目
开篇说明: 此篇博客是属于半教程博客,为什么说是半教程呢.因为我不会打算说什么理论性的东西,没必要.要看理论性的资料以及基础信息,请前往tkb至简和@阳光铭睿的博客查看文档资料. 开发环境: vs20 ...
- ABP源码分析三十六:ABP.Web.Api
这里的内容和ABP 动态webapi没有关系.除了动态webapi,ABP必然是支持使用传统的webApi.ABP.Web.Api模块中实现了一些同意的基础功能,以方便我们创建和使用asp.net w ...
- Git : SSH 协议服务器
SSH 协议用于为 Git 提供远程读写操作,是远程写操作的标准服务. SSH协议语法格式 对于拥有 shell 登录权限的用户账号,可以用下面的语法访问 Git 版本库: 语法 1 : ssh:// ...
- 读书笔记--SQL必知必会18--视图
读书笔记--SQL必知必会18--视图 18.1 视图 视图是虚拟的表,只包含使用时动态检索数据的查询. 也就是说作为视图,它不包含任何列和数据,包含的是一个查询. 18.1.1 为什么使用视图 重用 ...