## Partitioning The Telephone Book

### July 10, 2015

The city where I live used to publish a telephone directory; the “white pages” were distributed to all telephone customers once a year. Now my city no longer prints the directory; it is available on the internet, or you can pay an operator to look up a telephone number for you.

But there are still cities that print telephone directories, and some of those cities are big enough that the directory must be partitioned into multiple volumes. Consider this distribution of the first letters of customer’s last names:

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 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

I’m not sure what the units are (probably tens of thousands of telephone customers), but the total is 220, and the telephone company has decided to print 4 volumes, so each should be about 55 units. One possible partitioning is A-D, E-J, K-O, P-Z, with counts of 47, 50, 60 and 63 units, differences of 8, 5, 5 and 8 from the ideal of 55, and a total difference of 26. Another partitioning is A-E, F-K, L-O, P-Z, with counts of 62, 44, 51, and 63 units, differences of 7, 11, 4 and 8, and a total difference of 30, which is worse. Before continuing, you might want to work out the optimal solution by hand, finding a minimum score of 18.

Your task is to write a program that determines the partitioning of the telephone book that minimizes the total difference. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

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.