๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[์ด๊ฒƒ์ด ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with Python]_35_ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณธ๋ฌธ

๐Ÿฆฅ ์ฝ”ํ…Œ/์ด๊ฒƒ์ด ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with python

[์ด๊ฒƒ์ด ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with Python]_35_ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ง•์ง•์•ŒํŒŒ์นด 2022. 2. 13. 00:35
728x90
๋ฐ˜์‘ํ˜•

220213 ์ž‘์„ฑ

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” ใ€Ž์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹คใ€ ์˜ youtube๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค>

https://www.youtube.com/watch?v=Gj7s-Nrt1xE&list=PLVsNizTWUw7H9_of5YCB0FmsSc-K44y81&index=35 

 

 

 

 

 

 

 

1. ์‹ ์žฅํŠธ๋ฆฌ

: ๊ทธ๋ž˜ํ”„์—์„œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํฌํ•จํ•˜๋ฉด์„œ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„ ๊ทธ๋ž˜ํ”„

: ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์„œ๋กœ ์—ฐ๊ฒฐ๋˜๋ฉด์„œ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์กฐ๊ฑด์€ ํŠธ๋ฆฌ์œผ ใ…ฃ์กฐ๊ฑด!

 

 

 

2. ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ

: ์ตœ์†Œํ•œ์˜ ๋น„์šฉ์œผ๋กœ ๊ตฌ์„ฑ๋˜๋Š” ์‹ ์žฅ ํŠธ๋ฆฌ ์ฐพ๊ธฐ!

ex) N๊ฐœ์˜ ๋„์‹œ๊ฐ€ ์กด์žฌํ•˜๋Š” ์ƒํ™ฉ์—์„œ ๋‘ ๋„์‹œ ์‚ฌ์ด์— ๋„๋กœ๋ฅผ ๋†“์•„ ์ „์ฒด ๋„์‹œ๊ฐ€ ์„œ๋กœ ์—ฐ๊ฒฐ๋  ์ˆ˜ ์žˆ๊ฒŒ ๋„๋กœ๋ฅผ ์„ค์น˜

๋‘ ๋„์‹œ A, B ์„ ํƒํ–ˆ์„ ๋•Œ A์—์„œ B๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ๋ฐ˜๋“œ์‹œ ์กด์žฌํ•˜๋„๋ก ๋„๋กœ ์„ค์น˜

 

 

 

 

 

3. ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

: ๋Œ€ํ‘œ์ ์ธ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ

: ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜

1) ๊ฐ„์„  ๋ฐ์ดํ„ฐ๋ฅผ ๋น„์šฉ์— ๋”ฐ๋ผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

2) ๊ฐ„์„ ์„ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•˜๋ฉฐ ํ˜„์žฌ์˜ ๊ฐ„์„ ์ด ์‚ฌ์ดํด์„ ๋ฐœ์ƒํ•˜๋Š”์ง€ ํ™•์ธ

- ์‚ฌ์ดํด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ํฌํ•จ

- ์‚ฌ์ดํด ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ํฌํ•จ

3) ๋ชจ๋“  ๊ฐ„์„ ์— ๋Œ€ํ•˜์—ฌ 2๋ฒˆ ๊ณผ์ • ๋ฐ˜๋ณต

 

=> ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฐ„์„ ์˜ ๋น„์šฉ๋งŒ ๋ชจ๋‘ ๋”ํ•˜๋ฉด, ๊ทธ ๊ฐ’์ด ์ตœ์ข… ๋น„์šฉ์— ํ•ด๋‹น!

  • python
# ํŠน์ • ์›์†Œ๊ฐ€ ์†ํ•œ ์ง‘ํ•ฉ์„ ์ฐพ๊ธฐ
def find_parent(parent, x):
    # ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด, ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœ
    if parent[x] != x:
        parent[x] = find_parent(parent, parent[x])
    return parent[x]

# ๋‘ ์›์†Œ๊ฐ€ ์†ํ•œ ์ง‘ํ•ฉ์„ ํ•ฉ์น˜๊ธฐ
def union_parent(parent, a, b):
    a = find_parent(parent, a)
    b = find_parent(parent, b)
    if a < b:
        parent[b] = a
    else:
        parent[a] = b

# ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜์™€ ๊ฐ„์„ (Union ์—ฐ์‚ฐ)์˜ ๊ฐœ์ˆ˜ ์ž…๋ ฅ ๋ฐ›๊ธฐ
v, e = map(int, input().split())
parent = [0] * (v + 1) # ๋ถ€๋ชจ ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ

# ๋ชจ๋“  ๊ฐ„์„ ์„ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ์™€, ์ตœ์ข… ๋น„์šฉ์„ ๋‹ด์„ ๋ณ€์ˆ˜
edges = []
result = 0

# ๋ถ€๋ชจ ํ…Œ์ด๋ธ”์ƒ์—์„œ, ๋ถ€๋ชจ๋ฅผ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ์ดˆ๊ธฐํ™”
for i in range(1, v + 1):
    parent[i] = i

# ๋ชจ๋“  ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ธฐ
for _ in range(e):
    a, b, cost = map(int, input().split())
    # ๋น„์šฉ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ํŠœํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋น„์šฉ์œผ๋กœ ์„ค์ •
    edges.append((cost, a, b))

# ๊ฐ„์„ ์„ ๋น„์šฉ์ˆœ์œผ๋กœ ์ •๋ ฌ
edges.sort()

# ๊ฐ„์„ ์„ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•˜๋ฉฐ
for edge in edges:
    cost, a, b = edge
    # ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ง‘ํ•ฉ์— ํฌํ•จ
    if find_parent(parent, a) != find_parent(parent, b):
        union_parent(parent, a, b)
        result += cost

print(result)

 

  • c++
#include <bits/stdc++.h>

using namespace std;

// ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜(V)์™€ ๊ฐ„์„ (Union ์—ฐ์‚ฐ)์˜ ๊ฐœ์ˆ˜(E)
// ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ 100,000๊ฐœ๋ผ๊ณ  ๊ฐ€์ •
int v, e;
int parent[100001]; // ๋ถ€๋ชจ ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”
// ๋ชจ๋“  ๊ฐ„์„ ์„ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ์™€, ์ตœ์ข… ๋น„์šฉ์„ ๋‹ด์„ ๋ณ€์ˆ˜
vector<pair<int, pair<int, int> > > edges;
int result = 0;

// ํŠน์ • ์›์†Œ๊ฐ€ ์†ํ•œ ์ง‘ํ•ฉ์„ ์ฐพ๊ธฐ
int findParent(int x) {
    // ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด, ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœ
    if (x == parent[x]) return x;
    return parent[x] = findParent(parent[x]);
}

// ๋‘ ์›์†Œ๊ฐ€ ์†ํ•œ ์ง‘ํ•ฉ์„ ํ•ฉ์น˜๊ธฐ
void unionParent(int a, int b) {
    a = findParent(a);
    b = findParent(b);
    if (a < b) parent[b] = a;
    else parent[a] = b;
}

int main(void) {
    cin >> v >> e;

    // ๋ถ€๋ชจ ํ…Œ์ด๋ธ”์ƒ์—์„œ, ๋ถ€๋ชจ๋ฅผ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ์ดˆ๊ธฐํ™”
    for (int i = 1; i <= v; i++) {
        parent[i] = i;
    }

    // ๋ชจ๋“  ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ธฐ
    for (int i = 0; i < e; i++) {
        int a, b, cost;
        cin >> a >> b >> cost;
        // ๋น„์šฉ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ํŠœํ”Œ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๋น„์šฉ์œผ๋กœ ์„ค์ •
        edges.push_back({cost, {a, b}});
    }

    // ๊ฐ„์„ ์„ ๋น„์šฉ์ˆœ์œผ๋กœ ์ •๋ ฌ
    sort(edges.begin(), edges.end());

    // ๊ฐ„์„ ์„ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•˜๋ฉฐ
    for (int i = 0; i < edges.size(); i++) {
        int cost = edges[i].first;
        int a = edges[i].second.first;
        int b = edges[i].second.second;
        // ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ง‘ํ•ฉ์— ํฌํ•จ
        if (findParent(a) != findParent(b)) {
            unionParent(a, b);
            result += cost;
        }
    }

    cout << result << '\n';
}
  • java
