下面是我在apache graphX中构建的顶点和边。所有边缘都朝向两个方向。如何收集连接的组件以便输出(" Alice"," Bon"," Charlie"," David",&#34 ; ED""弗兰&#34),("波什"" BISH""辣妹&#34)
val vertexArray = Array(
(1L, ("Alice", 28)),
(2L, ("Bob", 27)),
(3L, ("Charlie", 65)),
(4L, ("David", 42)),
(5L, ("Ed", 55)),
(6L, ("Fran", 51)),
(7L, ("Bosh", 520)),
(8L, ("Bish", 50)),
(9L, ("Posh", 50))
)
val edgeArray = Array(
Edge(2L, 1L, 1),
Edge(2L, 4L, 1),
Edge(3L, 2L, 1),
Edge(3L, 6L, 1),
Edge(4L, 1L, 1),
Edge(5L, 2L, 1),
Edge(5L, 3L, 1),
Edge(5L, 6L, 1),
Edge(7L, 8L, 1),
Edge(8L, 9L, 1),
Edge(1L, 2L, 1),
Edge(4L, 2L, 1),
Edge(2L, 3L, 1),
Edge(3L, 4L, 1),
Edge(1L, 5L, 1),
Edge(2L, 5L, 1),
Edge(3L, 5L, 1),
Edge(6L, 5L, 1),
Edge(8L, 7L, 1),
Edge(9L, 8L, 1))
答案 0 :(得分:1)
您可以使用原始图形的顶点将生成的图形顶点连接起来,如documentation中所述。
首先让我们得到一个干净的映射ID - >名称
val users = graph.vertices.map{case (id, (name, age)) => (id, name)}
然后获取连接组件的顶点
val cc = graph.connectedComponents.vertices
如文件中所述,
连通分量算法使用其编号最小的顶点的ID
标记图的每个连通分量
在你的例子中有两个标签,1个用于Alice,Bon,Charlie,David,Ed和Fran,或者7个用于其他三个。 因此,我们现在可以将连接的组件与原始顶点连接,并按标签分组。经过一些清洁后,我们得到了理想的结果
val groups = users.join(cc)
.map{case (id, (name, label)) => (name, label)}
.groupBy(_._2)
.map{case (label, users) => users.map(_._1)}