[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
We have discussed how to create, add, delete, and save a graph. Today, we will continue to discuss some other questions about the graph, such as how to build the Minimum Spanning Tree in the graph environment. Why do we need to build a minimal spanning tree? In fact, the principle is very simple. For example, if there are n villages in a certain Township, the n villages must be connected. Now we plan to build a network cable between each village to implement the village communication project. So what can we do to achieve intercommunication between villages and villages while minimizing the total distance? To achieve this goal, you must build the Minimum Spanning Tree in the existing graph.
There are many methods to generate the minimal spanning tree. The prim method is one of them. What are the basic steps for generating the minimal spanning tree? It's easy. Let me hear it:
1) start with a certain vertex to find all the edges that can be accessed by the current vertex;
2) Find the smallest edge in the edge that has been searched. This edge must have a vertex that has not been accessed. Add the vertex that has not been accessed to our set and record the added edge;
3) Search for all edges accessible to the current set and repeat the process of 2 until no new vertex can be added;
4) at this time, the tree composed of all edges is the smallest spanning tree.
So how should I write the code? You can think for yourself.
A) First, we define the basic data structure.
Typedef struct _ DIR_LINE
{
Int start;
Int end;
Int weight;
Struct _ DIR_LINE * next;
} DIR_LINE;
Typedef struct _ MINI_GENERATE_TREE
{
Int node_num;
Int line_num;
Int * pNode;
DIR_LINE * pLine;
} MINI_GENERATE_TREE;
Typedef struct _ DIR_LINE
{
Int start;
Int end;
Int weight;
Struct _ DIR_LINE * next;
} DIR_LINE;
Typedef struct _ MINI_GENERATE_TREE
{
Int node_num;
Int line_num;
Int * pNode;
DIR_LINE * pLine;
} MINI_GENERATE_TREE;
B) Basic DIR_LINE operations
View plaincopy to clipboardprint? STATUS insert_line_into_queue (DIR_LINE ** ppLine, int start, int end, int weight)
{
DIR_LINE * pLine;
If (NULL = ppLine)
Return FALSE;
If (NULL = * ppLine ){
* PpLine = create_new_dir_line (start, end, weight );
Return TRUE;
}
PLine = create_new_dir_line (start, end, weight );
PLine-> next = * ppLine;
* PpLine = pLine;
Return TRUE;
}
STATUS delete_line_from_queue (DIR_LINE ** ppLine, DIR_LINE * pLine)
{
DIR_LINE * prev;
If (NULL = ppLine | NULL = * ppLine | NULL = pLine)
Return FALSE;
If (pLine = * ppLine ){
* PpLine = pLine-> next;
Goto final;
}
Prev = * ppLine;
While (pLine! = Prev-> next)
Prev = prev-> next;
Prev-> next = pLine-> next;
Final:
Free (pLine );
Return TRUE;
}
STATUS insert_line_into_queue (DIR_LINE ** ppLine, int start, int end, int weight)
{
DIR_LINE * pLine;
If (NULL = ppLine)
Return FALSE;
If (NULL = * ppLine ){
* PpLine = create_new_dir_line (start, end, weight );
Return TRUE;
}
PLine = create_new_dir_line (start, end, weight );
PLine-> next = * ppLine;
* PpLine = pLine;
Return TRUE;
}
STATUS delete_line_from_queue (DIR_LINE ** ppLine, DIR_LINE * pLine)
{
DIR_LINE * prev;
If (NULL = ppLine | NULL = * ppLine | NULL = pLine)
Return FALSE;
If (pLine = * ppLine ){
* PpLine = pLine-> next;
Goto final;
}
Prev = * ppLine;
While (pLine! = Prev-> next)
Prev = prev-> next;
Prev-> next = pLine-> next;
Final:
Free (pLine );
Return TRUE;
}