多源最短路径( Floyd算法)JAVA实现

多源最短路径--Floyd-Warshall算法 上一篇文章《所有节点对的最短路径问题 》中我们介绍到了Floyd-Warshall算法,由于代码较长,没有放到上一篇文章中,我们在这里单独给出Java实现的代码。 import java.util.*; class Floyd_Warshall { static final int MAX = 20; //最大点数 s 阅读详情

多源最短路径( Floyd算法)JAVA实现

package floyd;


public class Graph {	
	final int max=100;
	/*
	 * 顶点节点
	 */
	public class VexNode{
			int adjvex;
			int data;
	}
	
	VexNode[] vexNodes;
	int[] thevexs; //顶点集合
	int[][] edges = new int[max][max]; //边集合
	
	
	/*
	 * 创建图
	 */
	public void createGraph(Graph graph,int[][] A,int[] vexs) {
		thevexs=vexs;
		for (int i = 0; i < vexs.length; i++) {
			  for (int j = 0; j < vexs.length; j++) {
				  graph.edges[i][j] = A[i][j];
			}
		}
	}
	
	/*
	 * 输出图
	 */
	public void printGraph(Graph graph) {
		for (int i = 0; i < graph.thevexs.length; i++) {
			for (int j = 0; j < graph.thevexs.length; j++) {
				//没有路径则输出/
				if (graph.edges[i][j]==1000) {
					System.out.printf("%4s","/");
					 
				}else {
					System.out.printf("%4d",graph.edges[i][j]);
				}
				
			}
			System.out.println("\n");
		}
	}

}
package floyd;

public class Floyd {
	final int max = 100;
	final int INF = 1000;
	public void DispAllPath(Graph graph,int[][] A,int[][] path) {
		int i,j,k,s,d;
		int[] apath = new int[max];
		for (i=0;i<graph.thevexs.length;i++) {
				for(j=0;j<graph.thevexs.length;j++) {
						if (A[i][j] != INF && i!=j) {
							System.out.printf("顶点%d到%d最短路径长度为:%d\t路径:",i,j,A[i][j]);
							k=path[i][j];
							d=0;
							apath[d] = j;
							while (k!= -1&& k!=i) {
								d++;
								apath[d] = k;
								k = path[i][k];
							}
							d++;
							apath[d] = i;
							System.out.printf("%d", apath[d]);
							for(s=d-1;s>=0;s--) {
								System.out.printf("->%d",apath[s]);
							}
							System.out.println("\n");
						}
				}
		}
		
	}
	
	
	public void Dispdispath(int[][] A,int [][] path,int n,int k) {
		int i,j;
		System.out.printf("\tA[%d]\t\t\t\tpath[%d]\n",k,k);
		for(i=0;i<n;i++) {
				for(j=0;j<n;j++) {
					if (A[i][j]==INF) {
						System.out.printf("%4s", "*");
					} else {
						System.out.printf("%4d",A[i][j]);
					}
				}
				
				for(j=0;j<n;j++) {
					System.out.printf("%3d",path[i][j]);
				}
				System.out.println("\n");
		}
		
		
	}
	
	public Floyd(Graph graph) {
			int[][] A = new int[max][max];
			int[][] path = new int[max][max];
			int i,j,k;
			for(i=0;i<graph.thevexs.length;i++) {
					for(j=0;j<graph.thevexs.length;j++) {
						A[i][j]  = graph.edges[i][j];
						if (i!=j && graph.edges[i][j]<INF) {
							path[i][j] = i; 
						}else {
							path[i][j]=-1;
						}
					}
			}
			Dispdispath(A, path, graph.thevexs.length, -1);
			
			for(k=0;k<graph.thevexs.length;k++) {
				for(i=0;i<graph.thevexs.length;i++) {
					 for(j=0;j<graph.thevexs.length;j++) {
						 	if (A[i][j]>A[i][k]+A[k][j]) {
								A[i][j]=A[i][k]+A[k][j];
								path[i][j] = path[k][j];
							}
					 }
				}
				Dispdispath(A, path,graph.thevexs.length, k);
			}
			DispAllPath(graph, A, path);
			
	}
	
	
	
	
	
}
package floyd;



public class Test {

	public static void main(String[] args) {
		final int INF = 1000;
		int[] vexs = {0,1,2,3,4,5};
		int[][] A ={
				{0,50,10,INF,INF,INF},
				{INF,0,15,50,10,INF},
				{20,INF,0,15,INF,INF},
				{INF,20,INF,0,35,INF},
				{INF,INF,INF,30,0,INF},
				{INF,INF,INF,3,INF,0},
				
		};
		Graph graph = new Graph();
		graph.createGraph(graph, A, vexs);
		graph.printGraph(graph);
		Floyd floyd = new Floyd(graph);
	}

}

多源最短路径( Floyd算法)JAVA实现

package floyd;


public class Graph {	
	final int max=100;
	/*
	 * 顶点节点
	 */
	public class VexNode{
			int adjvex;
			int data;
	}
	
	VexNode[] vexNodes;
	int[] thevexs; //顶点集合
	int[][] edges = new int[max][max]; //边集合
	
	
	/*
	 * 创建图
	 */
	public void createGraph(Graph graph,int[][] A,int[] vexs) {
		thevexs=vexs;
		for (int i = 0; i < vexs.length; i++) {
			  for (int j = 0; j < vexs.length; j++) {
				  graph.edges[i][j] = A[i][j];
			}
		}
	}
	
	/*
	 * 输出图
	 */
	public void printGraph(Graph graph) {
		for (int i = 0; i < graph.thevexs.length; i++) {
			for (int j = 0; j < graph.thevexs.length; j++) {
				//没有路径则输出/
				if (graph.edges[i][j]==1000) {
					System.out.printf("%4s","/");
					 
				}else {
					System.out.printf("%4d",graph.edges[i][j]);
				}
				
			}
			System.out.println("\n");
		}
	}

}
package floyd;

public class Floyd {
	final int max = 100;
	final int INF = 1000;
	public void DispAllPath(Graph graph,int[][] A,int[][] path) {
		int i,j,k,s,d;
		int[] apath = new int[max];
		for (i=0;i<graph.thevexs.length;i++) {
				for(j=0;j<graph.thevexs.length;j++) {
						if (A[i][j] != INF && i!=j) {
							System.out.printf("顶点%d到%d最短路径长度为:%d\t路径:",i,j,A[i][j]);
							k=path[i][j];
							d=0;
							apath[d] = j;
							while (k!= -1&& k!=i) {
								d++;
								apath[d] = k;
								k = path[i][k];
							}
							d++;
							apath[d] = i;
							System.out.printf("%d", apath[d]);
							for(s=d-1;s>=0;s--) {
								System.out.printf("->%d",apath[s]);
							}
							System.out.println("\n");
						}
				}
		}
		
	}
	
	
	public void Dispdispath(int[][] A,int [][] path,int n,int k) {
		int i,j;
		System.out.printf("\tA[%d]\t\t\t\tpath[%d]\n",k,k);
		for(i=0;i<n;i++) {
				for(j=0;j<n;j++) {
					if (A[i][j]==INF) {
						System.out.printf("%4s", "*");
					} else {
						System.out.printf("%4d",A[i][j]);
					}
				}
				
				for(j=0;j<n;j++) {
					System.out.printf("%3d",path[i][j]);
				}
				System.out.println("\n");
		}
		
		
	}
	
	public Floyd(Graph graph) {
			int[][] A = new int[max][max];
			int[][] path = new int[max][max];
			int i,j,k;
			for(i=0;i<graph.thevexs.length;i++) {
					for(j=0;j<graph.thevexs.length;j++) {
						A[i][j]  = graph.edges[i][j];
						if (i!=j && graph.edges[i][j]<INF) {
							path[i][j] = i; 
						}else {
							path[i][j]=-1;
						}
					}
			}
			Dispdispath(A, path, graph.thevexs.length, -1);
			
			for(k=0;k<graph.thevexs.length;k++) {
				for(i=0;i<graph.thevexs.length;i++) {
					 for(j=0;j<graph.thevexs.length;j++) {
						 	if (A[i][j]>A[i][k]+A[k][j]) {
								A[i][j]=A[i][k]+A[k][j];
								path[i][j] = path[k][j];
							}
					 }
				}
				Dispdispath(A, path,graph.thevexs.length, k);
			}
			DispAllPath(graph, A, path);
			
	}
	
	
	
	
	
}
package floyd;



public class Test {

	public static void main(String[] args) {
		final int INF = 1000;
		int[] vexs = {0,1,2,3,4,5};
		int[][] A ={
				{0,50,10,INF,INF,INF},
				{INF,0,15,50,10,INF},
				{20,INF,0,15,INF,INF},
				{INF,20,INF,0,35,INF},
				{INF,INF,INF,30,0,INF},
				{INF,INF,INF,3,INF,0},
				
		};
		Graph graph = new Graph();
		graph.createGraph(graph, A, vexs);
		graph.printGraph(graph);
		Floyd floyd = new Floyd(graph);
	}

}
Java实现Floyd最短路径算法详解与实战 通过合理设计路径输出机制和调试策略,可以显著提升Floyd算法在实际项目中的可用性和稳定性。本章深入探讨了Floyd算法在Java实现中的一些关键问题,包括负权边的处理机制、三层循环的实现逻辑、路径矩阵的更新策略,以及调试与输出技巧。这些内容不仅增强了算法的鲁棒性,也为后续的测试与性能优化打下了坚实基础。 阅读详情

相关推荐

UE5新手必看:如何用蓝图系统快速打造赛车游戏(含Chaos Physics配置)

本文为UE5新手提供使用蓝图系统快速打造赛车游戏的完整指南,涵盖Chaos Physics配置、载具蓝图架构设计、控制逻辑实现等关键步骤。通过详细的实战案例和优化技巧,帮助开发者高效构建具备专业物理反馈的赛车游戏原型。

weixin_29001327的博客 387

java矩阵最小路径和_矩阵中的路径(Java代码实现附思路)

请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。下面给大家具体的分享了3种实现方法和思路。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。假如一条路径经过了矩阵中的某一个格子,那么这条路径不能再进入这个格子。例:矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一...

weixin_35735398的博客 482

提升RAG效果:为何 JSON 格式远胜 Markdown?

在构建强大的 RAG (检索增强生成) 系统时,文档解析是至关重要的第一步。它直接影响着后续的检索效率和生成质量。在众多文档格式中,JSON (JavaScript Object Notation) 格式正逐渐展现出其相对于传统 Markdown 格式的巨大优势。本文将深入探讨 JSON 在 RAG 系统文档解析中的卓越之处,并结合具体案例,让你理解为何 JSON 才是构建下一代智能应用的更优选择。RAG 系统的核心目标是让模型能够利用外部知识库,生成更精准、更贴合实际的答案。

simoncool23的博客 2719

dijkstra 算法_短小精悍的多源最短路径算法—Floyd算法

前言本文同步于公众号[bigsai],专注于数据结构与算法、java、python在图论中,在寻路最短路径中除了Dijkstra算法以外,还有Floyd算法也是非常经典,然而两种算法还是有区别的,Floyd主要计算多源最短路径。在单源正权值最短路径,我们会用Dijkstra算法来求最短路径,并且算法的思想很简单——贪心算法:每次确定最短路径的一个点然后维护(更新)这个点周围点的距离加入预选队列,等...

weixin_39617006的博客 343

防火墙断网后Adobe软件还弹窗“THIS APP HAS BEEN DISABLED”?原来是科学上网导致的问题!用这个简单方法一键解决!

很多小伙伴在使用上网工具的时候时,突然遇到Adobe弹出“THIS APP HAS BEEN DISABLED”的烦人提示,这个非正版弹窗真的让人头疼!即使你之前尝试过,有时也不奏效,究其原因,原来是因为你使用了代理网络,这时Adobe的流量并不是走本地,而是走代理,导致防火墙的拦截无效。不用慌!今天我来教大家如何通过上网工具,轻松禁用Adobe验证的联网流量,彻底解决这个问题!

qq_34093397的博客 3万+

Android 13.0 USB 麦克风音频采集优化:解决延时、杂音和无声问题

摘要:本文针对Android智能会议设备中USB麦克风存在的三大问题(录音延时、概率性杂音和无声)进行了深入分析和优化。通过预填充静音帧降低90%启动延时,分离采集/播放互斥锁消除杂音,采用持久化配置解决无声问题,并新增状态通知机制提升用户体验。优化后测试显示:录音延时从1200-2000ms降至100-200ms,故障率降至0%,CPU占用降低30%。该方案适用于各类USB音频设备和视频会议系统,为低延时音频采集提供了可靠解决方案。

ip0417的博客 230

数据结构(Java)最短路径(迪杰斯特拉、弗洛伊德算法)

迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个结点到其他结点的最短路径。 它的主要特点是以起始点为中心向外层层扩展(广度优先搜索思想),直到扩展到终点为止。 设置出发顶点为v,顶点集合V{v1,v2,vi...},v到V中各顶点的距离构成距离集合Dis,Dis{d1,d2,di...},Dis集合记录着v到图中各顶点的距离(到自身可以看作0,v到vi距离对应为di) 算法的通俗理解: 1.先定义 距离数组 和 已访问数组,并且初始化:与 G 点相关的权值照常填

萌萌虎的博客 5791

第一讲:halcon模板匹配捆绑测量矩形检测芯片针脚

使用halcon模板匹配捆绑测量矩形,检测位置发生变换的芯片针脚缺陷。

uiopmn1的博客 2995

java 多级图的最短路径

求最短路径众所周知有Dijistra算法、Bellman-ford等,除了这些算法,用动态规划也可以求出最短路径,时间复杂度为O(n^2), 跟没有优化的Dijistra算法一样(优化后的Dijistra算法时间复杂度为O((m+n)lgn))。 左侧1-15表示前一个节点,最上面一行1-15表示后一个节点,记这个图的矩阵为P,那么P[0][1...

weixin_30690833的博客 256

故障诊断 | CNN-BiGRU-Attention故障诊断

故障诊断 | CNN-BiGRU-Attention故障诊断

机器学习之心的博客,关注并私信文章链接,获取对应文章源码和数据。 675

手把手教你学PCIE(5.1)--pcie固件初始化的硬件初始化

PCIe 固件初始化的硬件初始化是一个复杂但至关重要的过程。通过正确配置 PCIe 控制器和相关硬件,可以确保系统的稳定性和性能。以上步骤和示例代码提供了一个基本的框架,实际开发中可能需要根据具体的硬件和需求进行调整和优化。

MHD0815的博客 1689

Java 动态规划实现最小路径和计算

题:最小路径和 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。 示例: 输入: [ [1,3,1], [1,5,1], [4,2,1] ] 输出: 7 解释: 因为路径 1→3→1→1→1 的总和最小。 实现: // 最小路径和 public static int minPathSum(int[][] arr){ if (arr.length == 0){ //.

韦的博客 604

海思3559av100平台lan9500模块调试记录

1. 前言 通过USB2.0接口扩展一路百兆虚拟网口 2. 配置lan9500驱动 make ARCH=arm64 menuconfig 开启LAN95XX驱动,编译进内核 保存内核配置 3. 编译内核 make ARCH=arm64 CROSS_COMPILE=aarch64-himix100-linux- uImage -j 20 cd ../../arm-trusted-firmware/arm-trusted-firmware ./mk.sh cp ...

jzwjzw19900922的博客 909

floyd算法(多源最短路径) Java实现

这里记录一下java实现的floyd算法模板。 package Graph; public class Floyd { //无穷大 public static int inf = Integer.MAX_VALUE; public static void main(String[] args) { // 图数据 Data [u, v, cost] ...

Aiven 776

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

弗洛伊德Floyd算法

Floyd算法:用于多源最短路径的求解,算出来的是所有节点到其余各节点之间的最短距离。 该算法的思路是:首先初始化距离矩阵,然后从第一个点开始逐渐更新矩阵点值。d[i][j]表示从i点到j点的距离。第k次更新时,判断d[i][k]+d[k][j]与d[i][j]的大小,如果前者小,则更新这个值,否则不变。 给一个例子: 具体的Floyd实现算法如下: import java.util.Scanne...

小小核桃 1745

深度学习工具包 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');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Floyd多源最短路径算法

import java.util.Scanner; /** * Created by wjz on 2017/3/25. * 多源最短路径算法:Floyd算法 */ public class Main4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in);

awjzb的博客 467

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

最短路径[floyd算法]-----视频讲解+代码实现

求最短路径,一般有三种方法:单源最短路径--Dijkstra算法单源最短路径--Bellman-Ford算法(少考)多源最短路径--的意思是只能从指定位置开始,求其他位置的最短路径。则不同,可以求任意两点间的最短路径。本文章主要讲解floyd算法。

HYY的博客 1314

清华突破计算机科学60年瓶颈,最短路径算法分析 Java实现 小白入手 (七)

Floyd-Warshall算法是一种用于计算图中所有顶点对之间最短路径的动态规划算法。该算法通过三重循环实现:外层循环遍历所有可能的中间顶点k,内层两重循环计算每对顶点i和j通过k的路径是否更短。算法时间复杂度为O(V^3),适用于包含负权边但不含负权环的图。示例代码展示了如何实现该算法,包括初始化距离矩阵、核心计算逻辑以及结果输出功能。对于不连通的顶点对,使用INF表示无穷大距离。最后输出的是一个包含所有顶点对最短距离的矩阵。

华为开发者布道师,天府英才计划,致力于大数据+AI 的应用创新,助力企业与个人AI创新赋能。 292

Floyd-Warshall

FloydWarshall 算法用于求解任意两个点之间的最短路径问题,被称为“多源最短路径问题”他的时间复杂度为O(N*N*N)。实现起来非常容易。 使用时搞清楚三次循环分别代表什么。public class FloydWarshall { public void floydwarshall(int e[][]){ for(int k=0;k<e.length;k++)

做 300

数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。

最短路径的算法有两个,Dijkstra算法 和 Floyd算法。Dijkstra算法 解决的是单源 最短路径问题。Floyd算法解决的是多源 最短路径问题,并且可以处理负权图。今天要讲的就是Dijkstra算法。(大写的i),进java交流群讨论互联网+技术。可索要PPT等资料。其他资料,建议先看本篇博客。Dijkstra算法和Floyd算法:https://blog.youkuaiyun.com/weixin_43872728/article/details/100662957。

冯安晨 959

【UCB CS 61B SP24】Lecture 24 - Shortest Paths: Dijkstra, Bellman-Ford, SPFA, Floyd

本文详细讲解了用于求解单源最短路问题的 Dijkstra、Bellman-Ford、SPFA 算法以及用于求解多源最短路问题的 Floyd 算法,并使用 Java 逐一实现这些算法。

一只快乐的小蒟蒻~ 1208

图的最短路径算法-java版

目录 最短路径 Floyd(弗洛伊德)算法 简介 算法思想 样例 复杂度 Dijkstra算法 简介 算法思想 样例 复杂度 java实现 图的基础代码 Floyd算法 dijkstra算法 测试 最短路径 所谓最短路径问题是指:如果从图中某一顶点(源点)到达另一顶点(终点)的路径可能不止一条,如何找到一条路径使得沿此路径上各边的权值总和(称为路径长度)达到最小...

今天吃了吗的博客 1万+

最短路径算法

Dijkstra算法是用来计算一个结点到其他结点的最短路径的算法。可以用来解决计算图论中单源最短路径问题。

一条漂流鱼 652

JAVA弗洛伊德算法_java实现Floyd算法

Floyd算法:用于多源最短路径的求解,算出来的是所有的节点到其余各节点之间的最短距离。该算法的思路是:首先初始化距离矩阵,然后从第一个点开始逐渐更新矩阵点值。d[i][j]表示从i点到j点的距离。第k次更新时,判断d[i][k]+d[k][j]与d[i][j]的大小,如果前者小,则更新这个值,否则不变。给一个例子:具体的floyd实现算法如下[java] view plain copypacka...

weixin_42517623的博客 384

Floyd 算法 和 非递归算法寻路

Floyd 算法是用于多源的最短路径的算法,给予动态规划的思想实现。 Floyd 算法的最短路径的寻找需要借助递归函数,但是一些时候,递归函数无法使用。因此,我将其改为了非递归函数。非递归函数使用栈作为数据存储器,查询路径(即最短路径的所有中间节点,按序输出)。 代码如下: 注:以下代码使用Java和spark混合实现,其中flod函数实现了Floyd算法。 另外两个函数分别

seekerhit的博客 1173

[leetcode]——四种图的最短路径算法

前言 以leetcode 743. Netword Delay Time为例。该题是要求一个有向图中,从某一个点开始,到达所有点的最短路径中的最大值。 图的最短路算法 单源最短路 不带负权边:Dijkstra 思路:确定一个出发点,每一个时间步,从所有没有到达过的点中,选择一个与出发点距离最短的点(如果不存在,则结束),此距离就是出发点到该点的最短距离。把该点设置为已到达,并将该点作为中间点,更新出发点和剩余未到达点的最短距离。注意两点: 1. 为什么"此距离就是出发点到该点的最短距离",因为如果存在另一条

jokerxsy的博客 1886
上一篇: 单源最短路径( Dijkstra算法)JAVA实现
下一篇: Java初解背包问题
Coder_py
博客等级 码龄10年 106粉丝 258原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值