最小生成树

单链聚类-解题分析 Time Limit: 1sec    Memory Limit:256MB Description Given n nodes in a two-dimensional space, we want to use single-link custering method to find k clusters. This is equivalent to finding an MST 阅读详情

Description
Given n nodes in a two-dimensional space, we want to use single-link custering method to find k clusters. This is equivalent to finding an MST (Minimum spanning tree) of these nodes and deleting k-1 longest edges.

Your job is to output the length of the (k-1)-th longest edges of the MST.

Input
There are multiple cases. For each case, the first line includes n and k (2<=k<=n<=100). The following n lines give the coordinates of n nodes. You may use Euclidean distance to measure the distance between two nodes.

Output
For each case, output the length of the (k-1)-th longest edges. The precision is set to 2 digits after the decimal point.

Sample Input
Copy sample input to clipboard
6 2
1 1
2 1
1 2
3 3
4 2
4 3

Sample Output
2.24

问题描述,给定n个二维空间结点,目标是用单链聚类的方法寻找k个簇。
题目说是单链聚类,其实就是最小生成树,输出第k-1大的边;
我用的是kruskal算法:

最小生成树算法总结 算法总结第5弹,最小生成树,有关生成树的问题有很多,这里先介绍个最常用的吧。 最小生成树 首先,生成树是建立在无向图中的,对于有向图,则没有生成树的概念,所以接下来讨论的图均默认为无向图。对于一个有n个点的图,最少需要n-1条边使得这n个点联通,由这n-1条边组成的子图则称为原图的生成树。一般来说,一个图的生成树并不是唯一的(除非原图本身就是一棵树)。 现在考虑带权图G,即图的边带权,则 阅读详情

相关推荐

什么是最小生成树以及如何构建

文章目录一,概念回顾 - 生成树1.1 什么是生成树1.2 如何建立无向图的生成树二,最小生成树(Minimum Spanning Trees)2.1 最小生成树的典型用途2.2 构造最小生成树(MST)2.2.1 MST 性质2.2.2 Prim 算法构建最小生成树2.2.3 Kruskal 算法构建最小生成树2.2.4 Prim 与 Kruskal 算法比较 预习:图的基本概念及图演算法 一,概念回顾 - 生成树 1.1 什么是生成树 生成树:所有顶均由边链接在一起,但不存在回路的图。 多一条边会出现

壮壮不太胖的学习笔记 5391

基于最小生成树的单连接算法

