最小生成树 kruskal_使用Kruskal算法求解Java最小生成树问题

最小生成树 kruskal

In Electronic Circuit we often required less wiring to connect pins together. We can model this wiring problem with a connected, undirected graph G=(V, E), where V is the set of pins, E is the set of possible interconnections between pair of pins, and for each edge we have a weight w(u,v) specifying the cost (amount of wire needed) to connect u and v. We then wish to find an acyclic subset T that connects all of the vertices and whose total weight w(T)= sum of all the weights in T is minimized . Since T is acyclic and connects all of the vertices, it must form a tree, which we call a spanning tree since it spans the graph G. We call this problem minimum spanning tree problem.

在电子电路中,我们通常需要较少的接线来将引脚连接在一起。 我们可以使用连接的无向图G =(V,E)来建模此布线问题,其中V是引脚组, E是引脚对之间可能的互连集,对于每个边,我们都有权重w( u,v)指定连接uv的成本(所需的电线数量)。 然后,我们希望找到一个无环子集T ,它连接所有顶点,并且总权重w(T)= T中所有权重的总和最小 。 由于T是无环的并且连接所有顶点,因此它必须形成一棵树,由于它跨越了图G ,因此我们将其称为生成树 。 我们称这个问题为最小生成树问题

minimum spanning tree problem


MST Green color edges are the selected edges for MST.

MST绿色边缘是MST的选定边缘。

There are two algorithm to solve this problem: Kruskal's Algorithm and Prim's Algorithm. Each of them run in O(E lg V )

有两种算法可以解决此问题: Kruskal算法Prim算法 。 它们每个都以O(E lg V)运行

Here we are discussing Kruskal's Algorithm...

在这里,我们讨论的是Kruskal算法...

克鲁斯卡尔算法 (Kruskal's Algorithm)

This Algorithm first makes the forest of each vertex and then sorts the edges according to their weights, and in each step, it adds the minimum weight edge in the tree that connects two distinct vertexes that do not belong to the same tree in the forest.

该算法首先创建每个顶点的森林,然后根据其权重对边缘进行排序,然后在每个步骤中,在连接两个不属于该森林中同一棵树的不同顶点的树中添加最小权重边缘。

It uses a disjoint set data structure to maintain several disjoint sets of elements. Each set contains the vertices in one tree of the current forest.

它使用不连续集数据结构来维护几个不连续元素集。 每一组包含当前森林的一棵树中的顶点。

Example: Here we are finding the cost of MST.

示例:在这里我们发现MST的成本。

Program:

程序:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class MST{
static class set{
int parent,rank;
}
//find set which represents vertex i
static int find(set subsets[],int i ){
if(subsets[i].parent==i)
return i;
return find(subsets,subsets[i].parent);
}
//function for adding  edges whose vertex belongs 
//to the different tree ie. UNION
static void UNION(set subsets[],int x,int y){
int xroot=find(subsets,x);
int yroot=find(subsets,y);
if(subsets[xroot].rank>subsets[yroot].rank)
subsets[yroot].parent=xroot;
else if(subsets[xroot].rank<subsets[yroot].rank)
subsets[xroot].parent=yroot;
else{
subsets[yroot].parent=xroot;
subsets[xroot].rank++;
}
}
static int mst(int n, Integer[][] edges) {
set subsets[]=new set[n];
//Create forest of vrtices that is separate tree for each vertex
for(int i=0;i<n;i++)   
{  
subsets[i]=new set();
subsets[i].parent=i;  // Each vertex is its own parent
subsets[i].rank=0;   //Having no child
}
int e=0,i=0,count=0;
//Create graph having exactly vertex-1 ie. n-1 edges
while(e<n-1){
//find set from which current vertex belongs
int x=find(subsets,edges[i][0]-1);  
//find set from which current vertex belongs
int y=find(subsets,edges[i][1]-1); 
if(x!=y){
count+=edges[i][2];  
e++;
// union the two vertex in the same tree 
//if they belong to the different set
UNION(subsets,x,y); 
}
i++;
}
return count;
}
public static void main(String[] args) 
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();   //number of nodes
int m = in.nextInt();  //number of edges
Integer [][]edges = new Integer[m][3];
for(int edges_i = 0; edges_i < m; edges_i++){
for(int edges_j = 0; edges_j < 3; edges_j++){
edges[edges_i][edges_j] = in.nextInt();
}
}
//Sort edges two dimensional array according to 
//its third column i.e. weight
Arrays.sort(edges,new Comparator<Integer[]>(){
@Override
public int compare(Integer[] i1,Integer[] i2){
//Comparing third column having index 2
return i1[2].compareTo(i2[2]);   
}
});
int result=mst(n,edges);
System.out.println(result);
in.close();
}
}