import java.util.*;

class Edge implements Comparable<Edge> {

    private int distance;
    private int nodeA;
    private int nodeB;

    public Edge(int distance, int nodeA, int nodeB) {
        this.distance = distance;
        this.nodeA = nodeA;
        this.nodeB = nodeB;
    }

    public int getDistance() {
        return this.distance;
    }

    public int getNodeA() {
        return this.nodeA;
    }

    public int getNodeB() {
        return this.nodeB;
    }

    // ๊ฑฐ๋ฆฌ(๋น„์šฉ)๊ฐ€ ์งง์€ ๊ฒƒ์ด ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง€๋„๋ก ์„ค์ •
    @Override
    public int compareTo(Edge other) {
        if (this.distance < other.distance) {
            return -1;
        }
        return 1;
    }
}

public class Main {

    // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜(V)์™€ ๊ฐ„์„ (Union ์—ฐ์‚ฐ)์˜ ๊ฐœ์ˆ˜(E)
    // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ 100,000๊ฐœ๋ผ๊ณ  ๊ฐ€์ •
    public static int v, e;
    public static int[] parent = new int[100001]; // ๋ถ€๋ชจ ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ
    // ๋ชจ๋“  ๊ฐ„์„ ์„ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ์™€, ์ตœ์ข… ๋น„์šฉ์„ ๋‹ด์„ ๋ณ€์ˆ˜
    public static ArrayList<Edge> edges = new ArrayList<>();
    public static int result = 0;

    // ํŠน์ • ์›์†Œ๊ฐ€ ์†ํ•œ ์ง‘ํ•ฉ์„ ์ฐพ๊ธฐ
    public static int findParent(int x) {
        // ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด, ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœ
        if (x == parent[x]) return x;
        return parent[x] = findParent(parent[x]);
    }

    // ๋‘ ์›์†Œ๊ฐ€ ์†ํ•œ ์ง‘ํ•ฉ์„ ํ•ฉ์น˜๊ธฐ
    public static void unionParent(int a, int b) {
        a = findParent(a);
        b = findParent(b);
        if (a < b) parent[b] = a;
        else parent[a] = b;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        v = sc.nextInt();
        e = sc.nextInt();

        // ๋ถ€๋ชจ ํ…Œ์ด๋ธ”์ƒ์—์„œ, ๋ถ€๋ชจ๋ฅผ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ์ดˆ๊ธฐํ™”
        for (int i = 1; i <= v; i++) {
            parent[i] = i;
        }

        // ๋ชจ๋“  ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ธฐ
        for (int i = 0; i < e; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int cost = sc.nextInt();
            edges.add(new Edge(cost, a, b));
        }

        // ๊ฐ„์„ ์„ ๋น„์šฉ์ˆœ์œผ๋กœ ์ •๋ ฌ
        Collections.sort(edges);

        // ๊ฐ„์„ ์„ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•˜๋ฉฐ
        for (int i = 0; i < edges.size(); i++) {
            int cost = edges.get(i).getDistance();
            int a = edges.get(i).getNodeA();
            int b = edges.get(i).getNodeB();
            // ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ง‘ํ•ฉ์— ํฌํ•จ
            if (findParent(a) != findParent(b)) {
                unionParent(a, b);
                result += cost;
            }
        }

        System.out.println(result);
    }
}

 

 

 

 

 

 

4. ์„ฑ๋Šฅ ๋ถ„์„

: ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๊ฐ€ E๊ฐœ ์ผ ๋•Œ O(ElogE) ์‹œ๊ฐ„๋ณต์žก๋„

: ๊ฐ€์žฅ ๋งŽ์€ ์‹œ๊ฐ„ ์š”๊ตฌ ๋ถ€๋ถ„์€ ๊ฐ„์„ ์„ ์ •๋ ฌ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ถ€๋ถ„!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•
Comments