查找邻接矩阵图的连通分量

Den*_*nti 14 algorithm graph-theory graph matrix

我有一个由Java中的邻接矩阵表示的随机图,我如何在该图中找到连接的组件(子图)?

我找到了BFS和DFS,但不确定它们是否合适,我也不知道如何为邻接矩阵实现它们.

有任何想法吗?

Wis*_*ind 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.
Run Code Online (Sandbox Code Playgroud)

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);
    }
Run Code Online (Sandbox Code Playgroud)

在执行任何这些过程之后,组件将具有多个连接的组件,并且对于每个顶点i,标记[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]);
}
Run Code Online (Sandbox Code Playgroud)