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\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...
随机推荐
- 分布式存储 FastDFS-5.0.5线上搭建
前言: 由于公司项目需要,最近开始学习一下分布式存储相关知识,确定使用FastDFS这个开源工具.学习之初,自己利用VMware虚拟机搭建了一个5台机器的集群,摸清了安装过程中可能出现的问 ...
- subline text3 使用总结
安装:http://www.sublimetext.com/3 插件扩展: 安装package control组件 按Ctrl+`调出console(注:安装有QQ输入法的这个快捷键会有冲突的,输入 ...
- idea打包jar的多种方式
这里总结出用IDEA打包jar包的多种方式,以后的项目打包Jar包可以参考如下形式: 用IDEA自带的打包形式 用Maven插件maven-shade-plugin打包 用Maven插件maven-a ...
- 简单的例子了解自定义ViewGroup(一)
在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...
- C#服务器获取客户端IP地址以及归属地探秘
背景:博主本是一位Windows桌面应用程序开发工程师,对网络通信一知半解.一日老婆逛完某宝,问:"为什么他们知道我的地址呢,他们是怎么获取我的地址的呢?" 顺着这个问题我们的探秘 ...
- python学习 正则表达式
一.re 模块中 1.re.match #从开始位置开始匹配,如果开头没有match()就返回none 语法:re.match(pattern, string, flags=0) pattern 匹配 ...
- 锋利的jQuery--表单等(读书笔记三)
1.input元素中的多选的,单选,不选,涉及属性checked 2.select元素中的选中,涉及selected 3.表单的验证 4.表格隔行变色 $("tr:odd&quo ...
- StructureMap 代码分析之Widget 之Registry 分析 (1)
说句实话,本人基本上没用过Structuremap,但是这次居然开始看源码了,不得不为自己点个赞.Structuremap有很多的类,其中有一个叫做Widget的概念.那么什么是Widget呢?要明白 ...
- 项目积累(三)CSS
公司不是专门做网站的,偶尔会接到客户让修改前端,有时候和让头疼,自己浏览器兼容问题处理不好. 慢慢积累吧. 先贴出来一些前端代码吧,如下: <div class="test" ...
- JavaScript 随机数
JavaScript内置函数random(seed)可以产生[0,1)之间的随机数,若想要生成其它范围的随机数该如何做呢? 生成任意范围的随机数 //生成[100,120)之间的随机数 Math.fl ...