设为首页 加入收藏

TOP

一步一步写算法(之克鲁斯卡尔算法 中) (二)
2014-11-24 00:04:09 来源: 作者: 【 】 浏览:40
Tags:步一步 算法 克鲁斯 卡尔
int value = 0;

int number = 0;

for(index = 0; index < length; index ++){

number = getVectexNumFromTree(pTree[index], start, end);

if(number > value)

value = number;

}

return (value == 2) 1 : 0;

} 线段的判断之后,如果发现在两颗独立的最小生成树上面,那么还需要进行合并操作,删除其中一个最小生成树,把另外一个生成树的所有点和线段都要添加到没有删除的这颗最小生成树上面。当然还有一点不要忘记了,最后还要加上端口分别在两棵树上的这个线段。

void mergeTwoMiniGenerateTree(MINI_GENERATE_TREE* pTree[], int length, int start, int end, int weight)

{

MINI_GENERATE_TREE* pFirst;

MINI_GENERATE_TREE* pSec;

DIR_LINE* line;

int index;

/* 删除多余的最小生成树*/

pFirst = find_tree_by_index(start);

pSec = find_tree_by_index(end);

delete_mini_tree_from_group(pTree, length, pSec);

/* 合并节点*/

for(index = 0; index < pSec->node_num; index ++){

pFirst->pNode[pFirst->node_num + index] = pSec->pNode[index];

}

pFirst->node_num += pSec->node_num;

/* 合并线段*/

for(line = pSec->pLine; line; line = line->next){

insert_line_into_queue(&pFirst->pLine, line->start, line->end, line->weight);

}

insert_line_into_queue(&pFirst->pLine, start, end, weight);

/* 函数返回*/

return;

}

void mergeTwoMiniGenerateTree(MINI_GENERATE_TREE* pTree[], int length, int start, int end, int weight)

{

MINI_GENERATE_TREE* pFirst;

MINI_GENERATE_TREE* pSec;

DIR_LINE* line;

int index;

/* 删除多余的最小生成树*/

pFirst = find_tree_by_index(start);

pSec = find_tree_by_index(end);

delete_mini_tree_from_group(pTree, length, pSec);

/* 合并节点*/

for(index = 0; index < pSec->node_num; index ++){

pFirst->pNode[pFirst->node_num + index] = pSec->pNode[index];

}

pFirst->node_num += pSec->node_num;

/* 合并线段*/

for(line = pSec->pLine; line; line = line->next){

insert_line_into_queue(&pFirst->pLine, line->start, line->end, line->weight);

}

insert_line_into_queue(&pFirst->pLine, start, end, weight);

/* 函数返回*/

return;

}

文章总结:

(1)本篇主要介绍了克鲁斯卡尔算法编写中需要处理的三个问题,排序、查找和合并

(2)复杂的函数都是由简单的函数构造而成的,我们可以把算法分成几个独立的部分,各个击破

(3)解决了这三个问题,下一篇博客就可以进行归总分析处理,逻辑上也十分清晰了

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇设置系统时间 系统时间网络更新 下一篇怎么用C语言动态往sqlite3里面插..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: