## Partitioning The Telephone Book

### July 10, 2015

We begin with the data:

`(define phone (list 16 4 17 10 15 4 4 6 7 14 9 17 27 6 1 9 0 12 20 8 0 3 4 0 3 4))`

Our first solution is brute force: generate all possible partitionings using the stars-and-bars algorithm, then score each one and keep the minimum. For a problem with *n* = 26 inputs and *k* = 4 partitions, there are *n*+*k*-1 `choose`

*k*-1 = 26+4-1 `choose`

4-1 = 29 `choose`

3 = 3654 possible partitionings (where `choose`

computes the binomial number):

`(define (parts stars bars)`

(if (= bars 1) (list (list stars))

(let loop ((i 0) (ps (parts stars (- bars 1))) (zs (list)))

(if (< stars i) zs

(if (null? ps)

(loop (+ i 1) (parts (- stars i 1) (- bars 1)) zs)

(loop i (cdr ps) (cons (cons i (car ps)) zs)))))))

The `score`

function computes the sum of the differences to the ideal partition:

`(define (score xs part ideal)`

(let loop ((xs xs) (part part) (zs (list)))

(if (pair? part)

(call-with-values

(lambda () (split (car part) xs))

(lambda (first rest)

(loop rest (cdr part) (cons first zs))))

(let loop ((zs (map sum (reverse zs))) (tot 0))

(if (null? zs) tot

(loop (cdr zs) (+ tot (abs (- (car zs) ideal)))))))))

Here we loop through the partitions, keeping track of the minimum as we go:

`(define (phone-book xs k)`

(let ((min-part '()) (min-diff 10000000))

(do ((ps (parts (length xs) k) (cdr ps)))

((null? ps) (values min-part min-diff))

(let ((s (score xs (car ps) (/ (sum xs) k))))

(when (< s min-diff)

(set! min-part (car ps))

(set! min-diff s))))))

And here’s the solution of our test problem, where (4 7 6 9) corresponds to A-D, E-K, L-Q, R-Z with a score of 18:

`> (partition phone 4)`

(4 7 6 9)

18

The brute force solution doesn’t scale well, being essentially exponential in the number of stars. So I tried a scanning algorithm that runs through the alphabet left to right, making the “greedy” choice to add letters up to the ideal partition, splitting where the current cumulative difference is least. That doesn’t work. The first partition starts with 16 + 4 + 17 + 10 = 47, then you have to decide whether to add 15. If you don’t, the difference is 55-47 = 8, and if you do, the difference is 47+15-55 = 7, so on the greedy principle we add 15 to the first partition, and we’ve gone wrong; if you carry that algorithm to the end, with boundaries at 55, 110, and 165, the partition is (5 6 6 9) and the score is 24. So the greedy algorithm doesn’t work.

Unfortunately I don’t know a better algorithm. My instinct is that there exists a dynamic programming algorithm with time complexity O(*kn*), but in several experiments I haven’t found it. Perhaps one of you readers can help.

We used `split`

from the Standard Prelude. You can run the program at http://ideone.com/kFK44h.

In Python. As the count for Q is zero, the answer is slightly ambiguous. [(‘A’, ‘D’), (‘E’, ‘K’), (‘L’, ‘Q’), (‘R’, ‘Z’)] or [(‘A’, ‘D’), (‘E’, ‘K’), (‘L’, ‘P’), (‘R’, ‘Z’)] are essentially the same answer.

BFI approach in perl… note the best way to get the size of the partitions is to generate the CDF and diffing this…

It was hard for me and i am still not satisfied with my solution.

In Scala Worksheet:

package programmingpraxis

// https://programmingpraxis.com/2015/07/10/partitioning-the-telephone-book/

object TelefonPartitioning {

val letters = List(“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “0”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”)

val num = List(16, 4, 17, 10, 15, 4, 4, 6, 7, 14, 9, 17, 27, 6, 1, 9, 0, 12, 20, 8, 0, 3, 4, 0, 3, 4)

val volumes = 4

val letterNumbers = letters zip num

def partitionsEndCounters(n: Int, parts: Int): Seq[Seq[Int]] = {

if (parts == 1) {

List(List(n))

} else {

for (

endIndex <- 1 to n – parts +1;

rest x + endIndex)

}

}

val total = num.sum

val optimal = total / volumes

val partitionsEndCts = partitionsEndCounters(num.size, volumes)

val partitionEndIndices = partitionsEndCts.map(x=> x.map { x => x-1 })

val partitions=partitionEndIndices.map { x => for(i x.map { x => num.slice(x._1, x._2+1).sum } }

val differencesSums = partitionSums.map { x => x.map { x => (optimal-x).abs }.sum }

val min = differencesSums.min

val beste = (differencesSums zip partitions) filter(p=>p._1==min)

val bestLetters = beste.unzip._2.map { x => x.map(x=>letters(x._1)+”-“+letters(x._2))}

//> bestLetters : Seq[scala.collection.immutable.IndexedSeq[String]] = Vector(

//| Vector(A-D, E-K, L-P, Q-Z), Vector(A-D, E-K, L-Q, R-Z))

}

Python. Basically using an A* search.

def get_pos_of(iterable, ref):

j = 0

for i in iterable:

if ref == i:

return j

j += 1

return j

def gen_dif(combination,letters,values):

temp = []

totaldifference=0

for c in combination:

a,b = c.split(‘-‘)[0],c.split(‘-‘)[1]

tj = 0

for i in range(get_pos_of(letters,a),get_pos_of(letters,b)):

tj+=values[i]

temp.append(tj)

totaldifference = abs(temp[0]-temp[1])+abs(temp[1]-temp[2])+abs(temp[2]-temp[3])

return totaldifference

def gen_combination(lisa):

telebooks=1

tempnumber=0

combination=[]

while telebooks <= 4:

random.seed()

if telebooks == 4:

try:

combination.append('%s-%s'%(lisa[tempnumber],'Z'))

except:

pass;

#print(combination)

else:

try:

r=random.randint(tempnumber,(len(lisa)-1)-(4-telebooks)-1)

except:

pass;

#print((len(lisa)-1)-(4-telebooks)-1)

if r==tempnumber:

r+=1

combination.append('%s-%s'%(lisa[tempnumber],lisa[r]))

tempnumber = r+1

telebooks+=1

return combination

def index_phonebook():

lisa = list("abcdefghijklmnopqrstuvwxyz".upper())

lisb = [16,4,17,10,15,4,4,6,7,14,9,17,27,6,1,9,0,12,20,8,0,3,4,0,3,4]

used_combinations = [['A-E','F-K','L-O','P-Z'],[]]

best_dif = 26

best_combination = ['A-D','E-J','K-O','P-Z']

for i in range(0,1000):

print(i)

while True:

temp_combination=gen_combination(lisa)

if temp_combination not in used_combinations:

used_combinations.append(temp_combination)

temp_dif = gen_dif(temp_combination,lisa,lisb)

if temp_dif < best_dif:

best_dif = temp_dif

best_combination=temp_combination

break;

print(best_dif)

print(best_combination)

> (parts 4 3)

((4 0 0) (3 0 1) (3 1 0) (2 0 2) (2 1 1) (2 2 0) (1 0 3) (1 1 2) (1 2 1) (1 3 0) (0 0 4) (0 1 3) (0 2 2) (0 3 1) (0 4 0))

we don’t need most of part in the above result because any phone book volume must have more than 1 phone numbers

(4 0 0)

(3 0 1)

(3 1 0)

(2 0 2)

(2 2 0)

(1 0 3)

(1 3 0)

(0 0 4)

(0 1 3)

(0 2 2)

(0 3 1)

(0 4 0)

So… below is the more efficient version of “parts” function

> (parts+ 4 3)

((2 1 1) (1 1 2) (1 2 1))

(define (parts+ stars bars)

(if (= bars 1)

(list (list stars))

(let loop ((i 1)

(ps (parts+ (- stars 1) (- bars 1)))

(zs (list)))

(if (> i (+ (- stars bars) 1))

zs

(if (null? ps)

(loop (1+ i) (parts+ (- stars i 1) (1- bars)) zs)

(loop i (cdr ps) (cons (cons i (car ps)) zs)))))))

> (length (parts 30 8))

10,295,472

> (length (parts+ 30 8))

1,560,780

(define (parts+ stars bars)

(if (= bars 1)

(list (list stars))

(let loop ((i 1)

(ps (parts*+ (- stars 1) (- bars 1)))

(zs (list)))

(if (> i (+ (- stars bars) 1))

zs

(if (null? ps)

(loop (1+ i) (parts+ (- stars i 1) (1- bars)) zs)

(loop i (cdr ps) (cons (cons i (car ps)) zs)))))))

(define (parts+ stars bars)

(if (= bars 1)

(list (list stars))

(let loop ((i 1)

(ps (parts+ (- stars 1) (- bars 1)))

(zs (list)))

(if (> i (+ (- stars bars) 1))

zs

(if (null? ps)

(loop (1+ i) (parts+ (- stars i 1) (1- bars)) zs)

(loop i (cdr ps) (cons (cons i (car ps)) zs)))))))

[\sourcecode]

I tried CLP in gprolog 1.4.4, but couldn’t find a heuristic to find the optimal score of 18.

I modified the prolog solution to only use constraints. No difference, except the program text is shorter and clearer: The score predicate vanishes, and the partition predicate is modified to be more direct.

If I fix Target on 63 instead of the calculated Last div 4 =:= 61 then branch-and-bound finds the 2 optimal solutions with score = 18: A-C, D-J, K-O, P-Z; and A-C, D-J, K-P, Q-Z.

Of course, the truncation of div: Last is 246, and 246 / 4 =:= 61.5; 4 * 0.5 =:= 2 + 61 =:= 63.