#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
//图节点
typedef struct VertexNode
{
char name;
VertexNode *p;
}Vertex,*pVertex;
//图
typedef struct
{
int vn;
int **E;
pVertex *V;
}Graph,*pGraph;
//根据算法导论 图25-1 初始化图
pGraph initGraph()
{
pGraph g=(pGraph)malloc(sizeof(Graph));
g->vn=5;
pVertex v1=(pVertex)malloc(sizeof(Vertex));
v1->name='1';
v1->p=NULL;
pVertex v2=(pVertex)malloc(sizeof(Vertex));
v2->name='2';
v2->p=NULL;
pVertex v3=(pVertex)malloc(sizeof(Vertex));
v3->name='3';
v3->p=NULL;
pVertex v4=(pVertex)malloc(sizeof(Vertex));
v4->name='4';
v4->p=NULL;
pVertex v5=(pVertex)malloc(sizeof(Vertex));
v5->name='5';
v5->p=NULL;
g->V=(pVertex*)malloc(g->vn*sizeof(pVertex));
g->V[0]=v1;
g->V[1]=v2;
g->V[2]=v3;
g->V[3]=v4;
g->V[4]=v5;
g->E = (int**)malloc(g->vn*sizeof(int*));
for(int i=0;i<g->vn;i++)
{
g->E[i]=(int*)malloc(g->vn*sizeof(int));
}
for(int i=0;i<g->vn;i++)
{
for(int j=0;j<g->vn;j++)
{
if(i==j)
g->E[i][j]=0;
else
g->E[i][j]=INT_MAX;
}
}
g->E[0][1]=3;
g->E[0][2]=8;
g->E[0][4]=-4;
g->E[1][3]=1;
g->E[1][4]=7;
g->E[2][1]=4;
g->E[3][2]=-5;
g->E[3][0]=2;
g->E[4][3]=6;
return g;
}
int** FloydWarshall(int **E,int n)
{
int**D = (int**)malloc(n*sizeof(int*));
for(int i=0;i<n;i++)
{
D[i]=(int*)malloc(n*sizeof(int));
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
D[i][j]=E[i][j];
}
}
for(int k=0;k<n;k++)
{
int**L = (int**)malloc(n*sizeof(int*));
for(int i=0;i<n;i++)
{
L[i]=(int*)malloc(n*sizeof(int));
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
L[i][j]=D[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int sum;
if(L[i][k]==INT_MAX || L[k][j]==INT_MAX)
sum=INT_MAX;
else
sum=L[i][k]+L[k][j];
if(L[i][j]<sum)
D[i][j]=L[i][j];
else
D[i][j]=sum;
}
}
for(int i=0;i<n;i++)
{
free(L[i]);
}
free(L);
}
return D;
}
void printD(int **D,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(D[i][j]<INT_MAX)
printf("%2d",D[i][j]);
else
printf("%2c",'*');
}
printf("\n");
}
}
void main()
{
pGraph g=initGraph();
int **D=FloydWarshall(g->E,g->vn);
printD(D,g->vn);
getchar();
}相关推荐
所有点对最短路径
NULL 博文链接:https://lisajoy512.iteye.com/blog/1238779
所有结点对的最短路径问题:本章对于给出信息的方式的不同 最短路径和矩阵乘法
显然,对于问题:所有结点对的最短路径问题,我们需要找到图中任何两个结点间的最短路径。同时,我们能轻易得对上一章所学的Bellman_Ford或是Dijkstra算法对每个结点都调用一次而得出结果——但显然,将会浪费很多时间。 以Dijkstra算法为例,若实现它的方式为数组实现,那么Extract会搜索整个数组,消耗O(|V|)的时间——加上外层嵌套的while循环,同时处理边所用到的时间为|E|,那么算法运行的总时间为O(|V|² + |E|) = O(|V|²)。而调用 n 次,也就需要O(|V|³)
CRC校验查表法详解
CRC校验的查表法理解算法原理基础计算查表法原理及计算查表法中的表CRC校验中数据逆序CRC校验中的其它参数 算法原理 CRC(Cyclic Redundance Check)循环冗余校验,是基于某个多项式计算出一组校验码,用于核对数据传输过程中是否被更改或传输错误 基础计算 假设需要检验的数据 g :10100111,这串二进制码可以表示为代数多项式g(x) = x^7 + x^5 + x^2 + x +1, 而作为除数的多项式 h(x) 可以自由选择或者按照国际标准选择,一般h(x)的阶数如果是m
847. 访问所有节点的最短路径
847. 访问所有节点的最短路径
麒麟银河操作系统V10部署ffmpeg(也能用于Linux系统)
部署ffmpeg用来处理视频的各种操作,如编码解码,格式转换,剪辑,处理实时视频流,音频流,加滤镜等
ENSP实验一:防火墙基础配置
配置client(内网)、FTP Server(外网)的IP地址。进入防火墙,输入密码:默认为admin@123。
图论(六):所有节点对的最短路径
所有节点对之间的最短路问题:给定一个加权有向图G=(V,E),对于每一对结点(u,v)∈V(u,v)\in V(u,v)∈V,找到从u到v的最短路径。 Input:用邻接矩阵表示有向加权图。 Output:一个n×n的矩阵D=(dij)D=(d_{ij})D=(dij),dijd_{ij}dij表示从i结点到j节点的最短路径。 一种简单的思路是对集合V中所有的结点使用单源最短路算法,这样就能求得所有结点之间的最短路径,时间复杂度为原时间复杂度×n(n为结点个数),但Dijkstra单源最短
离散系统傅里叶变换的常用结论证明
本文介绍了离散系统傅里叶变换的三个重要结论:1)时域卷积对应频域相乘(公式1-3);2)时域相乘对应频域卷积(公式4-6);3)帕塞瓦尔定理(公式7)及其广义形式,证明信号能量在变换域保持不变。文末提供了MATLAB卷积运算演示程序,通过动画展示离散信号卷积过程(翻转、平移、相乘、累加),并以方波信号为例给出运行结果图。该程序直观呈现了卷积运算的步骤与结果。
所有结点对的最短路径问题(All-Paris Shortest Paths)
《算法导论》所有结点对的最短路径问题学习笔记我们已经讨论了单源最短路径问题,在这篇博客中我们将求解一个图中所有节点之间的最短路径。一、最短路径和矩阵乘法分析可知在一个图中一条最短路径的所有子路径都是最短路径,很容易想到我们可以使用动态规划的思路来解决这个问题。设为从i到j的一条最短路径,且这条路径最多包含m条边。我们可以得到以下的递推公式:依据上面的递推公式我们可以编写代码如下,注意这里实现的是已...
数字设计实验:自动售货机
本文旨在介绍数字设计实验:自动售货机的设计思路和实现代码
Trae的付费之路还挺艰难,茫茫多的人想付付不了(附解决办法)
不得不说,Trae国际版这个付费其实整的挺复杂的,但没办法,毕竟有政策限制,付费成功后,你就是尊贵的Pro用户啦,使用Claude 4不排队还是挺爽的,而且600次其实不算少了,有一个小技巧,简单的问题就把模型切到Gemini-2.5-flag,因为使用这个模型是没有次数限制的。其次付费的时候有个ZIP Code,这东西也是个迷之存在,有的地区节点需要填,有的不需要,如果你选到了不需要填的节点,那很好,付费就行了,如果需要填,可以填一个51800试试。
【数据结构】最短路径算法实现(Dijkstra(迪克斯特拉),FloydWarshall(弗洛伊德) )
最短路径问题:从在带权有向图G中的某一顶点出发,找出一条通往另一顶点的最短路径,最短也就是沿路径各边的权值总和达到最小。单源最短路径问题:给定一个图G = ( V , E ) G=(V,E)G=(V,E),求源结点s ∈ V s∈Vs∈V到图中每个结点v ∈ V v∈Vv∈V的最短路径。
【Envi风暴】Envi5.6安装图文教程(附Envi5.6完整版下载)
【Envi风暴】Envi5.6与应用商店app store安装图文教程(附Envi5.6完整版下载)从文末网盘下载完整的ENVI5.6安装包,如下所示:双击envi56-win.exe,开始安装。将f__x64下的两个补丁文件拷贝到安装路径。将f__x86下的两个补丁文件拷贝到安装路径。
数据结构 —— FloydWarshall算法
数据结构 —— FloydWarshall算法
【RT-DETR有效改进】全新的SOATA轻量化下采样操作ADown(轻量又涨点,附手撕结构图)
本文给大家带来的改进机制是利用2024/02/21号最新发布的YOLOv9其中提出的ADown模块来改进我们的Conv模块,其中YOLOv9针对于这个模块并没有介绍,只是在其项目文件中用到了,我将其整理出来用于我们的YOLOv8的项目,经过实验我发现该卷积模块(作为下采样模块)首先可以大幅度降低参数值,其次其精度上也有很高的提升,同时本文的内容目前网络上并无其它人总结(也是最新发布的,可以说BUff是集满了)大家可以尝试以下在自己数据集上的效果。
WINCC7.3破解文件
WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!
【算法导论】【Floyd-Warshall 算法】每对节点之间的最短路径
所有结点对的最短路径问题 目录 所有结点对的最短路径问题 计算最短路径权重 - Floyd 算法 邻接矩阵 最短路径的结构 所有结点对最短路径问题的一个递归解 自底向上计算最短路径权重 打印所有最短路径 前驱矩阵 打印最短路径 参考 问题:有一个带权有向图 G = (V, E),V 为图的顶点集合,E 为边的集合,权重函数为 w:E → R,该函数将边映射到
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
最短路径-FloydWarshall
最短路径Floyd Warshall算法
IEM模型,地表粗糙度模拟
利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟
java算法导论_java算法导论之FloydWarshall算法实现代码
摘要: 算法导论之FloydWarshall算法求一个图中任意两点之间的最短路径FloydWarshall算法是通过动态规划来计算任意两点之间的最短路径如果普通求最短路径,可以对图进行V次(顶点数)BellmanFord算法。 这样的话时间复杂度为EV^2如果是稀疏图,则近似于V^3但是如果是密集图,则时间复杂度会近似达到V^4,这种情况需要优化,这里FloydWarshall通过动态规划进行优化...
【数据结构】基础:图的最短路径问题(附C++源码)
摘要:将会在数据结构专题中开展关于图论的内容介绍,其中包括四部分,分别为图的概念与实现、图的遍历、图的最小生成树以及图的最短路径问题。本文介绍图的最短路径问题,分别为Dijkstra算法、BellmanFord算法和FloydWarshall算法,从算法的概述内容出发,进行实例介绍,在进行代码的实现说明,最后对其进行测试。
JAVA:实现FloydWarshall任意两点间的最短路径的算法(附带源码)
JAVA:实现FloydWarshall任意两点间的最短路径的算法(附带源码)
数据结构——图及其C++实现 多源最短路径 FloydWarshall算法
前两篇学习的Dijkstra算法和Bellman-Ford算法都是用来求解图的单源最短路径,即从图中指定的一个源点出发到图中其他任意顶点的最短路径。Dijkstra算法不能求解带有负权重的图的最短路径,而Bellman-Ford算法弥补了这个缺点。本篇文章再来见识一下一个求解多源最短路径的算法——Floyd-Warshall算法。多源最短路径–Floyd-Warshall算法Floyd-Warshall算法是一种解决多源最短路径问题(任意两点间的最短路径)的算法。
算法导论——所有点对的最短路径:FloydWarshall算法
2019独角兽企业重金招聘Python工程师标准>>> ...
【算法】图的应用之求两个城市间的最短路径--FloydWarshall算法
【算法】图的应用之求两个城市间的最短路径
数据结构 之 【图的最短路径】(Dijstra、BellmanFord、FloydWarShall算法实现)
算法时间复杂度空间复杂度适用场景DijkstraO(V²)非负权图,稀疏图优先堆优化BellmanO(V²)含负权边单源问题,邻接表优化FloydO(V³)O(V²)多源最短路径,小规模图。
用Java编辑实现warshall算法_摘要: 算法导论之FloydWarshall算法求一个图中任意两点之间的最短路径 FloydWarshall算法是通过动态规划来计算任意两点...
摘要: 算法导论之FloydWarshall算法求一个图中任意两点之间的最短路径FloydWarshall算法是通过动态规划来计算任意两点之间的最短路径如果普通求最短路径,可以对图进行V次(顶点数)BellmanFord算法。 这样的话时间复杂度为EV^2如果是稀疏图,则近似于V^3但是如果是密集图,则时间复杂度会近似达到V^4,这种情况需要优化,这里FloydWarshall通过动态规划进行优化...
沃舍尔算法_最短路径算法1—最简单暴力的算法 FloydWarshall
Floyd-warshall算法是所有最短路径算法中最好实现的一种代码,他的核心代码只有五行for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(e[i][j]>e[i][k]+e[k][j])e[i][j]=e[i][k]+e[k][j];它能解决的问题叫做多源最短路径问题,算法进行过后可得到任...
722



被折叠的 条评论
为什么被折叠?



