์•Œ๊ณ ๋ฆฌ์ฆ˜

[์•Œ๊ณ ๋ฆฌ์ฆ˜]์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ: kruskal,prim์•Œ๊ณ ๋ฆฌ์ฆ˜ - Java๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

stonesy 2024. 2. 24. 14:38
728x90

๐Ÿ“–์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ

์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ๋ž€ 1)๋ชจ๋“  ์ •์ ์„ ํฌํ•จํ•˜๋ฉด์„œ 2)๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ตœ์†Œ์ธ ํŠธ๋ฆฌ๋ฅผ ๋งํ•œ๋‹ค.

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

 

๐Ÿ”Kruskal ์•Œ๊ณ ๋ฆฌ์ฆ˜

๊ฐ„์„  ์ค‘์‹ฌ → edgeList๊ฐ€ ํ•„์š”

1. ์ตœ์ดˆ ๋ชจ๋“  ๊ฐ„์„ ์„ ๊ฐ€์ค‘์น˜์— ๋”ฐ๋ผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ

2. ๊ฐ€์ค‘์น˜๊ฐ€ ๋‚ฎ์€ ๊ฐ„์„ ๋ถ€ํ„ฐ ์„ ํƒํ•˜๋ฉด์„œ ํŠธ๋ฆฌ๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. ์ด๋•Œ, ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋ฉด ๋‚จ์•„ ์žˆ๋Š” ๊ฐ„์„  ์ค‘ ๊ทธ ๋‹ค์Œ์œผ๋กœ ๊ฐ€์ค‘์น˜๊ฐ€ ๋‚ฎ์€ ์„ ํƒ

=>N-1๊ฐœ์˜ ๊ฐ„์„ ์ด ์„ ํƒ๋  ๋•Œ๊นŒ์ง€ 2๋ฐ˜๋ณต

*์„œ๋กœ์†Œ ์ง‘ํ•ฉ(Disjoint-set)์„ ์ด์šฉํ•œ Kruskal ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌํ˜„

์„œ๋กœ์†Œ ์ง‘ํ•ฉ(Disjoint-set)์€ ์ค‘๋ณต ํฌํ•จ๋œ ์›์†Œ๊ฐ€ ์—†๋Š” ์ง‘ํ•ฉ์„ ์˜๋ฏธํ•œ๋‹ค. 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

//๋ฉ”๋ชจ๋ฆฌ:49648KB, ์‹œ๊ฐ„:632ms
public class Main_1197_G4_์ตœ์†Œ์ŠคํŒจ๋‹ํŠธ๋ฆฌ_KRUSKAL_์กฐ์„œ์˜ {
    static int V; //์ •์ ์˜ ๊ฐœ์ˆ˜
    static int E; //๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜
    static Edge[] edgeList; //๊ฐ„์„ ๋ฆฌ์ŠคํŠธ

    ///์„œ๋กœ์†Œ์ง‘ํ•ฉ/////////////////////
    static int[] parents; //์„œ๋กœ์†Œ ์ง‘ํ•ฉ

    public static void make() {
        parents = new int[V+1];
        for(int i=1;i<V+1;i++){
            parents[i] = i;
        }
    }
    public static int find(int a){
        if(a==parents[a]) return a;
        return parents[a]=find(parents[a]);
    }
    public static boolean union(int a, int b){
        int aRoot = find(a);
        int bRoot = find(b);
        if(aRoot==bRoot) return false; //ํŠธ๋ฆฌ๋ฅผ ํ•ฉ์น  ์ˆ˜ ์—†์Œ
        parents[bRoot] = aRoot;
        return true;
    }
    ///////////////////////////////

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

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

        @Override
        public int compareTo(Edge o) {
            return Integer.compare(this.weight, o.weight);
        }
    }
    public static int MSTKruskal(){
        make();

        Arrays.sort(edgeList);

        int result = 0;
        int cnt = 0;
        for(Edge edge : edgeList){
            if(!union(edge.from,edge.to)) continue;
            result += edge.weight;
            if(++cnt==V-1) break; //์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ ์™„์„ฑ
        }

        return cnt==V-1?result:-1;
    }

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

        V = Integer.parseInt(st.nextToken());
        E = Integer.parseInt(st.nextToken());
        edgeList = new Edge[E];

        for(int i=0;i<E;i++){
            st = new StringTokenizer(br.readLine());
            int from = Integer.parseInt(st.nextToken());
            int to = Integer.parseInt(st.nextToken());
            int weight = Integer.parseInt(st.nextToken());
            edgeList[i]=new Edge(from,to,weight);
        }

        System.out.println(MSTKruskal());
    }
}

 

