动态规划:利用WarShell算法求有向图的传递闭包

无向图连通性判断的五种方法(BFS、DFS、Union-find、Warshell、Tarjan) 目录 无向连通图的相关定义 主要算法流程  DFS判断: BFS判断: Warshell判断: Union-Find判断: Tarjan判断: 无向连通图的相关定义 连通性:在图G中,两个不同的结点u和结点v之间若存在一条路,则称结点u和结点v是连通的。 无向连通图:若在无向图G中,任意两点都是连通的,则称图G为连通图。 主要算法流程  DFS判断: 从任一结点开始,进行一... 阅读详情
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace ConsoleApplication47
  6{
  7    class Program
  8    {
  9        static void Main(string[] args)
 10        {
 11            WarShall ws = new WarShall(4);
 12
 13            ws.SetVaule(0, 1, true);
 14            ws.SetVaule(1, 3, true);
 15            ws.SetVaule(3, 0, true);
 16            ws.SetVaule(3, 2, true);
 17
 18            Console.WriteLine("Before:");
 19
 20            ws.Print();
 21
 22            bool[][] result = ws.GetMatrix();
 23
 24            Console.WriteLine("After:");
 25
 26            ws.Print();
 27
 28        }

 29
 30        class WarShall
 31        {
 32            bool[][] _matrix;
 33
 34            public WarShall(int n)
 35            {
 36                _matrix = new bool[n][];
 37
 38                for (int i = 0; i < n; i++)
 39                {
 40                    _matrix[i] = new bool[n];
 41                }

 42            }

 43
 44            public void SetVaule(int x, int y, bool vaule)
 45            {
 46                _matrix[x][y] = vaule;
 47            }

 48
 49            public bool[][] Matrix
 50            {
 51                get
 52                {
 53                    return _matrix;
 54                }

 55            }

 56
 57            public bool[][] GetMatrix()
 58            {
 59                for (int i = 0; i < _matrix.Length; i++)
 60                {
 61                    for (int x = 0; x < _matrix.Length; x++)
 62                    {
 63                        if (x == i)
 64                        {
 65                            continue;
 66                        }

 67
 68                        for (int y = 0; y < _matrix.Length; y++)
 69                        {
 70                            if (y == i)
 71                            {
 72                                continue;
 73                            }

 74
 75                            if (_matrix[x][i] && _matrix[i][y])
 76                            {
 77                                _matrix[x][y] = true;
 78                            }

 79                        }

 80                    }

 81                }

 82
 83
 84                return _matrix;
 85            }

 86
 87            public void Print()
 88            {
 89                for (int i = 0; i < _matrix.Length; i++)
 90                {
 91                    bool[] s = _matrix[i];
 92                    for (int j = 0; j < s.Length; j++)
 93                    {
 94                        Console.Write(s[j]);
 95                        Console.Write("  ");
 96                    }

 97                    Console.WriteLine();
 98                }

 99            }

100        }

101    }

102}

103
Warshell算法判断传递包,包括自反包,对称包的函数 Warshell算法判断传递包,包括自反包,对称包的函数,从而判断等价关系,代码已通过Visual c++编译 立即下载

相关推荐

Zigbee开发环境搭建:从驱动安装到程序烧录全指南

本文提供了一份详尽的Zigbee开发环境搭建全指南,涵盖从驱动安装到程序烧录的完整流程。重点介绍了使用SmartRF Flash Programmer和仿真器进行程序下载的关键步骤,并详细说明了USB转串口驱动的配置方法,帮助开发者快速构建稳定的Zigbee开发基础。

weixin_29195217的博客 394

Warshall算法(用法详解,并转换成代码的形式)

关于Warshall算法,我先通过离散数学中求传递闭包来解释他的使用规则。 一般的,给定一个矩阵A(行列相等),我们对其使用Warshall算法: //注,该矩阵上只有0或1两种元素,做加法时,1+1还是1 1、先找到该矩阵的对角线,并从对角线的左上方开始为第一个元素 2、以对角线上第一个元素为中心,按列展开,寻找中心所在的列中所有不为0的元素 3、将“ 该中心所在的行 ”加到“ 该中心所在的列 ”中所有不为0的元素所在的行上 4、加完之后,以对角线上第二个元素为中心,按列展开,寻找该列中所有

qq_61711593的博客 2万+

四旋翼滑模控制:从理论到Simulink实现

本文深入探讨了四旋翼滑模控制的理论与Simulink实现,对比了其相较于PID控制在抗干扰和鲁棒性方面的显著优势。文章详细介绍了从精确动力学建模、内外环滑模控制器设计到指令解算的完整流程,并分享了Simulink仿真搭建、参数调试及抗扰性能验证的实战经验,帮助工程师掌握在复杂环境下提升四旋翼稳定性的进阶技能。

