Distributed task allocation in Drone Programming - Time & Space Complexity
When drones share tasks, we want to know how the time to assign tasks grows as more drones or tasks appear.
We ask: How does the work needed to allocate tasks change when the number of drones or tasks increases?
Analyze the time complexity of the following code snippet.
for drone in drones:
for task in tasks:
if drone.can_perform(task):
drone.assign(task)
break
This code tries to assign each drone the first task it can perform from a list of tasks.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops over drones and tasks.
- How many times: For each drone, it checks tasks until it finds one it can do.
As the number of drones and tasks grows, the checks increase roughly by multiplying their counts.
| Input Size (drones x tasks) | Approx. Operations |
|---|---|
| 10 x 10 | Up to 100 checks |
| 100 x 100 | Up to 10,000 checks |
| 1000 x 1000 | Up to 1,000,000 checks |
Pattern observation: The total work grows quickly as both drones and tasks increase, multiplying together.
Time Complexity: O(d * t)
This means the time to assign tasks grows proportionally to the number of drones times the number of tasks.
[X] Wrong: "The time only depends on the number of drones or only on tasks, not both."
[OK] Correct: Each drone may check many tasks, so both counts multiply to affect total time.
Understanding how nested loops affect time helps you explain how systems handle many drones and tasks efficiently.
"What if each drone could perform multiple tasks instead of just one? How would the time complexity change?"
Practice
Solution
Step 1: Understand distributed task allocation
It means dividing tasks so all drones get work fairly.Step 2: Identify the goal in drone programming
The goal is to avoid a single controller and let drones share tasks efficiently.Final Answer:
To share work fairly among drones without a central controller -> Option AQuick Check:
Distributed task allocation = fair work sharing [OK]
- Thinking one drone does all tasks
- Assuming tasks are assigned randomly
- Believing drones don't communicate
Solution
Step 1: Understand round-robin assignment
Round-robin cycles through drones by using modulo with drone count.Step 2: Apply modulo operator correctly
Using task_index % len(drones) cycles task indices over drone list indices.Final Answer:
task_index % len(drones) -> Option DQuick Check:
Modulo with drone count cycles tasks [OK]
- Reversing modulo operands
- Adding or multiplying instead of modulo
- Using modulo with wrong variable
tasks = ['scan', 'deliver', 'map', 'charge']
drones = ['droneA', 'droneB']
assignments = {}
for i, task in enumerate(tasks):
drone = drones[i % len(drones)]
assignments.setdefault(drone, []).append(task)
print(assignments)Solution
Step 1: Assign tasks using modulo
Tasks are assigned to drones cycling through drones list by i % 2.Step 2: Map tasks to drones
Index 0: 'scan' -> droneA, 1: 'deliver' -> droneB, 2: 'map' -> droneA, 3: 'charge' -> droneB.Final Answer:
{'droneA': ['scan', 'map'], 'droneB': ['deliver', 'charge']} -> Option AQuick Check:
Modulo cycles tasks evenly [OK]
- Mixing task order in assignments
- Ignoring modulo cycling
- Appending tasks to wrong drone
drones = ['D1', 'D2', 'D3']
tasks = ['photo', 'survey', 'deliver']
assignments = {}
for i in range(len(tasks)):
drone = drones[i // len(drones)]
assignments.setdefault(drone, []).append(tasks[i])
print(assignments)Solution
Step 1: Check drone index calculation
Using i // len(drones) results in index 0 for i=0,1,2 (len(drones)=3), assigning all tasks to 'D1' instead of cycling round-robin.Step 2: Identify correct operator
Modulo (%) should be used to cycle through drone indices safely.Final Answer:
Using integer division (//) instead of modulo (%) assigns all tasks to the first drone -> Option CQuick Check:
Use modulo (%) to cycle indices [OK]
- Confusing // with %
- Thinking range(len(tasks)) is wrong
- Misusing setdefault method
Solution
Step 1: Filter out 'wait' tasks correctly
Check task != 'wait' before assigning to drones.Step 2: Assign tasks using modulo for round-robin
Use i % len(drones) to cycle drones correctly.Step 3: Avoid using index() inside loop to prevent errors with duplicates
Using tasks.index(task) can cause wrong drone assignment if tasks repeat.Final Answer:
assignments = {} for i, task in enumerate(tasks): drone = drones[i % len(drones)] if task != 'wait': assignments.setdefault(drone, []).append(task) -> Option BQuick Check:
Filter first, then assign with modulo [OK]
- Using integer division instead of modulo
- Using tasks.index() causing wrong assignments
- Not skipping 'wait' tasks properly
Ready to prove it?
Pass the test and this pattern moves unknown
