I have the following code I'm struggling to understand. I have one interface declared as below:
public interface WeightedRelationshipConsumer {
boolean accept(int sourceNodeId, int targetNodeId, long relationId, double weight);
}
Then I have a second interface which accepts WeightedRelationshipConsumer declared as below:
public interface WeightedRelationshipIterator {
void forEachRelationship(int nodeId, Direction direction, WeightedRelationshipConsumer consumer);
}
Then in an implementation of Dijkstra's algorithm, I have the following code:
private void run(int goal, Direction direction) {
// `queue` is a Priority Queue that contains the vertices of the graph
while (!queue.isEmpty()) {
int node = queue.pop();
if (node == goal) {
return;
}
// `visited` is a BitSet.
visited.put(node);
// Gets the weight of the distance current node from a node-distance map.
double costs = this.costs.getOrDefault(node, Double.MAX_VALUE);
graph.forEachRelationship(
node,
direction, (source, target, relId, weight) -> {
updateCosts(source, target, weight + costs);
if (!visited.contains(target)) {
queue.add(target, 0);
}
return true;
});
}
}
It's the
graph.forEachRelationship(node, direction, (source, target, relId, weight) -> {
updateCosts(source, target, weight + costs);
if (!visited.contains(target)) {
queue.add(target, 0);
}
return true;
});
that confuses me. Specifically, what are the source, target, relId, weight and how are they resolved? These 4 variables are not defined anywhere else within this class. updateCosts() is as follows:
private void updateCosts(int source, int target, double newCosts) {
double oldCosts = costs.getOrDefault(target, Double.MAX_VALUE);
if (newCosts < oldCosts) {
costs.put(target, newCosts);
path.put(target, source);
}
}
Also, if there are resources that may be helpful to understand this type of code, please provide. Thanks.