亚马逊服务器做影视网站wordpress重新设置域名
web/
2025/10/1 11:00:04/
文章来源:
亚马逊服务器做影视网站,wordpress重新设置域名,我想找工作没学历,app开发需要多少费用介绍【0】README
0.1#xff09; 本文总结于 数据结构与算法分析#xff0c; 源代码均为原创#xff0c; 旨在 理解 Kruskal#xff08;克鲁斯卡尔#xff09;算法 的idea 并用 源代码加以实现#xff1b; 0.2#xff09;最小生成树的基础知识#xff0c;参见 http://blo…【0】README
0.1 本文总结于 数据结构与算法分析 源代码均为原创 旨在 理解 Kruskal克鲁斯卡尔算法 的idea 并用 源代码加以实现 0.2最小生成树的基础知识参见 http://blog.csdn.net/pacosonswjtu/article/details/49947085 【1】 Kruskal 算法使用到了不相交集ADT的union/find 操作
1.1第二种贪婪策略是 连续地按照最小的权选择边 并且当所选的边不产生圈时就可以吧它作为取定的边 1.2形式上 Kruskal算法是在处理一个森林——树的集合。 开始的时候 存在 |V|颗单节点树 而添加一边则将两棵树合并成一颗树 当算法终止的时候就只有一棵树了 这颗树就是最小生成树 1.3Kruskal算法的执行步骤 如下图所示看个荔枝 对上图的分析Analysis
A1当添加到森林中的边足够多时算法终止 实际上 算法就是要决定边u, v应该添加还是放弃。前一章节中的 Union/Find 算法在这里适用
1.4我们用到了一个恒定的事实在算法实施的任意时刻两个顶点属于同一个集合当且仅当它们在当前的森林中连通因此 每个顶点最初是在它自己的集合中
1.4.1 如果u 和 v 在同一个集合中 那么连接它们的边就要放弃 因为由于它们已经连通 了如果再添加一条边u, v就会形成一个圈了。1.4.2如果这两个顶点不在同一个集合中 则将该边加入 并对包含顶点u 和 v 的这两个集合实施一次合并。1.4.3容易看到这样将保持集合不变性 因为一旦边u, v添加到生成森林中 若w连通到 u 而 x连通到v 则x 和 w 必然是连通的 因此属于相同集合
1.5固然将边排序可便于选取不过用线性时间建立一个堆则是更好的想法
1.5.1此时 deleteMin 将使得边依序得到测试。 典型情况下 在算法终止前只有一小部分边需要测试 尽管测试所有的边的情况是有可能的例如还有一个顶点 v8以及值为100的边v5, v8那么所有的边都会要考察到
1.6因为一条边由3个部分的数据组成 所以在某些机器上吧优先队列实现成指向边的指针数组比实现成边的数组更为有效。
1.6.1这种实现的 效果在于 为重新排列堆 需要移动的只有那些指针 而大量的记录则不必移动
1.7时间复杂度该算法的最坏情形运行时间为 O|E|log|E| 它受堆操作控制。 注意 由于 |E|O(|V|^2) 因此这个运行时间实际上是 O|E|log|V|。在实践中 该算法要比这个时间界指示的时间快得多 【2】source code printing results将我的代码打印结果 同 “1.3” 上图中的手动模拟的 Kruskal 算法的结果进行比较你会发现 它们的结果完全相同这也证实了我的代码的可行性
2.0code specification
s1本代码采用了优先队列二叉小根堆来升序选取边s2本代码采用了用到了不相交集ADT的 find和setUion 操作来对边的两个vertexes 进行union 操作以及更新它们的根s3对于根的初始化我是这样初始化的—— parent[0]-1,parent[1]-2, parent[2]-3, parent 说白了就是 set的 一个 index 所以开始肯定是不一样的 然后在union的时候我只要检查是否 i -parent[i]-1 就可以知道 它是否是树的根s4 在合并的时候要对边的两个顶点 start 和 end 的 parent做update 这里涉及到4种情况—— start为根且end不为根start为根且end为根start为不为根且end为根start不为根且end不为根 干货本代码的重中之重以及新颖之处
2.1download source code https://github.com/pacosonTang/dataStructure-algorithmAnalysis/tree/master/chapter9/p240_kruskal 2.2source code at a glancefor complete code , please click the given link above
#include stdio.h
#include binaryheap.h// allocate memory for the vertexes with size
Vertex* makeEmptyVertexes(int size)
{Vertex *array; int i;array (Vertex*)malloc(size * sizeof(Vertex)); if(!array){Error(out of space, from func makeEmptyVertexes);return NULL;} // initializing the set index towards every vertex with its array indexfor(i 1; i size; i)array[i-1] -i;return array;
}void printParent(Vertex* vertexes, int size)
{int i;printf(\n\n\t the parent of every vertex at a glance);for(i0; isize; i)printf(\n\t parent[%d] %d, i, vertexes[i]);
}int find(Vertex *parent, Vertex single){while (single 0)single parent[single]; return single;
}//judge whether the vertex index is the parent or not, also 1 or 0
//if the vertex under index is not the parent ,thats to say its parent is one of other vertexes
int isParent(Vertex *parent, Vertex index)
{return parent[index] -index-1;
}void setUnion(Vertex *parent, Vertex start, Vertex end)
{ if(isParent(parent, start) ) // start is the parent {if(!isParent(parent, end)) // but end is not the parentend find(parent, end) 1; // find the parent of endparent[start] end; }else // start is not the parent {start -find(parent, start) - 1; // find the parent of startif(!isParent(parent, end)) // but end is not the parentend find(parent, end) 1; // find the parent of endparent[end] start;}
}void kruskal(BinaryHeap bh, int vertexNum)
{int counter;int set1;int set2; Vertex start;Vertex end;Vertex* parent;ElementType singleEdge; counter 0; parent makeEmptyVertexes(vertexNum);while(counter vertexNum - 1){singleEdge deleteMin(bh);start singleEdge.start;end singleEdge.end;set1 find(parent, start);set2 find(parent, end);// find the set of vertex start and endif(set1 ! set2){ setUnion(parent, start, end);counter;printf(\n\t weight(v%d,v%d) %d, singleEdge.start1, singleEdge.end1, singleEdge.weight);} }printParent(parent, vertexNum);printf(\n\n\t);
}int main()
{ BinaryHeap bh;ElementTypePtr temp; int vertexNum;int size 7;int capacity;int i;int j; int adjTable[7][7] {{0, 2, 4, 1, 0, 0, 0},{2, 0, 0, 3, 10, 0, 0},{4, 0, 0, 2, 0, 5, 0},{1, 3, 2, 0, 7, 8, 4},{0, 10, 0, 7, 0, 0, 6},{0, 0, 5, 8, 0, 0, 1},{0, 0, 0, 4, 6, 1, 0},}; vertexNum 7;capacity vertexNum * vertexNum;bh initBinaryHeap(capacity);temp makeEmptyElement();printf(\n\n\t test for kruskal alg building minimum spanning tree \n);//building binary heap with edge including 2 vertexs and its weight for(i 0; i size; i){for(j i1; j size; j) if(adjTable[i][j]) { temp-start i;temp-end j;temp-weight adjTable[i][j]; insertHeap(temp, bh); // insertAdj the adjoining table over}}kruskal(bh, vertexNum);return 0;
} // allocate memory for the array with size
ElementTypePtr *makeEmptyArray(int size)
{ElementTypePtr *array;int i;array (ElementTypePtr*)malloc(size * sizeof(ElementTypePtr)); if(!array){Error(out of space, from func makeEmptyArray);return NULL;}for(i0; isize; i) array[i] makeEmptyElement(); return array;
}// allocate memory for the single element
ElementTypePtr makeEmptyElement()
{ElementTypePtr temp;temp (ElementTypePtr)malloc(sizeof(ElementType));if(!temp){Error(out of space, from func makeEmptyElement!);return NULL;}return temp;
}
2.3printing results
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/85016.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!