12
$\begingroup$

We have $n$ charged and $n$ uncharged batteries and a radio which needs two charged batteries to work. Suppose we don't know which batteries are charged and which ones are uncharged. Find the least number of attempts sufficient to make sure the radio will work.

An attempt consists in putting two batteries in the radio and check if the radio works or not.


I can prove that it is $\leq n+3$ (or $\leq n+4$, look at the comment):

Say we have batteries $B_1,B_2,....B_{2n}$, $n$ of them charged and other not. If in one of $n$ attempts $$\{B_1,B_2\}, \{B_3,B_4\}, ...\{B_{2n-1},B_{2n}\}$$ radio work we are done. If in non of them work, then in each pair we must have charged and uncharged batteries. Thus in one of next 4 pairs:

$$\{B_1,B_3\}, \{B_1,B_4\}, \{B_2,B_3\}, \{B_2,B_4\}$$

must be a pair of charged batteries and we are done (so if 3 times doesn't work we know the last pair is charged).

But, can we reduce the number of attempts?

$\endgroup$
  • 1
    $\begingroup$ I wonder why it is $n+3$ and not $n+4$. The radio will not work until the charged batteries are inserted. :) $\endgroup$ – user Feb 16 at 13:22
1
$\begingroup$

Here is an alternative, more succinct derivation of the best method adapted from my answer to a question on the $n=4$ case which was marked as a duplicate of this one.

As in Quantus's answer, we consider a graph with $2n$ vertices (the batteries) and an edge between two vertices corresponding to charged batteries. The edges form a $K_n$ clique, so the question is equivalent to

What is the least number of edges in a graph $G$ on $2n$ vertices such that its complement $\overline G$ contains no $K_n$ clique?

The edges of $G$ are the pairs of batteries we test. If we can guarantee that a $G$ with $k$ edges has the stated property, then we can get the radio to work in at most $k$ attempts, or we can know a pair that will work in at most $k-1$.

We answer the above question by converting it to a dual form.

What is the most number of edges $\overline G$ can have such that it contains no $K_n$ clique?

By Turán's theorem, $\overline G$ attains its maximum number of edges when it is the Turán graph $T(2n,n-1)$, the complete $n-1$-partite graph where the $2n$ vertices are split as equally as possible. For $n\ge3$ this works out to $n-3$ partitions with $2$ vertices and $2$ partitions with $3$ vertices. Thus the number of edges in the optimal $G$ is the count of edges wholly within one partition of $\overline G$, or $$(n-3)T_2+2T_3=n-3+2\cdot3=n+3$$ If $n=2$, there is only one working pair of batteries and we might need to try all $6$ pairs. If $n=1$ it is clear that no pair of batteries will make the radio work.

$\endgroup$
6
$\begingroup$

If $n=1$ then this can be done in zero attempts because we know the radio won't work.

If $n=2$ I don't see a way of going below 5 (or 6) attempts.

If $n\ge 3$ then the number of attempts can be reduced to $n+2\space ($or $n+3)$

First two sets of three are tested $$\{B_1,B_2\}, \{B_2,B_3\}, \{B_3,B_1\}$$ $$ \{B_4,B_5\},\{B_5,B_6\}, \{B_6,B_4\}$$

Then they are tested in pairs

$$\{B_7,B_8\}, \{B_9,B_{10}\},\{B_{11},B_{12}\}... \{B_{2n-1},B_{2n}\}$$

The worst case scenario is if each set of three has one charged battery and each pair has one charged battery, except for the last pair $\{B_{2n-1},B_{2n}\}$ which must have two charged batteries. The two sets of three requires $6$ attempts and remaining pairs requires $n-4$ (or $n-3$) attempts . Thus reducing the number of total attempts of the op's strategy by one.

Note: the places where it says "$x$ (or $x+1$)" are the two interpretations of the problem (see comments of the original post)

Proof: Imagine a graph of $2n$ vertices (which represents the batteries) and an edge for each tested pair of points. So one question that one can ask is: given a graph of this type with two colors vertices, (red and blue) what is the maximum number red vertices that can be used such that no two red vertices are connected? The answer to this is same as the minimum number of non-overlapping complete subgraphs that uses all of the vertices.

