13

Here is the problem:

We have a square with some numbers from 1..N in some cells. It's needed to determine if it can be completed to a magic square.

Examples:

2 _ 6       2 7 6
_ 5 1  >>>  9 5 1
4 3 _       4 3 8

7 _ _ 
9 _ _  >>>  NO SOLUTION 
8 _ _

Is this problem NP-complete? If yes, how can I prove it?

Crosspost on MS

levanovd
  • 231
  • 1
  • 4
  • 1
  • 1
    @levanovd: This site is for research level problems in theoretical computer science, and your question may not be appropriate here. See the FAQ for more information. Maybe it is better to wait for answers from Math SE, since you have just post a question there within an hour. – Hsien-Chih Chang 張顯之 Dec 21 '10 at 09:03
  • Yes, I like your observation. This is another community so I thought that somebody here may help. Is it bad? – levanovd Dec 21 '10 at 09:03
  • 2
    No, asking for help is not a bad thing. But your question has to be in the scope of the site you asked. I think Math SE is proper for this question, and TCS SE is not. – Hsien-Chih Chang 張顯之 Dec 21 '10 at 09:06
  • If I tagged this question 'homework' at Math SE it doesn't mean that it is school-level question. It means that I faced it during my study. – levanovd Dec 21 '10 at 09:06
  • 1
    @Hsien-Chih Chang: ok, if anyone will vote for closing this question, I'll delete it asap. – levanovd Dec 21 '10 at 09:08
  • 1
    @lecanovd: Maybe there is no need to delete the question. But anyway, thank you for your help, and I do hope you will solve the problem soon!! – Hsien-Chih Chang 張顯之 Dec 21 '10 at 09:10
  • 5
    We do accept questions about proving NP-hardness especially when the problem is hard. For example, consider the three examples listed as answers here: http://meta.cstheory.stackexchange.com/questions/784/original-proofs-generated-on-the-parent-site – Suresh Venkat Dec 21 '10 at 10:12
  • 1
    Yes, and after thinking for a little while, I find this problem non-trivial. I suggest to keep the question open, unless an easy solution to this problem is known. – Hsien-Chih Chang 張顯之 Dec 21 '10 at 10:47
  • Here is a related discussion on meta about the level of NP-hardness proofs. – Hsien-Chih Chang 張顯之 Dec 21 '10 at 11:29
  • 1
    (1) I like the problem, but I do not like secret crossposting or simultaneous crossposting. So when you crosspost next time, please include links, preferably in both directions. (2) You tagged your question on Math SE as “homework.” Is it really homework? If so, I do not think that it is appropriate for TCS SE. (But I hope that it is not homework, because I cannot see how to solve it.) – Tsuyoshi Ito Dec 21 '10 at 13:09
  • @Tsuyoshi Ito: Added link to related MS question. In fact this is homework but it is a kind of my professor's joke – levanovd Dec 21 '10 at 15:34
  • 2
    If it is homework, then isn't it unethical for someone to provide a solution here? – Robin Kothari Dec 21 '10 at 15:57
  • 6
    If it's homework, we don't allow it, whether or not it's unethical. – Peter Shor Dec 21 '10 at 20:08
  • @Peter Shor: It's quite strange since homework questions are welcome on stackoverflow (http://stackoverflow.com/questions/tagged/homework) Also I don't think that there is any difference if the question is related to homework or not, I always help everybody if only I know the right answer regardless to question tags. – levanovd Dec 21 '10 at 20:25
  • Also this question is not homework now but question of interest. – levanovd Dec 21 '10 at 20:26
  • 13
    @levanovd: This is not stackoverflow. This community has an explicit policy forbidding homework questions. The fact that stackoverflow has a different policy doesn't matter here. – Jeffε Dec 21 '10 at 20:40
  • so I'm confused now. This is a nice question, but if it's homework it should die a bright flaming death.. – Suresh Venkat Dec 22 '10 at 09:58
  • @Suresh: @levanovd claimed that this is not a homework now. We should trust him, like we trust every users on this site. If later on it is founded that this is indeed a homework, we can close the question right away. But right now I would prefer to view it as a nice brain-twisting question. – Hsien-Chih Chang 張顯之 Dec 22 '10 at 13:04
  • Moreover, in Math SE the OP seems only asking for directions, and I think the answer by @pboothe works well, this definitely gives a new path of thinking. If the OP is really interested in the question, I think he/she should be able to solve the problem by the hint, or get stuck and revise the problem with new observations and questions. – Hsien-Chih Chang 張顯之 Dec 22 '10 at 13:28
  • In summary, my opinion is that we just leave the question as where it is, and @levanovd if you are still facing some difficulties after reading and trying the suggestions given by @pboothe, you can edit your question and provide your new thoughts about this interesting puzzle. Anyone? – Hsien-Chih Chang 張顯之 Dec 22 '10 at 13:35
  • 2
    @Hsien-Chih: The asker once said “In fact this is homework” in a comment, and he changed his claim without explanation after people pointed out that homework questions are not allowed here. I am totally confused. – Tsuyoshi Ito Dec 22 '10 at 18:08
  • 1
    @Tsuyoshi Ito: If you want to quote please quote the whole sentence: "In fact this is homework but it is a kind of my professor's joke". I've got this problem from my professor but I'm not sure if he even knows the solution. You can argue for a long time about if it is a homework, what is homework, etc. If you want to hear this, I will not be given any mark or get other bonuses for solving this problem. It is scientific interest only. – levanovd Dec 22 '10 at 23:22
  • So do you mean that you once incorrectly stated that this question was homework while it was not? Please understand that you contradicted yourself and that contradiction was confusing. – Tsuyoshi Ito Dec 22 '10 at 23:47
  • @Hsien-Chih Chang: I think there is difference between trusting and over-trusting, e.g. you can trust the students in your class but that does not mean that you should leave them alone during the final exam, we shouldn't mistrust new users but trust is built up by time. – Kaveh Dec 23 '10 at 07:16
  • We accept NP-hardness questions but if a question is elementary/homework-level, it should be closed (independent of being homework or not), with the exceptions mentioned in the related meta discussions, and the way to make the decision about closing is by asking the user for motivation and here the motivation seems to be a problem asked by an instructor. So I think if this question is of elementary level it should be close as it does not fall under the exception for acceptable elementary questions. – Kaveh Dec 23 '10 at 07:18
  • @Kaveh: I totally agree on your opinion, the hardest part is to decide whether a problem is at homework-level or not. (This has already been discussed on meta.) I am only a student who do not have the ability to decide whether this problem is at homework-level (though it seems to me like a non-trivial problem), and I will leave the judgement for this problem to the experts. – Hsien-Chih Chang 張顯之 Dec 23 '10 at 07:22
  • 3
    I do not know a solution and I do not think that this is at the level of homework. However, I might be missing something simple. Therefore if anyone knows a complete solution and thinks this question is homework-level, please just say so. In the meanwhile, I will assume that this question is not homework and that the [homework] tag used on Math SE and levanovd’s earlier comment were simply mistakes. – Tsuyoshi Ito Dec 23 '10 at 10:27
  • 1
    @Tsuyoshi: I've set open problems as homework before. In particular, asked for the number of mutually unbiased bases in 2 (easy problem) and 6 (open problem) complex dimensions as part of a linear algebra course. – Joe Fitzsimons Mar 22 '11 at 16:48
  • @Joe: I see. I guess that that means that homework problems are not necessarily homework-level. :) – Tsuyoshi Ito Mar 22 '11 at 17:33