Output

输出量

minimum spanning tree problem output

翻译自: https://www.includehelp.com/java/minimum-spanning-tree-problem-using-kruskals-algorithm.aspx

最小生成树 kruskal

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542724.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql数据库面试题 软件测试_软件测试数据库面试题一

前提本次分享只局限于 sql server 和 mysql 这两种数据库&#xff0c;其他数据库暂不总结正文1. 对查询的字段进行去重(distinct)用法注意&#xff1a;1. distinct【查询字段】&#xff0c;必须放在要查询字段的开头&#xff0c;即放在第一个参数&#xff1b;2. 只能在SELECT 语…

python数码时钟代码_python时钟的实现

from time importsleepimporttimeimportosclassClock(object):"""数字时钟""" def __init__(self, hour0, minute0, second0):"""初始化方法 :param hour: 时 :param minute: 分 :param second: 秒"""self._hourh…

PHP页面跳转

本文转载自&#xff1a;http://blog.sina.com.cn/s/blog_9a06890901014ol1.html PHP页面跳转一、header()函数 header函数中Location类型的标头是一种特殊的header调用&#xff0c;常用来实现页面跳转 注意&#xff1a;1、location和“:”号间不能有空格&#xff0c;否则不会跳…

如何打印出给定尺寸的方格_打印给定号码的表格| 8086微处理器

如何打印出给定尺寸的方格Problem statement: 问题陈述&#xff1a; Write an assembly language program in 8086 to print the table of a given integer. 在8086中编写汇编语言程序以打印给定整数的表。 Assumptions: Suppose the inputted number is at memory location …

python自动更新excel数据_如何更新Excel数据?(刷新所有查询)

我有一个带有一些查询的Excel xlsm文件。目前我每天打开它&#xff0c;点击“数据”选项卡中的“全部刷新”命令。我希望这件事能自动完成。我用python编写了一个脚本(我是python新手)。问题是&#xff0c;刷新数据并保存Excel文件后&#xff0c;刷新的数据不可见(我知道刷新工…

mongoDB 使用手册

2019独角兽企业重金招聘Python工程师标准>>> 1、基本操作db.AddUser(username,password) 添加用户db.auth(usrename,password) 设置数据库连接验证db.cloneDataBase(fromhost) 从目标服务器克隆一个数据库db.commandHelp(name) returns the help for the commanddb.…

android搜索框功能实现_巧用 Trie 树,实现搜索引擎关键词提示功能

来源 | 码海责编 | Carol封图 | CSDN 付费下载于视觉中国我们几乎每天都在用搜索引擎搜索信息&#xff0c;相信大家肯定有注意过这样一个细节:当输入某个字符的时候&#xff0c;搜索引框底下会出现多个推荐词&#xff0c;如下&#xff0c;输入「python」后&#xff0c;底下会出…

Python | 从用户输入数据,保存到文件,读取并打印

Here, we have to input the data from the user, to read the data from user, we use input() method, and then the input data we have to store in the file by using write() method and then by using read() method we can get the data. 在这里&#xff0c;我们必须从…

python语句print type 1234的输出结果是_Python语句 print(type(1J))的输出结果是

