✬ Algìrijmoi kai Poluplokìthta ✩ Dilexh 12 1 Μέγιστη κοινή ακολουθία Orismìc X =< x1 , x2 , · · · , xm >, mÐa llh akoloujÐa Z =< z1 , z2 , · · · , zk > eÐnai upì-akoloujÐa thc X en uprqei mÐa gnhsÐwc aÔxousa akoloujÐa < i1 , i2 , · · · , ik > apì deÐktec thc X tètoia ¸ste gia ìla ta j = 1 · · · k isqÔei xi = zj . j Me dedomènh mÐa akoloujÐa Pardeigma X =< A, B, C, A, D, E, A, F, B > Upì-akoloujÐa thc • X AkoloujÐa deikt¸n thc Z =< A, C, D, E, A > < 1, 3, 5, 6, 7 > Z =< A, B > < 1, 2 >, < 1, 9 X >, < 4, 9 > Mia kalÔterh anaparstash: X: <A B C A D E A F B> ✫ ✪ Z: <A C D E A> ✬ Algìrijmoi kai Poluplokìthta Dilexh 12 To prìblhma thc mègisthc koin c upì-akoloujÐac (MKU) ( ✩ 2 The longest common subsequence (LCS) problem) EÐsodoc: Duo 'Exodoc: H akoloujÐec X kai Y . mègisth koin upì-akoloujÐa. ParadeÐgmata X: A B C B D A B X: A B C B D A B Y: B D C A B A Y: B D C A B A X: A B C B D A B Y: B D C A B A ✫ ✪ ✬ Algìrijmoi kai Poluplokìthta Dilexh ✩ 12 3 GiatÐ meletme to prìblhma thc MKU? • LÔnetai me • 'Eqei arketèc qr sh dunamikoÔ programmatismoÔ. efarmogèc. Efarmog Me dedomènec 2 diaforetikèc prosdioristoÔn oi allagèc ekdìseic pou (versions) èginan apì thn enìc pr¸th progrmmatoc, na èkdosh èwc th deÔterh. LÔsh BreÐte thn MKU apì MKU twn dÔo proèrqetai apì thn mÐa èkdosh programmtwn. prosjèseic proc thn To keÐmeno afairèseic pou pou den eÐnai mèloc èginan kat thn metbash llh. ✫ ✪ ✬ Algìrijmoi kai Poluplokìthta ✩ Dilexh 12 4 Mia aploðk prosèggish • 'Estw • 'Elegxe kje upì-akoloujÐa tou Er¸thsh Apnthsh thc S to sÔnolo ìlwn twn dunat¸n upì-akolouji¸n twn S kai Y. kai prosdiìrise thn MKU. Poio eÐnai to mègejoc tou sunìlou 'Estw X S? k = min(legth(X), length(Y )). Tìte, |S| = 2k . Autì kajist thn aploðk prosèggish anedafik ! ✫ ✪ ✬ Algìrijmoi kai Poluplokìthta Dilexh ✩ 12 5 MÐa anadromik lÔsh 'Estw X =< x1 , x2 , · · · , xm > Xi Sumbìlise me kai me 'Estw Mac Yi c[i, j] hn upì-akoloujÐa to mègejoc na c[i, j] = . Xi =< x1 , x2 , · · · , xi > Yi =< y1 , y2 , · · · , yi > thn upì-akoloujÐa endiafèrei na MporoÔme Y =< y1 , y2 , · · · , yn > kai thc . MKU twn upologÐsoume to gryoume thn Xi kai Yj . c[m, n] . anadromik sqèsh: 0 en i=0 c[i − 1, j − 1] + 1 en i, j > 0 kai xi = y j max(c[i, j − 1], c[i − 1, j]) en i, j > 0 kai xi 6= yj Er¸thsh i) EÐnai h ii) Pwc Apnthsh parapnw katal xame sqèsh se j=0 swst ? aut ? i) NAI ✫ ✬ Algìrijmoi kai ii) • en ✪ Poluplokìthta c[i, j] = i=0 ken • en 6 en i=0 c[i − 1, j − 1] + 1 en i, j > 0 kai xi = y j max(c[i, j − 1], c[i − 1, j]) en i, j > 0 kai xi 6= yj kai j=0 : apì upì-akoloujÐa i, j > 0 ✩ 12 0 j=0 Toulqiston mÐa Dilexh tic akoloujÐec eÐnai ken . (sumbolÐzetai me xi = y j to ǫ 'Ara, h mình upì-akoloujÐa eÐnai h ). : < x1 , x2 , · · · , xi−1 , xi > = < y1 , y2 , · · · , yj−1 , yj > LCS(Xi , Yj ) = LCS(Xi−1 , Yj−1 ) ⊕ xi =⇒ c[i, j] = c[i − 1, j − 1] + 1 ( ⊕ sumbolÐzei th sunènwsh sumboloseir¸n / akolouji¸n) • en i, j > 0 kai xi 6= yj : < x1 , x2 , · · · , xi−1 , xi > 6= < y1 , y2 , · · · , yj−1 , yj > ✫ LCS[Xi , Yj ] = LCS[Xi , Yj−1 ] LCS[Xi−1 , Yj ] =⇒ c[i, j] = max(c[i, j − 1], c[i − 1, j]) ✪ ✬ Algìrijmoi kai Poluplokìthta ✩ Dilexh 12 7 MÐa anadromik prosèggish • Sthn perÐptwsh ìpou oi 2 upì-akoloujÐec den èqoun koinèc upì-akoloujÐec (ektìc thc ǫ), o algìrijmoc qreizetai ekjetikì qrìno. Pardeigma Sqediste to grfhma anadrom c gia to C[3, 4]. c[0,4] c[1,4] c[2,4] c[3,4] c[0,3] c[1,3] c[2,3] c[3,3] c[0,2] c[1,2] c[2,2] c[3,2] c[0,1] c[1,1] c[2,1] c[3,1] c[0,0] c[1,0] c[2,0] c[3,0] ✫ ✬ ✪ Algìrijmoi kai Poluplokìthta 'Askhsh Dilexh 12 Na upologisteÐ o arijmìc twn kl sewn thc diadikasÐac pou upologÐzei to ✩ 8 c[i, j] kat th dirkeia tou upologismoÔ tou c[m, n], ìpou 0 ≤ i ≤ m, 0 ≤ j ≤ n. • Profan¸c, uprqoun epikaluptìmena upì-probl mata ta opoÐa o algìrijmoc ja lÔsei parapnw apì mÐa forèc. (O anadromikìc algìrijmìc qreizetai toulqiston ekjetikì qrìno.) Parat rhsh To c[m, n] mporeÐ na upologisteÐ eÔkola xekin¸ntac apì th bsh (bottom-up). (Na sumplhrwjeÐ o pÐnakac ✫ c gramm -gramm apì ta arister proc ta dexi.) ✪ ✬ Algìrijmoi kai Poluplokìthta ✩ Dilexh 12 9 LCS length(X, Y ) m = length(X) n = length(Y ) for i = 1 to m do c[i, 0] = 0 for j = 1 to n do c[0, j] = 0 for i = 1 to m do for j = 1 to n do if xi = yj then c[i, j] = c[i − 1, j − 1] + 1 b[i, j] = ւ else if c[i − 1, j] ≥ c[i, j − 1] then c[i, j] = c[i − 1, j] b[i, j] = ↓ else c[i, j] = c[i, j − 1] b[i, j] = ← return c, b ShmeÐwsh ✫ O pÐnakac ✬ b Algìrijmoi kai Poluplokìthta Pardeigma ✩ Dilexh 12 10 X= A B C B D A B Y= B D C A B A j 0 1 B i 2 D 3 C 4 A 5 B 6 A 1 1 2 2 3 4 4 2 3 3 4 1 2 2 3 3 B 0 0 2 2 1 1 3 C 0 1 1 2 2 2 2 3 2 3 2 2 B 1 0 1 0 1 1 2 A 1 0 2 1 0 0 1 1 0 0 0 0 0 0 0 7 6 B A 0 5 D 4 0 ✫ ✪ qrhsimopoieÐtai gia thn ankthsh thc MKU. 0 ✪ ✬ Algìrijmoi kai Poluplokìthta Dilexh 12 Er¸thsh 1 Poia eÐnai h poluplokìthta thc LCS()? Apnthsh: O(mn) ✩ 11 Er¸thsh 2 Pìsoc qrìnoc qreizetai gia thn ankthsh thc MKU apì ton pÐnaka b? Apnthsh: O(m + n) Er¸thsh 3 Pìsh mn mh apaiteÐtai gia thn ektèlesh thc LCS()? Apnthsh: O(mn) Er¸thsh 4 EÐnai dunatì na beltiwjeÐ h poluplokìthta q¸rou sthn perÐptwsh ìpou jèloume na upologÐsoume mìno to m koc thc MKU? Apnthsh: Nai! Mìno 2 grammèc tou pÐnaka =⇒ ShmeÐwsh: ✫ O(min(m, n)) c eÐnai aparaÐthtoi. mn mh. Sthn perÐptwsh aut den mporeÐ na anakthjeÐ h MKU. ✪
© Copyright 2026 Paperzz