Skip to content

Latest commit

ย 

History

History
182 lines (127 loc) ยท 8.16 KB

TopologicalSort.md

File metadata and controls

182 lines (127 loc) ยท 8.16 KB

DAG(Directed Acyclic Graph)์™€ ์œ„์ƒ์ •๋ ฌ(Topological Sort)

์œ„์ƒ์ •๋ ฌ์ด๋ž€, ๊ทธ๋ž˜ํ”„๋ฅผ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
์ด ๋•Œ ๊ทธ๋ž˜ํ”„๋Š” DAG ๊ทธ๋ž˜ํ”„์—ฌ์•ผ ํ•˜๊ณ , ์ •๋ ฌ ๊ธฐ์ค€์€ ์ง„์ž… ์ฐจ์ˆ˜์˜ ๋น„๋‚ด๋ฆผ์ฐจ์ˆœ์ด๋‹ค.

๋น„๋‚ด๋ฆผ์ฐจ์ˆœ(nondecreasing order)์ด๋ž€, ๋ง ๊ทธ๋Œ€๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์ด ์•„๋‹Œ ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์˜ค๋ฆ„์ฐจ์ˆœ์ด์ง€๋งŒ ํŠน์ • ์›์†Œ(๊ฐ’์ด ๊ฐ™์€ ์›์†Œ๋“ค)์— ๋Œ€ํ•ด์„œ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์ด ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

DAG ๊ทธ๋ž˜ํ”„, ์ง„์ž… ์ฐจ์ˆ˜์˜ ๋น„๋‚ด๋ฆผ์ฐจ์ˆœ ๋“ฑ ์ด๊ฒŒ ๋ฌด์Šจ ๋ง์ธ์ง€ ์ข€ ๋” ์„ธ์„ธํ•˜๊ฒŒ ์•Œ์•„๋ณด์ž.

DAG(Directed Acyclic Graph) - ์‹ธ์ดํด์ด ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„

  • ๋ง ๊ทธ๋Œ€๋กœ ์‹ธ์ดํด(์ˆœํ™˜)์ด ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ด๋‹ค.
  • ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„ ์ž‘์—…๋“ค์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. (์˜ˆ : ๋Œ€ํ•™ ๊ณผ์ •์˜ ์„ ์ˆ˜๊ณผ๋ชฉ)

DAG ์˜ˆ์‹œ

DAGแ„‹แ…จแ„‰แ…ต

์„ ํ–‰์ž(predecessor)์™€ ํ›„ํ–‰์ž(successor)

DAG์—์„œ ๋…ธ๋“œ A์—์„œ ๋…ธ๋“œ B๋กœ์˜ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•  ๋•Œ,

  • A๋Š” B์˜ ์„ ํ–‰์ž(predecessor)์ด๋‹ค.
  • B๋Š” A์˜ ํ›„ํ–‰์ž(successor)์ด๋‹ค.

์ฆ‰๊ฐ ์„ ํ–‰์ž(immediate predecessor)์™€ ์ฆ‰๊ฐ ํ›„ํ–‰์ž(immediate successor)

DAG์—์„œ ๋…ธ๋“œ A์—์„œ ๋…ธ๋“œ B๋กœ์˜ ์—ฃ์ง€ e๊ฐ€ ์กด์žฌํ•  ๋•Œ,

  • A๋Š” B์˜ ์ฆ‰๊ฐ ์„ ํ–‰์ž(immediate predecessor)์ด๋‹ค.
  • B๋Š” A์˜ ์ฆ‰๊ฐ ํ›„ํ–‰์ž(immediate successor)์ด๋‹ค.

์œ„์ƒ์ •๋ ฌ(Topological Sort)

  • DAG์—์„œ ๊ทธ๋ž˜ํ”„์˜ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๋…ธ๋“œ๋“ค์„ ๋‚˜์—ดํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์ฆ‰, ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ๋ฐฐ์น˜ํ•œ ๊ฒƒ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ผ๋ฐ˜์ ์œผ๋กœ ์œ„์ƒ์ •๋ ฌ์˜ ๊ฒฐ๊ณผ๋Š” ์œ ์ผํ•˜์ง€ ์•Š๋‹ค.

แ„‹แ…ฑแ„‰แ…กแ†ผแ„Œแ…ฅแ†ผแ„…แ…งแ†ฏ

๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์—์„œ ์–ด๋–ค ๋…ธ๋“œ์— ๋“ค์–ด์˜ค๋Š” ์—ฃ์ง€์˜ ์ˆ˜๋ฅผ in-degree, ๋‚˜๊ฐ€๋Š” ์—ฃ์ง€์˜ ์ˆ˜๋ฅผ out-degree๋ผ๊ณ  ํ•œ๋‹ค๊ณ  ํ–ˆ๋‹ค. (๊ทธ๋ž˜ํ”„ ์šฉ์–ด ์ •๋ฆฌ ์ฐธ๊ณ )

์œ„ ๊ทธ๋ฆผ์„ ์‚ดํŽด๋ณด๋ฉด, ๊ฐ ๋…ธ๋“œ๋ณ„๋กœ ์ง„์ž… ์ฐจ์ˆ˜, ์ฆ‰ in-degree ๋ฅผ ๋‚˜์—ดํ•˜๊ณ  ์žˆ๋‹ค.

  • ์ด ๋•Œ in-degree๊ฐ€ 0์ธ ๋…ธ๋“œ(์‹œ์ž‘์ ) ๊ฐ€ ๋ฐ˜๋“œ์‹œ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.
    ์ด ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋ง์€ ์‹ธ์ดํด์ด ์กด์žฌํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ, DAG๊ฐ€ ์•„๋‹ˆ๋‹ค.
  • ์šฐ๋ฆฌ๋Š” ์ด in-degree ๊ฐ’์„ ์ด์šฉํ•ด์„œ ๊ทธ ๊ฐ’์ด 0 -> N ์ธ ์ˆœ์œผ๋กœ ํƒ์ƒ‰ํ•  ๊ฒƒ์ด๋‹ค.
    ์™œ๋ƒํ•˜๋ฉด "๊ทธ๋ž˜ํ”„๋ฅผ ์ง„์ž… ์ฐจ์ˆ˜์˜ ๋น„๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ"ํ•  ๊ฒƒ์ด๋‹ˆ๊นŒ !!
  • ํƒ์ƒ‰ ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด์„œ in-degree ๊ฐ’์ด 0์ธ ๋…ธ๋“œ๋“ค์€ ์ œ๊ฑฐํ•ด๋‚˜๊ฐˆ ๊ฒƒ์ด๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ๋…ธ๋“œ๋“ค์€ ์ ์  0์œผ๋กœ ์ˆ˜์ •ํ•œ ๋’ค ์ œ๊ฑฐํ•ด๊ฐ€๋ฉฐ ์ •๋ ฌ์„ ํ•ด๋‚˜๊ฐˆ ๊ฒƒ์ด๋‹ค.

์œ„ ์›๋ฆฌ๋ฅผ ์ดํ•ดํ•˜๊ณ  ๊ทธ๋ฆผ์„ ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ์˜ ๊ณผ์ •์„ ๊ฑฐ์ณ์„œ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•ด๋‚ด๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

  1. in-degree๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค. 1์ด๋‹ค.
  2. ๋…ธ๋“œ 1์„ ์ œ๊ฑฐํ•˜๊ณ , ๋…ธ๋“œ 1์˜ ์ฆ‰๊ฐ ํ›„ํ–‰์ž(immediate successor)๋“ค์ธ ๋…ธ๋“œ 2, 3, 4์˜ in-degree๋ฅผ 1์”ฉ ๊ฐ์†Œํ•œ๋‹ค.
  3. in-degree๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค. 3์ด๋‹ค.
  4. ๋…ธ๋“œ 3์„ ์ œ๊ฑฐํ•˜๊ณ , ๋…ธ๋“œ 3์˜ ์ฆ‰๊ฐ ํ›„ํ–‰์ž์ธ ๋…ธ๋“œ 4์˜ in-degree๋ฅผ 1 ๊ฐ์†Œํ•œ๋‹ค.
  5. in-degree๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค. 4์ด๋‹ค.
  6. ๋…ธ๋“œ 4๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋…ธ๋“œ 4์˜ ์ฆ‰๊ฐ ํ›„ํ–‰์ž๋“ค์ธ ๋…ธ๋“œ 2, 5์˜ in-degree๋ฅผ 1 ๊ฐ์†Œํ•œ๋‹ค.
  7. in-degree๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค. 2์ด๋‹ค.
  8. ๋…ธ๋“œ 2๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋…ธ๋“œ 2์˜ ์ฆ‰๊ฐ ํ›„ํ–‰์ž์ธ ๋…ธ๋“œ 5์˜ in-degree๋ฅผ 1 ๊ฐ์†Œํ•œ๋‹ค.
  9. in-degree๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค. 5์ด๋‹ค.
  10. ๋…ธ๋“œ 5๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค. ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์ œ๊ฑฐ๋˜์—ˆ๋‹ค.
  11. ์ œ๊ฑฐํ•œ ๋…ธ๋“œ๋“ค์„ ์ฐจ๋ก€๋กœ ๋ณด๋ฉด, ๋…ธ๋“œ 1 > 3 > 4 > 2 > 5 ์ด๋‹ค.

์ด๋ ‡๊ฒŒ ์œ„์ƒ์ •๋ ฌ [1, 3, 4, 2, 5] ๋ฅผ ๋„์ถœํ•˜์˜€๋‹ค. ๊ณผ์ •์„ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ์˜ ๋กœ์ง์„ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜๋ณต (๋…ธ๋“œ๊ฐ€ ๋ชจ๋‘ ์ œ๊ฑฐ๋  ๋•Œ๊นŒ์ง€) {
  1. in-degree๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค.
  2. ๊ทธ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
  3. ๊ทธ ๋…ธ๋“œ์˜ ์ฆ‰๊ฐ ํ›„ํ–‰์ž๋“ค์˜ in-degree๋ฅผ 1์”ฉ ๊ฐ์†Œํ•œ๋‹ค.
}

๊ทธ๋Ÿผ ์ด์ œ ์ด ๋กœ์ง์„ ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„์ง€ ์ƒ๊ฐํ•ด๋ณด์ž.

์œ„์ƒ์ •๋ ฌ์˜ ๊ตฌํ˜„

๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ

์šฐ์„  ๊ทธ๋ž˜ํ”„์˜ ์ •๋ณด๋ฅผ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์šฐ๋ฆฌ์—๊ฒŒ ํ•„์š”ํ•œ ์ •๋ณด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ๊ทธ๋ž˜ํ”„ ๋…ธ๋“œ์˜ ์ •๋ณด
  2. ๊ฐ ๋…ธ๋“œ ๋ณ„ in-degree ๊ฐ’
  3. ๊ฐ ๋…ธ๋“œ์˜ ๋ฐฉํ–ฅ ์—ฃ์ง€๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋…ธ๋“œ๋“ค

์œ„์˜ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ์— ์ ํ•ฉํ•œ ๊ทธ๋ž˜ํ”„ ํ‘œํ˜„ ๋ฐฉ์‹์€ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค.

๊ทธ๋ž˜ํ”„์˜ ์ •๋ณด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฃผ์–ด์ง„๋‹ค๊ณ  ํ•ด๋ณด์ž.

  • N : ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ (๋…ธ๋“œ๋Š” 1 ~ N)
  • M : ์—ฃ์ง€์˜ ๊ฐœ์ˆ˜
  • M๊ฐœ์˜ ๊ด€๊ณ„๋“ค : ๋…ธ๋“œ A -> ๋…ธ๋“œ B

๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๋Š” ๊ทธ๋ž˜ํ”„, in-degree ๊ฐ’ ์ด๋ ‡๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ ๊ฒƒ์ด๋‹ค.

  • ๊ทธ๋ž˜ํ”„ ๋งŒ๋“ค๊ธฐ : ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ๊ฐ ๋…ธ๋“œ ๋ณ„ LinkedList๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • in-degree ๋ฐฐ์—ด : ๊ฐ ์ธ๋ฑ์Šค๋ฅผ ๋…ธ๋“œ ๊ฐ’์œผ๋กœ ๋‘๊ณ , ๋…ธ๋“œ ๋ผ๋ฆฌ์˜ ๊ด€๊ณ„ ๊ฐ’์„ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ๊ทธ ๋…ธ๋“œ์— ๋“ค์–ด์˜จ ๊ฐ’์„ ์˜ฌ๋ ค์ค€๋‹ค.

์•„๋ž˜์˜ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ ํŽธ์˜๋ฅผ ์œ„ํ•ด ์ธ๋ฑ์Šค 0์€ ๋น„์›Œ๋‘์—ˆ๋‹ค.