【填空题】遍历输出文件所有行。 fopen("d:\\r2.txt","r") while True: str print(str,end) if not str: break f.close()【单选题】执行下列 Python语句将产生的结果是( ) i1 if (i): print(True) else: print( False)【单选题】Python语句 print(type(1/…

qt5.9.0调试如何查看变量的值_深入了解 Java 调试

Bug(俗称"八阿哥") 是软件开发绕不过的一道坎&#xff0c;因此调试便成了每位程序员一项必备的核心技能。调试不仅有助于理解程序的运行流程&#xff0c;还能改进代码质量&#xff0c;最终提高开发者解决问题的能力以及交付软件的品质。本文旨在讨论 Java 调试关键技…

python字符串转浮点数_Python | 打印不同的值(整数,浮点数,字符串,布尔值)...

python字符串转浮点数In the given example, we are printing different values like integer, float, string and Boolean using print() method in python. 在给定的示例中&#xff0c;我们使用python中的print()方法打印不同的值&#xff0c;例如整数&#xff0c;浮点数&…

(6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug...

如果POI-3.10往一个工作表&#xff08;sheet&#xff09;里面插入数据的话&#xff0c;需要注意了&#xff0c;其有一个不太被容易发现的bug。 被插入的工作表&#xff08;sheet&#xff09;里面的单元格没有包含任何的注解&#xff08;comment&#xff09;的时候&#xff0c;插…

mysql下拉刷新加载数据_下拉刷新、加载数据功能

paging nick加载更多getData();varm0,n2;//m:button点击次数 n:一次加载几条数据$(.page-btn-nick).click(getData);functiongetData(){$.ajax(paging.html).then(function(response){//测试url写本页面varobj{developer:[{name:nick},{name:ljy},{name:xzl},{name:jeson},{nam…

mcq 队列_人工智能逻辑才能问答(MCQ)

mcq 队列1) Why do we want to implement the concept of Logic in an AI system? So that the agent can have decision making capabilitySo that the agent can think and act humanlySo that the agent can apply the logic for finding the solution to any particular p…

第三周作业!

1、列出当前系统上所有已经登录的用户的用户名&#xff0c;注意&#xff1a;同一个用户登录多次&#xff0c;则只显示一次即可。答&#xff1a;本题思路&#xff1a;先用who命令列出当前登陆的用户信息&#xff0c;然后使用cut命令对字段进行分割&#xff0c;选出我们需要的字段…

python导入模块以及类_python模块的导入以及模块简介

标签&#xff1a; 一、模块的定义及类型 1、定义 模块就是用一堆的代码实现了一些功能的代码的集合&#xff0c;通常一个或者多个函数写在一个.py文件里&#xff0c;而如果有些功能实现起来很复杂&#xff0c;那么就需要创建n个.py文件&#xff0c;这n个.py文件的集合就是模块 …

mysql 指定数字排序_Mysql数据排序

排序数据普通字段排序按照单一字段排序按照多个字段排序手动指定排序顺序单个字段手动排序多个字段手动排序普通字段排序按照单一字段排序排序采用order by子句&#xff0c;order by后面跟上排序字段&#xff0c;排序字段可以放多个&#xff0c;多个采用逗号间隔&#xff0c;or…

《黃帝內經 —— 央視60集紀錄片》

下載地址&#xff1a; http://pan.baidu.com/s/1dFI8hxf 目錄 第一部 医史篇第1集&#xff1a;神奇的秘笈&#xff08;《黄帝内经》是部什么书&#xff09;第2集&#xff1a;赫赫始祖&#xff08;上&#xff09;&#xff08;黄帝、炎帝&#xff09;第3集&#xff1a;赫赫始祖&a…

mnist手写数字数据集_mnist手写数据集(1. 加载与可视化)

》》欢迎 点赞&#xff0c;留言&#xff0c;收藏加关注《《1. 模型构建的步骤&#xff1a;在构建AI模型时&#xff0c;一般有以下主要步骤&#xff1a;准备数据、数据预处理、划分数据集、配置模型、训练模型、评估优化、模型应用&#xff0c;如下图所示&#xff1a;【注意】由…

python凯撒密码实现_密码:凯撒密码及其Python实现

python凯撒密码实现Before we start let’s some basic terminology... 在开始之前&#xff0c;让我们先介绍一些基本术语... The art and science to achieve security by encoding messages to make them unreadable are known as Cryptography. That’s what the whole art…