Write the algorithm step by step (in 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]

 

 

 

 

C) write the minimal spanning tree, involving the process of creating, selecting, and adding

 

 

MINI_GENERATE_TREE * get_mini_tree_from_graph (GRAPH * pGraph)

{

MINI_GENERATE_TREE * pMiniTree;

DIR_LINE pDirLine;

 

If (NULL = pGraph | NULL = pGraph-> head)

Return NULL;

 

PMiniTree = (MINI_GENERATE_TREE *) malloc (sizeof (MINI_GENERATE_TREE ));

Assert (NULL! = PMiniTree );

Memset (pMiniTree, 0, sizeof (MINI_GENERATE_TREE ));

 

PMiniTree-> node_num = 1;

PMiniTree-> pNode = (int *) malloc (sizeof (int) * pGraph-> count );

Memset (pMiniTree-> pNode, 0, sizeof (int) * pGraph-> count );

PMiniTree-> pNode [0] = pGraph-> head-> start;

 

While (1 ){

Memset (& pDirLine, 0, sizeof (DIR_LINE ));

Get_dir_line_from_graph (pGraph, pMiniTree, & pDirLine );

If (pDirLine. start = 0)

Break;

 

PMiniTree-> line_num ++;

Insert_line_pai_queue (& pMiniTree-> pLine, pDirLine. start, pDirLine. end, pDirLine. weight );

Insert_node_into_mini_tree (& pDirLine, pMiniTree );

}

 

Return pMiniTree;

}

MINI_GENERATE_TREE * get_mini_tree_from_graph (GRAPH * pGraph)

{

MINI_GENERATE_TREE * pMiniTree;

DIR_LINE pDirLine;

 

If (NULL = pGraph | NULL = pGraph-> head)

Return NULL;

 

PMiniTree = (MINI_GENERATE_TREE *) malloc (sizeof (MINI_GENERATE_TREE ));

Assert (NULL! = PMiniTree );

Memset (pMiniTree, 0, sizeof (MINI_GENERATE_TREE ));

 

PMiniTree-> node_num = 1;

PMiniTree-> pNode = (int *) malloc (sizeof (int) * pGraph-> count );

Memset (pMiniTree-> pNode, 0, sizeof (int) * pGraph-> count );

PMiniTree-> pNode [0] = pGraph-> head-> start;

 

While (1 ){

Memset (& pDirLine, 0, sizeof (DIR_LINE ));

Get_dir_line_from_graph (pGraph, pMiniTree, & pDirLine );

If (pDirLine. start = 0)

Break;

 

PMiniTree-> line_num ++;

Insert_line_pai_queue (& pMiniTree-> pLine, pDirLine. start, pDirLine. end, pDirLine. weight );

Insert_node_into_mini_tree (& pDirLine, pMiniTree );

}

 

Return pMiniTree;

}

D) construct a selection function and select the most appropriate edge

 

Void get_dir_line_from_graph (GRAPH * pGraph, MINI_GENERATE_TREE * pMiniTree, DIR_LINE * pDirLine)

{

DIR_LINE * pHead;

DIR_LINE * prev;

VECTEX * pVectex;

LINE * pLine;

Int index;

Int start;

 

PHead = NULL;

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

Start = pMiniTree-> pNode [index];

PVectex = find_vectex_in_graph (pGraph-> head, start );

PLine = pVectex-> neighbor;

 

While (pLine ){

Insert_line_pai_queue (& pHead, start, pLine-> end, pLine-> weight );

PLine = pLine-> next;

}

}

 

If (NULL = pHead)

Return;

 

Delete_unvalid_line_from_list (& pHead, pMiniTree );

If (NULL = pHead)

Return;

 

Sort_for_line_list (& pHead );

Memmove (pDirLine, pHead, sizeof (DIR_LINE ));

 

While (pHead ){

Prev = pHead;

PHead = pHead-> next;

Free (prev );

}

Return;

}

Void get_dir_line_from_graph (GRAPH * pGraph, MINI_GENERATE_TREE * pMiniTree, DIR_LINE * pDirLine)

{

DIR_LINE * pHead;

DIR_LINE * prev;

VECTEX * pVectex;

LINE * pLine;

Int index;

Int start;

 

PHead = NULL;

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

Start = pMiniTree-> pNode [index];

PVectex = find_vectex_in_graph (pGraph-> head, start );

PLine = pVectex-> neighbor;

 

While (pLine ){

Insert_line_pai_queue (& pHead, start, pLine-> end, pLine-> weight );

PLine = pLine-> next;

}

}

 

If (NULL = pHead)

Return;

 

Delete_unvalid_line_from_list (& pHead, pMiniTree );

If (NULL = pHead)

Return;

 

Sort_for_line_list (& pHead );

Memmove (pDirLine, pHead, sizeof (DIR_LINE ));

 

While (pHead ){

Prev = pHead;

PHead = pHead-> next;

Free (prev );

}

Return;

}

 

 

 

E) Add a node function to include vertices that are not yet the minimum spanning tree into the minimum spanning tree.

 

 

Void insert_node_into_mini_tree (DIR_LINE * pLine, MINI_GENERATE_TREE * pMiniTree)

{

Int index;

 

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

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

PMiniTree-> pNode [pMiniTree-> node_num ++] = pLine-> end;

Return;

}

}

 

PMiniTree-> pNode [pMiniTree-> node_num ++] = pLine-> start;

Return;

}

Void insert_node_into_mini_tree (DIR_LINE * pLine, MINI_GENERATE_TREE * pMiniTree)

{

Int index;

 

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

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

PMiniTree-> pNode [pMiniTree-> node_num ++] = pLine-> end;

Return;

}

}

 

PMiniTree-> pNode [pMiniTree-> node_num ++] = pLine-> start;

Return;

}

Note:

 

(1) d and e are subfunctions called in c. If you observe them, you will understand them.

 

(2) The Minimum Spanning Tree is written in the top-down order. Although the subfunctions in c are completed, two subfunctions in d are not located.

 

(3) functions delete_unvalid_line_from_list and sort_for_line_list in d will be further introduced in the next article.

 

(4) As long as the algorithm can be compiled according to the manual calculation process, it is basically not a problem, but some details should be noted with caution.

 

 

 

 

 

[To be continued]

 

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.