Projeto e Análise de Algoritmos Prof. Ruy Luiz Milidiú 5/5/2014 Ruy Luiz Milidiú 1
Redução 5/5/2014 Ruy Luiz Milidiú 2
Resumo Objetivo Apresentar o príncipio da redução de um problema a outro Sumário O algoritmo de redução De subcadeia ótima para caminho mais curto De circuito hamiltoniano para caixeiro viajante De satisfabilidade para programação linear inteira De satisfabilidade para 3-satisfabilidade De 3SAT para 3-COLOR 5/5/2014 Ruy Luiz Milidiú 3
Redução Para quem só tem um martelo tudo se parece com pregos! 5/5/2014 Ruy Luiz Milidiú 4
Subcadeia de Soma Máxima Entrada a = (a 1, a 2,, a n ) um vetor de inteiros Saída: I e J tais que a I + a I+1 + + a J é máximo. quando todos os valores de a são negativos, então a subseqüência vazia é escolhida e o valor máximo da soma é considerado 0. 5/5/2014 Ruy Luiz Milidiú 5
Caminho mais longo 0 0 0 0 a 0 1 a 1 2 a 2 3 a 3 4 4 5 0 0 0 0 5/5/2014 Ruy Luiz Milidiú 6
Redução Subcadeia de soma máxima reduz para Caminho mais longo 5/5/2014 Ruy Luiz Milidiú 7
Circuito Hamiltoniano 5/5/2014 Ruy Luiz Milidiú 8
Circuito Hamiltoniano Entrada Saída Circuito simples que contém todos os vértices. G = (V,E) grafo não dirigido SIM G tem circuito Hamiltoniano NÃO G não tem circuito Hamiltoniano 5/5/2014 Ruy Luiz Milidiú 9
Caixeiro viajante 1 2 1 1 1 2 1 2 1 custo mínino n? 2 5/5/2014 Ruy Luiz Milidiú 10
Redução Circuito Hamiltoniano reduz para Caixeiro viajante 5/5/2014 Ruy Luiz Milidiú 11
SAT Entrada Satisfabilidade Saída C = C 1 C 2... C m C i = y k onde y k {x k, x k } k J(i) SIM existe atribuição consistente com C=TRUE NÃO caso contrário 5/5/2014 Ruy Luiz Milidiú 12
PLI 1 = (x 1 x 3 ) ( x 2 x 3 ) (x 2 x 3 ) (x 1 x 2 x 3 ) minimize x 1 + (1-x 3 ) tal que x 1 + (1-x 3 ) 1 (1-x 2 ) + x 3 1 x 2 + x 3 1 x 1, x 2, x 3 {0,1} x 1 + (1-x 2 ) + (1-x 3 ) 1 5/5/2014 Ruy Luiz Milidiú 13
Redução SAT reduz para PLI 5/5/2014 Ruy Luiz Milidiú 14
SAT Entrada Satisfabilidade Saída C = C 1 C 2... C m C i = y k onde k {x k, x k } k J(i) SIM existe atribuição consistente com C = 1? NÃO caso contrário 5/5/2014 Ruy Luiz Milidiú 15
3SAT 1 = x 11 (x 1 x 3 ) ( x 2 x 3 ) (x 2 x 3 ) (x 11 x 2 x 3 ) (x 1 x 2 x 3 x 4 x 7 x 8 ) x 11 = 1 x 11 = 0 1 = (x 1 x 3 ) ( x 2 x 3 ) (x 2 x 3 ) (x 11 x 2 x 3 ) (x 1 x 2 x 3 x 4 x 7 x 8 ) 5/5/2014 Ruy Luiz Milidiú 16
3SAT 1 = x 11 (x 1 x 3 ) ( x 2 x 3 ) (x 2 x 3 ) (x 11 x 2 x 3 ) (x 1 x 2 x 3 x 4 x 7 x 8 ) x 11 = 1 x 11 = 0 1 = (x 1 x 3 ) ( x 2 x 3 ) (x 2 x 3 ) ( 1 x 2 x 3 ) (x 1 x 2 x 3 x 4 x 7 x 8 ) 5/5/2014 Ruy Luiz Milidiú 17
3SAT 1 = x 11 (x 1 x 3 ) ( x 2 x 3 ) (x 2 x 3 ) (x 11 x 2 x 3 ) (x 1 x 2 x 3 x 4 x 7 x 8 ) x 11 = 1 x 11 = 0 1 = (x 1 x 3 ) ( x 2 x 3 ) (x 2 x 3 ) ( 1 x 2 x 3 ) (x 1 x 2 x 3 x 4 x 7 x 8 ) 5/5/2014 Ruy Luiz Milidiú 18
3SAT 1 = x 11 (x 1 x 3 ) ( x 2 x 3 ) (x 2 x 3 ) (x 11 x 2 x 3 ) (x 1 x 2 x 3 x 4 x 7 x 8 ) x 11 = 1 x 11 = 0 1 = (x 1 x 3 ) ( x 2 x 3 ) (x 2 x 3 ) ( 1 x 2 x 3 ) (x 1 x 2 x 3 x 4 x 7 x 8 ) 5/5/2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) ( x 2 x 3 y 2 ) ( x 2 x 3 y 2 ) (x 2 x 3 y 3 ) (x 2 x 3 y 3 ) (x 1 x 2 y 4 ) ( y 4 x 3 y 5 ) ( y 5 x 4 x 7 x 8 ) 5/5/2014 Ruy Luiz Milidiú 24
3SAT C = x 11 (x 1 x 3 ) ( x 2 x 3 ) (x 2 x 3 ) (x 1 x 2 x 3 ) (x 1 x 2 x 3 x 4 x 7 x 8 ) x 11 = 1 e 1 = (x 1 x 3 y 1 ) (x 1 x 3 y 1 ) ( x 2 x 3 y 2 ) ( x 2 x 3 y 2 ) (x 2 x 3 y 3 ) (x 2 x 3 y 3 ) (x 1 x 2 x 3 ) (x 1 x 2 y 4 ) ( y 4 x 3 y 5 ) ( y 5 x 4 y 6 ) ( y 6 x 7 x 8 ) 5/5/2014 Ruy Luiz Milidiú 25
Redução SAT reduz para 3SAT 5/5/2014 Ruy Luiz Milidiú 26
3-COLOR 5/5/2014 Ruy Luiz Milidiú 27
3-COLOR 3-COLOR de G=(V,E) f: V {1,2,3} {u,v} E então f(u) f(v) Problema de decisão uma 3-COLOR de G? 5/5/2014 Ruy Luiz Milidiú 28
3SAT 3-COLOR ( X X) = T (X Y) = T X X X Y G T F T 5/5/2014 Ruy Luiz Milidiú 29
3SAT 3-COLOR X Y cor(x) = cor(y) A B Z 5/5/2014 Ruy Luiz Milidiú 30
3SAT 3-COLOR X Y cor(x) = cor(y) A B então Z cor(z) = cor(x) = cor(y) 5/5/2014 Ruy Luiz Milidiú 31
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 1 X 2 X 2 X 3 X 3 G T F 5/5/2014 Ruy Luiz Milidiú 32
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 1 X 2 X 2 X 3 X 3 G T F 5/5/2014 Ruy Luiz Milidiú 33
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 34
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 35
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 36
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 37
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 38
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T Absurd! 5/5/2014 Ruy Luiz Milidiú 39
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 40
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 41
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 42
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 43
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 44
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 45
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 46
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 47
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 48
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 49
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 50
3SAT 3-COLOR C = (x 1 x 2 x 3 ) ( x 1 x 2 x 3 ) (x 1 x 2 x 3 ) X 1 X 2 X 3 T 5/5/2014 Ruy Luiz Milidiú 51
3SAT 3-COLOR C é capaz de satisfação existe f que é uma 3-COLOR de G 5/5/2014 Ruy Luiz Milidiú 52
Redução 3SAT reduz para 3-COLOR 5/5/2014 Ruy Luiz Milidiú 53
3-COLOR SAT 1 5 2 4 3 5/5/2014 Ruy Luiz Milidiú 54
3-COLOR SAT f(1) 1 f(5) 5 2 f(2) f(4) 4 3 f(3) 5/5/2014 Ruy Luiz Milidiú 55
3-COLOR SAT 1 cores bit bit bit 5 2 4 3 5/5/2014 Ruy Luiz Milidiú 56
3-COLOR SAT for u = 1..n for k =1,2,3 X uk indicadora de cor k ao nó u X uk = 1 X uk = 0 nó u tem cor k nó u não tem cor k 5/5/2014 Ruy Luiz Milidiú 57
3-COLOR apenas 3 cores distintas exatamente uma cor por nó cores diferentes para nós incidentes na mesma aresta 5/5/2014 Ruy Luiz Milidiú 58
Unicidade Um e apenas um! (x 1 x 2 x 3 ) (x 1 x 2 ) (x 1 x 3 ) (x 2 x 3 ) x y = x y (x 1 x 2 x 3 ) (x 1 x 2 ) (x 1 x 3 ) (x 2 x 3 ) 5/5/2014 Ruy Luiz Milidiú 59
Uma cor por nó A = A exatamente uma cor por nó (X 11 X 12 X 13 ) (X 11 X 12 ) (X 11 X 13 ) (X 12 X 13 ) (X n1 X n2 X n3 ) (X n1 X n2 ) (X n1 X n3 ) (X n2 X n3 ) 5/5/2014 Ruy Luiz Milidiú 60
Uma cor por nó V cores diferentes para nós indicentes na mesma aresta [(X u1 X v1 ) (X u2 X v2 ) (X u3 X v3 )] {u,v} A 5/5/2014 Ruy Luiz Milidiú 61
3-COLOR SAT C = A V Atribuição Verificação 5/5/2014 Ruy Luiz Milidiú 62
Equivalência SAT 3-SAT 3-COLOR SAT 5/5/2014 Ruy Luiz Milidiú 63
3-COLOR PLANAR-3-COLOR 5/5/2014 Ruy Luiz Milidiú 64
3-COLOR 5/5/2014 Ruy Luiz Milidiú 65
3-COLOR 5/5/2014 Ruy Luiz Milidiú 66
3-COLOR 5/5/2014 Ruy Luiz Milidiú 67
3-COLOR 5/5/2014 Ruy Luiz Milidiú 68
3-COLOR 5/5/2014 Ruy Luiz Milidiú 69
3-COLOR 5/5/2014 Ruy Luiz Milidiú 70
3-COLOR 5/5/2014 Ruy Luiz Milidiú 71
3-COLOR 5/5/2014 Ruy Luiz Milidiú 72
3-COLOR 5/5/2014 Ruy Luiz Milidiú 73
3-COLOR 5/5/2014 Ruy Luiz Milidiú 74
3-COLOR 5/5/2014 Ruy Luiz Milidiú 75
3-COLOR Cantos opostos tem a mesma cor! 5/5/2014 Ruy Luiz Milidiú 76
3-COLOR 3-COLOR PLANAR u v 5/5/2014 Ruy Luiz Milidiú 77 G
3-COLOR 3-COLOR PLANAR u v G* 5/5/2014 Ruy Luiz Milidiú 78
3-COLOR 3-COLOR PLANAR u v G 5/5/2014 Ruy Luiz Milidiú 79
3-COLOR 3-COLOR PLANAR G é 3-COLOR G é 3-COLOR 5/5/2014 Ruy Luiz Milidiú 80
3-COLOR 3-COLOR PLANAR G é 3-COLOR G é 3-COLOR u u v u 5/5/2014 Ruy Luiz Milidiú 81
3-COLOR 3-COLOR PLANAR G é 3-COLOR G é 3-COLOR u u u v u u 5/5/2014 Ruy Luiz Milidiú 82
3-COLOR 3-COLOR PLANAR G é 3-COLOR G é 3-COLOR u u u v u u 5/5/2014 Ruy Luiz Milidiú 83
3-COLOR 3-COLOR PLANAR G é 3-COLOR G é 3-COLOR u u u v u u 5/5/2014 Ruy Luiz Milidiú 84
3-COLOR 3-COLOR PLANAR G é 3-COLOR G é 3-COLOR u u u v u u 5/5/2014 Ruy Luiz Milidiú 85
Planar k-colorability PLANAR-2-COLOR linear time PLANAR-3-COLOR Hard PLANAR-4-COLOR O(1) time 5/5/2014 Ruy Luiz Milidiú 86
Planar k-colorability [Appel-Haken, 1976] Every planar map is 4-colorable Resolved century-old open problem 50 days processing many special cases First major theorem to be proved using computer False intuition If PLANAR-3-COLOR is hard then so is PLANAR-4-COLOR and PLANAR-5-COLOR 5/5/2014 Ruy Luiz Milidiú 87
Subset Sum Given a set natural of numbers {w 1,, w n } and an integer W, is there a subset that adds up to exactly W? Remark input integers are encoded in binary polynomial reduction must be polynomial in binary encoding. 5/5/2014 Ruy Luiz Milidiú 88
Subset Sum Input { 1, 4, 16, 64, 256, 1040, 1041, 1093, 1284, 1344 } W = 3754 Output 1 + 16 + 64 + 256 + 1040 + 1093 + 1284 = 3754 Yes 5/5/2014 Ruy Luiz Milidiú 89
3-SAT SUBSET-SUM C = (x y z) (x y z) (x y z) X Y Z C1 C2 C3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 5/5/2014 Ruy Luiz Milidiú 90 2 1 2 1 2 1 100010 100101 10100 10011 1110 1001 200 100 20 10 2 1 111444
Partition Given natural numbers v 1,, v m, can they be partitioned into two subsets that add up to the same value? ½ i v i 5/5/2014 Ruy Luiz Milidiú 91
SUBSET-SUM PARTITION W, w 1,, w n SUBSET-SUM instance PARTITION instance m = n+2 v 1 = w 1 v 2 = w 2 v n = w n v n+1 = 2. i w i - W v n+2 = i w i + W v n+1 = 2 i w i - W W v n+2 = i w i + W i w i - W The two new elements cannot be in the same part! 5/5/2014 Ruy Luiz Milidiú 92