From: Joris K. <de...@gm...> - 2012-10-18 13:11:08
|
Dear Alex, I'm doing something similar in my code. I'll show you what I did 1. I created a class Vertex, and two subclasses 'Student' and 'Stop', which both extend the class Vertex. 2. Next I created a class SBRGraph which extends ListenableUndirectedWeightedGraph<Vertex,DefaultWeightedEdge> 3. I wrote the following method: public <T extends Vertex> Set<T> getNeighbors(Vertex v, Class<T> vType){ if(!this.containsVertex(v)) return Collections.emptySet(); else{ Set<DefaultWeightedEdge> incidentEdges=this.edgesOf(v); Set<T> neighbors=new HashSet<T>(); for(DefaultWeightedEdge e: incidentEdges){ Vertex neighbor=(this.getEdgeTarget(e)!=v ? this.getEdgeTarget(e) : this.getEdgeSource(e)); if(neighbor.getClass() == vType) neighbors.add(vType.cast(neighbor)); } return neighbors; } } This method allows me to query neighbors of a given type. Let's say that I want to find all neighbors of the type Student for a given vertex v in the graph, then I would invoke: graph.getNeighbors(v,Student.class). The result would be an ArrayList<Student>. Obviously, if you just want to query the list of vertices of a given type, the easiest thing is to extend the addVertex() method in the graph and maintain lists of vertices of a specific type. br, Joris On Wed, Oct 17, 2012 at 4:03 AM, Alex Moir <am...@di...> wrote: > I am new to jgrapht. If I create a custom Vertex class that is abstract and > it has two sub classes, is there an easy way to get all the vertices of only > one of the subclass types? > > > > For example, if my custom vertex type is MyGenericVertex, and one subclass > is HumanVertex and the other is ComputerVertex. Is there a built in way to > efficiently get all the HumanVertex objects? > > > > I tried searching the archives for an answer to this and didn’t see one . > The closest thing I found was this topic: > > http://sourceforge.net/mailarchive/message.php?msg_id=22046693 > > > > Do I have to maintain my own data structures, one for HumanVertex and one > for ComputerVertex? > > > > Thanks, > > Alex > > > ------------------------------------------------------------------------------ > Everyone hates slow websites. So do we. > Make your web apps faster with AppDynamics > Download AppDynamics Lite for free today: > http://p.sf.net/sfu/appdyn_sfd2d_oct > _______________________________________________ > jgrapht-users mailing list > jgr...@li... > https://lists.sourceforge.net/lists/listinfo/jgrapht-users > |