ãããã¸ã«ã«ã½ã¼ã
Problem 79(2) - グラフ理論へ - ボクノス
ã®ç¶ãã§ãã
èªåãä½ã£ãã¢ã«ã´ãªãºã ããããã¸ã«ã«ã½ã¼ãã ã¨è¨ãååã«æ°ã¥ãã¦ãªãã£ãã®ã§ã»ã»ã»ã
ã¨ããã§ãããã¸ã«ã«ã½ã¼ãã£ã¦ä½è ï¼ï¼
æ¨æ¥ãæ°å®¿ã§ã©ã¼ã¡ã³é£ã£ãã
è
¹ãã£ã±ãã«ãªã£ãã®ã§ãæ¸è°·ã®ã¹ã¿ãã§ãè¶ãã¦ã
ãããããããæ°å®¿è¡ãåã«æ± è¢ã®ã¸ã¥ã³ã¯å ã§ããæ¬è¦ã¤ãã¦ãã»ã»ã»ã
ããï¼ä¿ºæ¨æ¥ä½ãã¦ããã ã£ãã»ã»ã»ã¨æãåºããªãã並ã¹ã¦ã¿ãã
- æ°å®¿âã©ã¼ã¡ã³
- ã©ã¼ã¡ã³âæ¸è°·âã¹ã¿ã
- æ± è¢âã¸ã¥ã³ã¯å âã©ã¼ã¡ã³
話ãã¾ã¨ããã¨ã
æ± è¢âã¸ã¥ã³ã¯å âæ°å®¿âã©ã¼ã¡ã³âæ¸è°·âã¹ã¿ã
ã¨ãªãã話ãé·ãã£ããã¹ã²ã¼å¤§å¤ã ã
æç³»åããã©ãã©ã ã£ã話ãä¸æ¬ã®ç·ã«ãã¦è©±ãã¾ã¨ãããããããããã¸ã«ã«ã½ã¼ãã¨ãããããã
Tarjanã®ã¢ã«ã´ãªãºã
ååä½ã£ãã®ãããããã¸ã«ã«ã½ã¼ãã®å®è£ ã®ä¸ã¤ããããã©ãç¡é§ãªé¨åãããã®ã§ã
ããã¡ãã£ã¨é ã®è¯ããã㪠Tarjanã®ã¢ã«ã´ãªãºã ã試ãã¦ã¿ã¾ãã
ãã¡ãããããã¨ã¯ã¹ã²ã¼åç´ã
é©å½ãªç¹ããæ·±ãåªå æ¤ç´¢ãã¾ãã
Aããå§ãã¦ãä¸çªæ·±ãã¨ãFã¸å°éãããããFãããã·ã¥ãå·¡ã£ãFã«ã¯ãã§ãã¯ãå ãã¦ããã¾ãã
æ·±ãåªå ãªã®ã§ãEãããã·ã¥ãD,C,Bã¨å·¡ãã¨ããä¸æè°ããããã¸ã«ã«ã½ã¼ãåºæ¥ã¡ãã£ãã
å ¨ã¦ã®ç¹ããã§ãã¯ããã°ãªã±ã¼ãO(é ç¹æ°+辺ã®æ°)ã§å®äºã§ãã
ãããã¸ã«ã«å¯è½ãªæ£ããã°ã©ããªããã©ã®ç¹ããã¹ã¿ã¼ããã¦ãå¿
ãæ«å°¾ã«è¾¿ãã¤ãã¯ãã ããæ«å°¾ããã½ã¼ãããããdfsã¹ã²ã¼ã
å®è£
ã¨ãããã¨ã§ã³ã¼ãã£ã³ã°ã
(define *graph* '((a b d) (b d c) (c d e f) (d e) (e f) (f))) (define (tsort g) (letrec ((acc '()) (iter (lambda (vec) (for-each (lambda (v) (if (not (member v acc)) (iter v))) (cdr (assq vec g))) (if (not (member vec acc)) (set! acc (cons vec acc)))))) (for-each (lambda (n) (iter (car n))) g) acc)) (tsort *graph*) ; (a b c d e f) (tsort (reverse *graph*)) ; (a b c d e f)
ãã ãã·ã³ãã«ï¼ï¼
(ã¨ã©ã¼ãã§ãã¯ã¨ããã¦ãªããã©)
éç ´å£ã§æ·±ãåªå
set!ã¨for-eachãã¤ã±ã¦ãªãã®ã§éç ´å£ã§ã
(define (tsort g) (letrec ((iter (lambda (vec acc) (let ((res (fold (lambda (v a) (if (member v a) a (iter v a))) acc (cdr (assq vec g))))) (if (member vec res) res (cons vec res)))))) (fold (lambda (n acc) (iter (car n) acc)) '() g)))
ã¯ãªã¼ã³ã ãã
åè
- acm/icpcまとめサイト - グラフ/深さ優先探索
- å³ãã¹ã²ã¼ãããããããä»åã¯ã¹ãããï¼ã ãã§ãªã±ã¼ã
- Spaghetti Source - トポロジカルソート
- ã¨ã©ã¼ãã§ãã¯ä»ããæ¿ããåèã«ãªãã¾ããã