๐Ÿ” Prim ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ •์  ์ค‘์‹ฌ → visited[]์„ ์ด์šฉํ•ด์„œ ์ •์  ๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ check

1. ์ž„์˜ ์ •์ ์„ ํ•˜๋‚˜ ์„ ํƒํ•ด์„œ ์‹œ์ž‘

2. ์„ ํƒํ•œ ์ •์ ๊ณผ ์ธ์ ‘ํ•˜๋Š” ์ •์ ๋“ค ์ค‘ ์ตœ์†Œ ๋น„์šฉ์˜ ๊ฐ„์„ ์ด ์กด์žฌํ•˜๋Š” ์ •์ ์„ ์„ ํƒ

=>๋ชจ๋“  ์ •์ ์ด ์„ ํƒ๋  ๋•Œ๊นŒ์ง€ 2๊ณผ์ •์„ ๋ฐ˜๋ณต

*์ธ์ ‘ํ–‰๋ ฌ(adjMatrix)์„ ์ด์šฉํ•œ Prim ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌํ˜„

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main_1197_G4_์ตœ์†Œ์ŠคํŒจ๋‹ํŠธ๋ฆฌ_PRIM_์กฐ์„œ์˜ {
    static int V; //์ •์ ์˜ ๊ฐœ์ˆ˜
    static int E; //๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜
    static int[][] adjMatrix; //์ธ์ ‘ํ–‰๋ ฌ
    static boolean[] visited; //์ •์  ๋ฐฉ๋ฌธ์—ฌ๋ถ€ check
    static int[] minEdge;

    public static int MSTPrim(){
        visited = new boolean[V+1];
        minEdge = new int[V+1];
        Arrays.fill(minEdge,Integer.MAX_VALUE);

        int result = 0;
        minEdge[1]=0;
        int c=0;
        for(c=0;c<V-1;c++){
            int min = Integer.MAX_VALUE;
            int minVertex = -1;

            for(int i=1;i<V+1;i++){
                if(!visited[i] && minEdge[i]<min){
                    min=minEdge[i];
                    minVertex=-1;
                }
            }

            if(minVertex==-1) break; //ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
            result+=min;
            visited[minVertex]=true;

            for(int i=1;i<V+1;i++){
                if(!visited[i] && adjMatrix[minVertex][i]!=0
                    &&adjMatrix[minVertex][i]<minEdge[i]){
                    minEdge[i]=adjMatrix[minVertex][i];
                }
            }
        }

        return c==V-1 ? result : -1;
    }

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

        V = Integer.parseInt(st.nextToken());
        E = Integer.parseInt(st.nextToken());
        adjMatrix = new int[V+1][V+1];

        for(int i=0;i<E;i++){
            st = new StringTokenizer(br.readLine());
            int from = Integer.parseInt(st.nextToken());
            int to = Integer.parseInt(st.nextToken());
            int weight = Integer.parseInt(st.nextToken());
            adjMatrix[from][to] = weight;
        }

        System.out.println(MSTPrim());
    }
}

๊ทธ๋Ÿฐ๋ฐ ์ธ์ ‘ํ–‰๋ ฌ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ ์ธ์ ‘ํ–‰๋ ฌ ์ž์ฒด๊ฐ€ 

 

 

๊ฐ„์„ ์ด ์ ์€ ํฌ์†Œํ–‰๋ ฌ์ธ ๊ฒฝ์šฐ์—๋Š” Kruskal ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด, ๊ฐ„์„ ์ด ๋งŽ์€ ๋ฐ€์ง‘ํ–‰๋ ฌ์ธ ๊ฒฝ์šฐ์—๋Š” Prim ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์œ ๋ฆฌํ•˜๋‹ค.

 

*๋ฐฑ์ค€: ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ

https://www.acmicpc.net/problem/1197

 

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

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ V(1 ≤ V ≤ 10,000)์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E(1 ≤ E ≤ 100,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ E๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ ์ •์ˆ˜ A, B, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” A๋ฒˆ ์ •์ ๊ณผ B๋ฒˆ ์ •์ ์ด

www.acmicpc.net

 

728x90