graph-Kruskal-algorithm

并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。
主要操作:
1. 初始化:每个点所在集合初始化为其自身。
2. 查找:查找元素所在的集合,即根节点。
3. 合并:合并之前要先判断两个元素是否属于同一集合,“查找”操作。


#include <iostream>
#include <queue>
#define max 200
using namespace std;

int minSpanningTree;
int nodeNum;
int father[max];
// http://blog.csdn.net/stary_yan/article/details/51427864#kruskal算法
struct edge {
    int start;
    int end;
    int weight;
    friend bool operator < (const edge& a, const edge& b) {
        return a.weight > b.weight;
        }
    };
priority_queue<edge> SQ;

int find(int x) {
    return x = father[x] ? x : find(father[x]);
    }

int kruskal() {
    minSpanningTree = 0;
    for ()
    }

  

时间: 2024-11-03 21:13:35

graph-Kruskal-algorithm的相关文章

C++ 实现MST kruskal&#39;s algorithm

#include<iostream> #include<vector> #include<list> #include<iomanip> #include<algorithm> using namespace std; enum{ INF = INT_MAX }; struct Edge{ int from; int to; int Wgt; Edge(int _from, int _to, int _Wgt = INF) : from(_fro

HDOJ 题目4408 Minimum Spanning Tree(Kruskal+Matrix_Tree)

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1408    Accepted Submission(s): 450 Problem Description XXX is very interested in algorithm. After learning the Prim algori

邻接表c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

graph.c #include <stdio.h> #include <stdlib.h> #include <limits.h> #include "aqueue.h" #define MAX_NUM 100 typedef char node_type; typedef struct arc_node { int pos; int distance; struct arc_node * next; } Arc_node;//保存Node节点的相

经典算法题每日演练——第十六题 Kruskal算法

原文:经典算法题每日演练--第十六题 Kruskal算法 这篇我们看看第二种生成树的Kruskal算法,这个算法的魅力在于我们可以打一下算法和数据结构的组合拳,很有意思的. 一:思想 若存在M={0,1,2,3,4,5}这样6个节点,我们知道Prim算法构建生成树是从”顶点”这个角度来思考的,然后采用“贪心思想” 来一步步扩大化,最后形成整体最优解,而Kruskal算法有点意思,它是站在”边“这个角度在思考的,首先我有两个集合. 1. 顶点集合(vertexs): 比如M集合中的每个元素都可以认

HackerRank &quot;Kruskal (MST): Really Special Subtree&quot;

Kruskal Algorithm is based on Union-Find - quite intuitive. #include <vector> #include <iostream> #include <queue> #include <unordered_map> #include <unordered_set> using namespace std; struct Edge { Edge() :s(0), t(0), d(0)

Graph visualization library in JavaScript (转)

Graph visualization library in JavaScript I've just put together what you may be looking for: http://www.graphdracula.net It's JavaScript with directed graph layouting, SVG and you can even drag the nodes around. Still needs some tweaking, but is tot

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

Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertex

贪心算法正确性证明(转载from刘子韬)

这里主要是介绍一种证明贪心算法是最优的一种方法:Exchange Argument (不知道应该怎么翻译到中文,交换参数?感觉听起来挺别扭的,不像是一个方法的名字~o(╯□╰)o) Exchange Argument的主要的思想也就是 先假设 存在一个最优的算法和我们的贪心算法最接近,然后通过交换两个算法里的一个步骤(或元素),得到一个新的最优的算法,同时这个算法比前一个最优算法更接近于我们的贪心算法,从而得到矛盾,原命题成立. 下面来看一个更为formal的解释: 步骤: Step0: 给出贪

最小生成树系列

背景: What is the minimum spanning tree? Given a connected , undirected graph, a spnning tree without circle of that graph is a subgraph that is a tree and connects all the vertices together. A single graph an have more than one spanning tree.If we ass

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

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX