From aa08fed5fee51f8b0156253b7d4955c995901644 Mon Sep 17 00:00:00 2001 From: imbajin Date: Mon, 21 Feb 2022 19:36:44 +0800 Subject: [PATCH 1/3] feat(api): support adamic-adar & resource-allocation algorithms --- .../java/com/baidu/hugegraph/api/API.java | 2 +- .../api/traversers/AdamicAdarAPI.java | 88 ++++++++++++++++++ .../api/traversers/ResourceAllocationAPI.java | 89 +++++++++++++++++++ .../baidu/hugegraph/version/ApiVersion.java | 5 +- .../algorithm/PredictionTraverser.java | 77 ++++++++++++++++ .../api/traversers/AdamicAdarAPITest.java | 59 ++++++++++++ .../traversers/ResourceAllocationAPITest.java | 59 ++++++++++++ .../traversers/TraversersApiTestSuite.java | 4 +- 8 files changed, 379 insertions(+), 4 deletions(-) create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java create mode 100644 hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java create mode 100644 hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java create mode 100644 hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java index 398f0a686d..91fbef80cf 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java @@ -45,7 +45,7 @@ public class API { - private static final Logger LOG = Log.logger(RestServer.class); + protected static final Logger LOG = Log.logger(RestServer.class); public static final String CHARSET = "UTF-8"; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java new file mode 100644 index 0000000000..cc1f4c6237 --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java @@ -0,0 +1,88 @@ +/* + * Copyright 2022 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.api.traversers; + +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_ELEMENTS_LIMIT; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_MAX_DEGREE; + +import javax.inject.Singleton; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; + +import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.api.API; +import com.baidu.hugegraph.api.graph.EdgeAPI; +import com.baidu.hugegraph.api.graph.VertexAPI; +import com.baidu.hugegraph.backend.id.Id; +import com.baidu.hugegraph.core.GraphManager; +import com.baidu.hugegraph.traversal.algorithm.PredictionTraverser; +import com.baidu.hugegraph.type.define.Directions; +import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.JsonUtil; +import com.codahale.metrics.annotation.Timed; +import com.google.common.collect.ImmutableMap; + +/** + * This API include similar prediction algorithms, now include: + * - Adamic Adar + * - Resource Allocation + * + * Could add more prediction algorithms in future + */ +@Path("graphs/{graph}/traversers/adamicadar") +@Singleton +public class AdamicAdarAPI extends API { + + @GET + @Timed + @Produces(APPLICATION_JSON_WITH_CHARSET) + public String get(@Context GraphManager manager, + @PathParam("graph") String graph, + @QueryParam("vertex") String current, + @QueryParam("other") String other, + @QueryParam("direction") String direction, + @QueryParam("label") String edgeLabel, + @QueryParam("max_degree") + @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree, + @QueryParam("limit") + @DefaultValue(DEFAULT_ELEMENTS_LIMIT) long limit) { + LOG.debug("Graph [{}] get adamic adar between '{}' and '{}' with " + + "direction {}, edge label {}, max degree '{}' and limit '{}'", + graph, current, other, direction, edgeLabel, maxDegree, + limit); + + Id sourceId = VertexAPI.checkAndParseVertexId(current); + Id targetId = VertexAPI.checkAndParseVertexId(other); + E.checkArgument(!current.equals(other), + "The source and target vertex id can't be same"); + Directions dir = Directions.convert(EdgeAPI.parseDirection(direction)); + + HugeGraph g = graph(manager, graph); + PredictionTraverser traverser = new PredictionTraverser(g); + double score = traverser.adamicAdar(sourceId, targetId, dir, + edgeLabel, maxDegree, limit); + return JsonUtil.toJson(ImmutableMap.of("adamic_adar", score)); + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java new file mode 100644 index 0000000000..f476faddcf --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java @@ -0,0 +1,89 @@ +/* + * Copyright 2022 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.api.traversers; + +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_ELEMENTS_LIMIT; +import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_MAX_DEGREE; + +import javax.inject.Singleton; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; + +import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.api.API; +import com.baidu.hugegraph.api.graph.EdgeAPI; +import com.baidu.hugegraph.api.graph.VertexAPI; +import com.baidu.hugegraph.backend.id.Id; +import com.baidu.hugegraph.core.GraphManager; +import com.baidu.hugegraph.traversal.algorithm.PredictionTraverser; +import com.baidu.hugegraph.type.define.Directions; +import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.JsonUtil; +import com.codahale.metrics.annotation.Timed; +import com.google.common.collect.ImmutableMap; + +/** + * This API include similar prediction algorithms, now include: + * - Adamic Adar + * - Resource Allocation + * + * Could add more prediction algorithms in future + */ +@Path("graphs/{graph}/traversers/resourceallocation") +@Singleton +public class ResourceAllocationAPI extends API { + + @GET + @Timed + @Produces(APPLICATION_JSON_WITH_CHARSET) + public String create(@Context GraphManager manager, + @PathParam("graph") String graph, + @QueryParam("vertex") String current, + @QueryParam("other") String other, + @QueryParam("direction") String direction, + @QueryParam("label") String edgeLabel, + @QueryParam("max_degree") + @DefaultValue(DEFAULT_MAX_DEGREE) long maxDegree, + @QueryParam("limit") + @DefaultValue(DEFAULT_ELEMENTS_LIMIT) long limit) { + LOG.debug("Graph [{}] get resource allocation between '{}' and '{}' " + + "with direction {}, edge label {}, max degree '{}' and " + + "limit '{}'", graph, current, other, direction, edgeLabel, + maxDegree, limit); + + Id sourceId = VertexAPI.checkAndParseVertexId(current); + Id targetId = VertexAPI.checkAndParseVertexId(other); + E.checkArgument(!current.equals(other), + "The source and target vertex id can't be same"); + Directions dir = Directions.convert(EdgeAPI.parseDirection(direction)); + + HugeGraph g = graph(manager, graph); + PredictionTraverser traverser = new PredictionTraverser(g); + double score = traverser.resourceAllocation(sourceId, targetId, dir, + edgeLabel, maxDegree, + limit); + return JsonUtil.toJson(ImmutableMap.of("resource_allocation", score)); + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java index ad1f14b76a..8710f50d6c 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java @@ -117,12 +117,13 @@ public final class ApiVersion { * [0.65] Issue-1506: Support olap property key * [0.66] Issue-1567: Support get schema RESTful API * [0.67] Issue-1065: Support dynamically add/remove graph + * [0.68] Issue-1763: Support adamic-adar & resource-allocation API */ // The second parameter of Version.of() is for IDE running without JAR - public static final Version VERSION = Version.of(ApiVersion.class, "0.67"); + public static final Version VERSION = Version.of(ApiVersion.class, "0.68"); - public static final void check() { + public static void check() { // Check version of hugegraph-core. Firstly do check from version 0.3 VersionUtil.check(CoreVersion.VERSION, "0.12", "0.13", CoreVersion.NAME); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java new file mode 100644 index 0000000000..231fe5cddf --- /dev/null +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java @@ -0,0 +1,77 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.traversal.algorithm; + +import java.util.Set; + +import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.backend.id.Id; +import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep; +import com.baidu.hugegraph.type.define.Directions; +import com.baidu.hugegraph.util.E; +import com.google.common.collect.ImmutableList; + +public class PredictionTraverser extends OltpTraverser { + + public PredictionTraverser(HugeGraph graph) { + super(graph); + } + + public double adamicAdar(Id source, Id target, Directions dir, + String label, long degree, long limit) { + Set neighbors = checkAndGetCommonNeighbors(source, target, dir, + label, degree, limit); + EdgeStep step = label == null ? new EdgeStep(graph(), dir) : + new EdgeStep(graph(), dir, ImmutableList.of(label)); + + double sum = 0.0; + for (Id vid : neighbors) { + sum += 1.0 / Math.log(this.edgesCount(vid, step)); + } + return sum; + } + + public double resourceAllocation(Id source, Id target, Directions dir, + String label, long degree, long limit) { + Set neighbors = checkAndGetCommonNeighbors(source, target, dir, + label, degree, limit); + EdgeStep step = label == null ? new EdgeStep(graph(), dir) : + new EdgeStep(graph(), dir, ImmutableList.of(label)); + + double sum = 0.0; + for (Id vid : neighbors) { + sum += 1.0 / this.edgesCount(vid, step); + } + return sum; + } + + private Set checkAndGetCommonNeighbors(Id source, Id target, + Directions dir, String label, + long degree, long limit) { + E.checkNotNull(source, "source id"); + E.checkNotNull(target, "the target id"); + this.checkVertexExist(source, "source"); + this.checkVertexExist(target, "target"); + E.checkNotNull(dir, "direction"); + checkDegree(degree); + SameNeighborTraverser traverser = new SameNeighborTraverser(graph()); + return traverser.sameNeighbors(source, target, dir, label, degree, limit); + } +} diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java new file mode 100644 index 0000000000..04a1b67ab5 --- /dev/null +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPITest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2022 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.api.traversers; + +import java.util.Map; + +import javax.ws.rs.core.Response; + +import org.junit.Before; +import org.junit.Test; + +import com.baidu.hugegraph.api.BaseApiTest; +import com.google.common.collect.ImmutableMap; + +public class AdamicAdarAPITest extends BaseApiTest { + + private final static String PATH = TRAVERSERS_API + "/adamicadar"; + + @Before + public void prepareSchema() { + BaseApiTest.initPropertyKey(); + BaseApiTest.initVertexLabel(); + BaseApiTest.initEdgeLabel(); + BaseApiTest.initVertex(); + BaseApiTest.initEdge(); + } + + @Test + public void testGet() { + Map name2Ids = listAllVertexName2Ids(); + + String markoId = name2Ids.get("marko"); + String joshId = name2Ids.get("josh"); + String peterId = name2Ids.get("peter"); + Response r = client().get(PATH, ImmutableMap.of("vertex", + id2Json(markoId), + "other", + id2Json(joshId))); + String content = assertResponseStatus(200, r); + assertJsonContains(content, "adamic_adar"); + } +} diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java new file mode 100644 index 0000000000..ede6b7031e --- /dev/null +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPITest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2022 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.api.traversers; + +import java.util.Map; + +import javax.ws.rs.core.Response; + +import org.junit.Before; +import org.junit.Test; + +import com.baidu.hugegraph.api.BaseApiTest; +import com.google.common.collect.ImmutableMap; + +public class ResourceAllocationAPITest extends BaseApiTest { + + private final static String PATH = TRAVERSERS_API + "/resourceallocation"; + + @Before + public void prepareSchema() { + BaseApiTest.initPropertyKey(); + BaseApiTest.initVertexLabel(); + BaseApiTest.initEdgeLabel(); + BaseApiTest.initVertex(); + BaseApiTest.initEdge(); + } + + @Test + public void testGet() { + Map name2Ids = listAllVertexName2Ids(); + + String markoId = name2Ids.get("marko"); + String joshId = name2Ids.get("josh"); + String peterId = name2Ids.get("peter"); + Response r = client().get(PATH, ImmutableMap.of("vertex", + id2Json(markoId), + "other", + id2Json(joshId))); + String content = assertResponseStatus(200, r); + assertJsonContains(content, "resource_allocation"); + } +} diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/TraversersApiTestSuite.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/TraversersApiTestSuite.java index 5ca1bef647..b29b685500 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/TraversersApiTestSuite.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/traversers/TraversersApiTestSuite.java @@ -43,7 +43,9 @@ ShortestPathApiTest.class, SingleSourceShortestPathApiTest.class, TemplatePathsApiTest.class, - WeightedShortestPathApiTest.class + WeightedShortestPathApiTest.class, + AdamicAdarAPITest.class, + ResourceAllocationAPITest.class }) public class TraversersApiTestSuite { } From 2fc2cc9ab7ec9089c607b21ec4db586610cc560c Mon Sep 17 00:00:00 2001 From: imbajin Date: Tue, 22 Feb 2022 15:37:24 +0800 Subject: [PATCH 2/3] update comment --- .../com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java | 7 ++----- .../hugegraph/api/traversers/ResourceAllocationAPI.java | 7 ++----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java index cc1f4c6237..cb4e2e0f01 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java @@ -45,11 +45,8 @@ import com.google.common.collect.ImmutableMap; /** - * This API include similar prediction algorithms, now include: - * - Adamic Adar - * - Resource Allocation - * - * Could add more prediction algorithms in future + * AdamicAdar is one of the prediction algorithms in graph, you can get more + * info and definition in wikipedia */ @Path("graphs/{graph}/traversers/adamicadar") @Singleton diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java index f476faddcf..94244b7125 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java @@ -45,11 +45,8 @@ import com.google.common.collect.ImmutableMap; /** - * This API include similar prediction algorithms, now include: - * - Adamic Adar - * - Resource Allocation - * - * Could add more prediction algorithms in future + * ResourceAllocation is one of the prediction algorithms in graph, you can get + * more info and definition in wikipedia */ @Path("graphs/{graph}/traversers/resourceallocation") @Singleton From 3fea9a5ecff4676e19d6e5171241edf3df6f2bb2 Mon Sep 17 00:00:00 2001 From: imbajin Date: Tue, 22 Feb 2022 18:13:58 +0800 Subject: [PATCH 3/3] avoid zero --- .../baidu/hugegraph/api/traversers/AdamicAdarAPI.java | 3 ++- .../api/traversers/ResourceAllocationAPI.java | 3 ++- .../traversal/algorithm/PredictionTraverser.java | 10 ++++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java index cb4e2e0f01..2c063264f0 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/AdamicAdarAPI.java @@ -46,7 +46,8 @@ /** * AdamicAdar is one of the prediction algorithms in graph, you can get more - * info and definition in wikipedia + * info and definition in: + * https://en.wikipedia.org/wiki/Adamic/Adar_index */ @Path("graphs/{graph}/traversers/adamicadar") @Singleton diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java index 94244b7125..d272b5f667 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/ResourceAllocationAPI.java @@ -46,7 +46,8 @@ /** * ResourceAllocation is one of the prediction algorithms in graph, you can get - * more info and definition in wikipedia + * more info and definition in: + * https://arxiv.org/pdf/0901.0553.pdf */ @Path("graphs/{graph}/traversers/resourceallocation") @Singleton diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java index 231fe5cddf..da6e924a8f 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/algorithm/PredictionTraverser.java @@ -43,7 +43,10 @@ public double adamicAdar(Id source, Id target, Directions dir, double sum = 0.0; for (Id vid : neighbors) { - sum += 1.0 / Math.log(this.edgesCount(vid, step)); + long currentDegree = this.edgesCount(vid, step); + if (currentDegree > 0) { + sum += 1.0 / Math.log(currentDegree); + } } return sum; } @@ -57,7 +60,10 @@ public double resourceAllocation(Id source, Id target, Directions dir, double sum = 0.0; for (Id vid : neighbors) { - sum += 1.0 / this.edgesCount(vid, step); + long currentDegree = this.edgesCount(vid, step); + if (currentDegree > 0) { + sum += 1.0 / currentDegree; + } } return sum; }