目标: 希望从大量集合中高效地找到所有的不连通图形
例如, 我有一个如下所示的数据文件:
A, B, C
C, D, E
A, F, Z
G, J
...
每个条目代表一组元素。 第一项 A,B,C = {A,B,C} 这也表明 A 和 B,A 和 C,B 和 C 之间有边。
我最初想到的算法如下:
1.parse all the entries into a list:
[
{A,B,C}
{C,D,E}
...
]
2.start with the first element/set of the list can called start_entry, {A,B,C} in this case
3.traverse other element in the list and do the following:
if the intersection of the element and start_entry is not empty
start_entry = start_entry union with the element
remove element from the list
4.with the updated start_entry, traverse the list again until there is not new update
上述算法应该返回一个连通图的顶点列表。然而,由于数据集大小的问题,我遇到了运行时问题。数据集包含大约100000个条目。
因此,我想知道是否有更有效的方法来查找连通图。
数据结构也可以改为以下形式(如果更容易)
A,B B,C E,F ...
每个条目表示图的一条边。
Find(S[x])
而不是parent[S[x]]
。 - user2357112