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

The cruise Kar algorithm is an algorithm used to calculate the Minimum Spanning Tree. Unlike the prim algorithm (upper, middle, and lower), the method for searching by node is different. The cruise Karl algorithm is based on a specific line segment. Now let's assume that a graph has m nodes and n edges. First, we need to regard m nodes as m independent spanning trees, and arrange n edges according to data from small to large. In n edges, we extract each edge in sequence. If the two nodes of the edge are located on the two sides, the two sides are merged into a tree; if the two nodes of the fruit tree are located in the same tree, ignore this edge and continue running. After all the edges are traversed, if all the spanning trees can be merged into one spanning tree, it is the minimum spanning tree we need to find. Otherwise, there is no minimum spanning tree.

 

The above algorithm may seem confusing. We can use an example to illustrate it,

 

 

? /*

* 9

* D -----------

* 3 |

* | 6 |

* A ------- B

* |

* | 7 | 5

* ------- C ----

**/

/*

* 9

* D -----------

* 3 |

* | 6 |

* A ------- B

* |

* | 7 | 5

* ------- C ----

**/Now there are four points. Where the A-D is 3, The A-C is 7, the A-B is 6, the B-D is 9, The B-C is 5, the following begins to calculate, we first default all vertices are independent of the Minimum Spanning Tree,

 

 

Copy to clipboardprint? /*

*

* D

*

* A B

*

* C

**/

/*

*

* D

*

* A B

*

* C

**/Step 1, in the ascending order, we add the smallest edge A-D,

 

 

Copy to clipboardprint? /*

*

* D

* 3 |

* |

* A B

*

*

* C

**/

/*

*

* D

* 3 |

* |

* A B

*

*

* C

**/Then, we find that the smallest side below is a B-C,

Copy to clipboardprint? /*

*

* D

* 3 |

* |

* A B

* |

* | 5

* C ----

**/

/*

*

* D

* 3 |

* |

* A B

* |

* | 5

* C ----

**/Next, we find that the smallest side is the A-B, because vertices A and vertices B are on different least spanning trees, so continue merging,

 

 

Copy to clipboardprint? /*

* D

* 3 |

* | 6

* A ---------- B

* |

* | 5

* C ----

**/

/*

* D

* 3 |

* | 6

* A ---------- B

* |

* | 5

* C ----

**/

Next, we will traverse the A-C, B-D, but we found that at this time the edge nodes have been traversed, so all ignored, the structure of the Minimum Spanning Tree is the above content.

 

So what is the data structure of the minimal spanning tree and how should it be defined? Do you still have to remember? We have discussed in the prim algorithm,

 

 

Copy to clipboardprint? /* Straight Edge */

Typedef struct _ DIR_LINE

{

Int start;

Int end;

Int weight;

Struct _ DIR_LINE * next;

} DIR_LINE;

 

/* Minimum Spanning Tree */

Typedef struct _ MINI_GENERATE_TREE

{

Int node_num;

Int line_num;

Int * pNode;

DIR_LINE * pLine;

} MINI_GENERATE_TREE;

 

/* Node edge information */

Typedef struct _ LINE

{

Int end;

Int weight;

Struct _ LINE * next;

} LINE;

 

/* Node information */

Typedef struct _ VECTEX

{

Int start;

Int number;

LINE * neighbor;

Struct _ VECTEX * next;

} VECTEX;

 

/* Graph Information */

Typedef struct _ GRAPH

{

Int count;

VECTEX * head;

} GRAPH;

/* Straight Edge */

Typedef struct _ DIR_LINE

{

Int start;

Int end;

Int weight;

Struct _ DIR_LINE * next;

} DIR_LINE;

 

/* Minimum Spanning Tree */

Typedef struct _ MINI_GENERATE_TREE

{

Int node_num;

Int line_num;

Int * pNode;

DIR_LINE * pLine;

} MINI_GENERATE_TREE;

 

/* Node edge information */

Typedef struct _ LINE

{

Int end;

Int weight;

Struct _ LINE * next;

} LINE;

 

/* Node information */

Typedef struct _ VECTEX

{

Int start;

Int number;

LINE * neighbor;

Struct _ VECTEX * next;

} VECTEX;

 

/* Graph Information */

Typedef struct _ GRAPH

{

Int count;

VECTEX * head;

} GRAPH;

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.