2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)

2 minute read

2020-1ํ•™๊ธฐ, ๋Œ€ํ•™์—์„œ โ€˜์•Œ๊ณ ๋ฆฌ์ฆ˜โ€™ ์ˆ˜์—…์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋ฐ”๋ฅผ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ์ง€์ ์€ ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค :)

๋ฐฉํ–ฅ์„ฑ์ด ์žˆ๋Š” <Directed Graph>๋ฅผ DFS ๋ฐฉ์‹์œผ๋กœ ์ˆœ์œ„ํ•˜๊ฒŒ ๋˜๋ฉด, ์šฐ๋ฆฌ๋Š” ๋ฐฉ๋ฌธ ์ˆœ์„œ์— ๋”ฐ๋ฅธ Traversal Tree๋ฅผ ์–ป๊ฒŒ ๋œ๋‹ค. ์ด ํŠธ๋ฆฌ๋ฅผ ์ด๋ฃจ๋Š” ๊ฐ vertex ๊ทธ๋ฆฌ๊ณ  edge์— ์ด๋ฆ„์„ ๋ถ™์ด๊ณ  ๋ถ„๋ฅ˜๋ฅผ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค!! ํ•œ๋ฒˆ ์‚ดํŽด๋ณด์ž!!

Terminology.

๋จผ์ € vertex์— ๋Œ€ํ•œ ์šฉ์–ด๋ถ€ํ„ฐ ์‚ดํŽด๋ณด์ž. ๊ฐ vertex๋Š” <root>, <descendant>, <ancestor>, <parent>, <child> ๋“ฑ์˜ ์šฉ์–ด๊ฐ€ ์žˆ๋‹ค. ์‰ฌ์šด ๊ฐœ๋…์ด๋ผ ๋”ฐ๋กœ ์„ค๋ช…์„ ํ•˜์ง„ ์•Š๊ฒ ๋‹ค.

๋‹ค์Œ์€ edge ๋“ค์— ๋Œ€ํ•œ ์šฉ์–ด๋ฅผ ์‚ดํŽด๋ณด์ž. ๋จผ์ € ์šฉ์–ด๋ฅผ ์ œ์‹œํ•˜๋ฉด, <tree edge>, <forward edge>, <back edge>, <cross edge>๊ฐ€ ์žˆ๋‹ค.

๋จผ์ € <tree edge>๋Š” ๋ง ๊ทธ๋Œ€๋กœ DFS tree๋ฅผ ์ด๋ฃจ๋Š” edge๋ฅผ ๋งํ•œ๋‹ค.

๋‹ค์Œ์œผ๋กœ <backward edge>๋Š” DFS ์ˆœํšŒ ๊ณผ์ •์—์„œ ํ•ด๋‹น edge๊ฐ€ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ vertex๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” edge ์ผ๋•Œ, <backward edge>๋ผ๊ณ  ํ•œ๋‹ค. <forward edge>์—ญ์‹œ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ vertex๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” edge์ง€๋งŒ, ์ด ๊ฒฝ์šฐ ๊ทธ ๋ฐฉํ–ฅ์ด (ancestor โ†’ descendent) ๋ฐฉํ–ฅ์ด๋‹ค. backward edge๋Š” (ancestor โ† descendent)๋‹ˆ ๋ง ๊ทธ๋Œ€๋กœ backwarding ํ•˜๋Š” edge์ธ ๊ฒƒ์ด๋‹ค!

<cross edge>๋ผ๋Š” ๊ฒƒ๋„ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๊ณตํ†ต๋œ ancestor๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด, ์ด๊ฒƒ์„ <cross edge>๋ผ๊ณ  ํ•œ๋‹ค.

์‚ฌ์‹ค ์•„๋ž˜์™€ ๊ฐ™์€ ๊ณต์‹์ด ์žˆ๊ธด ํ•œ๋ฐ, ๋ณธ์ธ์€ ์ด ๊ณต์‹๋ณด๋‹ค๋Š” (ancestor-descendent) ๊ด€๊ณ„๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ edge๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ํŽธ์ด๋‹ค. ์™œ๋ƒํ•˜๋ฉด, ์•„๋ž˜์˜ ๊ณต์‹์„ ์“ฐ๋ฉด, ๋ณธ์ธ ๊ธฐ์ค€์œผ๋กœ <cross edge>๊ฐ€ well-define ๋˜์ง€ ์•Š๋Š” ๋Š๋‚Œ์ด ๋“ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค;; ๐Ÿค”


์ž ์ด์ œ Directed Graph์—์„œ DFS ํ–ˆ์„ ๋•Œ, ๊ฐ edge๋ฅผ ๋ช…๋ช…ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ดค๋‹ค. ํ•˜์ง€๋งŒ, ์ด๋Ÿฐ ์˜๋ฌธ์ด ๋“ ๋‹ค.

"์™œ vertex์™€ edge์— ์ด๋ฆ„์„ ๋ถ™์˜€๋Š”๊ฐ€?"

๊ทธ์— ๋Œ€ํ•œ ๋‹ต์€ <DAG Directed Acyclic Graph>์— ์žˆ๋‹ค!! ์šฐ๋ฆฌ๋Š” Direct Graph๊ฐ€ cycle์„ ๊ฐ€์ง€๋Š”์ง€ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•ด DFS tree์˜ edge๋“ค์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Property.

A directed graph has a cycle if and only if its DFS reveals a back edge

์ฆ‰, ๋งŒ์•ฝ DFS๋กœ Directed Graph๋ฅผ ์ˆœํšŒํ–ˆ๋Š”๋ฐ, back edge๊ฐ€ ๋ฐœ๊ฒฌ๋œ๋‹ค๋ฉด ์šฐ๋ฆฌ๋Š” ๊ทธ Directed Graph๊ฐ€ DAG๊ฐ€ ์•„๋‹˜์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค!!

DFS๋Š” DAG๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!! DAG๋Š” <linearization>์ด๋ผ๋Š” ๊ณผ์ •์„ ํ†ตํ•ด DAG๋ฅผ ์„ ํ˜•์˜ sequence๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค!! ์ด๋•Œ, ์ด ์„ ํ˜•์˜ sequence๋ฅผ DAG๋ฅผ DFS๋กœ ์ˆœํšŒํ•˜๋Š” ์ˆœ์„œ๋กœ ์ƒ์„ฑํ•œ๋‹ค๋ฉด, ์šฐ๋ฆฌ๋Š” ์ด seq.๋ฅผ ๋ณด๊ณ  DAG๋ฅผ ๋ณต์›ํ•  ์ˆ˜ ์žˆ๋‹ค.

Categories:

Updated: