From: Joris K. <de...@gm...> - 2013-04-26 11:01:35
|
The answer to your question is in the jgrapht javadoc: boolean *addVertex*(V <http://jgrapht.org/javadoc/org/jgrapht/Graph.html> 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 <ara...@ya...>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 <de...@gm...> > *To:* arastoo bozorgi <ara...@ya...> > *Cc:* "jgr...@li..." < > jgr...@li...> > *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 <ara...@ya...>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 > jgr...@li... > https://lists.sourceforge.net/lists/listinfo/jgrapht-users > > > > > |