Anda di halaman 1dari 40

The NP-Completeness of NONOGRAM

The NP-Completeness of NONOGRAM

Mario G. Figueiró Zemor


Instituto de Informática
Universidade Federal do Rio Grande do Sul
mario.figueiro@ufrgs.br

June 14, 2018


The NP-Completeness of NONOGRAM

Sumário

1 Introdução
2 Caracterização
Notação
Definição formal
3 Nonogram ∈ NP
Algoritmo de verificação
4 Nonogram ∈ NP-difı́cil
3-Dimensional Matching
3DM ≤p Nonogram
Algoritmo de redução
Análise da Complexidade
5 Conclusão
The NP-Completeness of NONOGRAM
Introdução

Visão Geral

Um Nonogram, é um puzzle lógico que pode ser considerado como


um problema de reconstrução de imagem. É dado ao jogador uma
matriz MxN, onde cada linha e cada coluna possui uma descrição
consistindo de um ou mais inteiros, representando o número de
células consecutivas que precisam ser pintadas. Se o jogador con-
seguir colorir um subconjunto de células de modo que todas as re-
strições, ele resolveu o puzzle e ganhou o jogo (VAN RIJN, 2012,
p.29).
The NP-Completeness of NONOGRAM
Introdução

Exemplo

Figure: 4x6 Nonogram - Solução


The NP-Completeness of NONOGRAM
Caracterização
Notação

Notação

Dada uma Instância de um NONOGRAM, considere :


h = k linhas k
w = k colunas k
ri = sequência de números inteiros não negativos,
representando a descrição da i-ésima linha.
cj = sequência de números inteiros não negativos,
representando a descrição da j-ésima coluna.
P = matriz[h][w] que pode ser preenchida com os valores
{0,1}, onde 1 representa célula preenchida e 0 não preenchida.
The NP-Completeness of NONOGRAM
Caracterização
Definição formal

Definição formal

INPUT : h, w ∈ N e sequências de números inteiros


não-negativos, R = h r1, . . . , rh i e C = h c1, . . . , cw i

PROBLEMA DE DECISÃO : Existe uma atribuição de 0’s e 1’s


para a matriz P, tal que, as descrições R e C sejam satisfeitas?
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

Pseudocódigo

1 d e f v e r i f y N o n o g r a m ( h , w , R , C , P) :
2 i f ( i s S a m e S i z e ( h , w , P) ) t h e n :
3 f o r e a c h row i n R :
4 f o r e a c h i , d e s c r i p t i o n I i n row :
5 c h e c k e d = checkRow ( d e s c r i p t i o n I , P , i ) ;
6 i f ( ! checked ) then :
7 return false ;
8 foreach col in C:
9 foreach j , descriptionJ in col :
10 checked = checkCol ( d e s c r i p t i o n J , P, j ) ;
11 i f ( ! checked ) then :
12 return false ;
13 return true ;
14 else :
15 return false
Listing 1: Algoritmo de verificação
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

checkRow O(h)

1 d e f checkRow ( d e s c r i p t i o n , l i n , h , P) :
2 c o u n t = 0 ; c o l = 0 ; colMax = h −1;
3 w h i l e P [ l i n ] [ c o l ] !=1 and c o l <= colMax :
4 c o l ++;
5 w h i l e P [ l i n ] [ c o l ] !=0 and c o l <= colMax :
6 c o u n t ++; P [ l i n ] [ c o l ] = 2 ; c o l ++;
7 i f d e s c r i p t i o n == c o u n t and c o l <= colMax :
8 r e t u r n True ;
9 else :
10 return False ;
Listing 2: Verificação da descrição em uma determinada linha
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

checkCol O(w)

1 d e f c h e c k C o l ( d e s c r i p t i o n , c o l , w , P) :
2 c o u n t = 0 ; l i n = 0 ; l i n M a x = w−1;
3 w h i l e P [ l i n ] [ c o l ] !=2 and l i n <= l i n M a x :
4 l i n ++;
5 w h i l e P [ l i n ] [ c o l ] !=0 and l i n <= l i n M a x :
6 c o u n t ++; P [ l i n ] [ c o l ] = 1 ; l i n ++;
7 i f d e s c r i p t i o n == c o u n t and c o l <= l i n M a x :
8 r e t u r n True ;
9 else :
10 return False ;
Listing 3: Verificação da descrição em uma determinada coluna
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

