版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/。未经本作者同意不得转载。

https://blog.csdn.net/kenden23/article/details/26821635

寻找图中最小连通的路径,图例如以下:

算法步骤:

1. Sort all the edges in non-decreasing order of their weight.

2. Pick the smallest edge. Check if it forms a cycle with the spanning tree
formed so far. If cycle is not formed, include this edge. Else, discard it. 3. Repeat step#2 until there are (V-1) edges in the spanning tree.

关键是第二步难,这里使用Union Find来解决,能够差点儿小于O(lgn)的时间效率来推断是否须要推断的顶点和已经选择的顶点成环。

正由于这步,使得原本简单的贪心法。变得不那么简单了。

这样本算法的时间效率达到:max(O(ElogE) , O(ElogV))

原文參考:http://www.geeksforgeeks.org/greedy-algorithms-set-2-kruskals-minimum-spanning-tree-mst/

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h> class KruskalsMST
{
struct Edge
{
int src, des, weight;
}; static int cmp(const void *a, const void *b)
{
Edge *a1 = (Edge *) a, *b1 = (Edge *) b;
return a1->weight - b1->weight;
} struct Graph
{
int V, E;
Edge *edges;
Graph(int v, int e) : V(v), E(e)
{
edges = new Edge[e];
}
virtual ~Graph()
{
if (edges) delete [] edges;
}
}; struct SubSet
{
int parent, rank;
}; int find(SubSet *subs, int i)
{
if (subs[i].parent != i)
subs[i].parent = find(subs, subs[i].parent);
return subs[i].parent;
} void UnionTwo(SubSet *subs, int x, int y)
{
int xroot = find(subs, x);
int yroot = find(subs, y);
if (subs[xroot].rank < subs[yroot].rank)
subs[xroot].parent = yroot;
else if (subs[xroot].rank > subs[yroot].rank)
subs[yroot].parent = xroot;
else
{
subs[xroot].rank++;
subs[yroot].parent = xroot;
}
} Graph *graph;
Edge *res;
SubSet *subs; void initSubSet()
{
subs = new SubSet[graph->V];
for (int i = 0; i < graph->V; i++)
{
subs[i].parent = i;
subs[i].rank = 0;
}
} void mst()
{
res = new Edge[graph->V-1]; qsort(graph->edges, graph->E, sizeof(graph->edges[0]), cmp); initSubSet(); for (int e = 0, i = 0; e < graph->V - 1 && i < graph->E; i++)
{
Edge nextEdge = graph->edges[i];
int x = find(subs, nextEdge.src);
int y = find(subs, nextEdge.des);
if (x != y)
{
res[e++] = nextEdge;
UnionTwo(subs, x, y);
}
}
} void printResult()
{
printf("Following are the edges in the constructed MST\n");
for (int i = 0; i < graph->V-1; ++i)
printf("%d -- %d == %d\n", res[i].src, res[i].des, res[i].weight);
}
public:
KruskalsMST()
{
/* Let us create following weighted graph
10
0--------1
| \ |
6| 5\ |15
| \ |
2--------3
4 */
int V = 4; // Number of vertices in graph
int E = 5; // Number of edges in graph
graph = new Graph(V, E); // add edge 0-1
graph->edges[0].src = 0;
graph->edges[0].des = 1;
graph->edges[0].weight = 10; // add edges 0-2
graph->edges[1].src = 0;
graph->edges[1].des = 2;
graph->edges[1].weight = 6; // add edges 0-3
graph->edges[2].src = 0;
graph->edges[2].des = 3;
graph->edges[2].weight = 5; // add edges 1-3
graph->edges[3].src = 1;
graph->edges[3].des = 3;
graph->edges[3].weight = 15; // add edges 2-3
graph->edges[4].src = 2;
graph->edges[4].des = 3;
graph->edges[4].weight = 4; mst();
printResult();
}
~KruskalsMST()
{
if (res) delete [] res;
if (subs) delete [] subs;
if (graph) delete graph;
}
};

