我有一个由Java中的邻接矩阵表示的随机图,如何在该图中找到连接的组件(子图)?
我找到了BFS和DFS,但不确定它们是否合适,我也不知道如何为邻接矩阵实现它们。
有什么想法吗?
答案 0 :(得分:22)
您需要分配标记 - 长度为n的int数组,其中n是图形中的顶点数,并用零填充。然后:
1)对于BFS,请执行以下操作:
Components = 0;
Enumerate all vertices, if for vertex number i, marks[i] == 0 then
++Components;
Put this vertex into queue, and
while queue is not empty,
pop vertex v from q
marks[v] = Components;
Put all adjacent vertices with marks equal to zero into queue.
2)对于DFS,请执行以下操作。
Components = 0;
Enumerate all vertices, if for vertex number i, marks[i] == 0 then
++Components;
Call DFS(i, Components), where DFS is
DFS(vertex, Components)
{
marks[vertex] = Components;
Enumerate all vertices adjacent to vertex and
for all vertex j for which marks[j] == 0
call DFS(j, Components);
}
执行任何此类过程后,组件将具有多个连接组件, 对于每个顶点i,marks [i]将表示我所属的连通分量的索引。
两者都在O(n)时间内完成,使用O(n)存储器,其中n是矩阵大小。但我建议你BFS,因为它不会遇到堆栈溢出问题,并且它不会花时间在递归调用上。
Java中的BFS代码:
public static boolean[] BFS(boolean[][] adjacencyMatrix, int vertexCount, int givenVertex){
// Result array.
boolean[] mark = new boolean[vertexCount];
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(givenVertex);
mark[givenVertex] = true;
while (!queue.isEmpty())
{
Integer current = queue.remove();
for (int i = 0; i < vertexCount; ++i)
if (adjacencyMatrix[current][i] && !mark[i])
{
mark[i] = true;
queue.add(i);
}
}
return mark;
}
public static void main(String[] args) {
// Given adjacencyMatrix[x][y] if and only if there is a path between x and y.
boolean[][] adjacencyMatrix = new boolean[][]
{
{false,true,false,false,false},
{true,false,false,true,true},
{false,false,false,false,false},
{true,false,false,false,false},
{true,false,false,false,false}
};
// Mark[i] is true if and only if i belongs to the same connected component as givenVertex vertex does.
boolean[] mark = BFS(adjacencyMatrix, 5, 0);
for (int i = 0; i < 5; ++i)
System.out.println(mark[i]);
}
答案 1 :(得分:3)
您可以使用堆栈迭代地实现DFS,以消除递归调用和调用堆栈溢出的问题。实现与带队列的BFS非常相似 - 您只需在弹出顶点时标记顶点,而不是在将它们推入堆栈时标记顶点。
答案 2 :(得分:2)
使用scipy的稀疏模块,
假设您的输入是从(label_1,label_2)
到weight
的字典
你可以运行这段代码:
vertices, edges = dict2graph(cooccur_matrix, edge_threshold)
n, components = sparse.csgraph.connected_components(edges, directed=False)
print ('Found {n} components'.format(n=n))
components = collect_components(components,vertices)
components = [c for c in components if len(c)>=component_threshold]
print ('removed {k} small components'.format(k=n-len(components)))
print ('component sizes: '+ repr([len(c) for c in components]))
请参阅github上的完整要点here