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

WeightedEdge abstraction #3

Open
trs123 opened this issue Dec 19, 2014 · 2 comments
Open

WeightedEdge abstraction #3

trs123 opened this issue Dec 19, 2014 · 2 comments

Comments

@trs123
Copy link
Collaborator

trs123 commented Dec 19, 2014

Java seems not to have an abstraction of numbers or arithmetic values.

I first thought that the class Numbers would fill this role and designed a WeightedEdge<V, W extends Number> like this

public interface WeightedEdge<V, W extends Number> {
    W weight();
}

but this does not allow to formulate calculations with the weights, i.e. something like

public interface WeightedPath<V, E extends WeightedEdge<V, W>, W extends Number> 
    extends Path<V, E> {

    default W distance() {
        // Problem: Number.plus() does not exist
        return edges().reduce((sum, edge) -> sum.plus(edge.weight())); 
    }
}

So do we have to declare WeightedEdge to always use double for weights??

@monora
Copy link
Owner

monora commented Dec 22, 2014

(In reply to comment #0)

So do we have to declare WeightedEdge to always use double for weights??

That is how JGraphT does it (see DefaultWeightedEdge). I think this ok, since every metric on a graph is a mapping V x V -> real numbers.

@trs123
Copy link
Collaborator Author

trs123 commented Dec 27, 2014

Double is not really satisfying, as I can easily imagine integers, magnitudes with units or even more abstract magnitudes as weights. Of course you can say this can be mapped to real numbers, unfortunately doubles are not real numbers: just think of the problems with precision that doubles have.
Furthermore you just lose type information when doing this mapping.

So I'm not pleased with limiting weights to doubles at all :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants