首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphFrames检测独占出站关系

GraphFrames检测独占出站关系
EN

Stack Overflow用户
提问于 2018-11-11 06:27:50
回答 1查看 94关注 0票数 0

在我的图中,我需要检测没有入站关系的顶点。使用下面的示例,"a“是唯一一个与anyone无关的节点。

代码语言:javascript
复制
a -->  b
b -->  c
c -->  d
c -->  b

我真的很感谢任何能在我的图中检测"a“类型节点的例子。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-09-04 21:46:29

不幸的是,这种方法并不简单,因为graph.degress,graph.inDegrees,graph.outDegrees函数不会返回具有0边的顶点。(参见Scala的文档,它也适用于Python https://graphframes.github.io/graphframes/docs/_site/api/scala/index.html#org.graphframes.GraphFrame)

因此,下面的代码将始终返回一个空的dataframe

代码语言:javascript
复制
g=Graph(vertices,edges)

# check for start points 
g.inDegrees.filter("inDegree==0").show()
+---+--------+
| id|inDegree|
+---+--------+
+---+--------+

# or check for end points 
g.outDegrees.filter("outDegree==0").show()
+---+---------+
| id|outDegree|
+---+---------+
+---+---------+

# or check for any vertices that are alone without edge
g.degrees.filter("degree==0").show()
+---+------+
| id|degree|
+---+------+
+---+------+

有效的方法是对inDegree和outDegree结果进行左连接、右连接或完全连接,并根据相应列的空值进行过滤

连接将为您提供在开始位置和结束位置具有空值的合并列

代码语言:javascript
复制
g.inDegrees.join(g.outDegrees,on="id",how="full").show()

+---+--------+---------+
| id|inDegree|outDegree|
+---+--------+---------+
| b6|       1|     null|
| a3|       1|        1|
| a4|       1|     null|
| c7|       1|        1|
| b2|       1|        2|
| c9|       3|        1|
| c5|       1|        1|
| c1|    null|        1|
| c6|       1|        1|
| a2|       1|        1|
| b3|       1|        1|
| b1|    null|        1|
| c8|       3|     null|
| a1|    null|        1|
| c4|       1|        4|
| c3|       1|        1|
| b4|       1|        1|
| c2|       1|        3|
|c10|       1|     null|
| b5|       2|        1|
+---+--------+---------+

现在您可以根据搜索内容进行过滤

代码语言:javascript
复制
my_in_Degrees=g.inDegrees
my_out_Degrees=g.outDegrees

# get starting vertices (no more childs)
my_in_Degrees.join(my_out_Degrees,on="id",how="full").filter(my_in_Degrees.inDegree.isNull()).show()
+---+--------+---------+
| id|inDegree|outDegree|
+---+--------+---------+
| c1|    null|        1|
| b1|    null|        1|
| a1|    null|        1|
+---+--------+---------+


# get ending vertices (no more parents)
my_in_Degrees.join(my_out_Degrees,on="id",how="full").filter(my_out_Degrees.outDegree.isNull()).show()
+---+--------+---------+
| id|inDegree|outDegree|
+---+--------+---------+
| b6|       1|     null|
| a4|       1|     null|
|c10|       1|     null|
+---+--------+---------+
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53244049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档