Complex[4-7]

Qual o máximo de descrições em uma determinda linha?

(w+1)/2

4 f o r e a c h i , d e s c r i p t i o n I i n row :
5 c h e c k e d = checkRow ( d e s c r i p t i o n I , P , i ) ;
6 i f ( ! checked ) then :
7 return false ;

(w +1)/2
X
w +1
i=1
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

Complex[3-7]

Qual o no de descrições em R?

3 f o r e a c h row i n R :
4 f o r e a c h i , d e s c r i p t i o n I i n row :
5 c h e c k e d = checkRow ( d e s c r i p t i o n I , P , i ) ;
6 i f ( ! checked ) then :
7 return false ;

w (wX
X +1)/2
w +1
i=1 j=1
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

Complex[9-12]

Qual o máximo de descrições em uma determinda coluna?

(h+1)/2

9 foreach j , descriptionJ in col :


10 checked = checkCol ( d e s c r i p t i o n J , P, j ) ;
11 i f ( ! checked ) then :
12 return false ;

(h+1)/2
X
h+1
i=1
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

Complex[8-12]

Qual o no de descrições em C?

8 foreach col in C:
9 foreach j , descriptionJ in col :
10 checked = checkCol ( d e s c r i p t i o n J , P, j ) ;
11 i f ( ! checked ) then :
12 return false ;

h (h+1)/2
X X
h+1
k=1 l=1
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

Pior caso
2 i f ( i s S a m e S i z e ( h , w , P) ) t h e n :
3 f o r e a c h row i n R :
4 f o r e a c h i , d e s c r i p t i o n I i n row :
5 c h e c k e d = checkRow ( d e s c r i p t i o n I , P , i ) ;
6 i f ( ! checked ) then :
7 return false ;
8 foreach col in C:
9 foreach j , descriptionJ in col :
10 checked = checkCol ( d e s c r i p t i o n J , P, j ) ;
11 i f ( ! checked ) then :
12 return false ;
13 return true ;

w (wX
X +1)/2 h (h+1)/2
X X
(w + 1) + (h + 1) + 1
i=1 j=1 k=1 l=1
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

1o termo

w (wX
X +1)/2 w (wX
X +1)/2
(w + 1) = (w + 1) ∗ 1=
i=1 j=1 i=1 j=1
w
X
(w + 1)(w + 1)/2 1 = w (w + 1)(w + 1)/2 =
i=1
(1/2)w (w + 1)2 = ... =

w3 w
2 + w2 + 2
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

Substituindo o 1o termo...
2 i f ( i s S a m e S i z e ( h , w , P) ) t h e n :
3 f o r e a c h row i n R :
4 f o r e a c h i , d e s c r i p t i o n I i n row :
5 c h e c k e d = checkRow ( d e s c r i p t i o n I , P , i ) ;
6 i f ( ! checked ) then :
7 return false ;
8 foreach col in C:
9 foreach j , descriptionJ in col :
10 checked = checkCol ( d e s c r i p t i o n J , P, j ) ;
11 i f ( ! checked ) then :
12 return false ;
13 return true ;

h (h+1)/2
w3
X X
w
2 + w2 + 2 + (h + 1) + 1
k=1 l=1
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

2o termo

h (h+1)/2
X X h (h+1)/2
X X
(h + 1) = (h + 1) ∗ 1=
k=1 l=1 k=1 l=1
h
X
(h + 1)(h + 1)/2 1 = h(h + 1)(h + 1)/2 =
k=1
(1/2)h(h + 1)2 = ... =

h3 h
2 + h2 + 2
The NP-Completeness of NONOGRAM
Nonogram ∈ NP
Algoritmo de verificação

Substituindo o 2o termo...

h (h+1)/2
w3
X X
w
2 + w2 + 2 + (h + 1) + 1 =
k=1 l=1

