The algorithm is incorrect. Consider the following example:
Counterexample
|----F----| |-----G------| |-------D-------| |--------E--------| |-----A------| |------B------| |------C-------|
You can see that there is a solution of at least 3 in size, because you can pick A, B and C
First, let the calculation for each interval the number of intersections:
A = 2 [F, D] B = 4 [D, F, E, G] C = 2 [E, G] D = 3 [A, B, F] E = 3 [B, C, G] F = 3 [A, B, D] G = 3 [B, C, E]
Now consider the launch of your algorithm. In the first step, we will remove B because it intersects with the largest number of inversions and we get:
|----F----| |-----G------| |-------D-------| |--------E--------| |-----A------| |------C-------|
It is easy to see that now from {A, D, F} you can choose only one, because each pair intersects. The same case with {G, E, C} , so after deleting B you can choose at most one of {A, D, F} and at most one of {G, E, C} to get a total of 2 , which smaller than {A, B, C} .
The conclusion is that after removing B , which intersects with a large number of intervals, you cannot get the maximum number of disjoint films.
Correct solution
The problem is very well known, and one solution is to select the interval that ends first, delete all intervals intersecting with it and continue until the intervals are detected. This is an example of a greedy method, and you can find or develop proof of its correctness.