Multi-drone coordination concept in Drone Programming - Time & Space Complexity
When multiple drones work together, their coordination steps take time. We want to know how this time grows as we add more drones.
How does the number of drones affect the total coordination time?
Analyze the time complexity of the following code snippet.
function coordinateDrones(drones) {
for (let i = 0; i < drones.length; i++) {
for (let j = i + 1; j < drones.length; j++) {
drones[i].sendMessage(drones[j]);
drones[j].sendMessage(drones[i]);
}
}
}
This code makes each drone send messages to every other drone to coordinate their actions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops where each drone communicates with every other drone.
- How many times: The inner loop runs fewer times each iteration, but overall pairs grow with the square of the number of drones.
As we add more drones, the number of message exchanges grows quickly because each drone talks to all others.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 90 message pairs |
| 100 | About 9,900 message pairs |
| 1000 | About 999,000 message pairs |
Pattern observation: The number of operations grows roughly with the square of the number of drones.
Time Complexity: O(n²)
This means if you double the number of drones, the coordination steps roughly quadruple.
[X] Wrong: "Each drone only talks once, so time grows linearly with drones."
[OK] Correct: Each drone talks to every other drone, so the total messages grow much faster than just one per drone.
Understanding how tasks grow with more drones shows you can think about scaling and efficiency, a key skill in real projects.
"What if drones only communicated with their immediate neighbors instead of all others? How would the time complexity change?"