weixin_29163581的博客 375

Warshall沃舍尔算法

Warshall沃舍尔算法,python代码

醉蕤的博客 6799

从原理到实现:在《图灵完备》中构建一个144*256像素、1536时钟刻/帧的视频播放器

本文详细解析了在《图灵完备》游戏中构建144*256像素、1536时钟刻/帧视频播放器的全过程。从图灵完备原理出发,通过分层架构设计、视频数据预处理和关键电路实现,展示了如何用基础电子元件模拟现代显卡功能。重点介绍了像素坐标定位系统和颜色数据处理流水线的优化方案,为游戏内复杂电子系统设计提供实践指导。

weixin_42520025的博客 445

SAP 固定资产采购简介

SAP 固定资产采购是指企业通过采购流程获取长期使用并记录为固定资产的物品。固定资产是企业为长期使用而购置的资产,如机器设备、办公设备、建筑物、车辆等,通常这些资产的使用期限较长,并且在资产负债表上记录。在SAP系统中,固定资产采购流程通常与物料管理(MM)模块和固定资产管理(FI-AA)模块相结合。这个过程涉及从需求的提出、采购订单的创建,到货物接收、发票校验和资产激活的多个步骤。

我小时候很黑的博客 1731

动态规划在求解传递闭包问题中的应用(JAVA)--Warshell算法

动态规划在求解传递闭包问题中的应用:传递闭包:对于n个顶点有向图来说,如果第i个顶点到第j个顶点之间存在一条有效的有向路径(即长度大于0的路径),那么T(i, j) = 1,否则T(i, j) = 0。例如:求解传递闭包我们可以使用深度优先搜索和广度优先搜索,我们可以对每个顶点进行DFS/BFS,在对应的矩阵位置上置为1,遍历之后我们便得到整个图的传递闭包。但是这种方式并不是高效的算法,而Wars...

一串代码的博客 2039

DaVinci配置DID之三:代码实现

本系列主要基于DaVinCi工具链来展开具体DID读写的配置以及最终实现。1,CDD准备2,工具链配置3,代码实现4,验证结果本文讲述流程3话不多说,走你~博主是AutoSar小白,如果哪里表达的有问题,还请大佬们指点指点哈。接下来,让我们在下章关于测试验证的博文再相会哈~

weixin_48498880的博客 5361

warshell算法

warshell算法是用来求关系R的传递闭包t(R)的。 传统的t(R)算法:R+R^2+R^3+....... 当R^n出现循环时,R+R^2+......R^n即为t(R) warshell算法:假设存在C作为中间路径,取C=0,遍历关系矩阵M,如果c-&gt;i为1,j-&gt;c为1,那么Mij=1,c++实现遍历。 算法复杂度为O(n^3) 是很菜的算法,如果用来写算法题还是省...

一条菜狗的博客 3393

【车载以太网】ATU(Address Translation Unit / Address Table Unit)

【车载以太网】ATU(Address Translation Unit / Address Table Unit)

天赐好车的博客 152

【赛题解析&命令脚本配置】2023年全国职业院校技能大赛GZ073网络系统管理赛项赛题第10套模块A:网络构建 & (五)网络运维配置+(六)SDN网络配置-命令配置

【赛题解析&命令脚本配置】2023年全国职业院校技能大赛GZ073网络系统管理赛项赛题第10套模块A:网络构建 & (五)网络运维配置+(六)SDN网络配置-命令配置

君逍遥o 810

传递闭包

今天有人提到了传递闭包,我简单说说吧。  所谓传递性,可以这样理解:对于一个节点i,如果j能到i,i能到k,那么j就能到k。求传递闭包,就是把图中所有满足这样传递性的节点都弄出来,计算完成后,我们也就知道任意两个节点之间是否相连。  传递闭包的计算过程一般可以用Warshell算法描述:  For 每个节点i Do      For 每个节点j Do      If j能到i Then 

acm_1361677193的专栏 1万+

【深度学习】什么是交叉注意力机制?

输入序列:自注意力机制在同一个输入序列内建立注意力,交叉注意力机制在不同的输入序列间建立注意力。应用场景:自注意力机制多用于单一模态的任务(如纯文本任务),交叉注意力机制多用于多模态任务(如图像和文本的结合)。计算过程:自注意力机制的查询、键和值都来自同一个输入序列,而交叉注意力机制的查询来自一个输入序列,键和值来自另一个输入序列。

q742971636的博客 7372

Floyd——WarShell 算法————求传递闭包 和 图中两点最小路径 和 Knapsack问题

