Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z
faqs.org - Internet FAQ Archives

rec.puzzles Archive (combinatorics), part 05 of 35


[ Usenet FAQs | Web FAQs | Documents | RFC Index | Neighborhoods ]
Archive-name: puzzles/archive/combinatorics
Last-modified: 17 Aug 1993
Version: 4

See reader questions & answers on this topic! - Help others by sharing your knowledge
==> combinatorics/alphabet.blocks.p <==
What is the minimum number of dice painted with one letter on all six sides
such that all permutations without repetitions of n letters can be formed
by placing n dice together in a line?

==> combinatorics/alphabet.blocks.s <==
n=   2          3          4          5          6
   (8,4)      (9,7)      (9,3)      (10,7)     (11,7)

   aijklm     abcde?     acdefg     abcde?     abkmuz
   bijklm     fghij?     bhijkl     fghij?     bcpwy?
   cnopqr     klmno?     cmnopq     klmno?     cdlnvz
   dnopqr     pqrst?     dhnrvy     pqrst?     deqxu?
   estuvw     uvwxy?     eiosvw     uvwxy?     efmowz
   fstuvw     afkpu?     fjptwx     afkpu?     fgryv?
   gxyz??     bglqv?     gkquxy     bglqv?     ghnkx?
   hxyz??     chmrwz     lmrstu     chmrwz     hisuw?
              dinsxz     zab???     dinsxz     ijoly?
                                    ejotyz     jatvx?
                                               pqrstz

I think I can prove that there is no solution with 11 dice with 9
don't cares or with 10 dice, but I haven't checked all the details, so
I might have made a mistake.  In any case, that leaves open the case
of 11 dice with 8 don't cares; my guess is that it is not possible.

-- John Rickard (jrickard@eoe.co.uk)

==> combinatorics/coinage/combinations.p <==
Assuming you have enough coins of 1, 5, 10, 25 and 50 cents, how many
ways are there to make change for a dollar?

==> combinatorics/coinage/combinations.s <==
292.  The table is shown below:

Amount  00 05 10 15 20 25 30 35 40 45 50 55 60 65  70  75  80  85  90  95  100
Coins  
.01      1  1  1  1  1  1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1
.05      1  2  3  4  5  6  7  8  9 10 11 12 13 14  15  16  17  18  19  20  21
.10      1  2  4  6  9 12 16 20 25 30 36 42 49 56  64  72  81  90 100 110 121
.25      1  2  4  6  9 13 18 24 31 39 49 60 73 87 103 121 141 163 187 214 242
.50      1  2  4  6  9 13 18 24 31 39 49 62 77 93 112 134 159 187 218 253 292

The meaning of each entry is as follows:
If you wish to make change for 50 cents using only pennies, nickels and dimes,
go to the .10 row and the 50 column to obtain 36 ways to do this.

To calculate each entry, you start with the pennies.  There is exactly one
way to make change for every amount.  Then calculate the .05 row by adding 
the number of ways to make change for the amount using pennies plus the number
of ways to make change for five cents less using nickels and pennies.  This 
continues on for all denominations of coins.  

An example, to get change for 75 cents using all coins up to a .50, add the
number of ways to make change using only .25 and down (121) and the number of
ways to make change for 25 cents using coins up to .50 (13).  This yields the
answer of 134.

==> combinatorics/coinage/dimes.p <==
"Dad wants one-cent, two-cent, three-cent, five-cent, and ten-cent
stamps.  He said to get four each of two sorts and three each of the
others, but I've forgotten which.  He gave me exactly enough to buy
them; just these dimes."  How many stamps of each type does Dad want?
A dime is worth ten cents.  -- J.A.H. Hunter

==> combinatorics/coinage/dimes.s <==
The easy way to solve this is to sell her three each, for
3x(1+2+3+5+10) = 63 cents.  Two more stamps must be bought, and they
must make seven cents (since 17 is too much), so the fourth stamps are
a two and a five.

==> combinatorics/coinage/impossible.p <==
What is the smallest number of coins that you can't make a dollar with?
I.e., for what N does there not exist a set of N coins adding up to a dollar?
It is possible to make a dollar with 1 current U.S. coin (a Susan B. Anthony),
2 coins (2 fifty cent pieces), 3 coins (2 quarters and a fifty cent piece),
etc.  It is not possible to make exactly a dollar with 101 coins.

==> combinatorics/coinage/impossible.s <==
The answer is 77:

a) 5c  = 1 or 5;
b) 10c = 1 or 2 a's (1,2,6,10)
c) 25c = 1 or 2 b's + 1 a
d) 50c = 1 or 2 c's
e) $1  = 1 or 2 d's

total	penny	nickel	dime	quarter	half
5		1	2	1	1
6		3	1	1	1
7		5		1	1
8		4	3		1
9		6	2		1
10		8	1		1
11		10			1
12		7	4	1
13		9	3	1
14		11	2	1
15		13	1	1
16		15		1
17		14	3
18		16	2
19		18	1
20		20
21	5	13	3
22	5	15	2
23	5	17	1
24	5	19
25	10	12	3
26	10	14	2
27	10	16	1
28	10	18
29	15	11	3
30	15	13	2
31	15	15	1
32	15	17
33	20	10	3
34	20	12	2
35	20	14	1
36	20	16
37	25	9	3
38	25	11	2
39	25	13	1
40	25	15
41	30	8	3
42	30	10	2
43	30	12	1
44	30	14
45	35	7	3
46	35	9	2
47	35	11	1
48	35	13
49	40	6	3
50	40	8	2
51	40	10	1
52	40	12
53	45	5	3
54	45	7	2
55	45	9	1
56	45	11
57	50	4	3
58	50	6	2
59	50	8	1
60	50	10
61	55	3	3
62	55	5	2
63	55	7	1
64	55	9
65	60	2	3
66	60	4	2
67	60	6	1
68	60	8
69	65	1	3
70	65	3	2
71	65	5	1
72	65	7
73	70		3
74	70	2	2
75	70	4	1
76	70	6
77	can't be done
78	75	1	2
79	75	3	1
80	75	5
81	can't be done
82	80		2
83	80	2	1
84	80	4	
85	can't be done
86	can't be done
87	85	1	1
88	85	3	
89	can't be done
90	can't be done
91	90		1
92	90	2
93-95	can't be done
96	95	1
97-99	can't be done
100	100

==> combinatorics/color.p <==
An urn contains n balls of different colors.  Randomly select a pair, repaint
the first to match the second, and replace the pair in the urn.  What is the
expected time until the balls are all the same color?

==> combinatorics/color.s <==
(n-1)^2.

If the color classes have sizes k1, k2, ..., km, then the expected number of
steps from here is (dropping the subscript on k):

     2               k(k-1)              (j-1) (k-j)
(n-1)  -  SUM      ( ------  +   SUM   --------------- )
         classes,      2        1<j<k      (n-j)
      class.size=k

The verification goes roughly as follows.  Defining  phi(k) as  (k(k-1)/2 +
sum[j]...), we first show that  phi(k+1) + phi(k-1) - 2*phi(k) = (n-1)/(n-k)
except when k=n; the k(k-1)/2 contributes 1, the term j=k contributes
(j-1)/(n-j)=(k-1)/(n-k), and the other summands j<k contribute nothing.
Then we say that the expected change in phi(k) on a given color class is
k*(n-k)/(n*(n-1)) times (phi(k+1) + phi(k-1) - 2*phi(k)), since with
probability k*(n-k)/(n*(n-1)) the class goes to size k+1 and with the same
probability it goes to size k-1.  This expected change comes out to k/n.
Summing over the color classes (and remembering the minus sign), the
expected change in the "cost from here" on one step is -1, except when we're
already monochromatic, where the handy exception k=n kicks in.

