Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(algorithm): support single source shortest path algorithm #285

Merged
merged 24 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
28691fd
feat: ListValue add clear() method
diaohancai Nov 21, 2023
d9c5c84
feat: Source Target Shortest Path
diaohancai Nov 21, 2023
3aff95d
test: test data is complicated
diaohancai Nov 21, 2023
b6c0f4c
test: ListValue.clear unit test
diaohancai Nov 21, 2023
f5a5de9
fix: ring loop
diaohancai Nov 21, 2023
4c37b2d
optimize: message combine
diaohancai Nov 21, 2023
15f95f6
Merge branch 'master' into pr/285
imbajin Dec 4, 2023
6630b82
refactor(algorithm): Single Source Shortest Path
diaohancai Dec 26, 2023
9524900
refactor(algorithm): output json
diaohancai Dec 26, 2023
2804bd7
feat(algorithm): multiple target optimization
diaohancai Dec 26, 2023
cdaba23
feat(core): IdList Merge Combiner
diaohancai Dec 26, 2023
125bba6
chore(algorithm): simple adjustments
diaohancai Dec 31, 2023
9587802
optimization(algorithm): change reachedTargets from IdList to IdSet
diaohancai Jan 1, 2024
01cde50
chore: json style key
diaohancai Jan 22, 2024
6b42bf5
improve: convert id from string to ID with type
diaohancai Jan 25, 2024
55b0ed3
chore: add IdUtilTest unit test
diaohancai Jan 25, 2024
2828bb7
fix: all targets reached
diaohancai Feb 6, 2024
a0a0103
improve: remove unnecessary member var
diaohancai Feb 6, 2024
c4ca8fe
improve: source vertex and target vertex specify idType
diaohancai Feb 6, 2024
174584e
chore: get properties
diaohancai Feb 7, 2024
aa8238d
improve: input vertex id parse
diaohancai Feb 29, 2024
0b1c720
chore: log improvement
diaohancai Feb 29, 2024
6510424
test: apply exception testing
diaohancai Mar 3, 2024
d55c7c7
test: parse empty id throws IllegalArgumentException
diaohancai Mar 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 org.apache.hugegraph.computer.algorithm.path.shortest;

import org.apache.hugegraph.computer.core.combiner.Combiner;

public class ShortestPathCombiner implements Combiner<ShortestPathMessage> {

@Override
public void combine(ShortestPathMessage v1, ShortestPathMessage v2,
ShortestPathMessage result) {
ShortestPathMessage message = v2.totalWeight() < v1.totalWeight() ? v2 : v1;
result.value(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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 org.apache.hugegraph.computer.algorithm.path.shortest;

import java.io.IOException;
import java.util.List;

import org.apache.hugegraph.computer.core.graph.value.DoubleValue;
import org.apache.hugegraph.computer.core.graph.value.IdList;
import org.apache.hugegraph.computer.core.graph.value.ListValue;
import org.apache.hugegraph.computer.core.graph.value.Value;
import org.apache.hugegraph.computer.core.graph.vertex.Vertex;
import org.apache.hugegraph.computer.core.io.RandomAccessInput;
import org.apache.hugegraph.computer.core.io.RandomAccessOutput;

public class ShortestPathMessage implements Value.CustomizeValue<List<Object>> {

/**
* path
*/
private final IdList path;

/**
* weight of path
*/
private final ListValue<DoubleValue> pathWeight;

public ShortestPathMessage() {
this.path = new IdList();
this.pathWeight = new ListValue<DoubleValue>();
}

@Override
public void read(RandomAccessInput in) throws IOException {
this.path.read(in);
this.pathWeight.read(in);
}

@Override
public void write(RandomAccessOutput out) throws IOException {
this.path.write(out);
this.pathWeight.write(out);
}

@Override
public List<Object> value() {
return this.path.value();
}

public void value(ShortestPathMessage message) {
this.path.clear();
this.pathWeight.clear();

this.path.addAll(message.path().copy().values());
this.pathWeight.addAll(message.pathWeight().copy().values());
}

public void addToPath(Vertex vertex, double weight) {
this.path.add(vertex.id());
this.pathWeight.add(new DoubleValue(weight));
}

public void addToPath(IdList path, ListValue<DoubleValue> pathWeight,
Vertex vertex, double weight) {
this.path.addAll(path.copy().values());
this.pathWeight.addAll(pathWeight.copy().values());

this.path.add(vertex.id());
this.pathWeight.add(new DoubleValue(weight));
diaohancai marked this conversation as resolved.
Show resolved Hide resolved
}

public IdList path() {
return this.path;
}

public ListValue<DoubleValue> pathWeight() {
return this.pathWeight;
}

public double totalWeight() {
double totalWeight = 0;
for (int i = 0; i < this.pathWeight.size(); ++i) {
totalWeight += this.pathWeight.get(i).doubleValue();
}
return totalWeight;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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 org.apache.hugegraph.computer.algorithm.path.shortest;

import java.io.IOException;
import java.util.List;

import org.apache.hugegraph.computer.core.graph.value.DoubleValue;
import org.apache.hugegraph.computer.core.graph.value.IdList;
import org.apache.hugegraph.computer.core.graph.value.ListValue;
import org.apache.hugegraph.computer.core.graph.value.Value;
import org.apache.hugegraph.computer.core.graph.vertex.Vertex;
import org.apache.hugegraph.computer.core.io.RandomAccessInput;
import org.apache.hugegraph.computer.core.io.RandomAccessOutput;

public class ShortestPathValue implements Value.CustomizeValue<List<Object>> {

/**
* path
*/
private final IdList path;

/**
* weight of path
*/
private final ListValue<DoubleValue> pathWeight;
diaohancai marked this conversation as resolved.
Show resolved Hide resolved

/**
* total weight.
* infinity(Double.MAX_VALUE) means unreachable.
*/
private final DoubleValue totalWeight;

public ShortestPathValue() {
this.path = new IdList();
this.pathWeight = new ListValue<DoubleValue>();
this.totalWeight = new DoubleValue(Double.MAX_VALUE);
}

@Override
public void read(RandomAccessInput in) throws IOException {
this.path.read(in);
this.pathWeight.read(in);
this.totalWeight.read(in);
}

@Override
public void write(RandomAccessOutput out) throws IOException {
this.path.write(out);
this.pathWeight.write(out);
this.totalWeight.write(out);
}

@Override
public List<Object> value() {
return this.path.value();
}

public void updatePath(Vertex vertex, double weight) {
diaohancai marked this conversation as resolved.
Show resolved Hide resolved
this.path.add(vertex.id());
this.pathWeight.add(new DoubleValue(weight));
this.totalWeight.value(this.getTotalWeight());
}

public void updatePath(IdList path, ListValue<DoubleValue> pathWeight, Vertex vertex) {
this.path.clear();
this.pathWeight.clear();

this.path.addAll(path.copy().values());
this.pathWeight.addAll(pathWeight.copy().values());

this.path.add(vertex.id());
this.totalWeight.value(this.getTotalWeight());
}

public IdList path() {
return this.path;
}

public ListValue<DoubleValue> pathWeight() {
return this.pathWeight;
}

public double totalWeight() {
return this.totalWeight.doubleValue();
}

private double getTotalWeight() {
diaohancai marked this conversation as resolved.
Show resolved Hide resolved
double totalWeight = 0;
for (int i = 0; i < this.pathWeight.size(); ++i) {
totalWeight += this.pathWeight.get(i).doubleValue();
}
return totalWeight;
}
}
Loading
Loading