public class Main {

    static int N, M;
    static LinkedList<Integer>[] graph;
    static int[] indegree;

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        graph = new LinkedList[N + 1];
        for (int i = 1; i <= N; i++) {
            graph[i] = new LinkedList<>();
        }
        indegree = new int[N + 1];

        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            int from = Integer.parseInt(st.nextToken());
            int to = Integer.parseInt(st.nextToken());
            graph[from].add(to);
            indegree[to] ++;
        }

        System.out.println(Arrays.toString(graph));
        System.out.println(Arrays.toString(indegree));

        br.close();
    }
}

๊ทธ๋Ÿฌ๋ฉด ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐ’์ด ์ €์žฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

image

์œ„์ƒ์ •๋ ฌ ๊ตฌํ•˜๊ธฐ

์ž, ๊ทธ๋ž˜ํ”„๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์œผ๋ฏ€๋กœ ์ด์ œ ์•ž์—์„œ ์ •์˜ํ•œ ๋กœ์ง์„ ๊ตฌํ˜„ํ•  ์ฐจ๋ก€์ด๋‹ค. ๋‹ค์‹œ ํ•œ ๋ฒˆ ์‚ดํŽด๋ณด์ž.

๋ฐ˜๋ณต (๋…ธ๋“œ๊ฐ€ ๋ชจ๋‘ ์ œ๊ฑฐ๋  ๋•Œ๊นŒ์ง€) {
  1. in-degree๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค. (์ด ๋…ธ๋“œ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์ผ ์ˆ˜ ์žˆ๋‹ค.)
  2. ๊ทธ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
  3. ๊ทธ ๋…ธ๋“œ์˜ ์ฆ‰๊ฐ ํ›„ํ–‰์ž๋“ค์˜ in-degree๋ฅผ 1์”ฉ ๊ฐ์†Œํ•œ๋‹ค.
}

์œ„ ๋กœ์ง์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ณด์•˜์„ ๋•Œ, ์šฐ์„  in-degree๊ฐ€ 0์ธ ๋…ธ๋“œ๋“ค์„ ์ €์žฅํ•  ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ๊ณต๊ฐ„์„ zeros ๋ผ๊ณ  ์นญํ•ด๋ณด๊ฒ ๋‹ค. ์ด ๋•Œ zeros๋Š” ์–ด๋–ค ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„๊นŒ?

์šฐ๋ฆฌ๋Š” ๋จผ์ € in-degree๊ฐ€ 0์ธ ๋…ธ๋“œ๋“ค์„ ์™€๋ฅด๋ฅด ๊ฒ€์‚ฌ ํ•˜๊ณ  -> zeros์— ์ฐจ๊ณก์ฐจ๊ณก ์Œ“์€ ๋‹ค์Œ -> ๋งจ ์•ž์˜ ๋…ธ๋“œ๋ถ€ํ„ฐ ๊บผ๋‚ด์™€์„œ -> ๊ฐ ๋…ธ๋“œ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋‹ค์Œ ๋…ธ๋“œ๋“ค์„ ์ฐพ๊ณ  -> ๊ทธ ๋…ธ๋“œ๋“ค์˜ in-degree ๊ฐ’์„ -1์”ฉ ์—…๋ฐ์ดํŠธ ํ•˜๊ณ  -> ๊ฐ’์ด 0์ด ๋˜์—ˆ์„ ๋•Œ๋Š” ๋˜ ๊ทธ ์•„์ด๋“ค์„ zeros์— ๋„ฃ๊ณ  ....

์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•  ๊ฒƒ์ด๋‹ค. ์ฆ‰ zeros๋Š” ๋…ธ๋“œ ์ €์žฅ ํ›„ ์ฒ˜์Œ์— ๋“ค์–ด๊ฐ”๋˜ ๊ฒƒ๋ถ€ํ„ฐ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ํ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ฒ ๋‹ค.

Queue<Integer> zeros = new LinkedList<>();

for (int i = 1; i <= N; i++) {
    if (indegree[i] == 0) zeros.offer(i);
}

while (!zeros.isEmpty()) {
    int target = zeros.poll();
    System.out.print(target + " ");

    for (int node : graph[target]) {
        indegree[node] --;
        if (indegree[node] == 0) {
            zeros.offer(node);
        }
    }
}
System.out.println("");

๊ด€๋ จ ๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