One can rewrite the contribution from k as

   (n-1) SUM (k-j)/(n-j)
        0<j<k

which incorporates both the k(k-1)/2 and the previous sum over j.
That makes the proof a little cleaner.

==> combinatorics/full.p <==
Consider a string that contains all substrings of length n.  For example,
for binary strings with n=2, a shortest string is 00110 -- it contains 00,
01, 10 and 11 as substrings.  Find the shortest such strings for all n.

==> combinatorics/full.s <==
Knuth, Volume 2 Seminumerical Algorithms, section 3.2.2 discusses this problem.
He cites the following results:
Shortest length: m^n + n - 1, where m = number of symbols in the language.
Algorithms:
[Exercise 7, W. Mantel, 1897]
The binary sequence is the LSB of X computed by the MIX program:
    LDA X
    JANZ *+2
    LDA A
    ADD X
    JNOV *+3
    JAZ *+2
    XOR A
    STA X
[Exercise 10, M. H. Martin, 1934]
Set x[1] = x[2] = ... = x[n] = 0.  Set x[i+1] = largest value < n such that
substring of n digits ending at x[i+1] does not occur earlier in string.
Terminate when this is not possible.

If we instead consider the strings as circular, we have a well known
problem whose solution is given by any hamiltonian cycle in the de
Bruijn (or Good) graph of dimension K.  (Or equivalently an eulerian
circuit in the de Bruijn graph of dimension K-1) As a string of length
2^K is produced, it must be optimal, and any shortest sequence must be
an eulerian circuit in a dB graph.

The de Bruijn graph Tn has as its vertex set the binary n-strings.
Directed edges join n-strings that may be derived by deleting the left
most digit and appending a 0 or 1 to the right end.  de Bruijn + van
Ardenne-Ehrenfest (in 1951) counted the number of eulerian circuits in
Tn. There are 2^(2^(n-1)-n) of them.

Some examples:
K=2 1100
K=3 11101000
K=4 1111001011010000

The solution to the above problem (non-circular strings) can be found
by duplicating the first K-1 digits of the solution string at the end
of the string.  These are not the only solutions, but they
are of minimum length: 2^K + K-1.  

We can obtain a lower bound for the optimal sequence for the general case as
follows:

Consider first the simpler case of breaking into an answer machine which
accepts d+1 digits, values 0 to n-1.  We wish to find the minimal universal
code that will allow us access to any such answering machine.

Let us construct a digraph G = (V,E), where the n^d vertices are labelled
with a d sequence of digits.  Notation: let [v_{i,1},v_{i,2},...,v_{i,d}]
denote the labelling on node v_i.  An edge e = (v_i, v_j) is in E iff for k
in 1, ..., d-1: v_{i,k+1} = v_{j,k}, i.e., the last d-1 digits in the
labelling of the initial vertex of e is identical with the first d-1 digits
in the labelling of the terminal vertex of e.  We associate with each edge a
value, t(e) = v_{j,d}, the last digit in the labelling of the terminal
vertex.

The intuition goes as follows:  we are going to perform a Euler circuit of
the digraph, where the label on the current vertex gives the last d digits
in the output sequence so far.  If we make a transition on edge e, we output
the tone/digit t(e) as the next output value, thus preserving the invariant
on the labelling.

How do we know that a Euler circuit exists?  Simple:  a connected digraph
has an Euler circuit iff for all vertices v: indegree(v) = outdegree(v).
This property is trivially true for this digraph.

So, in order to generate a universal code for the AM, we simply output 0^d
(to satisfy the precondition for being in vertex [0,...,0]), and perform an
Euler circuit starting at node [0,...,0].

Now, the total length of the universal sequence is just the number of edges
traversed in the Euler circuit plus the initial precondition sequence, or
n^d * n + d (number of vertices times the out-degree) or n^{d+1} + d.  That
this is a minimal sequence is obvious.

