Step-by-step write algorithm (prim algorithm)

Source: Internet
Author: User

Original: Step by step write algorithm (prim algorithm)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Top two blogs We have discussed the prim minimum spanning tree algorithm and are familiar with the basic process. Basically, we write code in the top-down order. First, we build a structure, and then step by step to complete each of the sub-functions, so that finally constitute a complete prim algorithm calculation process.


F) Remove non-conforming data from the Dir_line queue, primarily 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;elsepcur = prev->next;continue;} Prev = Pcur;pcur = Pcur->next;} return;}

g) The function of determining dir_line legitimacy is used in the F) function, which we need to refine.

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 is to sort the dir_line data that is currently in the queue, in fact List Sort .

void Insert_for_sort_operation (dir_line** ppnode, dir_line* pnode)  {      dir_line* prev;      dir_line* cur;      /* Insert Pnode *      /if (Pnode->weight < (*ppnode)->weight) before first Data {          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) The algorithm itself has improved space, such as memory allocation is not to be rebuilt every time the Dir_line queue is open to discussion

2) algorithm writing is not a position, in the middle there are repeated more and more, write four or five times is very normal thing

3) When writing code, it is best to do edge modification, edge testing, which can increase the robustness of the code, on the one hand can improve their confidence

4) If possible, you can reuse previously written, stable algorithm code, such as sort, find, stack, binary tree and other code


Step-by-step write algorithm (prim algorithm)

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.