Skip to content

323. Number of Connected Components in an Undirected Graph#24

Open
Yuto729 wants to merge 1 commit intomainfrom
count-connected-components
Open

323. Number of Connected Components in an Undirected Graph#24
Yuto729 wants to merge 1 commit intomainfrom
count-connected-components

Conversation

@Yuto729
Copy link
Owner

@Yuto729 Yuto729 commented Dec 29, 2025

@Yuto729 Yuto729 changed the title Count Connected Components Number of Connected Components in an Undirected Graph Dec 29, 2025
@Yuto729 Yuto729 changed the title Number of Connected Components in an Undirected Graph 323. Number of Connected Components in an Undirected Graph Dec 29, 2025
Repository owner deleted a comment from github-actions bot Dec 29, 2025
Comment on lines +21 to +24
def traverse_graph(vertical):
stack = [vertical]
while stack:
v = stack.pop()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

頂点はvertexですね。
vertical -> start, v -> vertex/node としたい気がします。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一文字変数は、見て意味がはっきりしていてすぐに忘れて大丈夫、というマークなので、ここではもう少し長いほうがいいかもしれません。このあたりは、状況にもよるので、私の個人的な趣味の範囲です。

```py
class Solution:
def countComponents(self, n: int, edges: List[List[int]]) -> int:
def traverse_graph(vertical):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好みの範囲ですが、自分なら関数名をtreverse_from()とかにしそうです。

stack.append(neighbor)
visited.add(neighbor)

return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このreturnは不要ですね。

return component_count
```

- Hintに書いてあったやり方だとUnion-Findを使っている.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi:

union-find は、微妙に常識から外れるかな(多くの人が知っているだろうが知らなくてもドン引きはされない)、くらいの感覚です。DFS による解法のほうは常識でしょう。

のようですね。

https://discord.com/channels/1084280443945353267/1183683738635346001/1197738650998415500

Comment on lines +26 to +30
if neighbor in visited:
continue

stack.append(neighbor)
visited.add(neighbor)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前から読んできた人はこの辺りで未定義のvisitedがいきなりでてきてびっくりするかもしれません。

自分はEnclosingへのmutationを利用したい場合、関数定義の上にEnclosing変数の初期化を書くようにしています。コード自体は正しく動くので許容範囲なのかもしれませんが、自分はやや忌避感がありました。

Comment on lines +36 to +38
for edge in edges:
vertical_to_neighbors[edge[0]].append(edge[1])
vertical_to_neighbors[edge[1]].append(edge[0])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的にはタプルをunpackして、

Suggested change
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)

と書きたいです。

Comment on lines +182 to +185
node_to_visite = [start]
visited.add(start)
while node_to_visite:
node = node_to_visite.pop()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

細かいですがnode_to_visiteのnodeは複数形であって欲しいと思いました。

class Solution:
def countComponents(self, n: int, edges: List[List[int]]) -> int:
def traverse_graph(start):
node_to_visite = [start]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visitedに引っ張られた誤字だと思いますが、node_to_visitでしょうか。

Suggested change
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):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: verticalではなくvertexでしょうか.

https://github.com/docto-rin/leetcode/pull/28/changes#diff-4f6b01b75cf61fa706e6463e0a6840a6a0685f9f0cfcc46cc7dfb3530e908b18R34
スパースなグラフの場合にDictのほうが良いかも

- visitedをsetではなく, 長さnのリストにするのもあり. アクセスの計算量を考えるとsetを使うメリットはあまりなさそう. スパースであれば最初にn個分メモリを確保するのが無駄になるのでsetのほうがいいかも
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私が勘違いしていたらすみませんが,

スパースであれば最初にn個分メモリを確保するのが無駄になるのでsetのほうがいいかも

については,スパースかどうかによらずvisitedには全てのノードが追加されるので判断基準には含まれないのではないでしょうか?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants