3
Airline Seating Problem Anirban Mitra September 10, 2012 1 Introduction Quiet frequently passengers on a flight want to have a seat next to his/her buddy. Now if on a certain flight, there are too many of such requests, a hostess may think about the minimum number of swaps required to satisfy everyone; and create as little ruckus as possible in the process. Inspired by this problem, we try to solve a simplified version where there is a single linear array of seats and each passenger wants to sit with his/her buddy 1 . Lets say a passenger is happy if he/she is seated next to his/her buddy and call it a happy pair. Given any seat arrangement initially, we want to go to an arrangement where everyone is happy with minimum swaps. 2 Example For example, say from a given initial permutation . . A . B . B . C . A . C we can reach a happy state 2 in 3 swaps as shown . . A . B . B . C . A . C 1 This problem was presented by Dr. Michael Bender http://www.cs.sunysb.edu/ bender/ for Algorithms Seminar http://www.cs.sunysb.edu/ rezaul/Fall-2012/CSE642/ on 31 Aug, 2012 2 there are other possible happy states but only one is reachable in 3 swaps 1

Airline seatproblem

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Airline seatproblem

Airline Seating Problem

Anirban Mitra

September 10, 2012

1 Introduction

Quiet frequently passengers on a flight want to have a seat next to his/herbuddy. Now if on a certain flight, there are too many of such requests, a hostessmay think about the minimum number of swaps required to satisfy everyone;and create as little ruckus as possible in the process. Inspired by this problem,we try to solve a simplified version where there is a single linear array of seatsand each passenger wants to sit with his/her buddy 1. Lets say a passenger ishappy if he/she is seated next to his/her buddy and call it a happy pair. Givenany seat arrangement initially, we want to go to an arrangement where everyoneis happy with minimum swaps.

2 Example

For example, say from a given initial permutation

..A. B. B. C. A. C

we can reach a happy state 2 in 3 swaps as shown

..A. B. B. C. A. C

1This problem was presented by Dr. Michael Bender http://www.cs.sunysb.edu/∼bender/ for Algorithms Seminar http://www.cs.sunysb.edu/∼rezaul/Fall-2012/CSE642/on 31 Aug, 2012

2there are other possible happy states but only one is reachable in 3 swaps

1

Page 2: Airline seatproblem

..A. A. B. C. B. C

..A. A. B. B. C. C

3 Algorithm

Here is a greedy algorithm pseudocode to solve the problem for a given array ofsize N = 2k, S[0..(2k − 1)]

for i = 0 → k doCheck if S[2i], S[2i+1] is a happy pair, if not then swap S[2i+1] with thebuddy of S[i]

end for

4 An Intuitive Proof

Let us define Pi = (A[i], A[2i+ 1]) for every i ∈ [0, k) as a pair. Also, lets say acomplete pair set is a set of pairs such that for any entity in the set, the buddyis also in the same set. A minimum complete pair set is one which cannot befurther sub-divided into complete pair sets. For example, the minimum completepair set in

..A. B. C. D. X. Y. X. Y. A. C. D. B

are 2, namely (P0, P1, P4, P5), (P2, P3). In other words the goal is to sub-divide a given permutation into k minimum complete pair sets with minimumswaps.

Now each minimum complete pair set can be viewed as loop graph wherebuddies in separate pairs are connected by an edge 3.

3this representation was suggested by a friend Dhruv matani http://dhruvbird.com

2

Page 3: Airline seatproblem

..A. B.

A

.

C

.

D

.

B

.

C

.

D

With one swap, one can at most split the loop into two, in other words oneswap can at most increase the number of minimum complete pair sets by one.So, for any given minimum complete pair set, one less than the length of setswaps are required to completely disintegrate the loop into self-loops. So, theminimum number of swaps required to reach a happy state

= (len(P0)− 1) + (len(P1)− 1) + ...+ (len(Pk)− 1)

= 2k − count(minimum complete pair set) (1)

So, as long as with each swap, we keep on increasing the minimum completepair sets, we are proceeding optimally. Now, its interesting to see that the abovementioned algorithm has this property, hence it is an optimal algorithm.

3