将有向关系转换为无向关系

简介

在 GDS 中,某些算法(例如三角形计数和链路预测)需要无向关系。此过程将有向关系转换为无向关系,并将结果输出为新的关系类型。这对于转换由路径算法等产生的关系非常有用。

语法

CALL gds.graph.relationships.toUndirected(
    graphName: String,
    configuration: Map
)
YIELD
    inputRelationships: Integer,
    relationshipsWritten: Integer,
    mutateMillis: Integer,
    postProcessingMillis: Integer,
    preProcessingMillis: Integer,
    computeMillis: Integer,
    configuration: Map
表 1. 参数
名称 类型 可选 描述

graphName

字符串

图在目录中存储时所使用的名称。

配置

Map

用于配置 streamNodeProperties 的附加参数。

表 2. 配置
名称 类型 默认 可选 描述

关系类型 (relationshipType)

字符串

不适用

要转换为无向的关系类型。

mutateRelationshipType

字符串

不适用

要添加到图中的关系类型。

聚合 (aggregation)

Map 或 String

NONE

对平行关系的处理。允许的值为 NONEMINMAXSUMSINGLECOUNT。使用 Map 可以为每个关系属性指定聚合方式。默认情况下,我们将使用初始投影期间使用的现有聚合方式。

concurrency

整数

4 [1]

用于运行算法的并发线程数。

jobId

字符串

内部生成

可以提供一个 ID 以更轻松地跟踪算法的进度。

logProgress

布尔值

true

如果禁用,进度百分比将不会被记录。

1. 在 GDS 会话 中,默认值为可用处理器数量。

表 3. 结果
名称 类型 描述

inputRelationships

整数

已处理的关系数量。

relationshipsWritten

整数

添加的关系数量。

preProcessingMillis

整数

预处理图的毫秒数。

computeMillis

整数

运行算法的毫秒数。

postProcessingMillis

整数

未使用。

mutateMillis

整数

向投影图添加关系所需的毫秒数。

配置

Map

用于运行算法的配置。

示例

以下所有示例应在空数据库中运行。

本示例将 Cypher 投影作为标准。原生投影将在未来的版本中弃用。

为了演示如何将有向关系转换为无向关系,我们将创建一个小型 Neo4j 图并将其投影到我们的图目录中。

Visualization of the example graph
以下 Cypher 语句将在 Neo4j 数据库中创建示例图:
CREATE
  (alice:Person {name: 'Alice'}),
  (bob:Person {name: 'Bob'}),
  (carol:Person {name: 'Carol'}),
  (dave:Person {name: 'Dave'}),
  (eve:Person {name: 'Eve'}),
  (guitar:Instrument {name: 'Guitar'}),
  (synth:Instrument {name: 'Synthesizer'}),
  (bongos:Instrument {name: 'Bongos'}),
  (trumpet:Instrument {name: 'Trumpet'}),

  (alice)-[:LIKES { score: 5 }]->(guitar),
  (alice)-[:LIKES { score: 4 }]->(synth),
  (alice)-[:LIKES { score: 3}]->(bongos),
  (bob)-[:LIKES { score: 4 }]->(guitar),
  (bob)-[:LIKES { score: 5 }]->(synth),
  (carol)-[:LIKES { score: 2 }]->(bongos),
  (dave)-[:LIKES { score: 3 }]->(guitar),
  (dave)-[:LIKES { score: 1 }]->(synth),
  (dave)-[:LIKES { score: 5 }]->(bongos)
投影图
MATCH (person:Person)-[r:LIKES]->(instr:Instrument)
RETURN gds.graph.project(
  'personsAndInstruments',
  person,
  instr,
  {
    sourceNodeLabels: labels(person),
    targetNodeLabels: labels(instr),
    relationshipType: type(r),
    relationshipProperties: r { .score }
  }
)

以下代码展示了如何通过创建名为 INTERACTS 的新类型无向关系,将图中 LIKES 类型的关系从有向转换为无向。

将关系从有向转换为无向
CALL gds.graph.relationships.toUndirected(
  'personsAndInstruments',                                          (1)
  {relationshipType: 'LIKES', mutateRelationshipType: 'INTERACTS'}  (2)
)
YIELD
  inputRelationships, relationshipsWritten
1 投影图的名称。
2 一个包含“要转换为无向的关系类型”和“要添加到图中的关系类型”的 Map。
表 4. 结果
inputRelationships relationshipsWritten

9

18

下图展示了执行上述示例后,示例图在 Neo4j 中的样子。

Visualization of the example graph after converting the relationships to undirected