13.04.2013 Views

Algoritmos e complexidade Notas de aula - Arquivo Escolar

Algoritmos e complexidade Notas de aula - Arquivo Escolar

Algoritmos e complexidade Notas de aula - Arquivo Escolar

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

. B D C A B A<br />

. 0 0 0 0 0 0 0<br />

A 0 ↑0 ↑0 ↑0 ↖1 ←1 ↖1<br />

B 0 ↖1 ←1 ←1 ↑1 ↖2 ←2<br />

C 0 ↑1 ↑1 ↖2 ←2 ↑2 ↑2<br />

B 0 ↖1 ↑1 ↑2 ↑2 ↖3 ←3<br />

D 0 ↑1 ↖2 ↑2 ↑2 ↑3 ↑3<br />

A 0 ↑1 ↑2 ↑2 ↖3 ↑3 ↖4<br />

B 0 ↖ 1 ↑2 ↑2 ↑3 ↖4 ↑4<br />

5.2. Comparação <strong>de</strong> sequências<br />

Nesse caso, não tem método simples para reduzir o espaço <strong>de</strong> O(nm) (veja<br />

os comentários sobre o algoritmo <strong>de</strong> Hirschberg abaixo). Mantendo duas linhas<br />

ou colunas <strong>de</strong> c, gasta menos recursos, mas para recupar a subsequência<br />

comum, temos que manter O(nm) elementos em b.<br />

O algoritmo <strong>de</strong> Hirschberg [36], via Divisão e Conquista, resolve o problema<br />

da subsequência comum mais longa em tempo O(mn), mas com <strong>complexida<strong>de</strong></strong><br />

<strong>de</strong> espaço linear O(m + n). O algoritmo recursivamente divi<strong>de</strong> a tabela em<br />

quatro quadrantes e ignora os quadrantes superior-direito e inferior-esquerdo,<br />

visto que a solução não passa por eles. Após, o algoritmo é chamado recursivamente<br />

nos quadrantes superior-esquerdo e inferior-direito. Em cada chamada<br />

recursiva é criada uma lista com as operações executadas, e tal lista é concatenada<br />

ao final das duas chamadas recursivas. A recuperação da sequências<br />

<strong>de</strong> operações po<strong>de</strong> ser feita percorrendo-se linearmente esta lista.<br />

Print-SCML<br />

Algoritmo 5.3 (Print-SCML)<br />

Entrada Matriz b ∈ {←, ↖, ↑} m×n .<br />

Saída A maior subsequência Z comum entre X e Y obtida a partir <strong>de</strong> b.<br />

1 i f i = 0 or j = 0 then return<br />

2 i f b[i, j] =↖ then<br />

3 Print−SCML(b , X , i 1 , j 1 )<br />

4 p r i n t xi<br />

5 else i f b[i, j] =↑ then<br />

6 Print−SCML(b , X , i 1 , j )<br />

7 else<br />

8 Print−SCML(b , X , i , j 1 )<br />

103

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!