如何基于节点数量对节点集进行加权划分?现有代码节点超44时异常
First, let's align on your core requirement: You have a total number of nodes (e.g., 100), and you want to split them into a fixed number of parent nodes (e.g., 10), with each parent having a set number of child nodes (in your example, 9 per parent, adding up to 10 + 10*9 = 100 total nodes).
Your current code's problem stems from the recursive getSubMastersNumber method, which calculates parent node count using a binary split logic—this doesn't match your need to specify an exact number of parents. Beyond 44 nodes, this recursive logic starts producing parent counts that lead to unexpected (and broken) child node calculations.
Why Your Original Code Breaks
Let's look at what getSubMastersNumber actually does: it recursively counts parent nodes by splitting the total in half each time. For example:
- For 45 nodes, it returns 5 parents
- For 100 nodes, it returns 6 parents
This automatic count doesn't respect your requirement of 10 parents. Worse, when you calculate i/x-1 for child nodes, you don't account for leftover nodes when the total child count isn't perfectly divisible by the parent count. This leads to inconsistent or invalid splits once node numbers grow beyond a small threshold.
The Correct Solution: Fixed Parent Count with Weighted Distribution
We need to rewrite the logic to:
- Accept a fixed number of parent nodes
- Distribute remaining child nodes as evenly as possible (weighted, so some parents get an extra node if there's a remainder)
Here's the revised code:
public class NodePartition { public static void main(String[] args) { int totalNodes = 100; // Your total node count int targetParentCount = 10; // Exact number of parent nodes you want // Guard clause to avoid invalid inputs if (targetParentCount >= totalNodes) { System.out.println("Error: Parent count can't be >= total nodes (no children left to assign!)"); return; } int totalChildNodes = totalNodes - targetParentCount; int baseChildrenPerParent = totalChildNodes / targetParentCount; int extraChildren = totalChildNodes % targetParentCount; // Print summary System.out.println("Total Nodes: " + totalNodes); System.out.println("Target Parent Nodes: " + targetParentCount); System.out.println("Base Child Nodes per Parent: " + baseChildrenPerParent); System.out.println(extraChildren + " parents will get 1 extra child node"); System.out.println("---"); // Print individual parent assignments for (int parentId = 1; parentId <= targetParentCount; parentId++) { int childCount = baseChildrenPerParent; // Assign extra child to the first N parents if (parentId <= extraChildren) { childCount += 1; } System.out.println("Parent " + parentId + ": " + childCount + " child nodes"); } } }
How This Works
- Input Validation: First, we make sure we don't try to create more parents than total nodes (which would leave no children to assign).
- Calculate Child Nodes: Subtract the parent count from total nodes to get the number of child nodes to distribute.
- Even Distribution: Split the child nodes evenly across parents. If there's a remainder (e.g., 105 total nodes with 10 parents), we give the first
extraChildrenparents an extra node to keep the split as fair as possible. - Transparent Output: The code prints both a summary and individual parent assignments so you can verify the split.
For your example (100 nodes, 10 parents):
- Total child nodes = 90
- Base children per parent = 9
- No extra children needed, so every parent gets exactly 9 children—perfectly matching your requirement.
If you had 105 nodes and 10 parents:
- Total child nodes = 95
- Base children per parent =9, with 5 extra children
- The first 5 parents get 10 children each, the last 5 get 9—adding up to exactly 95 child nodes +10 parents =105 total.
内容的提问来源于stack exchange,提问作者user5991728

