getEdgeWeight in weighted Graph

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

getEdgeWeight in weighted Graph

Joris Kinable
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: getEdgeWeight in weighted Graph

Georg Hieronimus-2
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 <[hidden email]> 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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: getEdgeWeight in weighted Graph

Javier MV
+1


On Fri, Mar 21, 2014 at 4:01 PM, Georg Hieronimus <[hidden email]> 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 <[hidden email]> 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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users




--
Javier MV

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: getEdgeWeight in weighted Graph

Alex Moir

Good catch, I agree.

 

From: Javier MV [mailto:[hidden email]]
Sent: Friday, March 21, 2014 12:56 PM
To: [hidden email]
Subject: Re: [jgrapht-users] getEdgeWeight in weighted Graph

 

+1

 

On Fri, Mar 21, 2014 at 4:01 PM, Georg Hieronimus <[hidden email]> 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 <[hidden email]> 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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users




--
Javier MV


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: getEdgeWeight in weighted Graph

John Sichi
Administrator
I'll make the change (but I think for consistency with existing code,
I'll use NullPointerException instead of
UnsupportedArgumentException).

On Fri, Mar 21, 2014 at 1:02 PM, Alex Moir
<[hidden email]> wrote:

> Good catch, I agree.
>
>
>
> From: Javier MV [mailto:[hidden email]]
> Sent: Friday, March 21, 2014 12:56 PM
> To: [hidden email]
> Subject: Re: [jgrapht-users] getEdgeWeight in weighted Graph
>
>
>
> +1
>
>
>
> On Fri, Mar 21, 2014 at 4:01 PM, Georg Hieronimus
> <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jgrapht-users
>
>
>
>
> --
> Javier MV
>
>
> ------------------------------------------------------------------------------
> 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
> [hidden email]
> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: getEdgeWeight in weighted Graph

Joris Kinable
Hi John,

Thanks for your effort! A NullPointerException if perfectly fine and indeed a better choice than the UnsupportedArgumentException.

br,

Joris


On Fri, Mar 21, 2014 at 6:14 PM, John Sichi <[hidden email]> wrote:
I'll make the change (but I think for consistency with existing code,
I'll use NullPointerException instead of
UnsupportedArgumentException).

On Fri, Mar 21, 2014 at 1:02 PM, Alex Moir
<[hidden email]> wrote:
> Good catch, I agree.
>
>
>
> From: Javier MV [mailto:[hidden email]]
> Sent: Friday, March 21, 2014 12:56 PM
> To: [hidden email]
> Subject: Re: [jgrapht-users] getEdgeWeight in weighted Graph
>
>
>
> +1
>
>
>
> On Fri, Mar 21, 2014 at 4:01 PM, Georg Hieronimus
> <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jgrapht-users
>
>
>
>
> --
> Javier MV
>
>
> ------------------------------------------------------------------------------
> 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
> [hidden email]
> 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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users