Help about custom vertex

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Help about custom vertex

arastoo bozorgi
Hello
i am new in jgrapht and i have download it 2 days ago.
i want to make an undirect weighted graph with custom vertex structure. the vertex should have these properties:
1-string data
2-double teta
3-string status

i dont know how to define these vertex structure in JGrapht and make my graph with this vertex structure, also i dont know which type of graph should i use

please help me

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help about custom vertex

Joris Kinable
Dear,

Have a look at the hello world example: https://github.com/jgrapht/jgrapht/wiki/HelloWorld
There, an undirected graph is created where the vertices are of the type String.

Basically what you need to do is the following:
1. Create a new class that models your vertex, e.g.:
public class Vertex{
 String data;
 double teta;
 String status;
  public Vertex(){
     ...Constructor implementation here....
   }
}

Next you create a new undirected weighted graph using your newly created Vertex class as follows:
SimpleWeightedGraph<Vertex,DefaultWeightedEdge> myGraph=new SimpleWeightedGraph<Vertex,DefaultWeightedEdge>(DefaultWeightedEdge.class);

And then you can start adding vertices/edges, e.g.:
myGraph.addVertex(new Vertex());

br,

Joris
 


On Wed, Apr 24, 2013 at 7:22 PM, arastoo bozorgi <[hidden email]> wrote:
Hello
i am new in jgrapht and i have download it 2 days ago.
i want to make an undirect weighted graph with custom vertex structure. the vertex should have these properties:
1-string data
2-double teta
3-string status

i dont know how to define these vertex structure in JGrapht and make my graph with this vertex structure, also i dont know which type of graph should i use

please help me

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users



------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help about custom vertex

Joris Kinable
The answer to your question is in the jgrapht javadoc:

boolean addVertex(V v)
Adds the specified vertex to this graph if not already present. More formally, adds the specified vertex, v, to this graph if this graph contains no vertex u such that u.equals(v). If this graph already contains such vertex, the call leaves this graph unchanged and returns false. In combination with the restriction on constructors, this ensures that graphs never contain duplicate vertices.


So your own Vertex class needs to override the equals (and also the hashcode) methods. See for example this tutorial on how to do that: http://javarevisited.blogspot.be/2011/02/how-to-write-equals-method-in-java.html

Note that it is very important that your own Vertex class overrides both equals and hashcode, otherwise, the addVertex(V v) won't work as expected (as you noticed), but also methods like containsVertex(V v) will fail. 

br,

Joris


On Fri, Apr 26, 2013 at 12:45 PM, arastoo bozorgi <[hidden email]> wrote:
hello Joris
thank you for your answer
But i have another problem. when i use the class as:
SimpleWeightedGraph<String, DefaultWeightedEdge> myGraph=new SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class)
and when i add a new vertex, it will check if there is a same node before, if a same node with this new one has inserted to the graph, the addVertex method does not insert the new node again.
but when i use a class as you said for my nodes:
SimpleWeightedGraph<Vertex,DefaultWeightedEdge> myGraph=new SimpleWeightedGraph<Vertex,DefaultWeightedEdge>(DefaultWeightedEdge.class)
when i insert a node that has been inserted before, it does not prevent inserting the node, because the new node is a new instance of the Vertex class, just thier data are the same, and by this, my graph become useless.
how can i chech that a new node is inserted to the graph or not

best wishes



From: Joris Kinable <[hidden email]>
To: arastoo bozorgi <[hidden email]>
Cc: "[hidden email]" <[hidden email]>
Sent: Thursday, April 25, 2013 8:10 PM
Subject: Re: [jgrapht-users] Help about custom vertex

Dear,

Have a look at the hello world example: https://github.com/jgrapht/jgrapht/wiki/HelloWorld
There, an undirected graph is created where the vertices are of the type String.

Basically what you need to do is the following:
1. Create a new class that models your vertex, e.g.:
public class Vertex{
 String data;
 double teta;
 String status;
  public Vertex(){
     ...Constructor implementation here....
   }
}

Next you create a new undirected weighted graph using your newly created Vertex class as follows:
SimpleWeightedGraph<Vertex,DefaultWeightedEdge> myGraph=new SimpleWeightedGraph<Vertex,DefaultWeightedEdge>(DefaultWeightedEdge.class);

And then you can start adding vertices/edges, e.g.:
myGraph.addVertex(new Vertex());

br,

Joris
 


On Wed, Apr 24, 2013 at 7:22 PM, arastoo bozorgi <[hidden email]> wrote:
Hello
i am new in jgrapht and i have download it 2 days ago.
i want to make an undirect weighted graph with custom vertex structure. the vertex should have these properties:
1-string data
2-double teta
3-string status

i dont know how to define these vertex structure in JGrapht and make my graph with this vertex structure, also i dont know which type of graph should i use

please help me

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users






------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Help about custom vertex

