YuyaHorita
August 31, 2018
5.4k

# Difference Algorithm

- Myers Algorithm
- SES / LCS
- Edit Graph

- Paul Heckel Algorithm

August 31, 2018

## Transcript

2. ### "HFOEB  *OUSPEVDUJPO w ࠩ෼ͱ͸ʁ w ฤूͱ͍͏ߟ͑ํ  .ZFST"MHPSJUIN w

'FBUVSFT w 4&4-\$41SPCMFN w &EJU(SBQI w ݪཧ w .ZFST0WFSWJFX  1BVM)FDLFM"MHPSJUIN w 'FBUVSFT w ݪཧ w )FDLFM0WFSWJFX  4VNNBSZ

4. ### source = [1, 2, 3, 4, 5] ࠩ෼ͱ͸ʁ target =

[1, 3, 5, 7] EJ⒎ TPVSDF UBSHFU 
5. ### source = [1, 2, 3, 4, 5] ࠩ෼ͱ͸ʁ target =

[1, 3, 5, 7] EJ⒎ TPVSDF UBSHFU  %FMFUF %FMFUF *OTFSU
6. ### source = [1, 2, 3, 4, 5] ࠩ෼ͱ͸ʁ target =

[1, 3, 5, 7] EJ⒎ TPVSDF UBSHFU  %FMFUF %FMFUF *OTFSU &EJU4DSJQU
7. ### ฤूͱ͍͏ߟ͑ํ         

TPVSDF UBSHFU
8. ###  ฤूͱ͍͏ߟ͑ํ        

 TPVSDF UBSHFU EJ⒎ TPVSDF UBSHFU  %FMFUF %FMFUF *OTFSU

10. ### 'FBUVSFT w 4JNQMF w 0 /% \$PNQMFYJUZ w %FMFUF*OTFSU4DSJQU w

4IPSUFTU w "EPQUFEJO%J⒎6UJM IUUQTEFWFMPQFSBOESPJEDPNSFGFSFODFBOESPJETVQQPSUWVUJM%J⒎6UJM

12. ### ࠷୹ͷఆྔత൑அ w 4&44IPSUFTU&EJU4DSJQU w -\$4ɿ-POHFTU\$PNNPO4VCTFRVFODF w &EJU4DSJQUTPVSDFˠUBSHFUͷฤूʹඞཁͳ*OTFSU %FMFUF4DSJQUͷ͜ͱ w 4VCTFRVFODFɿ഑ྻ͔Β͍͔ͭ͘ͷཁૉΛऔΓআ͍ͯಘΒΕΔ഑ྻ

4&4-\$41SPCMFN
13. ### ࠷୹ͷఆྔత൑அ w 4&44IPSUFTU&EJU4DSJQU w -\$4ɿ-POHFTU\$PNNPO4VCTFRVFODF w &EJU4DSJQUTPVSDFˠUBSHFUͷฤूʹඞཁͳ*OTFSU %FMFUF4DSJQUͷ͜ͱ w 4VCTFRVFODFɿ഑ྻ͔Β͍͔ͭ͘ͷཁૉΛऔΓআ͍ͯಘΒΕΔ഑ྻ

source = [1, 2, 3, 4, 5] FY [1, 2, 5] [2, 3, 4, 5] [1, 2, 4, 3] [1, 3, 4, 6] 4&4-\$41SPCMFN
14. ### ࠷୹ͷఆྔత൑அ w 4&44IPSUFTU&EJU4DSJQU w -\$4ɿ-POHFTU\$PNNPO4VCTFRVFODF w &EJU4DSJQUTPVSDFˠUBSHFUͷฤूʹඞཁͳ*OTFSU %FMFUF4DSJQUͷ͜ͱ w 4VCTFRVFODFɿ഑ྻ͔Β͍͔ͭ͘ͷཁૉΛऔΓআ͍ͯಘΒΕΔ഑ྻ

4&4-\$41SPCMFN ˞EVBMQSPCMFN source = [1, 2, 3, 4, 5] FY [1, 2, 5] [2, 3, 4, 5] [1, 2, 4, 3] [1, 3, 4, 6]

17. ### &EJU(SBQI source = [A, B, C, A, B, B, A]

target = [C, B, A, B, A, C] ίϯϐϡʔλͰղ͚ͦ͏ͳײ͡ʹදݱ
18. ### source = [A, B, C, A, B, B, A] target

= [C, B, A, B, A, C] (0, 0) A B C A B B A C B A B A C 0 1 2 3 4 5 0 1 2 3 4 5 6 source target 4UBSU (7, 6) &OE
19. ### source = [A, B, C, A, B, B, A] target

