ã¯ããã«
ãããã«ã¡ã¯ãhirataraã§ãã
ãååã¾ã§ã®é£è¼ã§ãæ£è¦è¡¨ç¾ããNFAãæ§ç¯ããé¨åã¾ã§ãä½æãã¾ãããä»åã¯ãNFAãDFAã«å¤æããé¨åéåæ§ææ³ã解説ããä»ã¾ã§ä½ã£ãé¨åãçµã¿åããã¦æ£è¦è¡¨ç¾ã¨ã³ã¸ã³ãå®æããã¾ãã
対象èªè
- æ£è¦è¡¨ç¾ããã£ã¨ç¥ãããæ¹
- æ å ±ç§å¦åéã«èå³ãããæ¹
- æ£è¦è¡¨ç¾ã¨ã³ã¸ã³ãå®è£ ããå¿ è¦ãããæ¹
é¨åéåæ§ææ³
ãNFAãDFAã«å¤æããã«ã¯ãé¨åéåæ§ææ³ï¼subset constructionï¼ã¨å¼ã°ããæ¹æ³ãå©ç¨ãã¾ãã
ã第1åã§èª¬æããéããNFAã¯1ã¤ã®æåã®å ¥åã«ãã£ã¦ä¸æã«ç¶æ ã決ã¾ããªãã®ãç¹å¾´ã§ããã'a'ãå ¥åããã¨ã1ã«ãªããããããªããã2ã«ãªãããããã¾ãããããã¦ãNFAã®ã·ãã¥ã¬ã¼ãæ¹æ³ã«ã¯ãããã¯ãã©ãã¯ãå©ç¨ããæ·±ãåªå ã®æ¹æ³ã¨ãããä¸ã¤ãå¹ åªå ã®æ¹æ³ããããã¨ãç´¹ä»ãã¾ãããå¹ åªå ã®åä½ã§ã¯ãå ¥åã«ããé·ç§»ã®å ¨ã¦ã®å¯è½æ§ãéåã¨ãã¦ä¿æããªããæååã®å ¥åãå¦çãã¾ãã以åå©ç¨ããã以ä¸ã®é·ç§»å³ãè¦ãªããèãã¦ã¿ã¾ãããã
ãã¾ãããã®NFAã®åæç¶æ 㯠0 ã§ãã®ã§ãæåã«ã¨ãããç¶æ ã®éåã¯{ 0 } ã¨ãªãã¾ããããã¦ãã®ç¶æ ã§'a'ãå ¥åããã¨ã次ã«ã¨ãããç¶æ ã¯{0, 1, 2}ã§ãï¼εã¯ç©ºæåãªã®ã§ï¼å ¥åããªãã¦ãç¶æ 2ããç¶æ 0ã¸é·ç§»ãã¾ãï¼ãããã«è¦ã¦è¡ãã¨ã'b'ãå ¥åãããã¨{ 0, 2 }ã次ã«'a'ãå ¥åãããã¨{0, 1, 2} ... ã¨è¨ã£ãæãã§ç¶æ ã®éåãã©ãã©ãé·ç§»ãã¦ããã®ãåããã¾ãã
ããã®å¹ åªå ã®åä½ã注ææ·±ã観測ãã¦ã¿ã¦åãããã¨ã¯ãå ¥åã«å¯¾ãã¦æ¬¡ã«é·ç§»ããããç¶æ ãã¯è¤æ°ããããã§ããããåãããç¶æ ã®éåãã¯ä¸æã«å®ã¾ã£ã¦ããã¨è¨ããã¨ã§ããä¾ãã°æåã«'a'ãå ¥åããé¨åãè¦ãã¨ã次ã«é·ç§»ããç¶æ ã¯0ã«ãªããã1ã«ãªããã2ã«ãªããããããå®ã¾ãã¾ããããåãããç¶æ ã®éåã¯{0,1,2}ã¨ä¸ã¤ã«å®ã¾ãã¾ãããã®ãä¸ã¤ã«å®ã¾ããã¨è¨ãã®ãDFAã®æ§è³ªã¨ãã¾ãåè´ãã¦ãã¾ãã
ããã®ãããªèãæ¹ãå ã«ãã¦ãNFAã®ãåãããç¶æ ã®éåããDFAã®ãç¶æ ãã¨ãã¦NFAããDFAãä½æããæ¹æ³ããé¨åéåæ§ææ³ï¼subset constructionï¼ã¨å¼ã³ã¾ãã
é¨åéåæ§ææ³ã§DFAãæ§ç¯ãã
ãããã§ã¯ãå®éã«NFAããDFAãä½ã£ã¦ã¿ã¾ããããé¨åéåæ§ææ³ã«ãã£ã¦DFAãæ§æããå ´åãDFAã®5ã¤ã®è¦ç´ ã¯ãå ã¨ãªãNFAã使ã£ã¦ä»¥ä¸ã®ããã«æ§ç¯ããã¾ãã
DFAã®è¦ç´ | æ§ç¯æ¹æ³ |
ç¶æ ã®éå | NFAã®ãç¶æ ã®éåãã®ããéå |
æåã®éå | NFAã®ãæåã®éåã |
é·ç§»é¢æ° | NFAã®ãé·ç§»é¢æ°ãããä½æï¼å¾è¿°ï¼ |
éå§ç¶æ | NFAã®ãéå§ç¶æ ãã¨ç©ºæåï¼εï¼ã ãã§é·ç§»ã§ããNFAã®ãç¶æ ããããªãããã®DFAã®ãç¶æ ã |
åçç¶æ ã®éå | NFAã®ãåçç¶æ ãã1ã¤ä»¥ä¸å«ãããã®DFAã®ãç¶æ ãã®éå |
ãå ã»ã©ã®NFAãããã®æ¹æ³ã«ãã£ã¦DFAãæ§æããã¨ãå³ã®ããã«ãªãã¾ãã以éããã®å³ãè¦ãªãã説æãèªãã¨ããããããã¨æãã¾ãã
DFAã®ãç¶æ ã
ãç¶æ ã®éåã®æ§ç¯æ¹æ³ã®ä¸ã§ãããéåã¨è¨ãè¨èãåºã¦ãã¾ãããããã¯æ°å¦ã®éåè«ã®æ¦å¿µã§ãã念ã®ããç°¡åã«èª¬æãã¾ãã
ããããéåã®ããéåãã¨ã¯ããã®éåã®é¨åéåã®éåã®ãã¨ã§ããä¾ãã°ã{1, 2, 3} ã®ããéå㯠{{}, {1}, {2}, {3}, {1, 2}, {2, 3}, {3, 1}, {1, 2, 3}} ã¨ãªãã¾ããPythonã®ä¸çã§è¨ãã°ãfrozensetãè¦ç´ ã¨ããfrozensetãã¤ã¾ããå ¥ãåã«ããfrozensetã¨è¨ããã¨ã«ãªãã¾ãã
ãããããéåã«ããå®ç¾©ãããåãããªããã°ãåç´ã«ãDFAã®ç¶æ
ã¯ãNFAã®ç¶æ
ã®éåãã¨èããã¨åãããããã§ããã¤ã¾ããä»ã¾ã§NFAã§ã¯ã1ã2ãªã©ã®æ´æ°å¤ãç¶æ
ã¨å¼ãã§ãã¾ããããDFAã®ä¸çã§ã¯ä»£ããã«ãfrozenset([1, 2])
ãfrozenset([3, 4, 5])
ã®ãããªfrozensetåãç¶æ
ã¨å¼ã¶ã¨è¨ããã¨ã§ããç¶æ
é·ç§»å³ã®ä¸çã§è¨ãã°ãNFAã§ã¯ä¸¸ã®ä¸ã«1ã2ãæ¸ãã¦ããã®ããDFAã§ã¯ãã®ä»£ããã«1,2ã3,4,5ã®ããã«è¤æ°ã®æ°åãæ¸ãã¨è¨ããã¨ã«è©²å½ãã¾ãã
ããããªé¢¨ã«DFAã®ãç¶æ ã®éåããå®ãããã¨ã§ãNFAä¸ã§ç¶æ 2ãç¶æ 4ãåããããã¨è¨ããã¨ãDFAä¸ã§ç¶æ {2, 4}ã§ãããã¨è¨ãæãããã¨ãã§ããããã«ãªãã¾ããã¾ããNFAä¸ã§åãããç¶æ ããªããã¨ããDFAä¸ã§ã¯ç¶æ {}ï¼ç©ºéå:è¦ç´ ã®å ¥ã£ã¦ãªãéåï¼ã§è¡¨ããã¨ãã§ãã¾ãã
DFAã®ãé·ç§»é¢æ°ã
ã次ã«ãé·ç§»é¢æ°ã®æ§ç¯ã«ã¤ãã¦è§£èª¬ãã¾ãã第2åã§èª¬æããéããDFAã¨NFAã§ã¯é·ç§»é¢æ°ã®å¼æ°ã¨æ»ãå¤ãå¾®å¦ã«éãã¾ããå ãã¦å è¿°ã®ã¨ãããDFAã¨NFAã§ã¯ç¶æ ã表ãåãæ´æ°åã¨frozensetåã¨è¨ãããã«ç°ãªã£ã¦ãã¾ããé·ç§»é¢æ°ã®æ§ç¯ã«ããã¦ããã®2ã¤ã®éããååã«æ³¨æããå¿ è¦ãããã¾ãã以ä¸ã«ã¾ã¨ãã¦ããã¾ãã®ã§ãåèã«ãã¦ä¸ããã
ã | å¼æ° | æ»ãå¤ |
NFA | ä»ã®ç¶æ ï¼æ´æ°åï¼, æå or 空æåï¼εï¼ | 次ã®ç¶æ ã®éåï¼frozensetåï¼ |
DFA | ä»ã®ç¶æ ï¼frozensetåï¼, æå | 次ã®ç¶æ ï¼frozensetåï¼ |
ãããã§ã¯ãDFAã®é·ç§»é¢æ°ãã©ãããã¹ããèãã¦ã¿ã¾ããããDFAä¸ã®ãç¶æ ãã¯ãNFAä¸ã§ã®ãåãããç¶æ ã®éåãã§ããããªã®ã§ãDFAä¸ã®ãç¶æ ï¼frozensetåï¼ãã«å«ã¾ããNFAä¸ã®ããããã®ãç¶æ ï¼æ´æ°åï¼ããNFAã®é·ç§»é¢æ°ã§ç§»ããããããå ¨é¨éãããã®ã«è©²å½ããDFAä¸ã®ãç¶æ ï¼frozensetåï¼ãããDFAã®é·ç§»é¢æ°ã®æ»ãå¤ã¨ãªãããã§ãã
ããã£ã¨å ·ä½çã«ãDFAã®é·ç§»é¢æ°ã®ç«å ´ã§èãã¦ã¿ã¾ããããDFAã®é·ç§»é¢æ°ã«ã¯ãfrozensetã渡ã£ã¦ãã¾ããããã®ä¸ã«ã¯NFAã®ç¶æ ã§ããæ´æ°åãå ¥ã£ã¦ã¾ããNFAã®ç¶æ é·ç§»é¢æ°ã¯æ´æ°åãå¼æ°ã«ãã¾ãã®ã§ããããã®æ´æ°åãä¸ã¤ãã¤æ¸¡ããã¨ãã§ãã¾ããããã¦ãNFAã®ç¶æ é·ç§»é¢æ°ããã¯æ¬¡ã«ã¨ãããæ´æ°åã®ç¶æ ã®frozensetãæ»ã£ã¦ãã¾ããããããåéåã¨ãã¦éããfrozensetããDFAã®é·ç§»é¢æ°ã®æ»ãå¤ã¨ããã°ãå ã®è¦æ±ãæºããã¾ãã
ããã ããããã ãã§ã¯εï¼ã¤ãã·ãã³ï¼ç©ºæåå""ã®ãã¨ï¼ã®é·ç§»ã表ç¾ã§ãã¾ãããεã¯å ¥åããªãã¦ãé²ãé·ç§»ã§ãã®ã§ãããã表ç¾ããããã«ã¯1æåã®å ¥åã§è¤æ°åé·ç§»é¢æ°ãå¼ã³åºãå¿ è¦ãããã¾ããå ·ä½çã«ã¯ãã¾ãå ¥åããã1æåã§é·ç§»é¢æ°ãå¼ãã å¾ã次ã«ç©ºæååï¼""ï¼ã§é·ç§»é¢æ°ãå¼ã³ã¾ãã空æååã«ããé·ç§»ã¯ä½åº¦ãç¶ãå¯è½æ§ãããã®ã§ãé·ç§»ã§ããªããªãã¾ã§å帰çã«é·ç§»é¢æ°ãå¼ã°ãªããã°ãªãã¾ããã
DFAå¤æã®ããã®éå ·ãå®è£ ãã
ãæ¹éã大ä½è¦ãã¦ãã¾ããã®ã§ãDFAå¤æã«ä½¿ãããã®éå ·ãä½æãã¾ãã以ä¸ã®2ã¤ãä½ã£ã¦ããã¾ãã
epsilon_expand
ã¡ã½ããï¼é·ç§»é¢æ°ã§å©ç¨ï¼NonDisjointSets
ã¯ã©ã¹ï¼åçç¶æ ã§å©ç¨ï¼
epsilon_expandã¡ã½ãã
ãepsilon_expand
ã¡ã½ããã¯NondeterministicFiniteAutomaton
ã¯ã©ã¹ã®ã¡ã½ããã§ãç¶æ
éåãã空æååεã«ããé·ç§»ãç¹°ãè¿ãã¦å°éã§ããç¯å²ã®éåã«åºããã¡ã½ããã§ããåè¿°ããéãããé·ç§»é¢æ°ãã®ä½æã«å¿
è¦ãªå¦çã§ããã¾ãããéå§ç¶æ
ãã®ä½æã«ãå©ç¨ãã¾ãã
def epsilon_expand(self, set_): # 空æåã辿ãã¹ãç¶æ ãéãããã¥ã¼ que = set( set_ ) # 辿ãçµãã£ãç¶æ done = set() while que: # ãã¥ã¼ããåãåºã stat = que.pop() # 空æåã«ãã£ã¦è¾¿ããé·ç§»ã辿ã nexts = self.transition(stat, "") # ãã®ç¶æ ã¯è¾¿ãçµãã£ãã®ã§ãä¿å done.add(stat) # 辿ã£ã¦åºã¦æ¥ãç¶æ ããããã«ç©ºæåã§è¾¿ãã®ã«ããã¥ã¼ã«å± ãã for next_stat in nexts: # 辿ãçµãã£ã¦ãªãè¦ç´ ã ã if not next_stat in done: que.add(next_stat) return frozenset( done )
ããã®ã¡ã½ããã¯ãNFAã®ç¶æ ã®éåãå¼æ°ã«åãã¾ããããã¦ããããã空æåã§è¾¿ããç¯å²ã«ããå ¨ã¦ã®ç¶æ ãéããéåãæ»ãå¤ã¨ãã¦è¿ãã¾ããå¼æ°ãæ»ãå¤ããNFAã®ç¶æ ã®éåããªã®ã§ãæ´æ°åãå«ãfrozensetåã¨ãªãã¾ãã
ãå®è£
ãç°¡åã«èª¬æãã¾ããæåã«ãå¦çåã®ç¶æ
ãéããque
ã¨å¦çæ¸ã®ç¶æ
ãéããdone
ã¨è¨ã2ã¤ã®setåãç¨æãã¦ã¾ããque
ã«å
¥ã£ã¦ããç¶æ
ã«å¯¾ããNFAã®é·ç§»é¢æ°ï¼self.transitionï¼ã«ãã£ã¦ç©ºæåé·ç§»ãè¡ãã¾ããããã¦ãé·ç§»å¾ã®ç¶æ
ã«å¯¾ãã¦ãå帰çã«ç©ºæåã辿ããªããã°ãªããªãã®ã§ããã®æ°ããªç¶æ
ãå
¨ã¦å¾
ã¡è¡åã§ããque
ã«æ¾ãè¾¼ã¿ã¾ãã
ããã ããããã§å¦çæ¸ã®ç¶æ
ãque
ã«å
¥ãã¦ãã¾ãã¨que
ãä¸åã«æ¸ããç¡éã«ã¼ããã¦ãã¾ãã¾ããããã§ãå¦çæ¸ã®ç¶æ
ãåã³que
ã«å
¥ããã¨ã®ãªãããã«ãdone
ã§å¶å¾¡ãã¦ãã¾ãã空æååé·ç§»å¦çãçµãã£ãç¶æ
ã¯done
ã«å
¥ããdone
ã«å
¥ã£ã¦ããç©ãåã³que
ã«å
¥ããã¨ããªãããã«ãã¾ãã