Minimum Spanning Trees
Kruskal’s algorithm
always union the lightest link if two sets haven't been linked
typedef struct
{
char vertex[VertexNum]; //顶点表
int edges[VertexNum][VertexNum]; //邻接矩阵,可看做边表
int n,e; //图中当前的顶点数和边数
}MGraph; typedef struct node
{
int u; //边的起始顶点
int v; //边的终止顶点
int w; //边的权值
}Edge; void kruskal(MGraph G)
{
int i,j,u1,v1,sn1,sn2,k;
int vset[VertexNum]; //辅助数组,判定两个顶点是否连通
int E[EdgeNum]; //存放所有的边
k=0; //E数组的下标从0开始
for (i=0;i<G.n;i++)
{
for (j=0;j<G.n;j++)
{
if (G.edges[i][j]!=0 && G.edges[i][j]!=INF)
{
E[k].u=i;
E[k].v=j;
E[k].w=G.edges[i][j];
k++;
}
}
}
heapsort(E,k,sizeof(E[0])); //堆排序,按权值从小到大排列
for (i=0;i<G.n;i++) //初始化辅助数组
{
vset[i]=i;
}
k=1; //生成的边数,最后要刚好为总边数
j=0; //E中的下标
while (k<G.n)
{
sn1=vset[E[j].u];
sn2=vset[E[j].v]; //得到两顶点属于的集合编号
if (sn1!=sn2) //不在同一集合编号内的话,把边加入最小生成树
{
printf("%d ---> %d, %d",E[j].u,E[j].v,E[j].w);
k++;
for (i=0;i<G.n;i++)
{
if (vset[i]==sn2)
{
vset[i]=sn1;
}
}
}
j++;
}
}
Prim’s algorithm
maintain a key of each vertex to represent the lightest link which connected the vertex
initial all the key as maximum, begin from a vertex and update the adjoin vertex key.
#define MAX 100000
#define VNUM 10+1 //这里没有ID为0的点,so id号范围1~10 int edge[VNUM][VNUM]={/*输入的邻接矩阵*/};
int lowcost[VNUM]={0}; //记录Vnew中每个点到V中邻接点的最短边
int addvnew[VNUM]; //标记某点是否加入Vnew
int adjecent[VNUM]={0}; //记录V中与Vnew最邻近的点 void prim(int start)
{
int sumweight=0;
int i,j,k=0; for(i=1;i<VNUM;i++) //顶点是从1开始
{
lowcost[i]=edge[start][i];
addvnew[i]=-1; //将所有点至于Vnew之外,V之内,这里只要对应的为-1,就表示在Vnew之外
} addvnew[start]=0; //将起始点start加入Vnew
adjecent[start]=start; for(i=1;i<VNUM-1;i++)
{
int min=MAX;
int v=-1;
for(j=1;j<VNUM;j++)
{
if(addvnew[j]!=-1&&lowcost[j]<min) //在Vnew之外寻找最短路径
{
min=lowcost[j];
v=j;
}
}
if(v!=-1)
{
printf("%d %d %d\n",adjecent[v],v,lowcost[v]);
addvnew[v]=0; //将v加Vnew中 sumweight+=lowcost[v]; //计算路径长度之和
for(j=1;j<VNUM;j++)
{
if(addvnew[j]==-1&&edge[v][j]<lowcost[j])
{
lowcost[j]=edge[v][j]; //此时v点加入Vnew 需要更新lowcost
adjecent[j]=v;
}
}
}
}
printf("the minmum weight is %d",sumweight);
}
Minimum Spanning Trees的更多相关文章
- 【2018 ICPC亚洲区域赛徐州站 A】Rikka with Minimum Spanning Trees(求最小生成树个数与总权值的乘积)
Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a ...
- Gym102012A Rikka with Minimum Spanning Trees
题意 \(T\) 组数据,每组数据给定一个 \(n\) 个点,\(m\) 条边,可能含有重边自环的图,求出最小生成树的个数与边权和的乘积,对 \(10^9+7\) 取模. \(\texttt{Data ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- HDU 4408 Minimum Spanning Tree 最小生成树计数
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 4408 Minimum Spanning Tree
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- 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 ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
- Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
随机推荐
- 在chrome上隐藏video的option按钮
隐藏方法: video::-webkit-media-controls{ overflow:hidden !important;}video::-webkit-media-controls-enclo ...
- python-css基础知识
CSS 是指层叠样式表 (Cascading Style Sheets),样式定义如何显示html,用于对页面进行美化. css样式的存放位置有三种: 第一种,直接写在html标签里: <div ...
- springboot 使用Filter
1. 创建 Filter 类,实现 Filter 接口 import javax.servlet.*; import javax.servlet.annotation.WebFilter; impor ...
- 自动化定位——通过XPath定位元素
XPath是一种XML文档中定位元素的语言.该定位方式也是比较常用的定位方式 1通过属性定位元素 find_element_by_xpath("//标签名[@属性='属性值']") ...
- h5页面避免两个页面反复跳转死循环
项目中经常会碰到在一个页面加判断之后跳转另一个页面,并且无法返回,来回跳转,死循环,遇到这种情况可以在跳转时使用location,replace,关闭之前页面,与location.href 两者的不同 ...
- Vue+Element+computed实现购物车
本人写的小程序,功能还在完善中,欢迎扫一扫提出宝贵意见! 该购物车效果采用的是Element-ui库实现的. 采用了computed计算属性来实现逻辑操作. 功能分析: 全选/全不 ...
- 【nowcoder】 4th T1 动态点分治
题目链接:https://www.nowcoder.com/acm/contest/175/A 题目名字吓死人 std: 我 太可啪了 一道简单的模拟题.虽然我把题意想错了. 按照题意模拟输出和继承. ...
- 让input不可编辑
有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...
- sface
单步检测方法分为两类:anchor-based如ssd.RetinaNet;2)Anchor-free 如DenseBox.UnitBox;anchor-based处理的尺度范围虽小,更精准:anch ...
- Linux SSH登录服务器报ECDSA host key "ip地址" for has changed and you have requested strict checking错误
错误:ECDSA host key "ip地址" for has changed and you have requested strict checking. 解决方案:在终端 ...