= [C, B, A, B, A, C] (0, 0) A B C A B B A C B A B A C 0 1 2 3 4 5 0 1 2 3 4 5 6 source target NBUDIQPJOU QPJOUBTTBNFTZNCPM (7, 6) 4UBSU &OE
20. ### source = [A, B, C, A, B, B, A] target

= [C, B, A, B, A, C] (0, 0) A B C A B B A C B A B A C 0 1 2 3 4 5 0 1 2 3 4 5 6 source target NBUDIQPJOU QPJOUBTTBNFTZNCPM (7, 6) &EHFʹԊͬͯɺ4UBSU͔Β&OE·ͰҠಈ͢Δ ӈɾԼɾࣼΊํ޲ͷҠಈ㲈EFMFUF JOTFSU EPOPUIJOH 4UBSU &OE 4UBSU &OE
21. ### source = [A, B, C, A, B, B, A] target

= [C, B, A, B, A, C] (0, 0) A B C A B B A C B A B A C 0 1 2 3 4 5 0 1 2 3 4 5 6 source target NBUDIQPJOU QPJOUBTTBNFTZNCPM (7, 6) 4&4 4UBSU &OE 4UBSU࠲ඪ&OE࠲ඪͷ࠷୹ܦ࿏  4UBSU &OE
22. ### (0, 0) 0 1 2 3 4 5 (7, 6)

0 1 2 3 4 5 6 Delete A Delete B 3JHIU.PWFNFOU #PUUPN.PWFNFOU %JBHPOBM.PWFNFOU EFMFUF JOTFSU EPOPUIJOH TBNFTZNCPM NBUDIQPJOU QPJOUBTTBNFTZNCPM Insert B Insert C source = [A, B, C, A, B, B, A] target = [C, B, A, B, A, C] A B C A B B A C B A B A C target Delete B 4UBSU &OE source 4UBSU &OE

25. ### ҠಈίετJT%JBHPOBM  source = [A, B, C, A, B, B,

A] target = [C, B, A, B, A, C] (0, 0) 0 1 2 3 4 5 (7, 6) 0 1 2 3 4 5 6 Delete A Delete B NBUDIQPJOU QPJOUBTTBNFTZNCPM Insert B Insert C A B C A B B A C B A B A C target Delete B 4UBSU &OE ࠷୹ܦ࿏ͷ૯Ҡಈίετ % 4&4ͷεΫϦϓτ਺ source 4UBSU &OE

28. ### (0, 0) 0 1 2 3 4 5 (7, 6)

0 1 2 3 4 5 6 Delete A Delete B NBUDIQPJOU QPJOUBTTBNFTZNCPM Insert B Insert C source = [A, B, C, A, B, B, A] target = [C, B, A, B, A, C] A B C A B B A source C B A B A C target Delete B 4UBSU &OE 3 1 x - y = k 0 -1 2 Y Z 4UBSU &OE
29. ### (0, 0) 0 1 2 3 4 5 (7, 6)

0 1 2 3 4 5 6 Delete A Delete B NBUDIQPJOU QPJOUBTTBNFTZNCPM Insert B Insert C source = [A, B, C, A, B, B, A] target = [C, B, A, B, A, C] A B C A B B A source C B A B A C target Delete B 4UBSU &OE 3 1 x - y = k 0 -1 2 Y Z k + 1 k - 1 k 4UBSU &OE
30. ### (0, 0) 0 1 2 3 4 5 (7, 6)

0 1 2 3 4 5 6 Delete A Delete B NBUDIQPJOU QPJOUBTTBNFTZNCPM Insert B Insert C source = [A, B, C, A, B, B, A] target = [C, B, A, B, A, C] A B C A B B A source C B A B A C target Delete B 4UBSU &OE 3 1 0 -1 2 Y Z k + 1 k - 1 k ඇର֯ͳҠಈΛ%ճߦͬͨ࠷௕ͷܦ࿏ %͕ح਺ͳΒLح਺ͷର֯ઢ %͕ۮ਺ͳΒLۮ਺ͷର֯ઢ্Ͱࢭ·Δ 4UBSU &OE
31. ### For D ← 0 to M + N Do For

