From 28058ff6931878da5ae43a6c5c5e7deed4afdbb6 Mon Sep 17 00:00:00 2001 From: kazukiii Date: Tue, 25 Jun 2024 01:18:34 -0700 Subject: [PATCH] =?UTF-8?q?step1,=20step2,=20step3=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../README.md | 28 ++++++++++++++ .../step1.cpp | 28 ++++++++++++++ .../step2_bfs.cpp | 35 ++++++++++++++++++ .../step2_union_find.cpp | 37 +++++++++++++++++++ .../step3.cpp | 29 +++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 arai60/number-of-connected-components-in-an-undirected-graph/README.md create mode 100644 arai60/number-of-connected-components-in-an-undirected-graph/step1.cpp create mode 100644 arai60/number-of-connected-components-in-an-undirected-graph/step2_bfs.cpp create mode 100644 arai60/number-of-connected-components-in-an-undirected-graph/step2_union_find.cpp create mode 100644 arai60/number-of-connected-components-in-an-undirected-graph/step3.cpp diff --git a/arai60/number-of-connected-components-in-an-undirected-graph/README.md b/arai60/number-of-connected-components-in-an-undirected-graph/README.md new file mode 100644 index 0000000..bc197ab --- /dev/null +++ b/arai60/number-of-connected-components-in-an-undirected-graph/README.md @@ -0,0 +1,28 @@ +## 考察 +- 初見の問題 +- Number of Islandsを一般のグラフにしたような問題 +- 方針 + - DFS + - BFS + - Union Find +- Step1ではDFSでやってみる +- 再帰の深さは最大で2000 +- あとは実装 + +## Step1 +- DFSで実装 +- time: O(n), space: O(n) + +## Step2 +- BFSでも実装 + - time: O(n), space: O(n) +- Union Findでも実装 + - 前回Union by sizeもやったので、path compressionのみ実装 + - time: O(n log n), space: O(n) + - Class Data Members の命名については以下を参照した + - https://google.github.io/styleguide/cppguide.html#Variable_Names + +## Step3 +- 1回目: 4m20s +- 2回目: 3m48s +- 3回目: 3m03s diff --git a/arai60/number-of-connected-components-in-an-undirected-graph/step1.cpp b/arai60/number-of-connected-components-in-an-undirected-graph/step1.cpp new file mode 100644 index 0000000..800a289 --- /dev/null +++ b/arai60/number-of-connected-components-in-an-undirected-graph/step1.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int countComponents(int n, vector>& edges) { + vector> adjacency_list(n); + vector visited(n, false); + for (vector edge : edges) { + adjacency_list[edge[0]].push_back(edge[1]); + adjacency_list[edge[1]].push_back(edge[0]); + } + + int total = 0; + for (int i = 0; i < n; i++) { + if (visited[i]) continue; + visitAllConnectedNodes(i, adjacency_list, visited); + total++; + } + return total; + } + +private: + void visitAllConnectedNodes(int node, vector>& adjacency_list, vector& visited) { + visited[node] = true; + for (int next_node : adjacency_list[node]) { + if (visited[next_node]) continue; + visitAllConnectedNodes(next_node, adjacency_list, visited); + } + } +}; diff --git a/arai60/number-of-connected-components-in-an-undirected-graph/step2_bfs.cpp b/arai60/number-of-connected-components-in-an-undirected-graph/step2_bfs.cpp new file mode 100644 index 0000000..0efa867 --- /dev/null +++ b/arai60/number-of-connected-components-in-an-undirected-graph/step2_bfs.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int countComponents(int n, vector>& edges) { + vector> adjacency_list(n); + vector visited(n, false); + for (vector edge : edges) { + adjacency_list[edge[0]].push_back(edge[1]); + adjacency_list[edge[1]].push_back(edge[0]); + } + + int total = 0; + for (int i = 0; i < n; i++) { + if (visited[i]) continue; + visitAllConnectedNodes(i, adjacency_list, visited); + total++; + } + return total; + } + +private: + void visitAllConnectedNodes(int node, vector>& adjacency_list, vector& visited) { + queue nodes_to_visit; + visited[node] = true; + nodes_to_visit.push(node); + while (!nodes_to_visit.empty()) { + int current_node = nodes_to_visit.front(); + nodes_to_visit.pop(); + for (int next_node : adjacency_list[current_node]) { + if (visited[next_node]) continue; + visited[next_node] = true; + nodes_to_visit.push(next_node); + } + } + } +}; diff --git a/arai60/number-of-connected-components-in-an-undirected-graph/step2_union_find.cpp b/arai60/number-of-connected-components-in-an-undirected-graph/step2_union_find.cpp new file mode 100644 index 0000000..adf4184 --- /dev/null +++ b/arai60/number-of-connected-components-in-an-undirected-graph/step2_union_find.cpp @@ -0,0 +1,37 @@ +class UnionFind { +private: + vector parents_; + +public: + UnionFind(int n) { + parents_.resize(n, -1); + } + + int find(int x) { + if (parents_[x] == -1) return x; + return parents_[x] = find(parents_[x]); + } + + void unite(int x, int y) { + x = find(x); + y = find(y); + if (x == y) return; + parents_[x] = y; + } +}; + +class Solution { +public: + int countComponents(int n, vector>& edges) { + UnionFind uf(n); + for (vector edge : edges) { + uf.unite(edge[0], edge[1]); + } + + unordered_set unique_groups; + for (int i = 0; i < n; i++) { + unique_groups.insert(uf.find(i)); + } + return unique_groups.size(); + } +}; diff --git a/arai60/number-of-connected-components-in-an-undirected-graph/step3.cpp b/arai60/number-of-connected-components-in-an-undirected-graph/step3.cpp new file mode 100644 index 0000000..d0d6860 --- /dev/null +++ b/arai60/number-of-connected-components-in-an-undirected-graph/step3.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int countComponents(int n, vector>& edges) { + vector> adjacency_list(n); + vector visited(n, false); + + for (vector edge : edges) { + adjacency_list[edge[0]].push_back(edge[1]); + adjacency_list[edge[1]].push_back(edge[0]); + } + + int total = 0; + for (int i = 0; i < n; i++) { + if (visited[i]) continue; + visitAllConnectedNodes(i, adjacency_list, visited); + total++; + } + return total; + } + +private: + void visitAllConnectedNodes(int node, vector>& adjacency_list, vector& visited) { + visited[node] = true; + for (int next_node : adjacency_list[node]) { + if (visited[next_node]) continue; + visitAllConnectedNodes(next_node, adjacency_list, visited); + } + } +};