Next, let us consider the machine AM' where the security code is of the form
[0...n-1]^d [0...m-1], i.e., d digits ranging from 0 to n-1, followed by a
terminal digit ranging from 0 to m-1, m < n.

We build a digraph G = (V, E) similar to the construction above, except for
the following:  an edge e is in E iff t(e) in 0 to m-1.  This digraph is
clearly non-Eulerian.  In particular, there are two classes of vertices:

(1) v is of the form [0...n-1]^{d-1} [0...m-1]  (``fat'' vertices)

	and

(2) v is of the form [0...n-1]^{d-1} [m...n-1]  (``thin'' vertices)

Observations:  there are (n^{d-1} * m) fat vertices, and (n^{d-1} * (n-m))
thin vertices.  All vertices have out-degree of m.  Fat vertices have
in-degrees of n, and thin vertices have in-degrees of 0.  Color all the
edges blue.

The question now becomes:  can we put a bound on how many new (red) edges
must we add to G in order to make a blue edge covering path possible?
(Instead of thinking of edges being traversed multiple times in the blue
edge covering path, we allow multiple edges between vertices and allow each
edge to be traversed once.)  Note that, in this procedure, we add edges only
if it is allowed (the vertex labelling constraint).  We will first obtain a
lower bound on the length of a blue covering circuit, and then transform it
into a bound for arbitrary blue covering paths.

Clearly, we must add at least (n-m)*(n^{d-1}*m) edges incident from the fat
vertices.  [ We need (n-m) new out-going edges for each of (n^{d-1}*m)
vertices to bring the out-degree up to the in-degree. ]

Let us partition our vertices into sets.  Denote the range [0..m-1] by S,
the range [m..n-1] by L, and the range [0..n-1] by X.

Let S_0 = { v: v = [X^{d-1}S] }.  S_0 is just the set of fat vertices.
Define in(S_0) = number of edges from vertices not in S to vertices in S.
Define out(S_0) in the corresponding fashion, and let excess(S_0) =
in(S_0)-out(S_0).  Clearly, excess(S_0) = n^{d-1}m(n-m) from the argument
above.  Generalizing the requirement for Eulerian digraphs, we see that we
must add excess(S_0) edges from S_0 if the blue edges connected to/within
S_0 are to be covered by some circuit (edges may not be travered multiple
times -- we add parallel edges to handle that case).  In particular, edges
from S_0 will be incident on vertices of the form [X^{d-2}SX].  Furthermore,
they can not be [X^{d-2}SS] since that is a subset of S_0 and adding those
edges will not help excess(S_0).  [Now, these edges may be needed if we are
to have a circuit, but we do not consider them since they do not help
excess(S_0).] So, we are forced to add excess(S_0) edges from S_0 to S_1 = {
v: v = [X^{d-2}SL] }.  Color these newly added edges red.

Let us define in(S_1), out(S_1) and excess(S_1) as above for the modified
digraph, i.e., including the red excess(S_0) edges that we just added.
Clearly, in(S_1) = out(S_0) = n^{d-1}m(n-m), and out(S_1) = m*|S_1| =
m*n^{d-2}m(n-m), so excess(S_1) = n^{d-2}m(n-m)^2.  Consider S_0 union S_1.
We must add excess(S_1) edges to S_0 union S_1 to make it possible for the
digraph to be covered by a circuit, and these edges must go from {S_0 union
S_1} to S_2 = { v: v = [X^{d-3}SL^2] } by a similar argument as before.
Repeating this partitioning process, eventually we get to S_{d-1} = { v: v =
[SL^{d-1}] }, where union of S_0 to S_{d-1} will need edges to S_d = { v: v
= [L^d] }, where this process terminates.  Note that at this time,
excess(union of S_0 to S_{d-1}) = m(n-m)^d, but in(S_d) = 0 and out(S_d) =
m(n-m)^d, and the process terminates.

