数据结构 -- 图的联通分量(scala版)

连通分量

无向图G的极大连通子图称为G的连通分量( Connected Component)。任何连通图的连通分量只有一个,即是其自身,非连通的无向图有多个连通分量。如下,图中有两个连通分量。
在这里插入图片描述


图算法中功能

1).获得图中连通分量的个数
2).判断两个顶点是否连通
3).获取每个连通分量及其中的顶点集合


scala实现

import scala.collection.mutable.ArrayBuffer

class ConnectedComponents {
  private var G: Graph = _
  private var visited: Array[Int] = _
  private var connectedComponentsCount = 0

  def this(graph: Graph) = {
    this()
    G = graph
    visited = Array.ofDim[Int](graph.getV())
    for (j <- 0 until graph.getV()) {
      visited(j) = -1
    }
    for (i <- 0 until visited.length) {
      if (visited(i) == -1) {
        dfs(i, connectedComponentsCount)
        connectedComponentsCount += 1
      }
    }
  }

  private def dfs(v: Int, ccountid: Int): Unit = {
    visited(v) = ccountid
    for (x <- G.getAdjacentSide(v)) {
      if (visited(x) == -1) {
        dfs(x, ccountid)
      }
    }
  }
  
  /**
   * 获得图中连通分量的个数
   *
   * @return
   */
  def getConnectedComponentsCount() = {
    for (i <- 0 until visited.size) {
      print(s"${i} \t")
    }
    println()
    for (i <- 0 until visited.size) {
      print(s"${visited(i)} \t")
    }
    println()
    connectedComponentsCount
  }

  /**
   * 判断两个顶点是否连通
   *
   * @param v
   * @param w
   * @return
   */
  def isConnected(v: Int, w: Int): Boolean = {
    visited(v) == visited(w)
  }

  /**
   * 获得连通分量及顶点集合
   *
   * @return
   */
  def components(): Array[ArrayBuffer[Int]] = {
    val arr = Array.ofDim[ArrayBuffer[Int]](connectedComponentsCount)
    for (i <- 0 until connectedComponentsCount) {
      arr(i) = new ArrayBuffer[Int]()
    }
    for (j <- 0 until G.getV()) {
      arr(visited(j)) += j
    }
    arr
  }
}

object ConnectedComponents {
  def apply(): ConnectedComponents = new ConnectedComponents()

  def apply(graph: Graph): ConnectedComponents = new ConnectedComponents(graph)

  def main(args: Array[String]): Unit = {
    val graph = Graph().createGraph("./data/graph/g.txt")
    val connectedComponents = ConnectedComponents(graph)
    println(connectedComponents.getConnectedComponentsCount())
    println(connectedComponents.isConnected(0, 2))
    println(connectedComponents.isConnected(0, 4))
    val array = connectedComponents.components()
    for (x <- 0 until array.length) {
      print(s"第${x}个联通分量:")
      for (y <- array(x)) {
        print(s"${y} \t")
      }
      println()
    }
  }
}


0 	1 	2 	3 	4 	5 	6 	
0 	0 	0 	0 	1 	0 	0 	
2
true
false
第0个联通分量:0 	1 	2 	3 	5 	61个联通分量:4 	

g.txt

7 6
0 1
0 3
1 2
1 6
2 3
2 5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值