Kruskal method based on rapid sorting improvement
By integrating quick sorting and loop judgment operations into the Kruskal algorithm, the problem of insufficient execution speed of the Kruskal algorithm in the case of "more sides and fewer points" is solved, and a more efficient minimum spanning tree construction is achieved.
Patent Information
- Application Number
- CN202411580938.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-07
- Publication Date
- 2025-04-01
AI Technical Summary
In the case of "more edges and fewer points", the Kruskal algorithm has lower execution speed than the Prim algorithm because all edges participate in the sorting.
Improve the Kruskal algorithm, use the quick sorting method to sort edges, and integrate the operation of determining whether edges constitute loops into the quick sorting process, and abandon edges that do not belong to the smallest spanning tree in the middle, thereby reducing sorting overhead.
The operation of judging whether edges constitute loops through fusion reduces unnecessary overhead in the sorting process and improves the execution speed of the algorithm, especially in the case of "more edges and fewer points".
Smart Images

Figure CN119739898A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the field of computer application and relates to an improved Kruskal method based on quick sorting. Background Art
[0002] At present, the algorithms for the minimum spanning tree problem include the two most classic algorithms, Kruskal and Prim. In theory, the efficiency of both algorithms is O(|E|×log 2 |V|), but the actual execution speed is: Kruskal algorithm is faster when there are "more points and fewer edges", while Prim algorithm is faster when there are "more edges and fewer points".
[0003] Specifically, the steps of Kruskal's algorithm are: 1. Store the edges in the graph into a linear list and sort them by weight; 2. Take the edge with the smallest current weight from the linear list to determine whether it forms a loop; if so, discard it, otherwise save it as an edge in the minimum spanning tree and connect it between the two endpoints; 3. Repeat the previous step until all the edges of the minimum spanning tree are obtained (or the linear list is empty); The steps of Prim's algorithm are: 1. Mark all nodes as unvisited; select any node as the starting point, modify it to visited, and add it to the minimum spanning tree; change all adjacent points of the starting point to be visited, and add all adjacent edges to the minimum binary heap (the edges in the heap correspond to the nodes to be visited one by one); 2. Take out the edge with the smallest weight from the binary heap and save it as the edge in the minimum spanning tree; change the endpoint of the minimum edge (one end is a visited node and the other end is a node to be visited) to visited and add it to the minimum spanning tree; if there is an adjacent edge between the newly added node and a node to be visited, and the weight is less than the corresponding edge of the node to be visited in the binary heap, replace the original corresponding edge with this small weight edge and adjust the binary heap; if there is an adjacent edge between the newly added node and an unvisited node, change the unvisited node to a node to be visited and add the corresponding adjacent edge to the minimum binary heap; 3. Repeat the previous step until all the edges of the minimum spanning tree are obtained (or the binary heap is empty).
[0004] Therefore, when there are more edges than points, the execution speed of Kruskal's algorithm is slower than that of Prim's algorithm. The reason is that the edges that constitute the loop and do not belong to the minimum spanning tree are fully involved in the sorting in Kruskal's algorithm; while in Prim's algorithm, a considerable number of these edges that constitute the loop do not enter the binary heap and therefore do not participate in the heap operation because their weights are greater than the corresponding edges of the nodes to be visited in the binary heap; and when there are more points than edges, the execution speed of Prim's algorithm is slower than that of Kruskal's algorithm. The reason is that the "basic operations" on the heap are more time-consuming than the "basic operations" in quick sort. Summary of the invention
[0005] In view of the above problems, the purpose of the present invention is to propose a Kruskal method based on an improvement of quick sort; the technical problem solved by this method is that for edges that constitute a loop and do not belong to a minimum spanning tree, they participate in the sorting throughout the Kruskal algorithm, so the sorting time overhead is large, which leads to the execution speed of the Kruskal algorithm being slower than that of the Prim algorithm when there are "more edges and fewer points".
[0006] The technical solution of the present invention is: the Kruskal method based on the improvement of quick sorting described in the present invention abandons "first completing the sorting, and then judging from small to large whether the edges form a loop", adopts the quick sorting method to sort the linear list, and integrates the operation of judging whether the edges "form a loop" into the quick sorting; therefore, the edges that form a loop and therefore do not belong to the minimum spanning tree are discarded in the middle of the sorting, thereby reducing the sorting overhead.
[0007] Furthermore, the improved method incorporates the judgment of whether the edge "constitutes a loop"; the specific operation steps are as follows:
[0008] Step (1): Prepare an empty set S and an empty linear list L;
[0009] Step (2): Store the edges in the graph into a linear list L, and start executing quickKruskal(list) with L as the parameter;
[0010] Step (3): Execute quickKruskal(list) operation;
[0011] Step (4): Return a set S, whose edges form a minimum spanning tree of the graph.
[0012] Furthermore, in step (3), the specific steps of executing the quickKruskal(list) operation are as follows:
[0013] (3.1) If list is empty, return;
[0014] (3.2) If list contains only one edge, call checkOneEdge(theEdge, S) with edge as the parameter, and then return;
[0015] (3.3) Start the partition operation partition(list) to obtain the pivot edge of the linear list partition center;
[0016] This operation splits the list into three sub-linear lists: the left sub-list with a weight less than or equal to the pivot, the right sub-list with a weight greater than the pivot, and the middle sub-list containing an edge of the pivot.
[0017] As for the specific details of the operation, it is the same as the split operation in quick sort.
[0018] After step (3.3) is completed, perform the following steps:
[0019] (3.4) Recursively execute quickKruskal(list) with left as the parameter;
[0020] (3.5) Call checkOneEdge(theEdge, S) with pivot as parameter;
[0021] (3.6) Traverse the right subtable right and delete the edges that form the loop;
[0022] (3.7) Recursively execute quickKruskal(list) with right as the argument.
[0023] Furthermore, in step (3.5), the above detection operation checkOneEdge(theEdge,S)
[0024] First, determine whether the parameter edge theEdge forms a loop. If so, discard it; otherwise, store it in the set S.
[0025] As for judging whether an edge forms a loop, it is implemented with the help of a linear list or a union-find set, just like before the improvement.
[0026] When the edges that do not form a loop are stored in the set S, it is necessary to perform relevant operations on the linear list or union-find set to facilitate subsequent loop judgment.
[0027] Furthermore, in step (3.6), the traversal of the right sub-table right and the deletion of the edges forming the loop therein specifically include: this operation is only applied to the right sub-table right, but not to the left sub-table left;
[0028] The specific reason is that for a newly obtained segmentation center pivot, neither the left nor the right sub-table contains the edges that constitute the loop. Only after the left and pivot are processed, the right may contain the edges that constitute the loop.
[0029] The beneficial effects of the present invention are as follows: the present invention relates to a minimum spanning tree (MST) method for a graph; the minimum spanning tree is a classic problem in graph theory, and the method has theoretical and practical application value; the present invention integrates the operation of judging whether an edge "constitutes a loop" into quick sorting to reduce the sorting overhead, and the new method has high theoretical efficiency and fast execution speed; especially when "there are more edges and fewer points", the execution speed is significantly improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] Figure 1 is an operation flow chart of an embodiment of the present invention;
[0031] Figure 2 Schematic diagram of the worst case of the algorithm in the embodiment of the present invention (λ=|E| / |V| in the schematic diagram represents the ratio of the number of edges to the number of nodes of the graph to be processed). DETAILED DESCRIPTION
[0032] The specific technical scheme of the present invention is further described in detail below with reference to specific examples.
[0033] As shown in the figure, the improved Kruskal method based on quick sort described in the present invention abandons the method of "completion of sorting first, and then judging from small to large whether the edges constitute a loop", and adopts quick sort to sort the linear list, and integrates the operation of judging whether the edges "constitute a loop" into the quick sort; in this way, the edges that constitute a loop and therefore do not belong to the minimum spanning tree are discarded in the middle of the sorting, thereby reducing the sorting overhead.
[0034] Furthermore, the improved method incorporates the judgment of whether the edge "constitutes a loop"; the specific operation steps are as follows:
[0035] Step (1): Prepare an empty set S and an empty linear list L;
[0036] Step (2): Store the edges in the graph into a linear list L, and start executing quickKruskal(list) with L as the parameter;
[0037] Step (3): Execute quickKruskal(list) operation;
[0038] Step (4): Return a set S, whose edges form a minimum spanning tree of the graph.
[0039] Furthermore, in step (3), the specific steps of executing the quickKruskal(list) operation are as follows:
[0040] (3.1) If list is empty, return;
[0041] (3.2) If list contains only one edge, call checkOneEdge(theEdge, S) with edge as the parameter, and then return;
[0042] (3.3) Start the partition operation partition(list) to obtain the pivot edge of the linear list partition center;
[0043] This operation splits the list into three sub-linear lists: the left sub-list with a weight less than or equal to the pivot, the right sub-list with a weight greater than the pivot, and the middle sub-list containing an edge of the pivot. As for the specific details of the partition operation partition(list), it is the same as the partition operation in quick sort.
[0044] After step (3.3) is completed, perform the following steps:
[0045] (3.4) Recursively execute quickKruskal(list) with left as the parameter;
[0046] (3.5) Call checkOneEdge(theEdge, S) with pivot as parameter;
[0047] (3.6) Traverse the right subtable right and delete the edges that form the loop;
[0048] (3.7) Recursively execute quickKruskal(list) with right as the argument.
[0049] Furthermore, in step (3.5), the above detection operation checkOneEdge(theEdge,S) is used to determine whether the parameter edge theEdge forms a loop. If so, it is discarded; otherwise, it is stored in the set S.
[0050] As for judging whether an edge forms a loop, it is implemented with the help of a linear table or a union-find set, as before the improvement;
[0051] When the edges that do not form a loop are stored in the set S, it is necessary to perform relevant operations on the linear list or union-find set to facilitate subsequent loop judgment.
[0052] Furthermore, in step (3.6), the traversal of the right sub-table right and the deletion of the edges forming the loop therein specifically include: this operation is only applied to the right sub-table right, but not to the left sub-table left;
[0053] The specific reason is that for a newly obtained segmentation center pivot, neither the left nor the right sub-table contains the edges that constitute the loop. Only after the left and pivot are processed, the right may contain the edges that constitute the loop.
[0054] Specifically: When implementing the program, since there is an operation of traversing the right sublist and deleting the edges that constitute the loop, the linear list that stores the edges must use a doubly linked list instead of the array in the original algorithm. Otherwise, there will be an additional operation of moving array elements, which will greatly reduce the efficiency and speed of the algorithm.
[0055] However, the doubly linked list makes it difficult to implement the split operation in quick sort: the split operation requires judging whether the left pointer has moved to the right of the right pointer; when using an array, this judgment can be achieved through the position index of the array elements, but the elements in the doubly linked list have no position index; the solution is to add a position index attribute to each edge, as shown in the following Java language code;
[0056]
[0057] Doubly linked lists also make it difficult to implement element exchange operations within the split operation in quick sort; the solution is to exchange the edge-related attributes of the two elements without exchanging the position index, predecessor, and successor, as shown in the following Java language code.
[0058]
[0059] Theoretical efficiency of the present invention:
[0060] Several situations are listed here for analysis, and it is explained that the new method is fast in execution speed.
[0061] 1. The number of edges is close to the number of nodes
[0062] Here, |E| / |V|≈1, that is, the number of edges is almost equal to the number of nodes; thus, the number of edges abandoned in the middle of sorting (|E|-|V|+1) is extremely small and can be ignored; the sorting overhead of the present invention is almost not reduced, and the theoretical estimate is as follows:
[0063] |E|×log 2 (|E|)=O(|E|×log 2 |V|)
[0064] Note: After the sorting is completed, the prior art still needs to traverse the linear list from small to large, check each edge, and determine whether it forms a loop; while the present invention integrates the judgment of the loop into the sorting process, and there is no need to traverse the linear list again, so the actual execution speed is faster than the prior art.
[0065] 2. The number of edges is greater than the number of nodes
[0066] Here it means |E| / |V|>1, that is, the number of edges is obviously greater than the number of nodes; thus, the number of edges abandoned in the middle of sorting (|E|-|V|+1) is considerable and cannot be ignored; the sorting overhead of the present invention is reduced; as for the extent of the reduction, it needs to be further subdivided into the worst and general cases for discussion.
[0067] (1) [Worst case]: Let λ = |E| / |V|, where 1<λ≤(|V|-1) / 2; the worst case means that the structure of graph G meets the following characteristics:
[0068] ①, (2λ+1) nodes are connected into a complete subgraph G through (λ(2λ+1)) edges 0 ;
[0069] ② The first of the remaining nodes is connected to G through λ edges. 0 The graph composed of λ nodes is denoted by G 1 , and the weight of this λ edge is greater than G 0 Any of the following:
[0070] ③ The second of the remaining nodes is connected to G through λ edges 1 The graph composed of λ nodes is denoted by G 2 , and the weight of this λ edge is greater than G 1 Any of the following:
[0071] ④. And so on, ..., until the last remaining node n (n = |V|-(2λ+1)).
[0072] The sorting overhead of the present invention is reduced; the theoretical estimate is as follows:
[0073] |E|×log 2 ((|E|-λ(2λ+1)) / λ)<|E|×log 2 (|E| / λ)=O(|E|×log 2 |V|)
[0074] Note: The worst case is a low-probability event; in this case, especially when λ>1 but not λ>>1, the reduction in the sorting overhead of the present invention is very small; but in any case, the present invention does not need to traverse the linear table again after sorting, so the actual execution speed is faster than the prior art.
[0075] (2) [General case]: The general case refers to that the edges with large and small weights are randomly and evenly distributed among the nodes. In this case, the sorting overhead of the present invention is significantly reduced. Theoretically, it is estimated as follows:
[0076] O(|E|×log 2 (log 2 |V|))
[0077] Note: This is generally a high-probability event; in this case, the sorting overhead of the present invention is significantly reduced, so the actual execution speed is significantly faster than the prior art.
[0078] Execution speed:
[0079] The following table shows the program running time of the present invention, Kruskal algorithm and Prim algorithm; except for the last experiment (the input graph is a tree), the execution speed of the present invention is faster than the latter two.
[0080]
Claims
1. Kruskal method based on quick sorting, characterized in that: The quick sort algorithm is used to sort the linear list, and the judgment of whether the edge "constitutes a loop" is integrated into the quick sort; The edges that form a loop and therefore do not belong to the minimum spanning tree are discarded halfway through the sorting process, reducing the cost of sorting.
2. The Kruskal method based on quick sorting improvement according to claim 1, characterized in that: The operation steps are as follows: Step (1): Prepare an empty set S and an empty linear list L; Step (2): Store the edges in the graph into a linear list L, and start executing quickKruskal(list) with L as the parameter; Step (3): Execute quickKruskal(list) operation; Step (4): After returning the set S, the edges in it form a minimum spanning tree of the graph.
3. The improved Kruskal method based on quick sorting according to claim 2, characterized in that: In step (3), the specific steps of executing the quickKruskal(list) operation are as follows: (3.1) If list is empty, return; (3.2) If list contains only one edge, call checkOneEdge(theEdge, S) with edge as the parameter, and then return; (3.3) Start the partition operation partition(list) to obtain the pivot edge of the linear list partition center; (3.4) Recursively execute quickKruskal(list) with left as the parameter; (3.5) Call checkOneEdge(theEdge, S) with pivot as parameter; (3.6) Traverse the right subtable right and delete the edges that form the loop; (3.7) Recursively execute quickKruskal(list) with right as the argument.
4. The improved Kruskal method based on quick sorting according to claim 3, characterized in that: In step (3.3), the operation splits the list into three sub-linear lists: the left sub-list with a weight less than or equal to the pivot, the right sub-list with a weight greater than the pivot, and the middle sub-list containing an edge of the pivot.
5. The improved Kruskal method based on quick sorting according to claim 3, characterized in that: In step (3.5), the check operation checkOneEdge(theEdge,S) is used to determine whether the parameter edge theEdge forms a loop; if so, it is discarded, otherwise it is stored in the set S; As for judging whether an edge forms a loop, it is implemented with the help of a linear list or a union-find set.
6. The improved Kruskal method based on quick sorting according to claim 5, characterized in that: When the edges that do not form a loop are stored in the set S, operations must be performed on the linear list or union-find set to facilitate subsequent loop judgment.
7. The improved Kruskal method based on quick sorting according to claim 3, characterized in that: In step (3.6), the traversal of the right sub-table right and the deletion of the edges forming the loop therein specifically include: this operation is only applied to the right sub-table right, but not to the left sub-table left.
8. The improved Kruskal method based on quick sorting according to claim 7, characterized in that: The specific reason for the operation is that for a newly obtained segmentation center pivot, neither the left nor the right sub-tables contain edges that constitute a loop. Only after the left and the pivot are processed, the right sub-table can contain edges that constitute a loop.
Citation Information
Cited By
Minimum tree generation method with limited global contraction-node degree
CN121256093A