Skip to content

323 number of connected components in an undirected graph#20

Open
kitano-kazuki wants to merge 5 commits intomainfrom
323-number-of-connected-components-in-an-undirected-graph
Open

323 number of connected components in an undirected graph#20
kitano-kazuki wants to merge 5 commits intomainfrom
323-number-of-connected-components-in-an-undirected-graph

Conversation

@kitano-kazuki
Copy link
Owner

@kitano-kazuki
Copy link
Owner Author

5ky7/arai60#22 (comment)

実装開始から実装完了までの時間を計測し、それも PR に含めるとよいと思います。 Arai 60 の問題であれば、 1 問 10 分以内で実装できるとよいと思います。ほかの方の実装時間も参考にされることをおすすめします

次から所要時間を書く

return

visited = [False] * n
adj_matrix = create_adjacent_matrix()
Copy link

Choose a reason for hiding this comment

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

動作的には問題ないのですが、何に依存しているのかを明にするために edges を引数に取るようにしたいです。

Suggested change
adj_matrix = create_adjacent_matrix()
adj_matrix = create_adjacent_matrix(edges)

adjacent_matrix[node2][node1] = True
return adjacent_matrix

def visit_all_connected_nodes(cur_node, adj_matrix, visited):
Copy link

Choose a reason for hiding this comment

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

  • 変数名はよほど長くない限り、省略しないほうが分かりやすいです
  • _all_ は何か特別な強調をしたいニュアンスを受けました(が、そうではないですよね)
Suggested change
def visit_all_connected_nodes(cur_node, adj_matrix, visited):
def visit_connected_nodes(current_node, adjacent_matrix, visited):

Copy link

Choose a reason for hiding this comment

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

cur_nodestart_index あるいは start_node などどうでしょうか。current=現在の、というのが読み手には何なのか分かりにくそうです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

startの方が良さそうですね。ありがとうございます!

Comment on lines +226 to +235
if self.rank[parent1] < self.rank[parent2]:
self.parents[parent1] = parent2
return
elif self.rank[parent2] < self.rank[parent1]:
self.parents[parent2] = parent1
return
else:
self.parents[parent2] = parent1
self.rank[parent1] += 1
return
Copy link

Choose a reason for hiding this comment

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

  • returnが冗長に感じました
  • ランクが等しいときが一番特徴的な分岐なので、それを頭に持ってくると読みやすいと思いました
Suggested change
if self.rank[parent1] < self.rank[parent2]:
self.parents[parent1] = parent2
return
elif self.rank[parent2] < self.rank[parent1]:
self.parents[parent2] = parent1
return
else:
self.parents[parent2] = parent1
self.rank[parent1] += 1
return
if self.rank[parent1] == self.rank[parent2]:
self.parents[parent2] = parent1
self.rank[parent1] += 1
elif self.rank[parent1] < self.rank[parent2]:
self.parents[parent1] = parent2
else:
self.parents[parent2] = parent1

Copy link
Owner Author

Choose a reason for hiding this comment

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

アーリーリターンのほうがいいかなと思っていましたが、if文内の記述量が少ないので, 一目でわかるよう冗長なreturnはないほうが良さそうですね

Copy link

Choose a reason for hiding this comment

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

何らかの処理が続くのならearly returnは読み手を助けるのですが、このifブロックはその後で関数が終了することが見えているので、early returnするメリットが享受できないように感じました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

おっしゃる通りだと思います!ありがとうございます!

for neighbor_node in node_to_neighbors[node]:
if not visited[neighbor_node]:
frontier.append(neighbor_node)
return
Copy link

Choose a reason for hiding this comment

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

Suggested change
return

Comment on lines +41 to +44
parent = uf.find(i)
if parent in seen_parents:
continue
seen_parents.add(parent)
Copy link

Choose a reason for hiding this comment

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

parent という概念は UnionFind クラスだけが知っていれば良いように思いました。UnionFind クラスが num_components を管理して、get_num_groups() のようなgetterを用意するとスッキリしそうです。

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.

2 participants