11.01.2015 Views

Uvod u dinamicko programiranje

Uvod u dinamicko programiranje

Uvod u dinamicko programiranje

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Algoritam G: Pseudo kod problema NRP<br />

Input: Niz a dužine n<br />

Output: LIS - najduži rastući podniz niz a<br />

1 for k ← 1 to n do<br />

2 max = 0;<br />

3 for i ← 1 to k − 1 do<br />

4 if a[k] > a[i] and d[i] > max then<br />

5<br />

max = d[j];<br />

6 end<br />

7 end<br />

8 d[k] = max + 1;<br />

9 end<br />

10 max = 0;<br />

11 index = 0;<br />

12 for k ← 1 to n do<br />

13 if d[k] > max then<br />

14 max = d[k];<br />

15 index = k;<br />

16 end<br />

17 end<br />

18 LIS ← ∅;<br />

19 while d[index] > 1 do<br />

20 add a[index] to LIS;<br />

21 i = index − 1;<br />

22 while d[index] > d[i] + 1 do<br />

23 i = i − 1;<br />

24 end<br />

25 index = i;<br />

26 end<br />

27 add a[index] to LIS;<br />

28<br />

return LIS;<br />

10

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

Saved successfully!

Ooh no, something went wrong!