-
Notifications
You must be signed in to change notification settings - Fork 0
323. Number of Connected Components in an Undirected Graph #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kazukiii
wants to merge
1
commit into
main
Choose a base branch
from
arai60/number-of-connected-components-in-an-undirected-graph
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
arai60/number-of-connected-components-in-an-undirected-graph/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
28 changes: 28 additions & 0 deletions
28
arai60/number-of-connected-components-in-an-undirected-graph/step1.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| class Solution { | ||
| public: | ||
| int countComponents(int n, vector<vector<int>>& edges) { | ||
| vector<vector<int>> adjacency_list(n); | ||
| vector<bool> visited(n, false); | ||
| for (vector<int> 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<vector<int>>& adjacency_list, vector<bool>& visited) { | ||
| visited[node] = true; | ||
| for (int next_node : adjacency_list[node]) { | ||
| if (visited[next_node]) continue; | ||
| visitAllConnectedNodes(next_node, adjacency_list, visited); | ||
| } | ||
| } | ||
| }; | ||
35 changes: 35 additions & 0 deletions
35
arai60/number-of-connected-components-in-an-undirected-graph/step2_bfs.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| class Solution { | ||
| public: | ||
| int countComponents(int n, vector<vector<int>>& edges) { | ||
| vector<vector<int>> adjacency_list(n); | ||
| vector<bool> visited(n, false); | ||
| for (vector<int> 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<vector<int>>& adjacency_list, vector<bool>& visited) { | ||
| queue<int> 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); | ||
| } | ||
| } | ||
| } | ||
| }; |
37 changes: 37 additions & 0 deletions
37
arai60/number-of-connected-components-in-an-undirected-graph/step2_union_find.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| class UnionFind { | ||
| private: | ||
| vector<int> 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<vector<int>>& edges) { | ||
| UnionFind uf(n); | ||
| for (vector<int> edge : edges) { | ||
| uf.unite(edge[0], edge[1]); | ||
| } | ||
|
|
||
| unordered_set<int> unique_groups; | ||
| for (int i = 0; i < n; i++) { | ||
| unique_groups.insert(uf.find(i)); | ||
| } | ||
| return unique_groups.size(); | ||
| } | ||
| }; |
29 changes: 29 additions & 0 deletions
29
arai60/number-of-connected-components-in-an-undirected-graph/step3.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| class Solution { | ||
| public: | ||
| int countComponents(int n, vector<vector<int>>& edges) { | ||
| vector<vector<int>> adjacency_list(n); | ||
| vector<bool> visited(n, false); | ||
|
|
||
| for (vector<int> 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<vector<int>>& adjacency_list, vector<bool>& visited) { | ||
| visited[node] = true; | ||
| for (int next_node : adjacency_list[node]) { | ||
| if (visited[next_node]) continue; | ||
| visitAllConnectedNodes(next_node, adjacency_list, visited); | ||
| } | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
false は書かなくても zero initialization だったか自信ありますか。
vector は色々と特殊なので、どこかで調べるといいかもしれません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちら調べてみました。
vectorの場合、false を指定しなくても zero initialization になるんですね。
また、
vector<bool>に関しては実装依存ではあるが、各要素がsingle bitに対応付けられるような特殊なメモリ最適化が行われる場合が多いと理解しました。以下を参考にしました)
https://en.cppreference.com/w/cpp/container/vector
https://en.cppreference.com/w/cpp/container/vector_bool