Write the algorithm step by step (under the prim algorithm)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

In the first two blogs, we discussed the algorithm of the prim minimal spanning tree and were familiar with the basic process. Basically, we write code in the top-down order. First, we build an architecture, and then complete each of the sub-functions step by step, so as to form a complete prim algorithm computing process.

 

 

 

 

F) Delete the non-conforming data in the DIR_LINE queue, mainly the DIR_LINE data that has been accessed by both nodes.

 

 

Void delete_unvalid_line_from_list (DIR_LINE ** ppHead, MINI_GENERATE_TREE * pMiniTree)

{

DIR_LINE * prev;

DIR_LINE * pcur;

STATUS result;

 

Prev = NULL;

Pcur = * ppHead;

While (pcur ){

If (! Check_valid_for_line (pcur, pMiniTree )){

Result = delete_line_from_queue (ppHead, pcur );

Assert (TRUE = result );

 

If (NULL = prev)

Pcur = * ppHead;

Else

Pcur = prev-> next;

 

Continue;

}

 

Prev = pcur;

Pcur = pcur-> next;

}

 

Return;

}

Void delete_unvalid_line_from_list (DIR_LINE ** ppHead, MINI_GENERATE_TREE * pMiniTree)

{

DIR_LINE * prev;

DIR_LINE * pcur;

STATUS result;

 

Prev = NULL;

Pcur = * ppHead;

While (pcur ){

If (! Check_valid_for_line (pcur, pMiniTree )){

Result = delete_line_from_queue (ppHead, pcur );

Assert (TRUE = result );

 

If (NULL = prev)

Pcur = * ppHead;

Else

Pcur = prev-> next;

 

Continue;

}

 

Prev = pcur;

Pcur = pcur-> next;

}

 

Return;

}

G) f) The function used to determine the validity of DIR_LINE needs to be improved.

 

 

Int check_valid_for_line (DIR_LINE * pDirLine, MINI_GENERATE_TREE * pMiniTree)

{

Int index;

Int flag_start;

Int flag_end;

 

Flag_start = 0;

Flag_end = 0;

 

For (index = 0; index <pMiniTree-> node_num; index ++ ){

If (pDirLine-> start = pMiniTree-> pNode [index]) {

Flag_start = 1;

Break;

}

}

 

For (index = 0; index <pMiniTree-> node_num; index ++ ){

If (pDirLine-> end = pMiniTree-> pNode [index]) {

Flag_end = 1;

Break;

}

}

 

Return (1 = flag_start & 1 = flag_end )? 0: 1;

}

Int check_valid_for_line (DIR_LINE * pDirLine, MINI_GENERATE_TREE * pMiniTree)

{

Int index;

Int flag_start;

Int flag_end;

 

Flag_start = 0;

Flag_end = 0;

 

For (index = 0; index <pMiniTree-> node_num; index ++ ){

If (pDirLine-> start = pMiniTree-> pNode [index]) {

Flag_start = 1;

Break;

}

}

 

For (index = 0; index <pMiniTree-> node_num; index ++ ){

If (pDirLine-> end = pMiniTree-> pNode [index]) {

Flag_end = 1;

Break;

}

}

 

Return (1 = flag_start & 1 = flag_end )? 0: 1;

}

H) The last step is to sort the DIR_LINE data that is already in the queue, which is actually the chain table sorting.

 

 

Void insert_for_sort_operation (DIR_LINE ** ppNode, DIR_LINE * pNode)

{

DIR_LINE * prev;

DIR_LINE * cur;

/* Insert pNode before the first data */

If (pNode-> weight <(* ppNode)-> weight ){

PNode-> next = * ppNode;

* PpNode = pNode;

Return;

}

Cur = * ppNode;

While (cur ){

If (pNode-> weight <cur-> weight)

Break;

Prev = cur;

Cur = cur-> next;

}

PNode-> next = prev-> next;

Prev-> next = pNode;

Return;

}

 

Void sort_for_line_list (DIR_LINE ** ppNode)

{

DIR_LINE * prev;

DIR_LINE * curr;

If (NULL = ppNode | NULL = * ppNode)

Return;

Curr = (* ppNode)-> next;

(* PpNode)-> next = NULL;

While (curr ){

Prev = curr;

Curr = curr-> next;

Insert_for_sort_operation (ppNode, prev );

}

}

Void insert_for_sort_operation (DIR_LINE ** ppNode, DIR_LINE * pNode)

{

DIR_LINE * prev;

DIR_LINE * cur;

/* Insert pNode before the first data */

If (pNode-> weight <(* ppNode)-> weight ){

PNode-> next = * ppNode;

* PpNode = pNode;

Return;

}

Cur = * ppNode;

While (cur ){

If (pNode-> weight <cur-> weight)

Break;

Prev = cur;

Cur = cur-> next;

}

PNode-> next = prev-> next;

Prev-> next = pNode;

Return;

}

 

Void sort_for_line_list (DIR_LINE ** ppNode)

{

DIR_LINE * prev;

DIR_LINE * curr;

If (NULL = ppNode | NULL = * ppNode)

Return;

Curr = (* ppNode)-> next;

(* PpNode)-> next = NULL;

While (curr ){

Prev = curr;

Curr = curr-> next;

Insert_for_sort_operation (ppNode, prev );

}

}

 

 

 

Algorithm summary:

 

1) there is still room for improvement in the algorithm itself. For example, whether the DIR_LINE queue needs to be rebuilt every time the memory is allocated remains to be discussed.

 

2) algorithm writing is not in place. It is normal to write algorithms for four or five times.

 

3) it is best to modify and test code while writing it. This increases the robustness of the code and improves your confidence.

 

4) If possible, you can reuse the previously written and stable algorithm code, such as sorting, searching, stack, binary tree, and other code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.