Dominator Tree

C684b752388fae7f64d3a460308ea04b?s=47 knshnb
August 02, 2020

Dominator Tree

C684b752388fae7f64d3a460308ea04b?s=128

knshnb

August 02, 2020
Tweet

Transcript

  1. Dominator Treeೖ໳ knshnb 2020/08/02 1

  2. ͸͡Ίʹ ✴ Dominator Treeͱ͸ʁ ‣ Flowgraphʹରͯ͠Ұҙʹఆ·Δ໦ߏ଄ ‣ ϓϩάϥϜͷσʔλϑϩʔΛղੳ͢Δจ຺Ͱग़ͯ͘Δ ‣ Lengauer

    & Tarjan͕ޮ཰తͳΞϧΰϦζϜΛൃݟ ✴ ࢀߟ ‣ ΦϦδφϧ࿦จ: “A Fast Algorithm for Finding Dominators in a Flowgraph” ‣ sigma͞Μͷϒϩά: http://sigma425.hatenablog.com/ entry/2015/12/25/224053 2
  3. ໨࣍ ✴ ൃද಺༰ ‣ Dominator Tree͕ఆٛͰ͖Δ͜ͱΛஸೡʹ ‣ ͷΞϧΰϦζϜઆ໌ ‣ Lengauer-TarjanͷΞϧΰϦζϜΛ͔Δʔʔʔ֓͘ཁઆ໌

    ✴ Lengauer-TarjanͷΞϧΰϦζϜΛৄ͘͠஌Γ͍ͨํ͸sigma ͞Μͷϒϩά͕͓͢͢ΊͰ͢ ✴ ݩ࿦จ΋͔ͳΓಡΈ΍͍͢ʢূ໌શ෦௥ͬͯͳ͍͚Ͳ…ʣ O(nm) 3
  4. Dominator Treeͷఆٛ 4

  5. ఆٛ1 ✴ Flowgraph: ࢝఺͔Βશͯͷ௖఺ʹ౸ୡՄೳͳ༗޲άϥϑ ‣ ҎԼɺ࢝఺Λ ͱͨ͠FlowgraphΛߟ͑Δ ✴ ௖఺ ͕௖఺

    Λdominate͢Δ: ͔Β ΁ͷ೚ҙͷύε͕ Λ ௨Δʢ ͸ Λdominate͠ɺ ͸ Λdominate͠ͳ͍΋ͷͱ͢ Δʣ ‣ Λdominate͢Δ௖఺ू߹Λ ͷdominatorͱݺͿ r d v r v d r d d d v v 5
  6. Ex.1: ޓ͍ʹdominate͠ͳ͍ ✴ ʮ ͕ Λdominateʯ͔ͭʮ ͕ Λdominateʯ͸͋Γ͑ͳ͍ a b

    b a 6
  7. Ex.1: ޓ͍ʹdominate͠ͳ͍ ✴ ʮ ͕ Λdominateʯ͔ͭʮ ͕ Λdominateʯ͸͋Γ͑ͳ͍ ‣ ূ໌:

    ͔Βద౰ʹdfsͯ͠ɺ ͔ ʹ͸͡Ίͯ౸ୡͨ͠ͱ͖ ͷύεΛߟ͑Δɻͦͷύε্ʹ΋͏ยํͷ௖఺͸ଘࡏ͠ͳ ͍ɺ͢ͳΘͪͲͪΒ͔͸΋͏ยํΛdominate͠ͳ͍ɻ a b b a r a b 7
  8. Ex.2: dominateͷਪҠ཯ ✴ ͕ Λdominate, ͕ Λdominate ͕ Λdominate a

    b b c ⇒ a c 8
  9. Ex.2: dominateͷਪҠ཯ ✴ ͕ Λdominate, ͕ Λdominate ͕ Λdominate ‣

    ূ໌: ೚ҙͷ ύε্ʹ ͕ଘࡏɻͦͷ ্ʹ͸ඞͣ ͕ଘࡏ͢ΔɻΑͬͯ ͸ Λdominate͢Δɻ a b b c ⇒ a c r − c b r − b a a c 9
  10. Ex.3: dominator಺ͷશॱং ✴ ϧʔϓͷͳ͍શͯͷ ύεʹ͓͍ͯɺ ͷdominatorͷݱ ΕΔॱং͸ಉ͡Ͱ͋Δ r − v

    v 10
  11. Ex.3: dominator಺ͷશॱং ✴ ϧʔϓͷͳ͍શͯͷ ύεʹ͓͍ͯɺ ͷdominatorͷݱ ΕΔॱং͸ಉ͡Ͱ͋Δ ‣ ূ໌: Λ

    ͷdominatorͱͯ͠ɺϧʔϓͷͳ͍ύε(1) ͱ(2) ͕ଘࡏ͢ΔͱԾఆ͢Δɻ (1)ΑΓ ͔Β Λ௨Βͣʹ ʹ౸ୡͰ͖Δ͕ɺ͜Εͱ(2)ΑΓ ͕dominatorͰ͋Δ͜ͱʹໃ६ɻ ✴ ͜ΕʹΑͬͯdominatorͳ͍ͷશॱং͕ఆٛͰ͖Δ r − v v a, b v r − a − b − v r − b − a − v b a v a 11
  12. ఆٛ2 ✴ ͷimmediate dominator: ͷdominatorͰ͋ͬͯɺEx.3ͷશ ॱংͰ࠷΋ޙΖͷ΋ͷʢ ʹ͍ۙ΋ͷʣ ‣ ఆٛΑΓɺ Ҏ֎ʹ͍ͭͯҰҙʹଘࡏ͢Δ

    • ͱද͢ ‣ ಉ஋ͳఆٛ: ͷdominatorͰ͋ͬͯͦͷଞͷdominatorશ ͯʹdominate͞ΕΔ΋ͷ v v v r idom(v) v 12
  13. ex.4: ด࿏Λ࣋ͨͳ͍ ✴ ͷͱ͖ͷΈ ͔Β ʹลΛషͬͨάϥϑ͸ด࿏ Λ࣋ͨͳ͍ w = idom(v)

    w v 13
  14. ex.4: ด࿏Λ࣋ͨͳ͍ ✴ ͷͱ͖ͷΈ ͔Β ʹลΛషͬͨάϥϑ͸ด࿏ Λ࣋ͨͳ͍ ‣ ด࿏Λ࣋ͭͱԾఆ͢ΔɻdominateͷਪҠ཯ΑΓɺด࿏಺ ͷ͋Δ௖఺

    ͔ΒลΛͨͲ͍ͬͯ͘͜ͱͰɺ ͕ࣗ਎Λ dominate͍ͯ͠Δ͜ͱʹͳΓໃ६ɻ w = idom(v) w v v v 14
  15. Dominator Tree ✴ ͷͱ͖ͷΈ ͔Β ʹลΛషͬͨάϥϑ͸ɺ Ҏ ֎ͷ਌͕Ұҙʹఆ·Γɺด࿏Λ࣋ͨͳ͍ɻ͜Ε͸ Λࠜͱ͠ ͨࠜ෇͖໦ʹͳΔɻ

    w = idom(v) w v r r 15
  16. Dominator TreeΛٻΊΔ ΞϧΰϦζϜ 16

  17. ۪௚ͳΞϧΰϦζϜ ✴ ͕ Λdominate Λফͨ͠άϥϑͰ ͔Β ʹ౸ୡෆೳ ✴ ͷΞϧΰϦζϜ 1.

    ֤௖఺Λফͨ͠άϥϑͰ ͔ΒdfsͳͲͰ౸ୡ൑ఆ͢Δ͜ ͱͰɺ ௨Γͷdominateؔ܎͕Θ͔Δ 2. ֤௖఺ʹ͍ͭͯɺ1Λ༻͍ͯimmediate dominatorΛٻΊ Δɻ͜Ε͸dominator಺Ͱͦͷଞͷdominatorશͯʹ dominate͞Ε͍ͯΔ΋ͷͰ͋Δ d v ⇔ d r v O(nm) r n2 17
  18. Lengauer-TarjanͷΞϧΰϦζϜ ✴ semidominatorͱ͍͏֓೦Λఆٛͯ͠ɺͦΕʹؔ͢Δ͍͢͝ ੑ࣭Λ࢖͍·͢ ✴ ΞϧΰϦζϜ֓ཁ 1. Λࠜͱ͢Δdfs໦Λ࡞ͬͯ๚Εͨॱʹ൪߸Λ;Δ 2. semidominatorΛٻΊΔʢdfsٯॱʣ

    3. semidominator͔Βimmediate dominatorΛٻΊΔͷʹ ඞཁͳ௖఺ΛٻΊΔʢdfsٯॱʣ 4. Immediate dominatorΛٻΊΔʢdfsॱʣ ✴ ܭࢉྔ͸ ɺ΋ͬͱؤுΔͱ r O(m log(n)) O(mα(m, n)) 18
  19. ࣮૷্ͷ஫ҙ ✴ root͔Β֤௖఺·Ͱͷύε্ͷminΛ؅ཧͰ͖ΔUnionFindΛ ࢖͍·͢ ‣ ৄ͘͠͸http://sigma425.hatenablog.com/entry/ 2015/12/07/185047 ✴ ΋ͱ΋ͱͷ௖఺൪߸͔dfsॱͷ൪߸͔͕ͪ͝Όͪ͝ΌʹͳΓ ͕ͪ

    ‣ ݸਓతʹ͸શ෦൪߸ৼΓ௚ͪ͠Ό͏΄͏͕ϥΫͰͨ͠ ✴ ࣮૷: https://knshnb.github.io/competitive_library/library/src/ Graph/DominatorTree.hpp.html ✴ ݁ߏେมͰͨ͠… 19
  20. ऴΘΓʹ ✴ “The fast algorithm is … but almost as

    easy to program as the other two algorithms, …” ‣Lengauer͞ΜTarjan͞Μਖ਼ؾ ͔ʁʁʁ 20