I used JUNG for creating a large graph. I want to find a specific directed edge between each two nodes and set edge weight as specific parameter. The problem is findEdge method and the edges that are exported from created graph are not same (same value but not same object). This problem can result to null pointer exception! Is there anyway that I can change it for being same objects?! Here is my code:

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);
            }
        }
    }

In this case edge object is null which it should not be. Regards.

有帮助吗?

解决方案

As @whoAmI pointed out, assuming that the network is directed, you want to be asking for the successors rather than the neighbors.

However, there is a more elegant and efficient way of doing this. What you actually want is the outgoing edges, so ask for them directly:

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);
}

You shouldn't need to ask if the outgoing edges (or the neighbors) are null, because that should always return an empty Collection if there are no edges/neighbors.

That said, this logic doesn't make sense unless every Customer has only one incoming Transaction; otherwise each incoming edge will be setting the churnProb value of its target, and only the last value will be retained. You may want to look into this.

其他提示

getNeighbors returns all neighbors, even if the edge is directed from neighbor to customer.

findEdge returns an edge only if the edge is directed from customer to neighbor.

So this different is the cause for getting null edge between customer and neighbor.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top