What is artificial intelligence in AI for Everyone - Complexity Analysis
We want to understand how the effort to perform tasks with artificial intelligence changes as the tasks get bigger or more complex.
How does the time needed grow when AI handles more data or harder problems?
Analyze the time complexity of the following AI task process.
function aiProcess(data) {
let results = [];
for (let item of data) {
let processed = analyze(item);
results.push(processed);
}
return results;
}
function analyze(input) {
// Simulate some AI computation
return input * 2;
}
This code takes a list of data items and processes each one using a simple AI analysis function.
Look for repeated steps that take most of the time.
- Primary operation: Looping through each data item and analyzing it.
- How many times: Once for every item in the input list.
As the number of data items grows, the total work grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 analyses |
| 100 | 100 analyses |
| 1000 | 1000 analyses |
Pattern observation: Doubling the input doubles the work needed.
Time Complexity: O(n)
This means the time to complete the AI task grows directly in proportion to the number of data items.
[X] Wrong: "AI tasks always take the same time no matter how much data there is."
[OK] Correct: The time depends on how many items the AI needs to process; more data means more work.
Understanding how AI task time grows helps you explain and design efficient AI solutions in real projects.
"What if the analyze function itself called another loop over the input? How would the time complexity change?"