Function: 求:“有向图”传递闭包 求:图中两点“最短路径” Prepared Information: 传递闭包的概念: 图的基本知识: 1、求有向图的传递闭包 Example: Main idea: Input: R0 R1:寻找经过"1"的路径 R2:寻找经过"2"和"1,2"二个节点的路径 R3:寻找经过"3"和"1,2,3"的路径 R4:寻找经过"1"和"1,2,3,4"的路径 Output: Rn 【一条路径最多经过n个节点(从一个节点出发全部都经过再返回到.

quyanyanchenyi的博客 1012

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

DP之Warshall算法和Floyd算法

DP之Warshall算法和Floyd算法 上2篇详细分析了动态规划的一些理解,传统的教材上就大概说了下空间换时间,记忆以避免重复计算等。 然后我们在文章中深入的分析和解释了交叠子问题是怎么表现的,最优子结构的表现,多阶段决策(无后效性)的表现,递推式(状态转移方程),一个状态表示一个子 问题的解,动态规划就是将问题划分为规模不是太大的子问题,动态规划填矩阵的顺序

classicbear的专栏 9365

深度学习工具包 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")

动态规划 warshall算法、 Floyd算法

一个有n个顶点的有向图的传递闭包为:有向图中的初始路径可达情况可以参见其邻接矩阵A,邻接矩阵中A[i,j]表示i到j是否直接可达,若直接可达,则A[i,j]记为1,否则记为0;两个有向图中i到j有路径表示从i点开始经过其他点(或者不经过其他点)能够到达j点,如果i到j有路径,则将T[i,j]设置为1,否则设置为0;有向图的传递闭包表示从邻接矩阵A出发,求的所有节点间的路径可达情况,该矩阵就为所要求的

shuxingcq的博客 698

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

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

最短路径Ⅲ—Floyd-Warshall算法

Floyd算法 1.定义概览 Floyd-Warshall算法(Floyd-Warshall algorithm)是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权的最短路径问题,同时也被用于计算有向图的传递闭包。Floyd-Warshall算法的时间复杂度为O(N3),空间复杂度为O(N2)。   2.算法描述 1)算法思想原理:      Flo

winbobob的专栏 2248

Warshall算法求传递闭包

算法描述: 传递闭包的一种有效算法—Warshall算法,这种算法也便于计算机实现。 (1)置新矩阵A=M; (2)i=1; (3)对所有j如果A[j,i]=1,则对k=1,2,…,n,A[j,k]=A[j,k]∨A[i,k]; (4)i加1;(i是行,j是列) (5)如果i≤n,则转到步骤3),否则停止。例如: 原文出处:Warshall算法求传递闭包_foreverzili的博客-优快云博客_warshall算法求传递闭包 ...

听取WA声一片 7445

Warshell算法实现

      离散数学中有一种求传递递包的算法及求一个矩阵的传递递包,具体算法是: 首先用一个k控制次数 如果存在一个e[ i ][ k]==1; 使得e[ k ] [ j ]=1;  我们就可以把矩阵在原来的基础上所对应的 e[ i ][ j ]=1 具体代码如下 package com.company; public class Test { static int[][] ...

However_day的博客 1525

Warshall 算法

Warshall算法 介绍: Warshall在1962年提出了一个求关系的传递闭包的有效算法。其具体过程如下: N—S图 代码实现: import java.util.Scanner; public class Warshall { public static void main(String[] args) { System.out.println("请输入矩阵阶数:"); ...

奇迹 1万+

离散数学中的warshall算法应用

传递闭包欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 欢迎使用Markdown...

wjt_1025的博客 1042

求关系的传递闭包

成绩 10 开启时间 2017年04月26日 星期三 14:55 折扣 0.8 折扣时间 2017年05月15日 星期一 14:55 允许迟交 否 关闭时间 2017年06月1日 星期四 14:55 输入 一次输入一个关系矩阵,每一行两个相邻元素之间用一个空格隔开,输入元素的行与列分别

weixin_38199770的博客 4381

用warshall算法求图的传递闭包矩阵

传递闭包传递闭包的意思是说,如果顶点A能到达顶点B,并且顶点B能到达顶点C,那么顶点A一定能到达顶点C。因此可以通过修改原始邻接矩阵得到传递闭包矩阵,使用的方法就是warshall方法。原始邻接矩阵只表示哪些顶点是邻接顶点,而传递闭包矩阵表达的是每个顶点可以到达哪些顶点。warshall算法的关键代码//生成闭包矩阵 public void getClosureMatrix(){ for(i...

ASN_forever的博客 4208

编程实现1到N个数的所有排列组合

编程实现1到N个数的所有排列组合。如:n = 3则得到的序列如下:123, 132, 213, 231, 312, 321我的实现如下,大家看看如何:  1using System;  2using System.Collections.Generic;  3using System.Text;  4  5namespace ConsoleAppl

1994
上一篇: 时空权衡:利用额外的空间提高字符串匹配的速度
下一篇: 你了解常用的图象格式吗?
gpcuster
博客等级 码龄21年 297粉丝 99原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值