Блог пользователя pqpq123

Автор pqpq123, история, 6 часов назад, По-английски

There is a graph (might have cycles) in the form of an adjacency list. How can we find the 'size' of the largest connected component?

Example: 0: 1 2 1: 2: 1 3 3: 1 2 4 4: 2 3

Answer: 5 Reason: We start from 0 and go to 1 and 2, from 2 we can reach 3, from 3 we can reach 4.

What can be the most efficient way to calculate this?

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
6 часов назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

You can traverse the graph with DFS, and keep track of the visited nodes. For each node, you start DFS if it's not visited. Then you increment your size counter each time you discover a new neighbor and set it to visited, and you take the max size. It's O(n) I believe

»
4 часа назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

you can do DSU that stores the lengths of each component

»
2 часа назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

This is a standard problem. What you are referring to is, in the case of a directed graph, the notion of a "strongly connected component". There are a bunch of algorithms that do this such as Trajan's algo. You can find blogs discussing it on cp-algorithms and usaco.guide