Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster algorithm for counting active calls

We are implementing a density report for a call center. The result must be displayed as table with a row per day showing the maximum number of simultaneously active calls during that day.

We are building the lib behind the UI. The contract specifies we receive the number of calls for that day and two arrays of integers, one with the start time and one with the end time of each call, so, for example:

For a given day just two calls are received: One goes from time 20 to 30 and the other one from 10 to 20. The maximum number simultaneously calls is 1.

On the other hand, for another day, also two calls are received, one from 10 to 45 and the other from 15 to 40 then the maximum number of simultaneously calls is 2.

The contract for the web service is this

public static int GetMaxDensity(int N, int[] X, int[] Y)

And the data looks like this (suppose 3 calls where received that day). First one from 10 to 25, second one from 12 to 30 and third one from 20 to 23.

N = 3, 
X = {10, 12, 20}
Y = {25, 30, 23}

And the return must be: 3.

I've implemented this solution:

public static int GetMaxDensity(int N, int[] X, int[] Y) 
{
  int result = 0;
  for (int i = 0; i < N; i++) 
  {
      int count = 0, t = X[i];
      for (int j = 0; j < N; j++) 
      {
        if (X[j] <= t && t < Y[j])
        count++;
      }
      result = Math.max(count, result);
   }
   return result;
}

And it works great when the number of calls is up to 1000 (weekends) but within work days the number is pretty big and the calculation takes so long (>5 minutes). I now the reason could be my solution is using two nested cycles but I don't have pretty much experience with complex algorithms so my question is:

Given that I just need the maximum number of simultaneously calls (not the times nor the callers), which could be a faster way to perform this calculation if there is one.

like image 586
Erre Efe Avatar asked Jan 16 '23 14:01

Erre Efe


1 Answers

As N grows your time grows rapidly (N*N). A simple solution (if your times are in intervals of minutes past midnight) would be to create an array of 1440 ints that will contain the change in call counts for each minute through the day. Then you can loop just once from 0 to N-1, and for each element, adjust the count of the call count delta at that point in time by incrementing the value at the time the call starts, and decrementing at the time it ends. After that, just look through the counts to obtain the largest value. This should be much faster for larger values of N.

Since 1440 is a constant (for the last step), and the inputs do not need to be sorted, this should have linear time complexity. This algorithm's run time is not affected by the average call length.

public static int GetMaxDensity(int N, int[] X, int[] Y) {
    int rangeStart = Integer.MAX_VALUE;
    int rangeEnd = Integer.MIN_VALUE;
    for(int i=0; i<N; i++) {
        if (X[i] < rangeStart) rangeStart = X[i];
        if (Y[i] > rangeEnd) rangeEnd = Y[i];
    } 
    int rangeSize = rangeEnd - rangeStart + 1;
    int[] histogram = new int[rangeSize];
    for (int t = 0; t < rangeSize; t++) histogram[t] = 0;
    for (int i = 0; i < N; i++) {
        histogram[X[i]-rangeStart]++;
        histogram[Y[i]-rangeStart]--;
    }
    int maxCount = 0;
    int count = 0;
    for (int t = 0; t < rangeSize; t++) {
        count += histogram[t];
        if (count > maxCount) maxCount = count;
    }
    return maxCount;        
}

For comparision, with N=50,000 and random call lengths between 1 and 40 minutes, the algorithm in the question used 29,043 milliseconds, and this algorithm used 8 milliseconds. I ran these tests in c#, but they should be comparable to what Java would produce.

like image 158
hatchet - done with SOverflow Avatar answered Jan 30 '23 19:01

hatchet - done with SOverflow