Re: [jgrapht-users] getEdgeWeight in weighted Graph
Brought to you by:
barak_naveh,
perfecthash
From: Javier MV <jav...@dc...> - 2014-03-21 19:56:03
|
+1 On Fri, Mar 21, 2014 at 4:01 PM, Georg Hieronimus < geo...@go...> wrote: > i strongly agree with your suggestion. > i already had the problem by myself, but forgot to write a pull request > afterwards .... > It's quiet horrible to find bugs, if you dont know about the current > behavior. > > > On 21 March 2014 19:56, Joris Kinable <de...@gm...> wrote: > >> Dear, >> >> In the latest version of jgrapht (and perhaps also in other versions), >> the getEdgeWeight(E e) function is implemented as follows: >> >> public double getEdgeWeight(E e) >> { >> if (e instanceof DefaultWeightedEdge) { >> return ((DefaultWeightedEdge) e).getWeight(); >> } else { >> return WeightedGraph.DEFAULT_EDGE_WEIGHT; >> } >> } >> >> >> If e==null, i.e. the edge does not exist, then this function will return >> DEFAULT_EDGE_WEIGHT. This is strange as often invoking the getEdgeWeight >> function on a non-existing edge is simply a bug. >> >> I would suggest to modify the above function to something like: >> public double getEdgeWeight(E e) >> { >> if (e instanceof DefaultWeightedEdge) { >> return ((DefaultWeightedEdge) e).getWeight(); >> } else if(e==null){ >> throw new UnsupportedArgumentException("Edge cannot be null"); >> }else{ >> return WeightedGraph.DEFAULT_EDGE_WEIGHT; >> } >> } >> >> One common error would be something like: >> Edge e=graph.getEdge(i,j); //Edge (i,j) is not part of the graph, so >> e==null; >> cost += graph.getEdgeWeight(e); //Correct, does not invoke an exception >> >> >> At the very least, the javadoc for the getEdgeWeight function should >> mention the above pitfall by specify that invoking the getEdgeWeight >> function on null returns WeightedGraph.DEFAULT_EDGE_WEIGHT so it's the >> user's problem to check whether the edge is not equal to null. >> >> This is just a suggestion. If there is support for this change, I'll make >> a change request. >> >> br, >> >> Joris >> >> >> ------------------------------------------------------------------------------ >> Learn Graph Databases - Download FREE O'Reilly Book >> "Graph Databases" is the definitive new guide to graph databases and their >> applications. Written by three acclaimed leaders in the field, >> this first edition is now available. Download your free book today! >> http://p.sf.net/sfu/13534_NeoTech >> _______________________________________________ >> jgrapht-users mailing list >> jgr...@li... >> https://lists.sourceforge.net/lists/listinfo/jgrapht-users >> >> > > > ------------------------------------------------------------------------------ > Learn Graph Databases - Download FREE O'Reilly Book > "Graph Databases" is the definitive new guide to graph databases and their > applications. Written by three acclaimed leaders in the field, > this first edition is now available. Download your free book today! > http://p.sf.net/sfu/13534_NeoTech > _______________________________________________ > jgrapht-users mailing list > jgr...@li... > https://lists.sourceforge.net/lists/listinfo/jgrapht-users > > -- Javier MV |