What have we shown?  Adding up blue edges and the red edges gives us a lower
bound on the total number of edges in a blue-edges covering circuit (not
necessarily Eulerian) in the complete digraph.  This comes out to be
n^{d+1}-(n-m)^{d+1} edges.

Next, we note that if we had an optimal path covering all the blue edges, we
can transform it into a circuit by adding d edges.  So, a minimal path can
be no more than d edges shorter than the minimal circuit covering all blue
edges.  [Otherwise, we add d extra edges to make it into a shorter circuit.]

So the shortest blue covering path through the digraph is at least
n^{d+1}-{n-m}^{d+1}-d.  With an initial pre-condition sequence of length d
(to establish the transition invariant), the shortest universal answering
machine sequence is of length at least n^{d+1}-(n-m)^{d+1}.

While this has not been that constructive, it is easy to see that we can
achieve this bound.  If we looked at the vertices in each of the S_i's, we
just add exactly the edges to S_{i+1} and no more.  The resultant digraph
would be Eulerian, and to find the minimal path we need only start at the
vertex labelled [{n-1}^d], find the Euler circuit, and omit the last d edges
from the tour.

==> combinatorics/gossip.p <==
n people each know a different piece of gossip.  They can telephone each other
and exchange all the information they know (so that after the call they both
know anything that either of them knew before the call).  What is the smallest
number of calls needed so that everyone knows everything?

==> combinatorics/gossip.s <==
1 for n=2
3 for n=3
2n-4 for n>=4