2 Answers2

18

Filling a partially-filled Latin square is NP-Complete. "The complexity of completing partial Latin squares" Charles J. Colbourn. Discrete Applied Mathematics, Volume 8, Issue 1, April 1984, Pages 25-30 http://dx.doi.org/10.1016/0166-218X(84)90075-1

Can a Latin square problem be turned into a magic square problem via modular arithmetic? My intuition says yes, but the rest of my brain says "Get back to grading!"

Peter Boothe
  • 1,791
  • 13
  • 17
  • 2
    It would be nice to turn this into a rigorous argument. It is not at all clear to me how modular arithmetic would really help in reducing LATIN SQUARE COMPLETION to MAGIC SQUARE COMPLETION, or vice versa. It would be rather pretty if it could be made to work. – András Salamon Mar 19 '11 at 22:31
9

This question has two parts: first, is the problem in NP, and second, is it NP-hard?

For the first part, I have a positive answer with a non-obvious proof. (Thanks to Suresh for pointing out an earlier error.)


Consider the following way to formalize the question as a decision problem:

UNRESTRICTED MAGIC SQUARE COMPLETION
Input: positive integer $n$ given in unary, list of integers with their positions in an $n$ by $n$ grid
Question: do there exist integers for the remaining positions in the grid, so that the arrangement forms a magic square?

