Material selection criteria in 3D Printing - Time & Space Complexity
When choosing materials for 3D printing, it is important to understand how the selection process scales as more options or criteria are added.
We want to know how the time to decide grows when considering many materials and factors.
Analyze the time complexity of the following material selection process.
materials = [list of materials]
criteria = [list of selection criteria]
selected = []
for material in materials:
meets_all = True
for criterion in criteria:
if not check(material, criterion):
meets_all = False
break
if meets_all:
selected.append(material)
This code checks each material against all criteria and selects those that meet every requirement.
Identify the loops and checks that repeat.
- Primary operation: Checking each material against each criterion.
- How many times: For every material, all criteria are checked until one fails or all pass.
The time to select materials grows as you add more materials or more criteria.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 materials, 5 criteria | About 50 checks |
| 100 materials, 5 criteria | About 500 checks |
| 100 materials, 20 criteria | About 2000 checks |
Pattern observation: The total checks multiply as materials and criteria increase.
Time Complexity: O(m × c)
This means the time grows proportionally to the number of materials times the number of criteria.
[X] Wrong: "Checking more criteria won't affect the time much because we stop early sometimes."
[OK] Correct: While early stopping helps, in the worst case all criteria are checked for every material, so time still grows with both materials and criteria.
Understanding how time grows when checking many materials and criteria shows your ability to analyze processes that involve multiple factors, a useful skill in many technical decisions.
What if we sorted materials by likelihood to meet criteria and checked the easiest criteria first? How would that affect the time complexity?