w3 w h3 h
2 + w2 + 2 + 2 + h2 + 2 +1=

O(w 3 +h3 ) ou O(p 3 ) onde, p = max{w, h}


The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
3-Dimensional Matching

Definição

3-Dimensional Matching

Sejam X, Y, e Z, conjuntos disjuntos finitos, e seja M um subcon-


junto de X × Y × Z. Isto é, M consiste de triplas (x, y, z) de forma
que x ∈ X, y ∈ Y, and z ∈ Z. Dessa forma M’ ⊆ M é uma acopla-
mento tridimensional se obedece às seguintes regras: para quaisquer
duas triplas distintas (xi , yi , zi ) ∈ M e (xj , yj , zj ) ∈ M, temos que
xi 6= xj , yi 6= yj , e zi 6= zj .
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
3-Dimensional Matching

3DM - Exemplo 1

Figure: (a) Entrada M. (b)–(c) Soluções M 0 .


The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
3-Dimensional Matching

3DM - Definição formal*

INPUT: Seja M ⊆ X × Y × Z, onde X, Y e Z são conjuntos


disjuntos contendo o mesmo número q de elementos.

PROBLEMA DE DECISÃO: Existe um M 0 ⊆ M tal que M 0 é


um acoplamento tridimensional e kM 0 k=q?

PROBLEMA DE OTIMIZAÇÃO: Encontre um M 0


⊆ M tal que M 0 é um acoplamento tridimensional e kM 0 k é máximo.

* Definição de 3DM para o caso especial onde q = k X k=k Y k=k Z k


The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
3-Dimensional Matching

3DM - Exemplo 2

X = {x1 , x2 , x3 }; Y = {y1 , y2 , y3 }; Z = {z1 , z2 , z3 }

M = {hx1 , y1 , z1 i, hx1 , y2 , z3 i, hx2 , y2 , z3 i, hx2 , y3 , z2 i, hx3 , y2 , z3 i, hx3 , y3 , z2 i}

M 0 = {hx1 , y1 , z1 i, hx2 , y2 , z3 i, hx3 , y3 , z2 i}

n =k M k= 6

q =k M 0 k=k X k=k Y k=k Z k= 3


The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
3DM ≤p Nonogram

3DM ≤p Nonogram

Mostrar que ∃ uma 3DM ≤p Nonogram, ou seja, dada qualquer


instancia M do problema 3DM pode ser gerada uma uma instância
I de um NONOGRAM correspondente à M em tempo polinomial.

Primeirante, iremos mostrar a ideia da redução utilizando um


exemplo. Considere o exemplo 2, mostrado anteriormente:

Me = {hx1 , y1 , z1 i, hx1 , y2 , z3 i, hx2 , y2 , z3 i, hx2 , y3 , z2 i, hx3 , y2 , z3 i, hx3 , y3 , z2 i}


Me0 = {hx1 , y1 , z1 i, hx2 , y2 , z3 i, hx3 , y3 , z2 i}
qe = 3
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
3DM ≤p Nonogram

3DM ≤p Nonogram - Instância I

x1 x2 x3 y1 y2 y3 z1 z2 z3
<x1 , y1 , z1 > 1,5,5,5
0
<x1 , y2 , z3 > 1,7,7,1
0
<x2 , y2 , z3 > 3,5,7,1
0
<x2 , y3 , z2 > 3,7,3,3
0
<x3 , y2 , z3> 5,3,7,1
0
<x3 , y2 , z2 > 5,5,3,3
0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
3DM ≤p Nonogram

3DM ≤p Nonogram - Solução I

x1 x2 x3 y1 y2 y3 z1 z2 z3
*<x1 , y1 , z1 > • • • • 1,5,5,5
• • • • • • • • • • • • • • • • • • • • 0
<x1 , y2 , z3 > • • • • 1,7,7,1
• • • • • • • • • • • • • • • • • • • • 0
*<x2 , y2 , z3 > • • • • 3,5,7,1
• • • • • • • • • • • • • • • • • • • • 0
<x2 , y3 , z2 > • • • • 3,7,3,3
• • • • • • • • • • • • • • • • • • • • 0
<x3 , y2 , z3 > • • • • 5,3,7,1
• • • • • • • • • • • • • • • • • • • • 0
*<x3 , y3 , z2 > • • • • 5,5,3,3
• • • • • • • • • • • • • • • • • • • • 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
3DM ≤p Nonogram

