我使用 JUNG 创建了一个大图。我想找到每两个节点之间的特定有向边并将边权重设置为特定参数。问题是 findEdge 方法和从创建的图形导出的边不相同(值相同但对象不同)。这个问题可能会导致空指针异常!无论如何,我可以将其更改为相同的对象吗?!这是我的代码:
private void findNeighbors(Customer customer,
DirectedSparseGraph<Customer, Transaction> network) {
Collection<Customer> neighbors=network.getNeighbors(customer);
if(neighbors!=null){
for(Customer neighbor:neighbors){
Transaction edge=network.findEdge(customer, neighbor);
neighbor.setChurnProb(edge.getWeight());
churnedNet.add(neighbor);
}
}
}
在这种情况下,边缘对象为 null,但它不应该为 null。 问候。
最佳答案
正如 @whoAmI 所指出的,假设网络
是定向的,您需要询问后继者而不是邻居。
但是,有一种更优雅、更有效的方法可以做到这一点。您真正想要的是传出边缘,因此直接询问它们:
for (Transaction transaction : network.getOutgoingEdges(customer)) {
// get the other customer for this transaction
Customer otherCustomer = network.getOpposite(customer, transaction);
otherCustomer.setChurnProb(transaction.getWeight());
churnedNet.add(customer);
}
您不需要询问传出边缘(或邻居)是否为空,因为如果没有边缘/邻居,那么应该始终返回一个空集合。
也就是说,除非每个客户只有一个传入交易,否则这种逻辑没有意义;否则,每个传入边缘将设置其目标的 churnProb 值,并且仅保留最后一个值。您可能想调查一下。
关于java - 使用 JUNG 在 java 中需要相同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20757867/