连通分量
无向图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 6
第1个联通分量:4
g.txt
7 6
0 1
0 3
1 2
1 6
2 3
2 5