Entendendo a solução

Figure: (a) tripla ∈ M’, e (b) tripla ∈ M - M’


The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Algoritmo de redução

Estrutura de dados

A partir do Exemplo 2, temos que:


X = {x1 : 1, x2 : 2, x3 : 3}
Y = {y1 : 1, y2 : 2, y3 : 3}
Z = {z1 : 1, z2 : 2, z3 : 3}
M = [[x1 , y1 , z1 ], [x1 , y2 , z3 ], [x2 , y2 , z3 ], [x2 , y3 , z2 ], [x3 , y2 , z3 ], [x3 , y3 , z2 ]]
q=3
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Algoritmo de redução

Reduction

1 d e f r e d u c t i o n (M, X , Y , Z , q ) :
2 n = l e n (M) ;
3 h = 2∗ n ;
4 w = 6∗ q +2;
5 R = c o n s R o w D e f i n i t i o n (M, X , Y , Z , q ) ;
6 C = c o n s C o l D e f i n i t i o n (M, X , Y , Z , n ) ;
7 r e t u r n [ h ,w, R, C]
Listing 4: Algoritmo de Redução
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Algoritmo de redução

Criando descrição das Linhas

1 d e f c o n s R o w D e f i n i t i o n (M, X , Y , Z , q ) :
2 R = []
3 f o r e a c h t r i p l e i n M:
4 x i = X[ t r i p l e [ 0 ] ]
5 y j = Y[ t r i p l e [ 1 ] ]
6 zk = Z [ t r i p l e [ 2 ] ]
7 ri = []
8 r1 = f1 ( x i ) ;
9 r2 = f2 (q , yj , x i )
10 r 3 = f 3 ( q , zk , y j )
11 r 4 = f 4 ( q , zk )
12 r i = [ r1 , r2 , r3 , r 4 ]
13 r i i = [0]
14 R . append ( r i )
15 R . append ( r i i )
16 return R
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Algoritmo de redução

Funções f1,f2,f3 e f4

1 def f1 ( i ) :
2 r e t u r n ( 2 ∗ i −1)
3
4 def f2 (q , j , i ) :
5 r e t u r n ( 2 ∗ ( q+j −i ) −1)
6
7 def f3 (q , k , j ) :
8 r e t u r n ( 2 ∗ ( q+k−j ) −1)
9
10 def f4 (q , k) :
11 r e t u r n ( 2 ∗ ( q−k ) +1)
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Algoritmo de redução

Criando descrição das Colunas

1 d e f c o n s C o l D e f i n i t i o n (M, X , Y , Z , n ) :
2 C = [ ] , N = c o u n t E l e m e n t s (M)
3 C . append ( g t ( n ) )
4 foreach x in X:
5 nv = N [ x ]
6 x i = g1X ( n , nv ) , x i i = g2XY ( n , nv )
7 C . append ( x i ) , C . append ( x i i )
8 foreach y in Y:
9 nv = N [ y ]
10 y i = g1YZ ( n ) , y i i = g2XY ( n , nv )
11 C . append ( y i ) , C . append ( y i i )
12 foreach z in Z:
13 nv = N [ z ]
14 z i = g1YZ ( n ) , z i i = g2Z ( n , nv )
15 C . append ( z i ) , C . append ( z i i )
16 C . append ( g t ( n ) )
17 return C
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Algoritmo de redução

Funções g1 e g2

1 d e f g1X ( n , nv ) :
2 r e t u r n g t ( ( n−nv ) )
3
4 d e f g1YZ ( n ) :
5 r e t u r n g t ( ( n−1) )
6
7 d e f g2XY ( n , nv ) :
8 r e t u r n g t ( ( n−nv +1) )
9
10 d e f g2Z ( n , nv ) :
11 r e t u r n g t ( ( n−nv ) )
12
13 def gt ( t ) :
14 T = []
15 f o r e a c h number i n r a n g e ( 0 , t ) :
16 T . append ( 1 )
17 return T
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Algoritmo de redução

