use of ai.grakn.graql.internal.gremlin.spanningtree.graph.NodeId in project grakn by graknlabs.
the class GreedyTraversalPlan method greedyTraversal.
private static void greedyTraversal(List<Fragment> plan, Arborescence<Node> arborescence, Map<NodeId, Node> nodes, Map<Node, Map<Node, Fragment>> edgeFragmentChildToParent) {
Map<Node, Set<Node>> edgesParentToChild = new HashMap<>();
arborescence.getParents().forEach((child, parent) -> {
if (!edgesParentToChild.containsKey(parent)) {
edgesParentToChild.put(parent, new HashSet<>());
}
edgesParentToChild.get(parent).add(child);
});
Node root = arborescence.getRoot();
Set<Node> reachableNodes = Sets.newHashSet(root);
while (!reachableNodes.isEmpty()) {
Optional<Node> optionalNodeWithMinCost = reachableNodes.stream().min(Comparator.comparingDouble(node -> branchWeight(node, arborescence, edgesParentToChild, edgeFragmentChildToParent)));
assert optionalNodeWithMinCost.isPresent() : "reachableNodes is never empty, so there is always a minimum";
Node nodeWithMinCost = optionalNodeWithMinCost.get();
// add edge fragment first, then node fragments
getEdgeFragment(nodeWithMinCost, arborescence, edgeFragmentChildToParent).ifPresent(plan::add);
addNodeFragmentToPlan(nodeWithMinCost, plan, nodes, true);
reachableNodes.remove(nodeWithMinCost);
if (edgesParentToChild.containsKey(nodeWithMinCost)) {
reachableNodes.addAll(edgesParentToChild.get(nodeWithMinCost));
}
}
}
use of ai.grakn.graql.internal.gremlin.spanningtree.graph.NodeId in project grakn by graknlabs.
the class GreedyTraversalPlan method addNodeFragmentToPlan.
private static void addNodeFragmentToPlan(Node node, List<Fragment> plan, Map<NodeId, Node> nodes, boolean visited) {
if (!visited) {
node.getFragmentsWithoutDependency().stream().min(Comparator.comparingDouble(Fragment::fragmentCost)).ifPresent(firstNodeFragment -> {
plan.add(firstNodeFragment);
node.getFragmentsWithoutDependency().remove(firstNodeFragment);
});
}
node.getFragmentsWithoutDependency().addAll(node.getFragmentsWithDependencyVisited());
plan.addAll(node.getFragmentsWithoutDependency().stream().sorted(Comparator.comparingDouble(Fragment::fragmentCost)).collect(Collectors.toList()));
node.getFragmentsWithoutDependency().clear();
node.getFragmentsWithDependencyVisited().clear();
node.getDependants().forEach(fragment -> {
Node otherNode = nodes.get(new NodeId(NodeId.NodeType.VAR, fragment.start()));
if (node.equals(otherNode)) {
otherNode = nodes.get(new NodeId(NodeId.NodeType.VAR, fragment.dependencies().iterator().next()));
}
otherNode.getDependants().remove(fragment.getInverse());
otherNode.getFragmentsWithDependencyVisited().add(fragment);
});
node.getDependants().clear();
}
use of ai.grakn.graql.internal.gremlin.spanningtree.graph.NodeId in project grakn by graknlabs.
the class ChuLiuEdmondsTest method testNegativeWeightWithNodeObject.
@Test
public void testNegativeWeightWithNodeObject() {
Map<NodeId, Node> nodes = new HashMap<>();
Node node0 = Node.addIfAbsent(NodeId.NodeType.VAR, var("0"), nodes);
Node node1 = Node.addIfAbsent(NodeId.NodeType.VAR, var("1"), nodes);
Node node2 = Node.addIfAbsent(NodeId.NodeType.VAR, var("2"), nodes);
final WeightedGraph<Node> Isa = SparseWeightedGraph.from(ImmutableList.of(weighted(DirectedEdge.from(node0).to(node1), -0.69), weighted(DirectedEdge.from(node1).to(node2), 0), weighted(DirectedEdge.from(node2).to(node1), -4.62), weighted(DirectedEdge.from(node1).to(node0), 0)));
Weighted<Arborescence<Node>> weightedSpanningTree = ChuLiuEdmonds.getMaxArborescence(Isa, node2);
assertEquals(-4.62, weightedSpanningTree.weight, DELTA);
ImmutableMap<Node, Node> edges = weightedSpanningTree.val.getParents();
assertEquals(2, edges.size());
assertEquals(node2, edges.get(node1));
assertEquals(node1, edges.get(node0));
}
Aggregations