This can be achieved as follows: choose four people (A, B, C, and D) as
the "core group".  Each person outside the core group phones a member of
the core group (it doesn't matter which); this takes n-4 calls.  Now the
core group makes 4 calls: A-B, C-D, A-C, and B-D.  At this point, each
member of the core group knows everything.  Now, each person outside the
core group calls anybody who knows everything; this again requires n-4
calls, for a total of 2n-4.

The solution to the "gossip problem" has been published several times:

	1.  R. Tidjeman, "On a telephone problem", Nieuw Arch. Wisk. 3
		(1971), 188-192.

	2.  B. Baker and R. Shostak, "Gossips and telephones", Discrete
		Math. 2 (1972), 191-193.

	3.  A. Hajnal, E. C. Milner, and E. Szemeredi, "A cure for the
		telephone disease", Canad Math. Bull 15 (1976), 447-450.

	4. Kleitman and Shearer, Disc. Math. 30 (1980), 151-156.

	5.  R. T. Bumby, "A problem with telephones", Siam J. Disc. Meth. 2
		(1981), 13-18.

==> combinatorics/grid.dissection.p <==
How many (possibly overlapping) squares are in an mxn grid?  Assume that all
the squares have their edges parallel to the edges of the grid.

==> combinatorics/grid.dissection.s <==
Given an n*m grid with n > m.

Orient the grid so n is its width.  Divide the grid into two portions, 
an m*m square on the left and an (n-m)*m rectangle on the right.
Count the squares that have their upper right-hand corners in the
m*m square.  There are m^2 of size 1*1, (m-1)^2 of size 2*2, ...
up to 1^2 of size m*m.  Now look at the n-m columns of lattice points
in the rectangle on the right, in which we find upper right-hand
corners of squares not yet counted.  For each column we count m new
1*1 squares, m-1 new 2*2 squares, ... up to 1 new m*m square.

Combining all these counts in summations:

	 m		   m
total = sum i^2 + (n - m) sum i
	i=1		  i=1

	(2m + 1)(m + 1)m   (n - m)(m + 1)m
      = ---------------- + ---------------
		6		  2

      = (3n - m + 1)(m + 1)m/6

-- David Karr

==> combinatorics/permutation.p <==
Compute the nth permutation of k numbers (or objects).

==> combinatorics/permutation.s <==
#include <stdio.h>

/*
adapted from 'Notation as a Tool of Thought', by K.E.Iverson

Radix Representation of Permutations
*/

/* direct from radix; of given order */
dfr(short direct[],short radix[],long order)
{
	if (order)
	{
		direct[0] = radix[0];
		dfr (direct+1, radix+1, order-1);
		while (--order)
			direct[order] += direct[order] >= direct[0];
	}
}
 
/* radix representation; of given order and given index */
rr(short radix[], long order, long index)
{
	int i;
	
	for (i=1; i<=order; i++)
	{
		radix[order-i] = index % i;
		index = index/i;
	}
}
 
show(short perm[],long order)
{
	while(order--)
		printf("%hd ",*perm++);
	printf("\n");
}
 
 
short parity(short radix[],long order)
{
	long p=0;
	
	while(order--)
		p+=*radix++;
	return p%2;
}
 
void usage(char *name)
{
    fprintf(stderr,"usage: %s order number_of_permutation\n",name);
    exit(-1);
}
 
main(int argc, char *argv[])
{
#define MAX_ORDER	512
	short radix[MAX_ORDER], direct[MAX_ORDER];
	long order, nth;
	
	if (argc!=3) usage(argv[0]);
	order = atol(argv[1]);
	nth = atol(argv[2]);
	rr(radix, order, nth-1);	/* where 0 is the first permuatation */
	dfr(direct, radix, order);
	
	printf("radix  "); show(radix,order);
	printf("direct "); show(direct,order);
	printf("parity %d\n",parity(radix,order));
}
 
 
-- 
J. Henri Schueler, H&h Software, Toronto	+1 416 698 9075 
jhs@ipsa.reuter.com

==> combinatorics/subsets.p <==
Out of the set of integers 1,...,100 you are given ten different
integers.  From this set, A, of ten integers you can always find two
disjoint non-empty subsets, S & T, such that the sum of elements in S
equals the sum of elements in T.  Note: S union T need not be all ten
elements of A.  Prove this.

==> combinatorics/subsets.s <==
There are 2^10 = 1,024 subsets of the 10 integers, but there can be only 901
possible sums, the number of integers between the minimum and maximum sums.
With more subsets than possible sums, there must exist at least one sum that
corresponds to at least two subsets.  Call two subsets with equal sums A and B.
Let C = A intersect B; define S = A - C, T = B - C.  Then S is disjoint from T,
and sum(S) = sum(A-C) = sum(A) - sum(C) = sum(B) - sum(C) = sum(B-C) = sum(T).
QED

Addendum: 9 integers suffice.  This was part of my Westinghouse project
in 1981 (the above problem was in Martin Gardner's Scientific American
column not long before).  The argument is along the same lines, but
a bit more complicated; for starters you only work with the subsets
consisting of 3, 4, 5, or 6 of the 9 elements.

Let M(n) be the smallest integer such that there exists an n-element
set {a1,a2,a3,...,an=M(n)} of positive integers all 2^n of whose
subsums are distinct.  The pigeonhole argument of subsets.s shows that
M(n)>2^n/n, and it is known that M(n)>c*2^n/sqrt(n) for some c>0.
It is still an unsolved problem (with an Erdos bounty) whether
there is a positive constant c such that M(n)>c*2^n for all n.

--Noam D. Elkies (elkies@zariski.harvard.edu)
  Dept. of Mathematics, Harvard University

==> combinatorics/transitions.p <==
How many n-bit binary strings (0/1) have exactly k transitions
(an adjacent pair of dissimilar bits, i.e., a 01 or a 10)?

==> combinatorics/transitions.s <==
A transition can occur at an adjacent pair (i,i+1) where 1<=i<i+1<=n.
Since there are k transitions, there are C(n-1,k) total number of ways
that transitions can occur.  But the string may start with a 1 or a 0
(after which its transitions uniquely determine the string).  So there
are a total of 2C(n-1,k) such strings.

User Contributions:

Comment about this article, ask questions, or add new information about this topic:


[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
archive-comment@questrel.questrel.com





Last Update March 27 2014 @ 02:12 PM