Full permutation method and device based on depth-first search optimization algorithm
By classifying the repeated vertices in the depth-first search algorithm and building a second connected graph of statistical markers, the inefficiency of arrangement caused by node repetition in the connected graph is solved, and more efficient acquisition of full permutation results is achieved.
Patent Information
- Application Number
- CN202110916994.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-11
- Publication Date
- 2024-11-05
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
In the depth-first search algorithm, the repetition of nodes in the communication graph composed of nodes to be arranged results in duplication of arrangement results, reducing the efficiency of full arrangement.
By classifying the vertices with undifferentiated traversal results into one category, a second connected graph is constructed and the statistics of each vertex are marked, and the second connected graph is traversed based on the statistics to obtain a full arrangement result, and a classification node is classified to improve search efficiency.
The traversal efficiency of the connected graph in full arrangement is improved, the repetitive results are reduced, and the efficiency of the full arrangement is improved.
Smart Images

Figure CN113610234B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical fields of artificial intelligence, big data, etc., and in particular to a full arrangement method and device based on a depth-first search optimization algorithm. Background Art
[0002] When performing full permutation, one method is to use a depth-first search algorithm to traverse the connected graph of the nodes to be permuted. However, in a specific scenario of depth-first search, there are duplicate nodes in the connected graph of the nodes to be permuted. Using the traditional depth-first search algorithm, the permutation results obtained will be duplicated, and a step of deduplication must be added, which greatly reduces the efficiency of full permutation. Summary of the invention
[0003] The embodiment of the present invention proposes a full permutation method based on a depth-first search optimization algorithm to improve the traversal efficiency of a connected graph in the full permutation, thereby improving the full permutation efficiency. The method includes:
[0004] Obtaining a first connected graph for full permutation;
[0005] Classifying the first vertices in the first connected graph that have no difference in traversal results into a type of vertices, taking the type of vertices as a second vertex, constructing a second connected graph, marking a statistical number of each second vertex, where the statistical number is the number of first vertices owned by the second vertex;
[0006] According to the statistical number, traverse the second connected graph to obtain a traversal result;
[0007] Based on the traversal results, a full arrangement result is obtained.
[0008] The embodiment of the present invention proposes a full arrangement device based on a depth-first search optimization algorithm to improve the traversal efficiency of a connected graph in the full arrangement, thereby improving the efficiency of the full arrangement. The device includes:
[0009] A first connected graph obtaining module, used to obtain a first connected graph for full permutation;
[0010] A second connected graph construction module, used to classify the first vertices in the first connected graph that are indifferent to the traversal results as a type of vertices, take the type of vertices as a second vertex, construct a second connected graph, and mark the statistics of each second vertex, where the statistics are the number of first vertices owned by the second vertex;
[0011] A traversal module, used for traversing the second connected graph according to the statistical number to obtain a traversal result;
[0012] The full arrangement result obtaining module is used to obtain the full arrangement result based on the traversal result.
[0013] An embodiment of the present invention further proposes a computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned full permutation method of the optimization algorithm based on depth-first search when executing the computer program.
[0014] The embodiment of the present invention further proposes a computer-readable storage medium, which stores a computer program for executing the above-mentioned full permutation method based on the optimization algorithm of the depth-first search.
[0015] In an embodiment of the present invention, a first connected graph for full permutation is obtained; first vertices in the first connected graph that are indifferent to traversal results are classified as a type of vertex, and the type of vertex is used as a second vertex to construct a second connected graph, and the statistics of each second vertex are marked, and the statistics are the number of first vertices owned by the second vertex; according to the statistics, the second connected graph is traversed to obtain traversal results; based on the traversal results, a full permutation result is obtained. In the above process, the nodes in the search space are classified, and a group of nodes with no difference in search results are regarded as one type, and then the search points of the search space become various types of nodes, rather than individual nodes, thereby improving the traversal efficiency of the connected graph in the full permutation, and then improving the efficiency of the full permutation. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the prior art descriptions. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work. In the drawings:
[0017] Figure 1 It is a flowchart of a full arrangement method based on a depth-first search optimization algorithm in an embodiment of the present invention;
[0018] Figure 2 A flowchart of the loop traversal steps in an embodiment of the present invention;
[0019] Figure 3-Figure 22 They are respectively step-by-step diagrams of the process of performing a deep search in an embodiment of the present invention;
[0020] Fig.23 It is a schematic diagram of a full arrangement device based on a depth-first search optimization algorithm in an embodiment of the present invention;
[0021] Fig.24 Schematic diagram of a computer device in an embodiment of the present invention. DETAILED DESCRIPTION
[0022] To make the purpose, technical solution and advantages of the embodiments of the present invention more clear, the embodiments of the present invention are further described in detail below in conjunction with the accompanying drawings. Here, the exemplary embodiments of the present invention and their descriptions are used to explain the present invention, but are not intended to limit the present invention.
[0023] In the description of this specification, the terms "include", "including", "have", "contain", etc. are all open terms, which mean including but not limited to. The descriptions with reference to the terms "one embodiment", "a specific embodiment", "some embodiments", "for example", etc. mean that the specific features, structures or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present application. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures or characteristics described may be combined in a suitable manner in any one or more embodiments or examples. The order of steps involved in each embodiment is used to schematically illustrate the implementation of the present application, and the order of steps is not limited and can be appropriately adjusted as needed.
[0024] Figure 1 FIG. 1 is a flowchart of a full arrangement method based on a depth-first search optimization algorithm in an embodiment of the present invention, such as Figure 1 As shown, the method includes:
[0025] Step 101, obtaining a first connected graph for full permutation;
[0026] Step 102, classifying the first vertices in the first connected graph that have no differentiation in traversal results as a type of vertices, taking the type of vertices as a second vertex, constructing a second connected graph, marking a statistical number of each second vertex, the statistical number being the number of first vertices owned by the second vertex;
[0027] Step 103, traversing the second connected graph according to the statistical number to obtain a traversal result;
[0028] Step 104, obtaining a full arrangement result based on the traversal result.
[0029] In the above embodiment, the nodes in the search space are classified, and a group of nodes with no difference in search results are regarded as one category. Then the search points in the search space become various types of nodes instead of individual nodes, thereby improving the traversal efficiency of the connected graph in the full arrangement, and further improving the efficiency of the full arrangement.
[0030] In one embodiment, traversing the second connected graph according to the statistics to obtain a traversal result includes:
[0031] Sorting all second vertices in the second connected graph according to a preset rule to obtain a first sorted vertex set after the first sorting;
[0032] A loop traversal step is performed on all second vertices of the first sorted vertex set until all second vertices are selected to obtain a traversal result.
[0033] Figure 2 This is a flowchart of a loop traversal step in an embodiment of the present invention. In one embodiment, the loop traversal step includes:
[0034] Step 1: Select a second vertex from all second vertices in the first sorted vertex set as the current vertex and execute the following step 2 until all second vertices are selected:
[0035] Step 2, reduce the current vertex mark's statistics by one;
[0036] Step 3, sorting the current vertex and all the second vertices whose statistics among the rear adjacent points of the current vertex are greater than zero according to a preset rule to obtain a second sorted vertex set after the second sorting, wherein the rear adjacent points are second vertices that can be directly reached from the current vertex;
[0037] Step 4, select a second vertex from the second sorted vertex set as the current vertex, return to step 2, if the rollback condition is met, perform a rollback operation and repeat step 4; if the current vertex does not have a previous second vertex, return to step 1; if the traversal end condition is met, determine that a traversal is over, record the traversal results, perform a rollback operation, and repeat step 4.
[0038] In one embodiment, the fallback condition is that the count of the last second vertex in the second sorted vertex set is zero;
[0039] The traversal end condition is that the statistics of all second vertices in the second sorted vertex set are zero;
[0040] The rollback operation is to add 1 to the statistics of the current vertex, and determine that the current vertex is the second vertex above the current vertex.
[0041] It should be noted that the method proposed in the embodiment of the present invention can also be used in technical fields such as big data that require full permutation.
[0042] Two specific embodiments are given below to illustrate the application of the method proposed in the embodiment of the present invention.
[0043] Embodiment 1
[0044] Suppose a road is to be planted with trees. There are 2, 4, 3, and 4 trees of four types, A, B, C, and D, respectively. It is required that the same trees cannot be adjacent to each other. Give a tree planting plan. Among them, there are two trees of type A, which is the first vertex that has no difference in the traversal result.
[0045] Classifying the first vertex, it is obvious that there are four types of trees: A, B, C, and D. The statistical numbers num are 2, 4, 3, and 4 respectively, forming 4 second vertices.
[0046] Sort the second vertex in the order of A, B, C, D.
[0047] Select A as the current vertex, that is, select seed A, and the statistical number of A num-1=1.
[0048] Since the same trees cannot be adjacent to each other, the next second vertex can only be selected from the subsequent adjacent points of the current vertex, that is, from B, C, and D. B is judged first according to the order. The statistical number num of B>0, so B is selected as the statistical number num-1=3 of the current vertex B.
[0049] Select the next second vertex from A, C, and D, select A, and the statistical number of A, num-1, is 0.
[0050] Select the next second vertex from B, C, and D, select B, and the statistical number of B, num-1, is 2.
[0051] Select the next second vertex from A, C, and D. Since the statistical number num of A is 0, select C, and the statistical number num-1 of C is 2.
[0052] Select the next second vertex from A, B, and D. Since the statistical number num of A is 0, select B, and the statistical number num-1 of B is 1.
[0053] Similarly, select C (C's statistic num-1 = 1), B (B's statistic num-1 = 0), C (C's statistic num-1 = 0), D (D's statistic num-1 = 3) in turn. At this point, it is found that there are no vertices to choose from, because there is only one tree, D, and the same trees cannot be adjacent, so start to back off, as shown in Table 1.
[0054] Table 1
[0055]
[0056] D's statistical number num+1=4 returns to C. C can only choose D so it goes back again. C's statistical number num+1=1 returns to B. Just now B chose C among A, C, and D. This time he chooses D. D's statistical number num-1=3.
[0057] For the next second vertex, since both trees A and B have been used up, only C can be selected, and the statistical number of C num-1=0.
[0058] The next second vertex can only be D, and the statistical number of D num-1 = 2. Now there is only tree type D left, so back off, as shown in Table 2 below.
[0059] Table 2
[0060]
[0061] The statistical number num+1 of D is 3, the statistical number num+1 of C is 1, the statistical number num+1 of D is 4, the statistical number num+1 of B is 1, and we go back to C. The next tree of C just now selected B, and now we select D in the next order in the sorting. The statistical number num-1 of D is 3.
[0062] The subsequent steps will not be described in detail. Table 3 below shows a feasible solution that is finally formed, and the traversal results. Based on the traversal results, a full arrangement of multiple trees can be performed.
[0063] Table 3
[0064]
[0065] Embodiment 2
[0066] Assume that there is a search space consisting of different traversal orders of a connected graph composed of 4 vertices, in which two vertices are exactly the same, and the four vertices are referred to as 1, 2, 2, and 3. According to the method of the present invention, the first step is to classify the four vertices, which can obviously be divided into three categories: 1, 2, and 3, among which the category "2" has two first vertices. Figure 3-Figure 22 They are respectively step-by-step diagrams of the process of performing a deep search in an embodiment of the present invention, wherein each record result is a traversal result, and a total of three traversal results are given, namely ①②②③, ①②③②, ①③②②, Fig. 22 You can continue to traverse and get more results.
[0067] In summary, in the method proposed in the embodiment of the present invention, a first connected graph for full permutation is obtained; first vertices in the first connected graph that are indifferent in traversal results are classified as a type of vertex, and the type of vertex is used as a second vertex to construct a second connected graph, and the statistics of each second vertex are marked, and the statistics are the number of first vertices owned by the second vertex; according to the statistics, the second connected graph is traversed to obtain traversal results; based on the traversal results, a full permutation result is obtained. In the above process, the nodes in the search space are classified, and a group of nodes that are indifferent in the search results are regarded as a type, and then the search points of the search space become various types of nodes, rather than individual nodes, thereby improving the traversal efficiency of the connected graph in the full permutation, and then improving the efficiency of the full permutation.
[0068] The embodiment of the present invention further proposes a full permutation device based on a depth-first search optimization algorithm, the principle of which is similar to the full permutation method based on a depth-first search optimization algorithm, and will not be described in detail here.
[0069] Fig.23 FIG. 1 is a schematic diagram of a full permutation device based on a depth-first search optimization algorithm in an embodiment of the present invention. Fig.23 As shown, including:
[0070] A first connected graph obtaining module 2301, used to obtain a first connected graph for full permutation;
[0071] A second connected graph construction module 2302 is used to classify the first vertices in the first connected graph that are indifferent to the traversal results as a type of vertex, take the type of vertex as a second vertex, construct a second connected graph, and mark the statistics of each second vertex, where the statistics is the number of first vertices owned by the second vertex;
[0072] A traversal module 2303, configured to traverse the second connected graph according to the statistical number to obtain a traversal result;
[0073] The full arrangement result obtaining module 2304 is used to obtain the full arrangement result based on the traversal result.
[0074] In one embodiment, traversing the module includes:
[0075] A sorting module, used for sorting all second vertices in the second connected graph according to a preset rule to obtain a first sorted vertex set after the first sorting;
[0076] The loop traversal module is used to perform a loop traversal step on all second vertices of the first sorted vertex set until all second vertices are selected to obtain a traversal result.
[0077] In one embodiment, the loop traversal step includes:
[0078] Step 1: Select a second vertex from all second vertices in the first sorted vertex set as the current vertex and execute the following step 2 until all second vertices are selected:
[0079] Step 2, reduce the current vertex mark's statistics by one;
[0080] Step 3, sorting the current vertex and all the second vertices whose statistics among the rear adjacent points of the current vertex are greater than zero according to a preset rule to obtain a second sorted vertex set after the second sorting, wherein the rear adjacent points are second vertices that can be directly reached from the current vertex;
[0081] Step 4, select a second vertex from the second sorted vertex set as the current vertex, return to step 2, if the rollback condition is met, perform a rollback operation and repeat step 4; if the current vertex does not have a previous second vertex, return to step 1; if the traversal end condition is met, determine that a traversal is over, record the traversal results, perform a rollback operation, and repeat step 4.
[0082] In one embodiment, the fallback condition is that the count of the last second vertex in the second sorted vertex set is zero;
[0083] The traversal end condition is that the statistics of all second vertices in the second sorted vertex set are zero;
[0084] The rollback operation is to add 1 to the statistics of the current vertex, and determine that the current vertex is the second vertex above the current vertex.
[0085] In summary, in the device proposed in the embodiment of the present invention, a first connected graph for full permutation is obtained; first vertices in the first connected graph that are indifferent to traversal results are classified as a type of vertex, and the type of vertex is used as a second vertex to construct a second connected graph, and the statistics of each second vertex are marked, and the statistics are the number of first vertices owned by the second vertex; according to the statistics, the second connected graph is traversed to obtain traversal results; based on the traversal results, a full permutation result is obtained. In the above process, the nodes in the search space are classified, and a group of nodes with no difference in search results are regarded as one type, and then the search points of the search space become various types of nodes, rather than individual nodes, thereby improving the traversal efficiency of the connected graph in the full permutation, and then improving the efficiency of the full permutation.
[0086] An embodiment of the present invention further provides a computer device, Fig.24 Schematic diagram of a computer device in an embodiment of the present invention, the computer device can implement all steps in the full permutation method based on the depth-first search optimization algorithm in the above embodiment, and the computer device specifically includes the following contents:
[0087] Processor (processor) 2401, memory (memory) 2402, communication interface (CommunicationsInterface) 2403 and communication bus 2404;
[0088] The processor 2401, the memory 2402, and the communication interface 2403 communicate with each other through the communication bus 2404; the communication interface 2403 is used to realize information transmission between relevant devices such as the server-side device, the detection device, and the user-side device;
[0089] The processor 2401 is used to call the computer program in the memory 2402, and when the processor executes the computer program, all steps in the full permutation method based on the depth-first search optimization algorithm in the above embodiment are implemented.
[0090] An embodiment of the present invention also provides a computer-readable storage medium that can implement all steps in the full permutation method of the optimization algorithm based on depth-first search in the above embodiment. The computer-readable storage medium stores a computer program that, when executed by a processor, implements all steps of the full permutation method of the optimization algorithm based on depth-first search in the above embodiment.
[0091] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0092] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0093] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0094] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0095] The specific embodiments described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A full permutation method based on a depth-first search optimization algorithm, It is characterized in that include: Obtaining a first connected graph of a full arrangement of a plurality of trees for tree planting planning; Classifying the first vertices in the first connected graph that have no difference in traversal results into a type of vertices, taking the type of vertices as a second vertex, constructing a second connected graph, marking a statistical number of each second vertex, where the statistical number is the number of first vertices owned by the second vertex; According to the statistical number, traverse the second connected graph to obtain a traversal result; Based on the traversal result, a full arrangement result is obtained; According to the statistical number, traversing the second connected graph to obtain a traversal result, including: sorting all second vertices in the second connected graph according to a preset rule to obtain a first sorted vertex set after the first sorting; performing a loop traversal step on all second vertices in the first sorted vertex set until all second vertices are selected to obtain a traversal result; The loop traversal step includes: Step 1: Select a second vertex from all second vertices in the first sorted vertex set as the current vertex and execute the following step 2 until all second vertices are selected: Step 2, reduce the current vertex mark's statistics by one; Step 3, sorting the current vertex and all the second vertices whose statistics among the rear adjacent points of the current vertex are greater than zero according to a preset rule to obtain a second sorted vertex set after the second sorting, wherein the rear adjacent points are second vertices that can be directly reached from the current vertex; Step 4, select a second vertex from the second sorted vertex set as the current vertex, return to step 2, if the rollback condition is met, perform a rollback operation and repeat step 4; if the current vertex does not have a previous second vertex, return to step 1; if the traversal end condition is met, determine that a traversal is over, record the traversal results, perform a rollback operation, and repeat step 4.
2. The full permutation method based on the optimization algorithm of depth-first search as claimed in claim 1, It is characterized in that The fallback condition is that the statistical number of the last second vertex in the second sorted vertex set is zero; The traversal end condition is that the statistics of all second vertices in the second sorted vertex set are zero; The rollback operation is to add 1 to the statistics of the current vertex, and determine that the current vertex is the second vertex above the current vertex.
3. A full permutation device based on a depth-first search optimization algorithm, It is characterized in that include: A first connected graph obtaining module, used to obtain a first connected graph of a full arrangement of multiple trees to be planted; A second connected graph construction module, used to classify the first vertices in the first connected graph that are indifferent to the traversal results as a type of vertices, take the type of vertices as a second vertex, construct a second connected graph, and mark the statistics of each second vertex, where the statistics are the number of first vertices owned by the second vertex; A traversal module, used for traversing the second connected graph according to the statistical number to obtain a traversal result; A full arrangement result obtaining module, used for obtaining a full arrangement result based on the traversal result; The traversal module includes: a sorting module, which is used to sort all the second vertices in the second connected graph according to a preset rule to obtain a first sorted vertex set after the first sorting; a loop traversal module, which is used to perform a loop traversal step on all the second vertices in the first sorted vertex set until all the second vertices are selected to obtain a traversal result; The loop traversal step includes: Step 1: Select a second vertex from all second vertices in the first sorted vertex set as the current vertex and execute the following step 2 until all second vertices are selected: Step 2, reduce the current vertex mark's statistics by one; Step 3, sorting the current vertex and all the second vertices whose statistics among the rear adjacent points of the current vertex are greater than zero according to a preset rule to obtain a second sorted vertex set after the second sorting, wherein the rear adjacent points are second vertices that can be directly reached from the current vertex; Step 4, select a second vertex from the second sorted vertex set as the current vertex, return to step 2, if the rollback condition is met, perform a rollback operation and repeat step 4; if the current vertex does not have a previous second vertex, return to step 1; if the traversal end condition is met, determine that a traversal is over, record the traversal results, perform a rollback operation, and repeat step 4.
4. The full permutation device based on the optimization algorithm of depth-first search as claimed in claim 3, It is characterized in that The fallback condition is that the statistical number of the last second vertex in the second sorted vertex set is zero; The traversal end condition is that the statistics of all second vertices in the second sorted vertex set are zero; The rollback operation is to add 1 to the statistics of the current vertex, and determine that the current vertex is the second vertex above the current vertex.
5. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, It is characterized in that When the processor executes the computer program, the method according to any one of claims 1 to 2 is implemented.
6. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores a computer program for executing the method according to any one of claims 1 to 2.