Contando o no ocorrências de xi, yi e zi

1 d e f c o u n t E l e m e n t s (M) :
2 N = {} #D i c t
3 f o r e a c h t r i p l e i n M:
4 f o r each element i n t r i p l e :
5 i f e l e m e n t i n N : #O( 1 ) h a s h
6 N [ e l e m e n t ] += 1
7 else :
8 N[ element ] = 1
9 return N
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Análise da Complexidade

Complexidade da redução

2 n = l e n (M) ;
3 h = 2∗ n ;
4 w = 6∗ q +2;
5 R = c o n s R o w D e f i n i t i o n (M, X , Y , Z , q ) ;
6 C = c o n s C o l D e f i n i t i o n (M, X , Y , Z , n ) ;

1 + Complex[5] + Complex[6]
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Análise da Complexidade

Complex[5] - consRowDefinition
2 R = []
3 f o r e a c h t r i p l e i n M:
4 x i = X[ t r i p l e [ 0 ] ]
5 y j = Y[ t r i p l e [ 1 ] ]
6 zk = Z [ t r i p l e [ 2 ] ]
7 ri = []
8 r1 = f1 ( x i ) ;
9 r2 = f2 (q , yj , x i )
10 r 3 = f 3 ( q , zk , y j )
11 r 4 = f 4 ( q , zk )
12 r i = [ r1 , r2 , r3 , r 4 ]
13 r i i = [0]
14 R . append ( r i )
15 R . append ( r i i )

n
X
1=n−1+1=n
i=1
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Análise da Complexidade

Substituindo...

2 n = l e n (M) ;
3 h = 2∗ n ;
4 w = 6∗ q +2;
5 R = c o n s R o w D e f i n i t i o n (M, X , Y , Z , q ) ;
6 C = c o n s C o l D e f i n i t i o n (M, X , Y , Z , n ) ;

1 + n + Complex[6]
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Análise da Complexidade

Complex[6] - consColDefinition
2 C = [ ] , N = c o u n t E l e m e n t s (M)
3 C . append ( g t ( n ) )
4 foreach x in X:
5 nv = N [ x ]
6 x i = g1X ( n , nv ) , x i i = g2XY ( n , nv )
7 C . append ( x i ) , C . append ( x i i )
8 foreach y in Y:
9 nv = N [ y ]
10 y i = g1YZ ( n ) , y i i = g2XY ( n , nv )
11 C . append ( y i ) , C . append ( y i i )
12 foreach z in Z:
13 nv = N [ z ]
14 z i = g1YZ ( n ) , z i i = g2Z ( n , nv )
15 C . append ( z i ) , C . append ( z i i )
16 C . append ( g t ( n ) )

q
X
1 + 2n + 3 ∗ (1 + 2n)
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Análise da Complexidade

Complex[6] - consColDefinition

q
X
1 + 2n + 3 ∗ (1 + 2n) =
i=1
q
X
1 + 2n + 3 ∗ (1 + 2n) ∗ 1=
i=1
1 + 2n + 3q + 6nq
The NP-Completeness of NONOGRAM
Nonogram ∈ NP-difı́cil
Análise da Complexidade

Substituindo...

2 n = l e n (M) ;
3 h = 2∗ n ;
4 w = 6∗ q +2;
5 R = c o n s R o w D e f i n i t i o n (M, X , Y , Z , q ) ;
6 C = c o n s C o l D e f i n i t i o n (M, X , Y , Z , n ) ;

1 + (n) + (1 + 2n + 3q + 6nq) =

O(n ∗ q) ou

O(p 2 ) onde p = max{n, q}


The NP-Completeness of NONOGRAM
Conclusão

Conclusão

NONOGRAM ∈ NP ∧ NONOGRAM ∈ NP-Hard =⇒


NONOGRAM ∈ NP-Complete

Anda mungkin juga menyukai