If we add the restriction that each of the integers $1,2,\dots,n^2$ must occur precisely once in the magic square, then the resulting MAGIC SQUARE COMPLETION decision problem is obviously in NP. The definition of a magic square in the 1911 Encyclopædia Britannica, following Euler, has this restriction; in contrast, the Wikipedia article currently uses the terminology "normal magic square" and reserves "magic square" for the unrestricted version.

With an $n$ by $n$ grid, at least $n$ numbers must be given, otherwise the answer is trivially "YES" for the unrestricted version. The size of the input can therefore be assumed to require more than $n$ bits in this case. For the normal version, it is possible that there are inputs that require few bits but which have no solution; to avoid such complications I have specified that $n$ is given in unary.

The argument uses a bound on the possible size of integers that appear in solutions. In the normal case this bound is obviously $n^2$, but in the general case it is not a priori obvious that such a bound exists. It turns out that an exponential bound exists.

Theorem (Tyszka, Theorem 12): Any system of Diophantine equations involving equations of the form $x_i = 1$ and $x_i = x_j + x_k$, for $i,j,k \in \{1,2,\dots,n\}$, either has no integer solution, or has a solution in which every $x_i$ is an integer and at most $\sqrt{5}^{n-1}$ in absolute value.

This also appeared as Theorem 4.7 in:

Cipu recently announced an asymptotically better bound of $2^n$. (Note that the smallest possible bound is $2^{n-1}$.) The argument builds on a bound on the determinant of a matrix, due to Waldi.

Theorem (Cipu, 2011): Any system of Diophantine equations involving equations of the form $x_i = 1$ and $x_i = x_j + x_k$, for $i,j,k \in \{1,2,\dots,n\}$, either has no integer solution, or has a solution in which every $x_i$ is an integer and at most $2^n$ in absolute value.

Even more recently, Freitas, Friedland and Porta showed that the $2^{n-1}$ bound holds, as a corollary of their more general Theorem 1.1.

This yields the following:

Corollary: If an instance of UNRESTRICTED MAGIC SQUARE COMPLETION of size $N$ has a solution, then it has a solution using only numbers up to $2^{O(N^2)}$.

This means that one can use $O(N^4)$ space to guess a solution up to the bound, and then check in $O(N^8)$ time whether it is a solution. The precise polynomial depends on whether one uses Tyszka's or Cipu's result for the bound, and how one expresses the Diophantine system of equations representing the magic square. In either case, the number of variables required in a Diophantine system of the form posed by Tyszka is no more than $n^2 + 2(n+1)(n-2)+1 = 3n^2-2n-3$. This is achieved with $n-2$ variables for partial sums for each row, column, and diagonal and a grand total variable linking these together. Beyond the magic square itself, a further polynomial number of variables is required for the numbers in the square: a number requiring $m$ bits can be represented by using $O(m^2)$ intermediate variables.

For the second part of the question, as far as I can tell either version of MAGIC SQUARE COMPLETION should be NP-hard, but I do not have reductions. It is worth noting that there are procedures to construct normal magic squares of arbitrarily large size; moreover, the number of normal magic squares seems to grow superpolynomially with $n$ (see OEIS A006052) so the underlying language does not seem to be sparse.


Using Papadimitriou's bound on the solutions of an instance of INTEGER LINEAR PROGRAMMING, one can also show that the version where the numbers must all be non-negative is also in NP.

Theorem (Papadimitriou): Let $A$ be an $r \times s$ matrix and $b$ an $r$-vector, both with entries from $\{-a, -a+1, \dots, a-1, a\}$. Then if $Ax = b$ has a solution in non-negative integers, it also has one where every component is in $\{0,1,\dots,s(ra)^{2r+1}\}$.

The constraints forming the magic square can be expressed as a linear program using $a=1$, with $s=n^2+1$ variables and $r=2n+2$ inequalities. This yields a somewhat larger bound than the bound above where negative numbers are allowed, but the certificate is still of polynomial size. (Thanks to Tony Tan for pointing out the result of Papadimitriou.)

  • Christos H. Papadimitriou, On the complexity of integer programming, JACM 28 765–768, 1981. (link)
András Salamon
  • 19,000
  • 3
  • 64
  • 150
  • I guess I'm confused. if there's a poly bound on the size of the answers, then we are guaranteed to have a guess that can be read and verified in polynomial time. – Suresh Venkat Mar 19 '11 at 18:06
  • @Suresh: Apologies for the errors, this answer turned out a bit harder to write down than I expected. – András Salamon Mar 19 '11 at 18:12