323. Number of Connected Components in an Undirected Graph#24
323. Number of Connected Components in an Undirected Graph#24
Conversation
| def traverse_graph(vertical): | ||
| stack = [vertical] | ||
| while stack: | ||
| v = stack.pop() |
There was a problem hiding this comment.
頂点はvertexですね。
vertical -> start, v -> vertex/node としたい気がします。
There was a problem hiding this comment.
一文字変数は、見て意味がはっきりしていてすぐに忘れて大丈夫、というマークなので、ここではもう少し長いほうがいいかもしれません。このあたりは、状況にもよるので、私の個人的な趣味の範囲です。
| ```py | ||
| class Solution: | ||
| def countComponents(self, n: int, edges: List[List[int]]) -> int: | ||
| def traverse_graph(vertical): |
There was a problem hiding this comment.
好みの範囲ですが、自分なら関数名をtreverse_from()とかにしそうです。
| stack.append(neighbor) | ||
| visited.add(neighbor) | ||
|
|
||
| return |
| return component_count | ||
| ``` | ||
|
|
||
| - Hintに書いてあったやり方だとUnion-Findを使っている. |
There was a problem hiding this comment.
fyi:
union-find は、微妙に常識から外れるかな(多くの人が知っているだろうが知らなくてもドン引きはされない)、くらいの感覚です。DFS による解法のほうは常識でしょう。
のようですね。
https://discord.com/channels/1084280443945353267/1183683738635346001/1197738650998415500
| if neighbor in visited: | ||
| continue | ||
|
|
||
| stack.append(neighbor) | ||
| visited.add(neighbor) |
There was a problem hiding this comment.
前から読んできた人はこの辺りで未定義のvisitedがいきなりでてきてびっくりするかもしれません。
自分はEnclosingへのmutationを利用したい場合、関数定義の上にEnclosing変数の初期化を書くようにしています。コード自体は正しく動くので許容範囲なのかもしれませんが、自分はやや忌避感がありました。
| for edge in edges: | ||
| vertical_to_neighbors[edge[0]].append(edge[1]) | ||
| vertical_to_neighbors[edge[1]].append(edge[0]) |
There was a problem hiding this comment.
個人的にはタプルをunpackして、
| for edge in edges: | |
| vertical_to_neighbors[edge[0]].append(edge[1]) | |
| vertical_to_neighbors[edge[1]].append(edge[0]) | |
| for vertex1, vertex2 in edges: | |
| vertical_to_neighbors[vertex1].append(vertex2) | |
| vertical_to_neighbors[vertex2].append(vertex1) |
と書きたいです。
| node_to_visite = [start] | ||
| visited.add(start) | ||
| while node_to_visite: | ||
| node = node_to_visite.pop() |
There was a problem hiding this comment.
細かいですがnode_to_visiteのnodeは複数形であって欲しいと思いました。
| class Solution: | ||
| def countComponents(self, n: int, edges: List[List[int]]) -> int: | ||
| def traverse_graph(start): | ||
| node_to_visite = [start] |
There was a problem hiding this comment.
visitedに引っ張られた誤字だと思いますが、node_to_visitでしょうか。
| node_to_visite = [start] | |
| node_to_visit = [start] |
| ```py | ||
| class Solution: | ||
| def countComponents(self, n: int, edges: List[List[int]]) -> int: | ||
| def traverse_graph(vertical): |
| https://github.com/docto-rin/leetcode/pull/28/changes#diff-4f6b01b75cf61fa706e6463e0a6840a6a0685f9f0cfcc46cc7dfb3530e908b18R34 | ||
| スパースなグラフの場合にDictのほうが良いかも | ||
|
|
||
| - visitedをsetではなく, 長さnのリストにするのもあり. アクセスの計算量を考えるとsetを使うメリットはあまりなさそう. スパースであれば最初にn個分メモリを確保するのが無駄になるのでsetのほうがいいかも |
There was a problem hiding this comment.
私が勘違いしていたらすみませんが,
スパースであれば最初にn個分メモリを確保するのが無駄になるのでsetのほうがいいかも
については,スパースかどうかによらずvisitedには全てのノードが追加されるので判断基準には含まれないのではないでしょうか?
解く問題
Number of Connected Components in an Undirected Graph(Neetcode代替)
次に解く問題
Word Ladder