Geeks : Kruskal’s Minimum Spanning Tree Algorithm 最小生成树的更多相关文章

  1. MST(Kruskal’s Minimum Spanning Tree Algorithm)

    You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...

  2. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  3. 说说最小生成树(Minimum Spanning Tree)

    minimum spanning tree(MST) 最小生成树是连通无向带权图的一个子图,要求 能够连接图中的所有顶点.无环.路径的权重和为所有路径中最小的. graph-cut 对图的一个切割或者 ...

  4. 最小生成树(Minimum Spanning Tree)——Prim算法与Kruskal算法+并查集

    最小生成树——Minimum Spanning Tree,是图论中比较重要的模型,通常用于解决实际生活中的路径代价最小一类的问题.我们首先用通俗的语言解释它的定义: 对于有n个节点的有权无向连通图,寻 ...

  5. HDU 4408 Minimum Spanning Tree 最小生成树计数

    Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. 【算法】关于图论中的最小生成树(Minimum Spanning Tree)详解

    本节纲要 什么是图(network) 什么是最小生成树 (minimum spanning tree) 最小生成树的算法 什么是图(network)? 这里的图当然不是我们日常说的图片或者地图.通常情 ...

  7. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

  8. Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  9. hdu 4408 Minimum Spanning Tree

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

随机推荐

  1. CentOS7安装后连不上网络无法使用yum

    更新日期:2018年5月31日 笔者今天在本地VMware中安装了CentOS7后,使用yum安装wget的时候发现不能下载,并有下图所示的提示: 于是,笔者就去问度娘,然后就找到了如下各种回复: 1 ...

  2. vue+SSM验证码实现

    源码:https://github.com/HannahLihui/StudentManager-SSM/tree/master/SSM-git/StudentManager-SSM-master 1 ...

  3. elasticsearch 6.3 安装手记

    系统环境 centos 7 elasticsearch 6.3 需要 JDK 8 版本,先安装 JDK 8. ES6.3 安装地址: https://www.elastic.co/guide/en/e ...

  4. xshell复制粘贴

    用户看到这个标题肯定会觉得小编脑子坏掉了,复制粘贴不就是Ctrl+C,Ctrl+V嘛,但是在xshell却不尽然. 现象: 在xshell界面中需要用到之前的一段代码,自然是选中,熟练的键入Ctrl+ ...

  5. styled-components 弃用 injectGlobal

    styled-components 最新版本是v4.1.2,但是从v4开始,就酱原来的injectGlobal方法用createGlobalStyle替换了.用法上也有一些不同了: 我今天直接引inj ...

  6. vue-video监听touch事件

    vue-video是基于 Vue 的简洁 HTML5 视频播放器组件,但是并没有监听touch事件,也就是说在移动端按键无效. 本文讲述如何改写其vue组件,使其兼容移动端.只需要在其原有的mouse ...

  7. Java基础学习—思维导图

    找到两张Java学习的思维导图,特别适合我这样的菜鸟学习,贴过来和小伙伴分享.

  8. 大数据平台的技术演化之路 诸葛io平台设计实例

    如今,数据分析能力正逐渐成为企业发展的标配,企业通过数据分析的过程将数据中的信息提取出来,进行处理.识别.加工.呈现,最后成为指导企业业务发展的知识和智慧.而处理.识别.加工.呈现的过程从本质上来讲, ...

  9. Spring Boot—17MongoDB

    在MongoDB中插入如下的数据 db.baike.insert( { _id: 'freemark', desc: '新一代模板语言', tag: [ 'IT', '模板语言' ], comment ...

  10. Java实验案例(接口)

    实验任务 任务一:设计实现发声接口 任务二:动物乐园 实验内容 任务一:设计实现发声接口 任务目的: 理解并掌握如何定义接口 掌握接口的实现方式 任务描述: 设计和实现一个Soundable接口,该接 ...