Skip to content

Latest commit

ย 

History

History
91 lines (74 loc) ยท 2.55 KB

README.md

File metadata and controls

91 lines (74 loc) ยท 2.55 KB

Kruskal Algorithm

๊ฐ„์„  ์ค‘์‹ฌ์œผ๋กœ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ, ๊ฐ„์„ ์ด ์ ์„ ๋•Œ ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•
1. ๊ฐ„์„ ๋“ค์„ ๊ฐ€์ค‘์น˜์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
2. ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ„์„ ์„ ์„ ํƒํ•˜์—ฌ ๊ทธ ๊ฐ„์„ ์ด ์ง€๊ธˆ๊นŒ์ง€ ๋งŒ๋“ค์–ด์ง„ MST์™€ ์‚ฌ์ดํด์„ ํ˜•์„ฑํ•œ๋‹ค๋ฉด ์ œ์™ธํ•˜๊ณ , ์•„๋‹ˆ๋ฉด MST์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
3. ๋ชจ๋“  ๊ฐ„์„ ์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

๊ตฌํ˜„ ์ฝ”๋“œ

๋ฐฑ์ค€ 1197๋ฒˆ ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ

import java.io.*;
import java.util.*;

public class Main {


    static int find(int x, int[] parent) {
        if (parent[x] == x) return x;
        int nx = find(parent[x], parent);
        parent[x] = nx;
        return nx;
    }

    static boolean union(int x, int y, int[] parent) {
        x = find(x, parent);
        y = find(y, parent);
        if (x == y) return false;
        parent[y] = x;
        return true;
    }
    
    static int kruskal(int V, int E, Edge[] edges, int[] parents) {
        int answer = 0, cnt = 0;
        for (int i = 1; i <= V; i++)
            parents[i] = i;

        for (Edge edge: edges) {
            if (!union(edge.from, edge.to, parents)) continue;
            answer += edge.weight;
            cnt++;
            if (cnt == V - 1) return answer;
        }

        return answer;
    }

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

        int V = Integer.parseInt(st.nextToken());
        int E = Integer.parseInt(st.nextToken());
        Edge[] edges = new Edge[E];
        int[] parents = new int[V + 1];
        int A, B, C;

        for (int i = 0; i < E; i++) {
            st = new StringTokenizer(br.readLine());
            A = Integer.parseInt(st.nextToken());
            B = Integer.parseInt(st.nextToken());
            C = Integer.parseInt(st.nextToken());
            edges[i] = new Edge(A, B, C);
        }
        Arrays.sort(edges);

        System.out.println(kruskal(V, E, edges, parents));
        br.close();
    }

    static class Edge implements Comparable<Edge> {
        int from;
        int to;
        int weight;

        Edge(int from, int to, int weight) {
            this.from = from;
            this.to = to;
            this.weight = weight;
        }

        @Override
        public int compareTo(Edge o) {
            return this.weight - o.weight;
        }
    }
}