本文介绍基于最小生成树(MST)的单连接聚类算法,分为凝聚和分裂两种,都十分简单。(基于MST的单连接凝聚算法)step 1. 假设有n个待聚类的对象,把每个对象都当做一个独立的簇,画出 这n个点的完全图G(∞)G(\infty),根据G(∞)G(\infty)得到最小生成树,每条边的权重就是对象之间的距离,重复第二步和第三步直到只剩下一个簇。 step 2. 找出MST中权重最小的一条边(如果

tyh70537的博客 2327

算法——最小生成树

最小生成树(Minimum Spanning Tree,简称MST)是指一张连通图的子图,它包含图中的所有顶点,并且是图中所有生成树中权值之和最小的一个。最小生成树常用于解决网络设计、电力传输、道路规划等问题。祝您好运!

百锦再的博客 1万+

Sicily Single-link Clustering| Prim算法

Single-link Clustering Total: 229 Accepted: 90Time Limit: 1sec Memory Limit:256MB Description Given n nodes in a two-dimensional space, we want to use single-link custering method to find k cl

linzch3的博客 1090

最小生成树MATLAB代码实现

假设 WN=(V,{E}) 是一个含有 n 个顶点的连通网,则按照克鲁斯卡尔算法构造最小生成树的过程为:先构造一个只含 n 个顶点,而边集为空的子图,若将该子图中各个顶点看成是各棵树上的根结点,则它是一个含有 n 棵树的一个森林。之后,从网的边集 E 中选取一条权值最小的边,若该条边的两个顶点分属不同的树,则将其加入子图,也就是说,将这两个顶点分别所在的两棵树合成一棵树;反之,若该条边的两个顶点已落在同一棵树上,则不可取,而应该取下一条权值最小的边再试之。依次类推,直至森林中只有一棵树,也即子图中含有 n-1条边为止。

EXCEL在数学建模中的应用

一、excel的数据处理功能 二、EXCEL的数据分析功能 三、总体分布的假设检验

<OJ_Sicily>Single-link Clustering

使用prim算法生成最小生成树,求出长度为前k-1个的边,把这k-1个边删除就能将n个节点进行聚类了。

Vipin_Pei的博客 2086

解决mamba_ssm库安装难题:两种高效方法详解

本文详细解析了mamba_ssm库安装失败的常见原因,并提供了两种高效的解决方案。针对其依赖C++/CUDA扩展编译的特性,文章手把手指导用户通过GitHub克隆源码编译安装,或寻找匹配的预编译Wheel文件直接安装,帮助开发者彻底解决环境配置难题,顺利部署Mamba模型。

weixin_29056865的博客 553

最小生成树—prime算法

生成树 一个连通图的生成树是该连通图的一个极小连同子图,它含有图中全部顶点,和构成一棵树的(n-1)条边。 如果在一棵生成树上添加任何一条边,必定构成一个环,因为这条边使得它依附的那两个顶点之间有了第二条路径。 一棵有n个顶点的生成树(连通无回路图)有且仅有(n-1)条边,但是,有(n-1)条边的图不一定都是生成树。如果一个图有n个顶点和小于(n-1)条边,则是非连通图;如果它有多于(n-1)条边,则一定有回路。 如果图中任意两点都是连通的,那么图被称作连通图。 如果此图是有向图,且双向都有路径,则称为强

weixin_46157720的博客 1万+

高德地图-获取定位-失败原因-浏览器定位异常场景

提示:也能实现,但是需要https协议,而且请求时间很长,要么你用梯子,还是不太合适.

shi15926lei的博客 4204

C#操作SQLite数据库帮助类——SQLiteHelper

using System; using System.Collections.Generic; using System.Data.SQLite; using System.Data; namespace Com.ZCWL.Rock.Helper { public class SQLiteHelper { private static string connectionString =

跟健哥出去走走 2万+

prim算法求最小生成树

最小生成树(Minimum Spanning Tree,简称MST)是图论中的一个概念。给定一个连通的无向图,最小生成树是指包含图中所有顶点的一棵树,且该树的所有边的权重之和最小。连通性:最小生成树必须包含图中的所有顶点,并且通过边将它们连接起来,确保整个图是连通的,即任意两个顶点之间都有路径。(一颗有 n 个顶点的生成树有且仅有 n−1 条边,如果生成树中再添加一条边,则必定成环。无环:最小生成树是一棵树,所以不能包含任何环(即回路)。最小权重:最小生成树的边权重之和应当尽可能地小。

sin1810335764的博客 4672

顺序功能图基本结构

顺序功能图(Sequential Function Chart,SFC)是一种用于描述工业控制系统中顺序控制流程的图形化编程语言,广泛应用于PLC(可编程逻辑控制器)编程。其核心思想是将复杂的控制流程分解为多个有序的步骤,通过逻辑条件实现步骤间的切换。顺序功能图通过图形化的方式简化了复杂控制逻辑的设计,尤其适用于需要严格顺序控制的工业自动化场景。[初始步] → 转换条件1 → [步1] → 转换条件2 → [步2] → ...[步X] → → [步B] → 转换条件B → → [合并步]

ayingqin1的博客 1224

MATLAB 最小生成树

本文介绍了 MATLAB 中如何求解最小生成树。

小嗷犬的博客 4426

[ensp报错:USG6000V错误代码45解决方案]

[ensp报错:USG6000V:错误代码45解决] [ensp报错:USG6000V:错误代码45解决] 第一种处理方式: ensp的45错误帮助文档里的解决方法:(这里就不说了,上华为爸爸的图) ensp的45错误:如果你的45错误不是描述的那个,可能是ensp与virtualbox不兼容了。 错误描述:具体表现为在virtualbox里没有vfw_usg的条目,虚拟介质管理器里也没有相应的条目: 就是vfw_usg_vdi这个 这种情况下你把ensp卸载重装多少次都没用,原因在两个软件的兼容

weixin_45456536的博客 3万+

静态分析工具COBOT与Testbed对比

Testbed为规则检查工具的代表,市场占有量非常大,但随着时间的推移,大家从规则匹配转向了用静态分析工具检查Bug,所以规则检查型工具占有量比开始时候已经减少,但仍有一些比较落后的单位还在继续采购此类工具。3、通过对开源工程的测试,进行误漏报率的对比,发现COBOT(库博)的误漏报率相比Testbed较低,因COBOT主打国内市场,在众多军工单位进行大量试用及使用,吸取了众多单位的意见和建议,更加符合国内军工单位的使用习惯及测试要求。4、此外库博在功能上还可以进行计划任务,检测队列,检测并发的功能支持。

chengoodflower的博客 2054

最小生成树算法的相关变形题

由于吉大近几年的考试中,算法大题里频繁地出现最小生成树算法的变形题,因此在此整理一下。PS:prim算法不管是画图还是代码题真的难用,kruskal算法才是永远滴神。

销夜的博客 1931

RK3399快速上手 | 01- 开发环境搭建(固件烧写、编译工具链安装、kernel编译)

一、编译工具链 编译工具链使用gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu。 下载地址: https://releases.linaro.org/components/toolchain/binaries/6.3-2017.05/arm-linux-gnueabihf/ wget https://releases.linaro.org/components/toolchain/binaries/6.3-2017.05/arm-linux-gnueabi

Mculover666的博客(嵌入式) 8835

最小生成树刷题汇总

Acwing 346 走廊泼水节 给定一棵 N 个节点的树,要求增加若干条边,把这棵树扩充为完全图,并满足图的唯一最小生成树仍然是这棵树。 求增加的边的权值总和最小是多少。 注意: 树中的所有边权均为整数,且新加的所有边权也必须为整数。 输入格式 第一行包含整数 tt,表示共有 tt 组测试数据。 对于每组测试数据,第一行包含整数 NN。 接下来 N−1N−1 行,每行三个整数 X,Y,ZX,Y,Z,表示 XX 节点与 YY 节点之间存在一条边,长度为 ZZ。 输出格式 每组数据输出一个整数,表示权值总和最

徒手拆机甲的博客 1932

083、多帧降噪(MFR)与VST算法:低照度场景下的信噪比提升与鬼影抑制

本文深入探讨了多帧降噪(MFR)和VST算法在低照度场景中的应用与优化。作者从实际项目案例出发,指出单帧降噪的局限性,详细分析了MFR的核心原理与工程实现中的关键环节,包括帧对齐、帧选择、融合策略和鬼影抑制等技术要点。文章特别强调VST变换对噪声均匀化的重要性,并分享了实际开发中的调试经验与参数优化技巧,如"三明治"调试法和性能平衡策略。最后指出,优秀的MFR方案需要在信噪比提升、鬼影抑制和纹理保留之间找到场景化的平衡点,建议开发者通过真实场景测试验证算法效果。全文既有理论分析,又包含丰富的实战经验和技术细

zzfmlxs的博客 121

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

图论:最小生成树

本文介绍了两种最小生成树算法:Prim算法和Kruskal算法。Prim算法采用贪心策略,从节点角度构建最小生成树,通过维护节点到树的距离实现优化,适用于稠密图,时间复杂度为O(V²)或O(ElogV)。Kruskal算法通过边构建最小生成树,使用并查集判断连通性,按边权排序后逐步选择边,适用于稀疏图,时间复杂度为O(ElogE)。文章详细分析了两种算法的实现步骤、模板代码(包括邻接矩阵和邻接表版本)以及适用场景,并通过寻宝问题展示了具体应用。两种算法各有优势,应根据图的稠密程度选择合适的实现方式。

豪德又AC啦 1834

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

最小生成树详解

本文介绍了最小生成树的概念及其两种主要算法:Kruskal算法和Prim算法。Kruskal算法通过按边权排序并利用并查集避免环来构建最小生成树,适合稀疏图;Prim算法则从原点出发逐步扩展最近节点,适合稠密图。文章还通过多个例题(如繁忙的都市、无线通讯网等)展示了算法的应用,并介绍了超级源点等技巧。最后强调深入理解树结构是解决此类问题的关键,建议通过多做题来提高解题能力。不同问题需要灵活选择算法,构建特殊的最小生成树来满足需求。

joezhoutj的博客 817

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

最小生成树的一些性质和理解

1)       定义在一棵树里添加一条边,并在产生的圈里删除一条边叫做一次操作。(也就是说换掉一条边并且保证结果是树),则树A和B是无向图的两个生成树,则A可以通过若干次操作变成B。   证:把树看作边的集合,如果B中有一条A没有的边,则把这条边加到A上,A产生一个圈中至少有一条是B中没有的边,把这条边删掉,则A仍然是生成树,A,B集合相同的边多了一条,重复这个过程直到A B包含的边相同。

zengchenacmer的专栏 9133

图论——最小生成树

最小生成树(MinimumSpanningTrees)的概念连通图的一颗生成树(SpanningTree)是包含图的所有顶点的连通无环子图(也就是一棵树)。加权连通图的一颗最小生成树是图的一颗权重最小的生成树,其中,树的权重定义为所有边的权重总和。)将加权图每个顶点都看做森林,然后将图中每条邻接边的边权按照升序的方式进行排列,接着从排列好的邻接边表中抽取边权最小的边,写入该边的起始顶点和结束顶点,连接顶点将森林构成树,然后读取起始结束顶点的邻接边,优先抽取边权小的邻接边,继续连接顶点将森林构成树。...

faith_xy的博客 3765
上一篇: Fibonacci 数列
下一篇: 各种排序算法
HowardLin
博客等级 码龄11年 2粉丝 21原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值