I'll use an example to explain this let $G$ be a graph where that uses six vertices and six edges $V_1,V_2,V_3,V_4,V_5,V_6$

$(V_1-V_2),(V_2-V_3),(V_3-V_4),(V_2-V_4),(V_3-V_5),(V_4-V_6)$

There are several sets of non-overlapping subgraphs that use all of the vertices. For example we could use six $K_1's$ each using one vertex. We could also use one $K _3 \{V_2,V_3,V_4\}$ and three $K_1's \{V_1\},\{V_5\},\{V_6\}$. However the smallest number of non-overlapping subgraphs is three, by using three $K_2's \{V_1,V_2\},\{V_3,V_5\},\{V_4,V_6\}$. So the maximum number of red vertices that this graph can hold without any two being connected is three. In other words six batteries three of which are charged could be tested by the method that corresponds to the graph and the radio may not be turned on.

In short the charged batteries are distributed among complete graphs of testing pairs. If two of these batteries are in the same complete graph then one of the tests turned on the radio. By the pigeon hole principle we want to reduce the number of minimum number of non-overlapping complete subgraphs to $n-1$. This will guarantee that two charged batteries are in the same subgraph. When creating $K_2's$ they each combine two $K_1$ and only use one edge. This is the "cheapest edge cost" of the complete graphs. The $K_2's$ doesn't bring the minimum subgraphs down to $n-1$. It only brings it down to $n$. The reduction of one more subgraph requires the "absorption" of two additional vertices. This can be done in two ways. The first by using a $K_4$ which absorbs two additional vertices or using two $K_3's$ which each absorbs one vertex. $K_4$ requires four additional edges while the two $K_3's$ together only uses three additional edges.

$\endgroup$
  • $\begingroup$ This is great, but is it the best? $\endgroup$ – Aqua Feb 16 at 14:49
  • $\begingroup$ @greedoid I just edited my answer which includes a proof of optimization. About the same time as someone else posted a new answer! $\endgroup$ – quantus14 Feb 16 at 21:32
5
$\begingroup$

I assume that you have to actually place two working batteries in, not just find them. Any algorithm to solve this problem is of the following form:

  • Test some pair $E_1=\{v_1,w_1\}$ of batteries.

  • If that does not work, test a different pair $E_2=\{v_2,w_2\}$.

  • $\vdots$

  • Finally, test $E_k=\{v_k,w_k\}$.

Furthermore, the order of the pairs $E_i$ does not matter; if the above order works, then so does any permutation $E_{\pi(i)}$ of the pairs. All that matters is the set of edges tested, so an algorithm corresponds to a graph on $2n$ vertices. This algorithm is successful iff, for every coloring of the vertices so $n$ are white and $n$ are black, there exists an edge whose endpoints are both white.

We prove by induction that for all $n\ge 3$, any successful algorithm has at least $n+3$ edges. To see this, let $G$ be a graph on $2n$ vertices with at most $n+2$ edges. The average degree of the vertices is at most $$ (2n+4)/(2n)=1+2/n<2. $$ Therefore, there exists some vertex $v$ with degree at most $1$. If $\deg v= 1$, let $w$ be its neighbor. If $\deg v=0$, let $w$ be an arbitrary other vertex for which $\deg w\ge 1$.

Let $G'$ be the graph formed by removing $v$ and $w$, and all edges incident to either of them. At least one edge is removed, so $G'$ has at most $(n-1)+2$ edges. We will now show that $G'$ has a coloring with $(n-1)$ white and black vertices and no white edges. There are two cases:

  • For the base case $n=3$, $G'$ has $2\cdot 2$ vertices and at most $5-1=4$ edges. Choose any two vertices $x,y$ in $G'$ which are not adjacent and color them white, then color the other two vertices black.

  • For the inductive step $n\ge 4$, $G'$ has $2(n-1)$ vertices and at most $(n-1)+2$ edges. By the inductive hypothesis, $G'$ is unsuccessful, so it has a coloring with no white edges.

This coloring for $G'$ can be extended to a coloring for $G$ by coloring $w$ black and $v$ white. All edges added back in have at least one black vertex, $w$, so $G$ has no white edges. This proves $G$ is unsuccessful, taking care of the base case and inductive step.

