Alarm flooding prevention in SCADA systems - Time & Space Complexity
When preventing alarm flooding in SCADA systems, it's important to know how the system handles many alarms quickly.
We want to understand how the time to process alarms grows as more alarms come in.
Analyze the time complexity of the following code snippet.
function preventAlarmFlooding(alarms) {
let recentAlarms = new Set();
for (let alarm of alarms) {
if (!recentAlarms.has(alarm.id)) {
processAlarm(alarm);
recentAlarms.add(alarm.id);
}
}
}
This code processes a list of alarms but only acts on each unique alarm once to avoid flooding.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each alarm in the alarms list.
- How many times: Once for each alarm in the input list.
As the number of alarms increases, the system checks each alarm once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of operations grows directly with the number of alarms.
Time Complexity: O(n)
This means the time to process alarms grows in a straight line as more alarms come in.
[X] Wrong: "Checking each alarm multiple times is okay because alarms are rare."
[OK] Correct: In busy systems, alarms can come fast and many, so checking repeatedly wastes time and can cause delays.
Understanding how to handle many alarms efficiently shows you can manage system load and keep operations smooth.
"What if we used a list instead of a set to track recent alarms? How would the time complexity change?"