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

 

 

 

 

As mentioned above, the cruise Kar algorithm is added according to the weights of each line in sequence, which involves the sorting of weights. How to sort it? You can use the simplest Bubble Sorting Algorithm. However, the data structure is sorted here. What should I do? Then we have to adopt a general sorting algorithm.

 

Void bubble_sort (void * array [], int length, int (* compare) (void *, void *), void (* swap) (void **, void **))

{

Int outer;

Int inner;

For (outer = length-1; outer> 0; outer --){

For (inner = 0; inner <outer; inner ++ ){

If (compare (array [inner], array [inner + 1])

Swap (& array [inner], & array [inner + 1]);

}

}

Return;

}

Void bubble_sort (void * array [], int length, int (* compare) (void *, void *), void (* swap) (void **, void **))

{

Int outer;

Int inner;

For (outer = length-1; outer> 0; outer --){

For (inner = 0; inner <outer; inner ++ ){

If (compare (array [inner], array [inner + 1])

Swap (& array [inner], & array [inner + 1]);

}

}

Return;

} So, we need to add the compare and swap functions belonging to DIR_LINE,

 

Int compare (void * data1, void * data2)

{

DIR_LINE * line1 = (DIR_LINE *) data1;

DIR_LINE * line2 = (DIR_LINE *) data2;

Return (line1-> weight> line2-> weight )? 1: 0;

}

 

Void swap (void ** data1, void ** data2)

{

DIR_LINE * median;

DIR_LINE ** line1;

DIR_LINE ** line2;

 

Line1 = (DIR_LINE **) data1;

Line2 = (DIR_LINE **) data2;

 

Median = * line1;

* Line1 = * line2;

* Line2 = median;

}

Int compare (void * data1, void * data2)

{

DIR_LINE * line1 = (DIR_LINE *) data1;

DIR_LINE * line2 = (DIR_LINE *) data2;

Return (line1-> weight> line2-> weight )? 1: 0;

}

 

Void swap (void ** data1, void ** data2)

{

DIR_LINE * median;

DIR_LINE ** line1;

DIR_LINE ** line2;

 

Line1 = (DIR_LINE **) data1;

Line2 = (DIR_LINE **) data2;

 

Median = * line1;

* Line1 = * line2;

* Line2 = median;

} After sorting, we start inserting line segments. when inserting line segments, we need to know if the current line segment already exists in a minimum spanning tree, if so, the line segment will be ignored. Therefore, there is still a judgment problem in the middle,

 

Int getVectexNumFromTree (MINI_GENERATE_TREE * pTree, int start, int end)

{

Int index;

Int total;

 

Total = 0;

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

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

Total ++;

Continue;

}

 

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

Total ++;

}

}

 

Return total;

}

 

Int isDoubleVectexExistInTree (MINI_GENERATE_TREE * pTree [], int length, int start, int end)

{

Int index = 0;

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;

}

Int getVectexNumFromTree (MINI_GENERATE_TREE * pTree, int start, int end)

{

Int index;

Int total;

 

Total = 0;

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

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

Total ++;

Continue;

}

 

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

Total ++;

}

}

 

Return total;

}

 

Int isDoubleVectexExistInTree (MINI_GENERATE_TREE * pTree [], int length, int start, int end)

{

Int index = 0;

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;

} After determining a line segment, if two independent least spanning trees are found, merge the two trees to delete one of them, add all the points and line segments of the other spanning tree to the minimal spanning tree that has not been deleted. Of course, do not forget it. Finally, add the line segment of the port on the two sides respectively.

 

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;

 

/* Delete the redundant minimal spanning tree */

PFirst = find_tree_by_index (start );

PSec = find_tree_by_index (end );

Delete_mini_tree_from_group (pTree, length, pSec );

 

/* Merge nodes */

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

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

}

PFirst-> node_num + = pSec-> node_num;

 

/* Merge line segments */

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

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

}

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

 

/* Function return */

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;

 

/* Delete the redundant minimal spanning tree */

PFirst = find_tree_by_index (start );

PSec = find_tree_by_index (end );

Delete_mini_tree_from_group (pTree, length, pSec );

 

/* Merge nodes */

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

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

}

PFirst-> node_num + = pSec-> node_num;

 

/* Merge line segments */

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

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

}

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

 

/* Function return */

Return;

}

Summary:

 

(1) This article mainly introduces three problems that need to be addressed in the coding of the cruise Card Algorithm: sorting, searching and merging.

 

(2) complex functions are constructed by simple functions. We can divide the algorithms into several independent parts and break them apart.

 

(3) After solving these three problems, the next blog will be able to perform a centralized analysis, which is logically clear.

 

 

 

 

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.