Académique Documents
Professionnel Documents
Culture Documents
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
Exemplo
Notação
Definição formal
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]
(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]
(h+1)/2
(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=
Definição
3-Dimensional Matching
3DM - Exemplo 1
3DM - Exemplo 2
n =k M k= 6
3DM ≤p Nonogram
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
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
Estrutura de dados
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
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
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
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
Conclusão