Re: [jgrapht-users] Extracting parts of the graph
Brought to you by:
barak_naveh,
perfecthash
From: J K. <j.k...@gm...> - 2017-09-14 11:13:41
|
Currently there's no build-in function which can do that automatically for you. We are currently making this functionality available somewhere in the next few weeks, see Pull Request: https://github.com/jgrapht/jgrapht/pull/423 In the mean time, to get the desired behavior, you could implement your own DepthFirstSearch iterator. Or more conveniently, just copy mine: https://github.com/jkinable/jgrapht/blob/95daef5a6826818d00e113ac610415a763c5aee0/jgrapht-core/src/main/java/org/jgrapht/traverse/DepthFirstIterator.java This iterator has a new function getDepth(V v) which allows you to query the depth of a specific node (after the DFS completes, or after the node has been visited). So in order to query all nodes at a certain depth, you could write an additional function: //cache the nodes at a given depth Map<Integer,Set<V>> depthMap=new HashMap<>(); for(V v : graph.vertexSet()){ int depth=dfs.getDepth(v); if(!depthMap.containsKey(depth)) depthMap.put(depth, new HashSet<V>()); depthMap.get(depth).add(v); } Now you can easily query all nodes at a given depth: depthMap.get(3); On Thu, Sep 14, 2017 at 1:00 PM, Bobi Adji Andov <bob...@gm...> wrote: > Hello all, > > I am a new user to JGraphT and as every other newbie I am running into > ideas that I would like to solve with this library, but I am still not sure > how to do that. > > I am building a directed graph and I am wandering is there a way to get > all the nodes at certain depth (starting from a vertex). For example > something like get all nodes at depth 3 from certain vertex. > > Thanks in advance, > Slobodan > > -- > > > > *Slobodan Adji-Andovskype:bobiadziandov* > > ------------------------------------------------------------ > ------------------ > Check out the vibrant tech community on one of the world's most > engaging tech sites, Slashdot.org! http://sdm.link/slashdot > _______________________________________________ > jgrapht-users mailing list > jgr...@li... > https://lists.sourceforge.net/lists/listinfo/jgrapht-users > > |