Splitting models for print bed fit in 3D Printing - Time & Space Complexity
When splitting a 3D model to fit a print bed, it's important to understand how the time to split grows as the model size increases.
We want to know how the work needed changes when the model gets bigger or more complex.
Analyze the time complexity of the following code snippet.
function splitModel(modelParts, bedSize) {
let splitParts = [];
for (let part of modelParts) {
if (part.size > bedSize) {
let halves = splitPart(part);
splitParts.push(...halves);
} else {
splitParts.push(part);
}
}
return splitParts;
}
function splitPart(part) {
// Splits part into two smaller parts
return [part.slice(0, part.size / 2), part.slice(part.size / 2)];
}
This code checks each part of a model and splits it if it is too big to fit on the print bed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each model part and splitting parts that are too large.
- How many times: The loop runs once for each part, and splitting can cause more parts to be added and checked again.
As the number of parts increases, the time to check and possibly split each part grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and some splits if needed |
| 100 | About 100 checks and more splits, possibly doubling parts |
| 1000 | About 1000 checks and many splits, increasing parts significantly |
Pattern observation: The work grows roughly in proportion to the number of parts, but splitting can increase parts, making the work grow faster.
Time Complexity: O(n)
This means the time to split models grows roughly in direct proportion to the number of parts to check and split.
[X] Wrong: "Splitting a model always takes the same time no matter how big it is."
[OK] Correct: Larger or more complex models have more parts to check and split, so the time needed grows with model size.
Understanding how splitting models scales with size shows you can think about how tasks grow and manage complexity, a useful skill in many technical roles.
"What if the splitting function split parts into three smaller parts instead of two? How would the time complexity change?"