$\endgroup$
4
$\begingroup$

Props to @quantus14 for the best algorithm. Let me show why it's the best.

We'll just consider deterministic algorithms, where we think of the $2n$ batteries numbered $1$ through $2n$ as input and your algorithm just tests pairs $P_1,\dots,P_k$ in sequence, with $P_i$ some predetermined 2-element set of $[2n]$ for each $i$ until it tests a pair $P_j$ with two working batteries. We could rephrase the proof to work for other algorithms, by just taking a single example runtime and analyzing that, but it's more annoying.

To be clear: we will count the final step, where you plug the working batteries in.

We can do the case $n=2$ easily. There are 6 pairs and only one will make the radio turn on. Any algorithm that doesn't test one of the 6 pairs will fail for input with those two batteries the only working ones. So you can't have an algorithm with less than $6>4=n+2$ steps. Now assume $n>2$ and we have shown there is no algorithm taking (n-1)+2 steps or fewer for $2(n-1)$ batteries, half working and half dead.

Suppose there is an algorithm that works in $n+2$ steps (if you could do it in fewer, just add some useless steps to make it so). Let's call the batteries $b_1,\dots,b_{2n}$. Let's make a graph $G$ with $b_1,\dots,b_{2n}$ as vertices and an edge between $b_i$ and $b_j$ if they are tested together in a pair (in the case of its longest runtime) in your algorithm. There are $n+2$ edges in $G$, so the average degree of a vertex is $2(n+2)/(2n) = 1+2/n$.

Suppose there are two vertices of degree 0. Removing those two vertices and any edge in $G$ (between $b_i$ and $b_j$, say) gives a fast algorithm for $2(n-1)$ batteries, since if there is an input which fails only because $b_i$ and $b_j$ are no longer connected, then we could make a failing input for the larger algorithm which replaces the value of $b_i$ or $b_j$ with dead and make both the lone vertices working.

Suppose there is exactly one vertex with degree 0. The average degree of the other vertices is $2(n+2)/(2n-1) = 1 + 5/(2n-1) \le 2$, so either every vertex has degree 2 or there is a vertex of degree 1. If every vertex has degree 2, we must have a $2n-1$ cycle, which has too many edges if $n>3$, and which doesn't work as an algorithm when $n=3$ (pentagon plus lone vertex). So there's a vertex of degree 1. Removing the vertex of degree 0 and the vertex of degree 1 (along with its one edge) gives a fast algorithm for $2(n-1)$ batteries, half working and half dead: take possible failing input from smaller graph, set the vertex of degree 1 to dead and the lone vertex to working, and you get failing input for the larger algorithm.

Thus there are no vertices of degree 0. Suppose there is a vertex $b_i$ of degree 1, connected to $b_j$. Remove $b_i$, $b_j$, and all the edges connected to $b_j$. This is a fast algorithm for $2(n-1)$ vertices: for any input to this new algorithm, we could assign 'dead' to $b_j$ and 'working' to $b_i$ and the old algorithm would solve it but not make use of $b_i$ or $b_j$.

So now all vertices have degree at least 2. The average degree is $2(n+2)/(2n) = 1 + 2/n$, so we must have $n = 2$, and we're done.

$\endgroup$
  • 1
    $\begingroup$ I just edited my answer I think you beat me by 8 seconds!! $\endgroup$ – quantus14 Feb 16 at 21:34
  • $\begingroup$ Any way I will have to study deterministic algorithm,I'm not familiar with that subject. $\endgroup$ – Aqua Feb 16 at 21:39
  • 1
    $\begingroup$ This is really clever! However, you lost me at "all vertices of degree $1$ come in pairs". For example, 8 vertices and 6 edges could be a union of two paths each with 4 vertices and 3 edges. I think a tad more care is needed in this case. $\endgroup$ – Mike Earnest Feb 17 at 18:30
  • 1
    $\begingroup$ Whoops! You are very right. Lemme try to fix it. $\endgroup$ – csprun Feb 17 at 18:53
  • 2
    $\begingroup$ I think I fixed it. Let me know if you agree now. Thanks for the comment! $\endgroup$ – csprun Feb 17 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.