the train marshalling problem joe ryan university of ballarat joint work with elias dahlhaus, peter...
TRANSCRIPT
The Train Marshalling Problem
Joe RyanUniversity of Ballarat
joint work withElias Dahlhaus, Peter Horak and Mirka Miller
Outline of Talk
1. Define the problem
2. TMP is NP-Complete• Reduction from NMTS• TMP NMTS
3. Upper bound in terms of n
4. Conclusion
The Train Marshalling Problem
• The cars have different destinations, • we use auxiliary rails to rearrange their order so
that all cars with the same destination will be grouped together.
• Since, in general, there may be several trains at the station to be processed at the same time, the target is to use as few auxiliary rails as possible for each train.
1 6 112 73 4 5 8 9 10
1 6 112 73 4 5 8 9 10
Station 1: Cars 1,6,11Station 2: Cars 5,10Station 3: Cars 3,8Station 4: Cars 2,7Station 5: Cars 4,9
Track A
Track B
Track C
Track D
Reassemble the train from Track D followed, in order, by tracks C, B and A.
1 8 56 311 2 7 4 9 10
Station 1 Station 2Station 5Station 3Station 4
Train Ready to Distribute Carriages
The Train Marshalling Problem
Given a partition S of {1,…,n} into disjoint sets
S1,…,St, find the smallest number k = K(S) so that there exists a permutation p(1),…,p(t) of {1,…,t} with the property: The sequence of numbers 1,2,…,n,1,2,…,n,…,1,2,…,n, where the interval1,2,…,n is repeated k times, contains all the
elements of Sp(1) , then all elements of Sp(2),… etc., and finally all elements of Sp(t) .
The Train Marshalling Problem
n = 11 1,2,3,4,5,6,7,8,9,10,11
S = {S1,S2,S3,S4,S5}
S1 = {1,6,11}
S2 = {5,10}
S3 = {3,8}
S4 = {2,7}
S5 = {4,9}
k = K(S) = 4
1234567891011,1234567891011,1234567891011,1234567891011
Numerical Matching with Target Sums*
a1,a2,a3, …,ai,…,aj,…,ak,…,an-1,an
b1,b2,b3, …,bi,…,bj,…,bk,…,bn-1,bn
c1,c2,c3, …,ci,…,cj,…,ck,…,cn-1,cn
+
=
+
=
* Garey and Johnson:- Problem SP17
Numerical Matching with Target Sums
Instance: positive integers
I: a1,a2,…,an,b1,b2,…,bn,c1,c2,…cn
Question: Are there permutations 1 and 2
such that for all i = 1,2,…n,
a1(i) + b2(i) = ci ?Restriction
ai 2, bi 2, cj bi, cj ai + 2.
The Reduction
• Let a1,a2,…,an,b1,b2,…,bn,c1,c2,…cn be positive integers satisfying the restrictions.
• Let N = nci.
• To the set (train) IN = {1,2,…,N} we assign a partition, R, of 2n elements (destinations);
S1,…,Sn,T1,…Tn.
• Divide the train into blocks X1,X2,…Xn
and subblocks Xi = Dij, j = 1,…,ci (in order).
The Reduction
• For any subblock Dij, and any destination
M, Dij contains either 0 or 1 car going to M.
• There is a car of destination Sk in Dij
iff j = ci−ak+1,…,ci
• And a car of destination Tk in Dij
iff j = 1,…,bk.
• In any subblock, cars with S destinations precede cars with T destinations and Si (Ti) precedes Sj (Tj) iff i < j.
The Reduction
• In each block there are ai cars with destination Si and bi cars with destination Ti.
• In each block there are ai + bi = ci cars.• So there are N = nci cars in R.• The first 2 subblocks in any block contain n
cars of T destinations (bi > 2) and no cars of S destinations (ai+2 cj).
• The last subblock is made up of n cars of S destinations (bi < cj).
Example
Let a1=2,a2=3,a3=3,b1=3,b2=4,b3=3,c1=5,c2=7,c3=6.
Then train R consists of N = 3(5+7+6) = 54 cars.
Recall
• There is a car of destination Sk in Dij
iff j = ci−ak+1,…,ci
• And a car of destination Tk in Dij
iff j = 1,…,bk.
Example
Let a1=2,a2=3,a3=3,b1=3,b2=4,b3=3,c1=5,c2=7,c3=6.
Then train R consists of N = 3(5+7+6) = 54 cars
t1t2t3−t1t2t3−s2s3t1t2t3−s1s2s3t2−s1s2s3║t1t2t3−t1t2t3−t1t2t3−t2
−s2s3−s1s2s3−s1s2s3║t1t2t3−t1t2t3−t1t2t3−s2s3t2−s1s2s3−s1s2s3
3−3−5−4−3║3−3−3−1−2−3−3║3−3−3−3−3−3
Partition isT1={1,4,9,19,22,25,37,40,43} T2={2,5,10,15,20,23,26,28,38,41,44,48}
T3={3,6,11,21,24,27,39,42,45} S1={12,16,31,34,49,52}
S2={7,13,17,29,32,35,46,50,53} S3={8,14,18,30,33,36,47,51,54}
Claim: TMP is NP-Complete
K(R) 2n−1 a1,…,an,b1,…,bn,c1,…,cn have a numerical matching.
(i.e.) the conditions of NMTS are satisfied.
K(R) 2n−1 NMTS
• We have permutations 1 and 2 satisfying
NMTS and need to show K(R) 2n−1.
• i.e. there is an order O of the 2n destinations so that the sequence
J = (2n−1)IN contains all cars of the first destination of O followed by all cars of the second etc.
K(R) 2n−1 NMTS
Let O = T2(n),S1(n),T2(n-1),S2(n-1),…,T2(1),S1(1)
nic
iac
ib
ni
ni
ii
i
D
D
D
D
mod)1(
1
mod)1(1
mod)1(
)(1
)(2
Choose as first car of T2(i) on J, the car in
Then the last placed car of T2(i) is in
Choose as first car of S1(i) on J, the car in
Then the last placed car of S1(i) is in
Example
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
S1 T1
S2 T2
S3 T3
K(R) 2n−1 NMTS
• Cars from the first destination are placed in the first round.
• Cars from the i-th destination are placed in the (i-1)-th round and the beginning of the i-th round.
• The last car of T2(1) is in
• So all the cars of S1(1) can
be placed in the same (2n−1)-th round.
1
)1(2bD
for this part
K(R) 2n−1 NMTS
• Consider the sequence
J = 1,2,…,N,1,2,…,N,…,1,2,…,N (2n−1) repetitions
• For destination M, let fM be the car of M placed first and lM the car of M placed last.
• Let JM be the interval on J from fM to lM.
• For each m IN we say m is covered by M
(destination) if at least 1 of the (2n−1) occurrences of m in J belong to interval JM.
Example
JT3 = [3,45], JS3
= [47,54][1,36]
4 is covered by both destinations T3 and S3
JT3 covers 4 in the first round of J and JS3
covers 4 in the
second round of J.
46 is covered by neither of them.
40 is covered by T3 but not by S3.
Each element of IN belongs to at most 2n−1 intervals.
K(R) 2n−1 NMTS
• Let xi = number of car of Tn in Di1
• Let yi = number of car of S1 in Dici
For i=1,..,n, xi is not covered by either
i) Any T destination (T Tn) with fT in
ii) Any Sj destination with fSj in
For i=1,…n, yi is not covered only by
iii) Any T destination with fT in
iv) Any S destination (S S1) with fS in
iac
i
jiD
D
1
2
ic
ni
iD
D mod)1(1
Example
• Let xi = number of car of Tn in Di1
• Let yi = number of car of S1 in Dici
car not covered by type
x1 = 3 S1 ii)
x2 = 21 S2 ii)
x3 = 39 S3 ii)
y1 = 16 T1 iii)
y2 = 34 T2 iii)
y3 = 52 T3 iii)
K(R) 2n−1 NMTS
• All T destinations are of type ii) so all S destinations are of type iii).
• For each i there is one T with fT in Xi and one S with fS in Xi.
• Let Tk be the T with fTk X(i+1)modn
• Let Sj be the S with fSj Xi.
• Then lTk < fSj
.
• Subblock with lTk precedes subblock with fSj
K(R) 2n−1 NMTS
• Tk does not cover the last ci−bk subblocks in Xi.
• Sj does not cover the first ci−aj subblocks in Xi.
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,2930,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
Tk Sj ci-bk ci-aj
X1 T1 S1 2 3 5
X2 T2 S2 3 4 7
X3 T3 S3 3 3 6
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54
S1 T1S2 T2S3 T3
3−3−5−4−3║3−3−3−1−2−3−3║3−3−3−3−3−3
a1 = 2 a2 = 3 a3 = 3
b1 = 3 b2 = 4 b3 = 3
c1 = 5 c2 = 7 c3 = 6
K(R) 2n−1 NMTS
• Tk does not cover the last ci−bk subblocks in Xi.• Sj does not cover the first ci−aj subblocks in Xi.
So ci−bk+ci−aj ci
i.e. ci aj + bk
but we can set 1(i) = j and 2(i) = k
Then we get, for i = 1,…,n, ci a1(i) + b2(i)
However we have ai + bi = ci
so inequality becomes equality.
The Upper Bound
• Since TMP is NP-complete, an upper bound for K(S) in terms of n may be useful. (We know that for t destinations K(S) t−1.)
• Let Kn be that maximum of K(S) where S ranges over all partitions of {1,…,n}.
• Main result for this section
Kn = n/4 + ½
Notation
• For S a partition of {1,…,n}, say
S = {S(1), S(2),…,S(s)}, we use S(i) to denote the set containing i. If we use a number xi, yi etc. then this number is from the same set as i.
So S(i) = {i, xi, yi,…}
• Let T−1 be the train obtained from T by reversing the cars. Then K(T) = K(T−1).
Observations
• For S′ = {S(i1),…,S(ir)} a subset of S where |S(i1) … S(ir)| = t then
K(S) K(S′) + K(S−S′) K(S′) + Kn-t
• Let S contain a destination with r cars or r destinations with a single car each. Then, K(S) 1 + Kn−r
Observations
• If all numbers of S(i) are smaller than all numbers of destination S(j) then we say that S(i) and S(j) do not overlap.
• If |S(i)| + |S(j)| = r and S(i) and S(j) do not overlap then K(S) 1 + Kn−r
Lemma
• Let S(i1),…,S(ik) be destinations of S such that |S(i1)| + |S(ik)| = t and all the numbers of the interval {1,…,j} belong to
U = S(i1)…S(ik). If it is possible to arrange all numbers from U into k rounds so that the last (k-th) round contains only numbers from the interval {1,…,j} then
K(S) k−1+Kn−t
Kn n/4 + ½ • We may assume n 5, and that n is even.
From the previous observations we have –
• At most one destination contains a single car.
• For all other destinations 2 |S(i)| 3.• Any two destinations with at least 4 cars in
total must overlap.
The Proof
Case 1: Let |S(1)| = |S(2)| = 2, then
K(S) 1+Kn−4 n/4 + ½
S(1) = {1,x1} differs from S(2) = {2,x2}, then either 1, x1, x2, * 2 or 2, x2, x1, * 1.
x2 x1
1
2
.
.
.
The Proof
Case 2: If one of S(1) and S(2) is of cardinality 1, then
K(S) 1+Kn−4 n/4 + ½Suppose |S(1)|=1, then there is no overlap with
any destination of cardinality 3. If there is no 3 car set then |S(n−1)|=|S(n)|=2 and use K(T−1).
If |S(2)|=1 then either |S(1)|=3 whence 2,x1,y1,*1Or there is |S(i)|=3 such that S(2) and S(i) do not
overlap.
The Proof
Case 3: Let |S(1)| = |S(2)| = 3 then either
K(S) 1+Kn−4 n/4 + ½ or K(S) 2+Kn−8 n/4 + ½
Let S(i) be the set with the smallest index after 2.
If |S(i)|=1 then the 3 sets can be placed in 2 rounds with any of 1,2,…,i in the third and use Lemma.
If |S(i)|=3 then the three sets can be placed in 2 rounds with 1,2,…,i in the third and use Lemma.
i
1 x1
2 x2
y2y1
xi yi
i
2 x2
1 x1
y1y2
xi yi
The Proof
Case 3: Let |S(1)| = |S(2)| = 3 then either
K(S) 1+Kn−4 n/4 + ½ or K(S) 2+Kn−8 n/4 + ½
Let S(i) be the set with the smallest index after 2.
If |S(i)|=2 then we can add a dummy car to S(i) and number it n+1 (it comes at the end of the train). Call this new partition S′. Then, employing the Lemma, we have
K(S) K(S′) 2+Kn+1−9 2+Kn−8 n/4 + ½
The Proof
Case 4: Let |S(1)| + |S(2)| = 5, then either
K(S) 1+Kn−4 n/4 + ½ or K(S) 2+Kn−8 n/4 + ½
Let S(1), S(2) be {a1,a2}, {b1,b2,b3}, then
If a2<b2<b3 use a1,a2,b2,b3,*b1 and the Lemma
If b2<b3<a2 use b1,b2,b3,a2,*a1 and the Lemma
If b2<a2<b3 then
4a: consider S(j) such that |S(j)|=3 and S(j)={c1,c2,c3}. Now consider U, the union of S(1), S(2), S(j) viz, 1,2,d3,d4,…,d8 (in increasing order).
The Proof
4a: We have 1<2<d3<d4<…<d8 and a2= di, i={5,6,7}The following satisfy the assumptions of the
Lemma and yield the bound;If a2=d7 use b1,b2,b3,*c1,c2,c3,a2,*a1
If a2=d6 use c1,c2,c3,b3,*b1,b2,a2,*a1 (2 cases)If a2=d5 use b1,b2,b3,c2,c3,*c1,a2,*a1 b3<c2<c3
c1,c2,c3,b3,*b1,b2,a2,*a1 c2<c3<b3
For d5=a2<c2<b3<c3 use previous cases on S(n−1), S(n) and consider K(S−1).
The Proof
4b: Let S(n), S(n−1) be {u1,u2}, {v1,v2,v3}.Assume there is no such S(j)={c1,c2,c3}, then
{b1,b2,b3} = {v1,v2,v3}. Since n is even there must be a destination with a single car, {w}.
Then either 4b1: {a1,a2} = {u1,u2} which gives, since the
sets must overlap, {b1,b2,b3} = {1,b2,n} so we can use w, b2, n,* 1,
or
The Proof
4b2: {a1,a2} {u1,u2} then consideri) {a1,b1} = {1,2}, {b3,u2} = {n-1,n} and u1<b2<w<a2
ii) Same as i) with a2 and w interchanged. Apply Lemma to S(1), S(2), {u1,u2} and {w} withi) b1,b2,b3,u2,*u1,w,a2,*a1 for u2>b3
u1,u2,b3,*b1,b2,w,a2,*a1 for u2<b3
i) a1,a2,w,u2,*u1,b2,b3,*b1
The proof of the statement Kn n/4 + ½ is now complete.
Kn = n/4 + ½ • We present a partition showing equality.
• Let k = n/2 and S = {S(1),…,S(k)} where S(i) = {i, i+k}, i = 1,…,k.
• Let A be an arrangement of cars in K(S) rounds of In.
• Then any round contains at most 4 cars and the first round contains at most 3 cars.
• So K(S) (n−3)/4+1 = n/4 + ½
Conclusion
A dispatcher in a marshalling yard knows that the TMP is NP-complete and that they must assume that they need min(Kn,T−1) auxiliary rails (T is the number of destinations).
To improve this bound the dispatcher needs someadditional information. The minimum number ofcars per destination should be readily available.
ConclusionLet K(n,m) = max K(s) where the train has n cars
and at least m go to any one destination. ThenConjecture:
K(n,m) (m1)d2/n+1/m where d = n/m
Putting S(i) = {i,i+d,…,i+(m−1)d}, i = 1,…,d givesK(n,m) (m−1)d2/n+1/m where d = n/m.
So that if the conjecture is true, this bound is the best possible.