I am trying to come up with an algorithm for calculating the maximum number of busy channels in the telephone system.
There is a Call Data Registration (CDR) file, which specifies the begin (TB) and end (TE) time of each call.
From it one can build an array T: ( (T1B, T1E), (T2B, T2E), (T3B, T3E), (T4B, T4E), (T5B, T5E), (T6B, T6E), (T7B, T7E), (T8B, T8E), (T9B, T9E) )
Multiple calls can occur simultaneously. Each call occupies one channel.
Example is in the attached picture. T - time, N - number of channels.
As you can see, there is a maximum of 4 occupied channels between T6B and T2E.
In principle, there can be several such maximum values.
I have some basic ideas how to start working on this, вut I am sure that this is a well-known problem that has a solution in the form of a proven algorithm.

- 119
- 8
-
You need to look at the use of an [Erlang](https://en.wikipedia.org/wiki/Erlang_(unit)) analysis – itprorh66 Jul 28 '23 at 13:34
1 Answers
You are right it is indeed a common problem. You can state it this way: Given an array of segments find a maximum number of intersecting segments.
Intuition
Basically, you can look at your image. Declare cumulative variable, then go form left to right, any time the call is started you add one, when the call is ended you substract one. All time maximum value of variable is the answer.
Algorithm
Let segments
be an array of pairs (start, end)
of length N
.
Construct array events
of pairs (time, event)
, where
time
equals to all possiblestart
andend
fromsegments
;event
equals to1
iftime
is a start of any segment and-1
if it is an end.
Sort array events
in ascending order.
Let cur = 0
be a cumulative variable.
Let mmax = 0
be a maximum number of intersecting segments.
Iterate over array events
and on every iteration:
- Add current value to
cur
- Set
mmax
to maximum betweenmmax
andcur
max
is an answer.
Note
You should properly handle situations when in any given moment one call is ending and another one is starting (which event comes first?).

- 340
- 2
- 11
-
Thanks, @Andrey. This is the straightforward solution that I was thinking about, too. Of course it should work. I asked a question here in hope to find some non-evident efficient algoritm. I came across a mention of using a red-black tree algorithm to solve this problem. But I haven't figured out how to use it yet. https://stackoverflow.com/questions/14780324/point-of-maximum-overlap – Vodnik Jul 28 '23 at 16:21
-
1@Vodnik, I don't think it's possible to solve this problem with better complexity than `O(NlogN)`. If you find any interesting or fast solution, please leave link somewhere there. – Andrey Jul 28 '23 at 16:45