Home > Net >  Minimum jumps required to make a group of persons sit together- Proof of Greedy Approach?
Minimum jumps required to make a group of persons sit together- Proof of Greedy Approach?

Time:06-17

I found one greedy algorithm problem, where we have to find min no. of jumps required to make persons sit together.
Here is a link to the article for reference.

Question Description from article:

  • Title:
    Minimum jumps required to make a group of persons sit together.

  • Description:
    Given a string S of length N consisting of ‘x’ and ‘.’. The given string represents a row of seats where ‘x’ and ‘.’ represent occupied and unoccupied seats respectively. The task is to minimize the total number of hops or jumps to make all the occupants sit together i.e., next to each other, without having any vacant seat between them.

  • Examples:

    • Input: S = “. . . . x . . x x . . . x . .”
      Output: 5
      Explanation: Make the person at 5th seat jump 2 places to the 7th seat. Make the person at 13th seat jump 3 places to the 10th seat.Therefore, total number of jumps required = 2 3 = 5.

    • Input: S = “x x x . . . . . . . . x x x x x x”
      Output: 24
      Explanation: Move the occupants from 1st, 2nd and 3rd position to the 9th, 10th, 11th positions respectively. Therefore, the total number of jumps required = (11 – 3) (10 – 2) (9 – 3) = 24.

  • Approach:
    The idea is to use a Greedy Approach to solve this problem. Observe that it is always optimal to shift the elements towards the median element among the persons or the center person among all the persons present. The number of jumps will always be minimum when we shift points to the median.

The greedy solution says to shift the elements towards the median. But no proof of this has been found. Does there exist some mathematical proof of using this approach?

CodePudding user response:

Let a[i] be initial position of person i (0-based indexing), where order of people is set to be in order of increasing positions, and n be the count of people. Suppose that final interval of positions, where all people sit consecutively, starts at position t. Since relative order of people doesn't change, we see that person i moves from a[i] to t i. When and how exactly this move happens is unimportant to us, but there's never need for a person to jump in the direction away from its target position, so it contributes |a[i] - (t i)| == |t - (a[i] - i)| == |t - b[i]| jumps, where b, defined by b[i] = a[i] - i, is a non-decreasing array since a is strictly increasing (thus b[i 1] - b[i] == a[i 1] - (a[i] 1) >= 0).

So, minimal total number of jumps for resulting interval starting at position t is total[t] = sum(i, |t - b[i]|). Now, d_total[t] = total[t 1] - total[t] == sum(i, |t - b[i] 1| - |t - b[i]|) == sum(i, t >= b[i] ? 1 : -1) == count(i, t >= b[i]) - count(i, t < b[i]) == 2*count(i, t >= b[i]) - n is the 'discrete derivative' of total, and it's easy to see that as t increases, d_total[t] is non-decreasing, increasing at points where t == b[i] for some i by 2*count(i, t == b[i]). The smallest value of total happens at the smallest t for which d_total[t] is positive (which means count(i, t >= b[i]) > n/2).

In particular, for odd n == 2*k 1 such t is exactly the median of b, and 'center' of resulting interval is t k == b[k] k == a[k], i.e. median of a, as is assumed in the solution. For even n == 2*k we have inclusive interval of t between b[k - 1] and b[k] where total[t] is minimal (d_total[t] == 0 for t є [b[k - 1]; b[k] - 1] and t == b[k] is the smallest where d_total[t] > 0), which means that resulting interval's 'left center' is t k - 1 є [a[k - 1]; a[k] - 1] (or, equivalently, 'right center' t k є [a[k - 1] 1; a[k]]), so we have several optimal solutions in this case if a[k] > a[k - 1].

In any case, as we see, optimal strategy is to jump towards the median (or 'median interval' in even n case, but since median is then defined as average of the ends of this interval, we still can say 'towards the median') of the starting people positions.

Finally, maybe this is obvious, but for completeness: note that the obtained global minimum t doesn't produce interval that goes beyond the input string because, as is easy to prove in both parity cases, a[0] <= t <= t n - 1 <= a[n - 1], and a elements are positions withing this string. So, this global minimum is always suitable for our purposes.

  • Related