k ← -D to D in steps of 2 Do Until D Path gets to furthest point Do Move on edges If (N, M) is the reached point Then Found the SES. Array[with: k] M = source.count N = target.count %͕ح਺ͳΒLح਺ͷର֯ઢ %͕ۮ਺ͳΒLۮ਺ͷର֯ઢ্Ͱࢭ·Δ &EJU(SBQI্Ͱ࠷ԕͷύε͸֎प Complexity: O((M+N)D) ίετ%Ͱߦ͚ͨ࠷ԕͷ࠲ඪΛLઢຖʹه࿥ %શͯԼҠಈ %શͯӈҠಈ &OE࠲ඪ
32. ### .ZFST0WFSWJFX w 4&4-\$41SPCMFNΛղͨ͘ΊͷΞϧΰϦζϜ w &EJU(SBQI w Ҡಈίετ % ɾLઢͷ֓೦Λ׆༻ w

%FMFUF *OTFSUऔಘޙɺͦΕΛ.PWF 6QEBUF4DSJQUʹม׵͢Δ ͜ͱ͸Մೳ

34. ### w 4JNQMF0QUJNJ[BCMF w 0 / \$PNQMFYJUZ w %FMFUF *OTFSU .PWF4DSJQU

w /PU"MXBZT4IPSUFTU w #BTFPG 'FBUVSFT 3Y%BUB4PVSDFT

36. ### TPVSDF UBSHFU 4ZNCPM5BCMF       

     &MFNFOU DPVOUJOTPVSDF DPVOUJOUBSHFU JOEFYJOTPVSDF                OVMM        OVMM source = [1, 2, 3, 4, 5, 4] target = [1, 3, 5, 2, 2, 7] w TPVSDF UBSHFUͷ֤ཁૉ &MFNFOU Λ,FZͱͯ͠ɺ%JDUJPOBSZͷ4ZNCPM5BCMFʹొ࿥ w 7BMVFʹ͸ͦͷཁૉͷ TPVSDF಺Ͱͷ਺ɺUBSHFU಺Ͱͷ਺ɺTPVSDF಺ͰͷJOEFY ˞ݸ͚ͩͷ৔߹ ݸPS̎ݸҎ্ͷ৔߹͸OVMM
37. ### TPVSDF UBSHFU 4ZNCPM5BCMF       

     &MFNFOU DPVOUJOTPVSDF DPVOUJOUBSHFU JOEFYJOTPVSDF                OVMM        OVMM source = [1, 2, 3, 4, 5, 4] target = [1, 3, 5, 2, 2, 7] w TPVSDF UBSHFUͷ֤ཁૉ &MFNFOU Λ,FZͱͯ͠ɺ%JDUJPOBSZͷ4ZNCPM5BCMFʹొ࿥ w 7BMVFʹ͸ͦͷཁૉͷ TPVSDF಺Ͱͷ਺ɺUBSHFU಺Ͱͷ਺ɺTPVSDF಺ͰͷJOEFY ɾશཁૉΛ؅ཧ͢Δ4ZNCPM5BCMFΛҰͭ༻ҙ ɾTPVSDF UBSHFUͦΕͧΕͷཁૉͷొ࿥࡞ۀΛߦ͏ ˞ݸ͚ͩͷ৔߹ ݸPS̎ݸҎ্ͷ৔߹͸OVMM
38. ### UBSHFU TPVSDF 4ZNCPM5BCMF       &MFNFOU

DPVOUJOTPVSDF DPVOUJOUBSHFU JOEFYJOTPVSDF                OVMM        OVMM w TPVSDF UBSHFUͷͦΕͧΕͷཁૉͱରରԠͨ͠\TPVSDF UBSHFU^3FGFSFODFTΛ༻ҙ͢Δɻ w ରରԠ͍ͯ͠Δཁૉ΁ͷࢀরΛอ࣋͠ɺࢀরݩ͸4ZNCPM5BCMFPS΋͏ยํͷ഑ྻ ˞ͲͪΒʹ΋ͦͷཁૉ͕͋Δ৔߹͸΋͏ยํͷ഑ྻ͕༏ઌ       TZNCPM5BCMF TZNCPM5BCMF TZNCPM5BCMF TZNCPM5BCMF TZNCPM5BCMF TZNCPM5BCMF UBSHFU3FGFSFODFT
39. ### UBSHFU TPVSDF 4ZNCPM5BCMF       &MFNFOU

DPVOUJOTPVSDF DPVOUJOUBSHFU JOEFYJOTPVSDF                OVMM        OVMM w TPVSDF UBSHFUͷͦΕͧΕͷཁૉͱରରԠͨ͠\TPVSDF UBSHFU^3FGFSFODFTΛ༻ҙ͢Δɻ w ରରԠ͍ͯ͠Δཁૉ΁ͷࢀরΛอ࣋͠ɺࢀরݩ͸4ZNCPM5BCMFPS΋͏ยํͷ഑ྻ ˞ͲͪΒʹ΋ͦͷཁૉ͕͋Δ৔߹͸΋͏ยํͷ഑ྻ͕༏ઌ       UIF0UIFS UIF0UIFS UIF0UIFS UIF0UIFS TZNCPM5BCMF TZNCPM5BCMF UBSHFU3FGFSFODFT
40. ### UIF0UIFS UIF0UIFS UIF0UIFS UIF0UIFS TZNCPM5BCMF TZNCPM5BCMF    

  UIF0UIFS UIF0UIFS UIF0UIFS TZNCPM5BCMF UIF0UIFS TZNCPM5BCMF       TPVSDF TPVSDF3FGFSFODFT UBSHFU UBSHFU3FGFSFODFT
41. ### w UIF0UIFSࢀরͷ৔߹.PWF w TZNCPM5BCMFࢀরͷ৔߹TPVSDFˠ%FMFUF UBSHFUˠ*OTFSU UIF0UIFS UIF0UIFS UIF0UIFS UIF0UIFS TZNCPM5BCMF

TZNCPM5BCMF       UIF0UIFS UIF0UIFS UIF0UIFS TZNCPM5BCMF UIF0UIFS TZNCPM5BCMF       TPVSDF TPVSDF3FGFSFODFT UBSHFU UBSHFU3FGFSFODFT
42. ### ૬खଆͷ഑ྻʹཁૉ͕͋ΔͳΒɺJOEFYΛม͑Δ.PWFͰ0, 4ZNCPM5BCMF͔͠ࢀরग़དྷͳ͍ͳΒɺ TPVSDFͳΒ%FMFUFɺUBSHFUͳΒ*OTFSU͢Δ͔͠ແ͍ UIF0UIFS UIF0UIFS UIF0UIFS UIF0UIFS TZNCPM5BCMF TZNCPM5BCMF 

     UIF0UIFS UIF0UIFS UIF0UIFS TZNCPM5BCMF UIF0UIFS TZNCPM5BCMF       ˞ແବͳ.PWF͕ൃੜ͢ΔՄೳੑ༗Γ TPVSDF TPVSDF3FGFSFODFT UBSHFU UBSHFU3FGFSFODFT w UIF0UIFSࢀরͷ৔߹.PWF w TZNCPM5BCMFࢀরͷ৔߹TPVSDFˠ%FMFUF UBSHFUˠ*OTFSU
43. ### )FDLFM0WFSWJFX w )BTI5BCMFΛ׆༻ͨ͠ɺཁૉͷJOEFYऔಘ w TPVSDFͱUBSHFUͷ֤ཁૉͷࢀরؔ܎ͷߏங w %FMFUF *OTFSU .PWFʹՃ͑ͯɺ6QEBUFͷऔಘ΋Մೳ w

.ZFSTͱ͸จԽ͕ҟͳΓɺ4&4-\$41SPCMFN͸ؔ܎ͳ͍
44. ### w ར༻෯ͷ޿͞ɾ\$PNQMFYJUZ༏ҐੑͳͲ͕ಛ௃తͳɺ.ZFSTɾ)FDLFMͷ ͭͷݪཧΛղઆɻ͓໾ʹཱͯΕ͹ w IUUQTHJUIVCDPNIPSJUBZVZB%J⒎FSFODF"MHPSJUIN\$PNQBSJTPO w 0 / ͷ\$PNQMFYJUZΛ࠾༻͍ͯ͠ΔϥΠϒϥϦ΋͋Δ͕ɺ6*͕བྷΉࠩ෼ߋ৽ ϥΠϒϥϦͱͯ͋͑ͯͦ͠ΕΛબͿཧ༝͸༨Γແ͍͔΋ʁ

w બఆΞϧΰϦζϜɾ࣍ݩߋ৽ɾશCBUDI6QEBUF0QFSBUJPOTରԠɾ଎౓ɾFUD 4VNNBSJ[F  IUUQTHJUIVCDPNSB%J⒎FSFODF,JU
45. ### ɾHeckel, Paul (1978), "A Technique for Isolating Differences Between Files”,

Communications of the ACM, 21: 264–268, doi:10.1145/359460.359467, retrieved 2011-12-04 ɾE. W. Myers, ‘‘An O(ND) difference algorithm and its variations,’’ Algorithmica, 1 (1986), pp. 251−266. 3FGFSFODFT