Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • BFS
  • boj
  • DFS
  • django
  • 백트래킹
  • 프로그래머스
  • Network
  • 다이나믹 프로그래밍
  • docker
  • Kubernetes

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

Algorithm/DS, algorithms

Union Find Basic Code C++

2021. 4. 19. 20:21

Union Find를 이해하기 위한 기본 코드

 

findTopnode를 통해서 최상단 노드를 알 수 있다

union_node를 통해서 입력된 수의 최상단노드를 찾아 두 수를 연결해주는 작업을 한다

check_node를 통해서 두 수가 서로 연결 되었는지 알 수 있다

www.cs.usfca.edu/~galles/visualization/DisjointSets.html

 

Disjoint Sets Visualization

 

www.cs.usfca.edu

위 사이트에서 눈으로 직접 보면서 이해할 수 있다!! 좋음

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <vector>
using namespace std;
 
int findTopnode(vector<int> node, int x){
    if(node[x] == x){
        return x;
    }
    return findTopnode(node, node[x]);
}
 
void union_node(vector<int> &node, int a, int b){
    a = findTopnode(node, a);
    b = findTopnode(node, b);
    if(a < b){
        node[b] = a;
    }
    else{
        node[a] = b;
    }
}
 
bool check_node(vector<int> node, int a, int b){
    a = findTopnode(node, a);
    b = findTopnode(node, b);
    if(a == b){
        return true;
    }
    else{
        return false;
    }
}
 
int main(){
    vector<int> Node;
    Node.push_back(0);
 
    for(int i = 1; i <= 10; i++){
        Node.push_back(i);
    }
    union_node(Node, 1, 2);
    union_node(Node, 2, 3);
    union_node(Node, 3, 4);
    union_node(Node, 5, 6);
    union_node(Node, 7, 8);
 
    if(check_node(Node, 1, 5)){
        cout << "YES\n";
    }
    else{
        cout << "NO\n";
    }
    union_node(Node, 4, 5);
 
    if(check_node(Node, 1, 5)){
        cout << "YES\n";
    }
    else{
        cout << "NO\n";
    }
    for(int i = 0; i < Node.size(); i++){
        cout << Node[i] << "\n";
    }
    // for(auto w : Node){
    //     cout << w << " ";
    // }
    cout << "\n" << findTopnode(Node, 6);
    return 0;
}
 
Colored by Color Scripter
cs

'Algorithm > DS, algorithms' 카테고리의 다른 글

[자료구조] Single Linked List 예제 코드 #2 (C)  (2) 2021.06.14
[자료구조] Single Linked List 예제 코드#1 (C)  (0) 2021.06.14
힙 정렬(heap sort) <priority queue> C++  (0) 2021.05.29
Minimum Spanning Tree (MST) - Kruskal  (0) 2021.05.10
Stack Permutation C++  (0) 2021.04.19
    'Algorithm/DS, algorithms' 카테고리의 다른 글
    • [자료구조] Single Linked List 예제 코드#1 (C)
    • 힙 정렬(heap sort) <priority queue> C++
    • Minimum Spanning Tree (MST) - Kruskal
    • Stack Permutation C++

    티스토리툴바