Joris Kinable
From the looks of it, your error is probably in the equals() method. In your initial post, you wrote that the data type of your variable data is of the type String.
In your code, you write:

Node temp = (Node) obj;
return data == temp.data;

This obviously only works if your nodes have the same data object. For example:

public class Node{
  String data;
  public Node(String data){
     this.data=data;
  }
}

String s1="a";
String s2="a";

Node n1=new Node(s1);
Node n2=new Node(s2);
Node n3=new Node(s1);

n1.equals(n2); //This wil return false using your equals implementation.
n1.equals(n3); //This will return true.

You probably want this instead in your equals method:
 Node temp = (Node) obj;
  return data.equals(temp.data);


This issues however is a basic java implementation issues and doesn't really have anything to do with jgraph. Perhaps it would be better to restrict the conversation to jgraph related issues.


On Fri, Apr 26, 2013 at 3:17 PM, arastoo bozorgi <[hidden email]> wrote:
Hello
I have overwriten the equal and hashCode as below:

 @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj == null || obj.getClass() != this.getClass()) {
            return false;
        }

        Node temp = (Node) obj;
        return data == temp.data;

    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((data == null) ? 0 : data.hashCode());
        return result;


    }

its equal method works correct. alse i test the hashCode method. i make to nodes by my own Node Class and return their hash code, both of them are the same, but when i call the containsVertex(V v), it does not work correctlly.
i dont know where is my fault.


From: Joris Kinable <[hidden email]>
To: arastoo bozorgi <[hidden email]>; "[hidden email]" <[hidden email]>
Sent: Friday, April 26, 2013 3:31 PM

Subject: Re: [jgrapht-users] Help about custom vertex

The answer to your question is in the jgrapht javadoc:

boolean addVertex(V v)
Adds the specified vertex to this graph if not already present. More formally, adds the specified vertex, v, to this graph if this graph contains no vertex u such that u.equals(v). If this graph already contains such vertex, the call leaves this graph unchanged and returns false. In combination with the restriction on constructors, this ensures that graphs never contain duplicate vertices.


So your own Vertex class needs to override the equals (and also the hashcode) methods. See for example this tutorial on how to do that: http://javarevisited.blogspot.be/2011/02/how-to-write-equals-method-in-java.html

Note that it is very important that your own Vertex class overrides both equals and hashcode, otherwise, the addVertex(V v) won't work as expected (as you noticed), but also methods like containsVertex(V v) will fail. 

br,

Joris


On Fri, Apr 26, 2013 at 12:45 PM, arastoo bozorgi <[hidden email]> wrote:
hello Joris
thank you for your answer
But i have another problem. when i use the class as:
SimpleWeightedGraph<String, DefaultWeightedEdge> myGraph=new SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class)
and when i add a new vertex, it will check if there is a same node before, if a same node with this new one has inserted to the graph, the addVertex method does not insert the new node again.
but when i use a class as you said for my nodes:
SimpleWeightedGraph<Vertex,DefaultWeightedEdge> myGraph=new SimpleWeightedGraph<Vertex,DefaultWeightedEdge>(DefaultWeightedEdge.class)
when i insert a node that has been inserted before, it does not prevent inserting the node, because the new node is a new instance of the Vertex class, just thier data are the same, and by this, my graph become useless.
how can i chech that a new node is inserted to the graph or not

best wishes



From: Joris Kinable <[hidden email]>
To: arastoo bozorgi <[hidden email]>
Cc: "[hidden email]" <[hidden email]>
Sent: Thursday, April 25, 2013 8:10 PM
Subject: Re: [jgrapht-users] Help about custom vertex

Dear,

Have a look at the hello world example: https://github.com/jgrapht/jgrapht/wiki/HelloWorld
There, an undirected graph is created where the vertices are of the type String.

Basically what you need to do is the following:
1. Create a new class that models your vertex, e.g.:
public class Vertex{
 String data;
 double teta;
 String status;
  public Vertex(){
     ...Constructor implementation here....
   }
}

Next you create a new undirected weighted graph using your newly created Vertex class as follows:
SimpleWeightedGraph<Vertex,DefaultWeightedEdge> myGraph=new SimpleWeightedGraph<Vertex,DefaultWeightedEdge>(DefaultWeightedEdge.class);

And then you can start adding vertices/edges, e.g.:
myGraph.addVertex(new Vertex());

br,

Joris
 


On Wed, Apr 24, 2013 at 7:22 PM, arastoo bozorgi <[hidden email]> wrote:
Hello
i am new in jgrapht and i have download it 2 days ago.
i want to make an undirect weighted graph with custom vertex structure. the vertex should have these properties:
1-string data
2-double teta
3-string status

i dont know how to define these vertex structure in JGrapht and make my graph with this vertex structure, also i dont know which type of graph should i use

please help me

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users









------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Loading...