Worst case analysis going from inner most loop to outer most (with mild abuse of the "=" sign):
-> O(K_SAMPLES) -- complexity of just the k-loop
-> neighbors * O(K_SAMPLES) -- complexity of f-loop accounted for
= SAMPLES * O(K_SAMPLES) -- since neighbors = SAMPLES in worst case
= O(SAMPLES * K_SAMPLES)
-> O(SAMPLES) + O(SAMPLES * K_SAMPLES) -- adding complexity of enumerate()
= O(SAMPLES + SAMPLES * K_SAMPLES)
= O(SAMPLES * K_SAMPLES)
The SAMPLES
term was dropped since SAMPLES * K_SAMPLES
grows asymptotically faster. More formally,
When C >= 2, SAMPLES >= 1, K_SAMPLES >= 1 then
SAMPLES + SAMPLES * K_SAMPLES <= C(SAMPLES * K_SAMPLES)
SAMPLES * (K_SAMPLES + 1) <= SAMPLES * C * K_SAMPLES
K_SAMPLES + 1 <= C * K_SAMPLES
For more info on big-O with multiple variables, see here. Continuing on with the last loop we have:
-> SAMPLES * O(SAMPLES * K_SAMPLES) -- complexity of i-loop accounted for
= O(SAMPLES^2 * K_SAMPLES)
Note that depending on the average number returned by find_neighbors(i)
, it can make the average big-O different.