-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ShowSelectedTracksAction: don't get stuck if there's a loop in the graph #229
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dfeb54d
ShowSelectedTracksAction: don't get stuck if there's a loop in the graph
maarzt c2d8ac1
ExampleGraph1, ExampleGraph2: make spots more accessible
maarzt 20cd749
ShowSelectedTrackActions: extract method findSelectedSubtreeRoots
maarzt 617a256
Refactor TreeUtils.findSelectedSubtreeRoots(...)
maarzt 4dde1ad
Add TreeUtils.findRealRoots(...) method
maarzt a8bc77f
Refactor and bug fix in TreeUtils
maarzt ced4b96
TreeUitls: improve javadoc
maarzt cafe95b
TreeUtilsTest: rename test methods
maarzt e49cb67
TreeUtils: change semantics of findRootsOfTheGivenNodes
maarzt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package org.mastodon.util; | ||
|
||
import java.util.Collection; | ||
|
||
import org.mastodon.collection.RefCollections; | ||
import org.mastodon.collection.RefList; | ||
import org.mastodon.collection.RefSet; | ||
import org.mastodon.collection.RefStack; | ||
import org.mastodon.graph.Edge; | ||
import org.mastodon.graph.Graph; | ||
import org.mastodon.graph.Vertex; | ||
|
||
public class TreeUtils | ||
{ | ||
|
||
private TreeUtils() | ||
{ | ||
// prevent instantiation of utility class | ||
} | ||
|
||
/** | ||
* This method finds a subset of the given {@code selectedVertices} that | ||
* contains only the roots of the subtrees that are selected. The order | ||
* of the returned list follows the order of the {@code roots} list and | ||
* the order of the outgoing edges of the graphs vertices. | ||
* <p> | ||
* Example {@code graph}: | ||
* <pre> | ||
* A B | ||
* / \ / \ | ||
* a1 a2 b1 b2 | ||
* | / \ / \ | ||
* a3 b3 b4 b5 b6 | ||
* / \ | ||
* a4 a5 | ||
* </pre> | ||
* | ||
* If {@code selectedVertices} contains: {@code {a2, a4, a5, b1, b3, b4, b6}}, | ||
* then the returned list will be: {@code [a2, b1, b6]}. | ||
*/ | ||
public static < V extends Vertex< ? > > RefList< V > findSelectedSubtreeRoots( | ||
final Graph< V, ? > graph, | ||
final RefList< V > roots, | ||
final RefSet< V > selectedVertices ) | ||
{ | ||
final RefList< V > selectedSubtreeRoots = RefCollections.createRefList( graph.vertices() ); | ||
final RefSet< V > visitedNodes = RefCollections.createRefSet( graph.vertices() ); | ||
|
||
for ( final V root : roots ) | ||
for ( final DepthFirstIteration.Step< V > step : DepthFirstIteration.forRoot( graph, root ) ) | ||
if ( ensureNoLoop( step, visitedNodes ) && !step.isSecondVisit() ) { | ||
|
||
final V node = step.node(); | ||
if ( selectedVertices.contains( node ) ) | ||
{ | ||
selectedSubtreeRoots.add( node ); | ||
step.truncate(); // don't visit the child nodes | ||
} | ||
} | ||
|
||
return selectedSubtreeRoots; | ||
} | ||
|
||
private static < V extends Vertex< ? > > boolean ensureNoLoop( DepthFirstIteration.Step< V > step, RefSet< V > visitedNodes ) | ||
{ | ||
if ( !step.isFirstVisit() ) | ||
return true; | ||
|
||
boolean isLoop = !visitedNodes.add( step.node() ); // The depth first iteration enters a node for the second time. -> there's a loop. | ||
if ( isLoop ) | ||
step.truncate(); // Break the loop by not visiting the child nodes. | ||
|
||
return !isLoop; | ||
} | ||
|
||
maarzt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* This method returns the root nodes of the trees containing any of the | ||
* given {@code nodes}. | ||
* <p> | ||
* Example: | ||
* <pre> | ||
* A B | ||
* / \ / \ | ||
* a1 a2 b1 b2 | ||
* | / \ / \ | ||
* a3 b3 b4 b5 b6 | ||
* / \ | ||
* a4 a5 | ||
* </pre> | ||
* <p> | ||
* If {@code nodes} contains {@code {a2, a4}} then the method will return | ||
* {@code {A}}. | ||
* <p> | ||
* If {@code nodes} contains {@code {a2, a4, b6}} then the method will | ||
* return {@code {A, B}}. | ||
*/ | ||
public static <V extends Vertex<E>, E extends Edge< V > > RefSet< V > findRootsOfTheGivenNodes( Graph< V, E > graph, Collection< V > nodes ) | ||
{ | ||
return filterRoots( graph, computePredecessors( graph, nodes ) ); | ||
} | ||
|
||
/** | ||
* @return the set of predecessors of the given {@code nodes}. Please note | ||
* that returned set also contains all the given {@code nodes}. | ||
*/ | ||
private static < V extends Vertex<E>, E extends Edge< V > > RefSet< V > computePredecessors( Graph< V, E > graph, Collection< V > nodes ) | ||
{ | ||
// The following code performs an inverse depth first search starting | ||
// from the given nodes. The set of visited nodes is returned. | ||
V ref = graph.vertexRef(); | ||
V ref2 = graph.vertexRef(); | ||
try | ||
{ | ||
final RefSet< V > visited = RefCollections.createRefSet( graph.vertices() ); | ||
visited.addAll( nodes ); | ||
final RefStack< V > stack = RefCollections.createRefStack( graph.vertices() ); | ||
stack.addAll( visited ); | ||
while ( ! stack.isEmpty() ) { | ||
V node = stack.pop( ref ); | ||
for ( E edge : node.incomingEdges() ) { | ||
V previousNode = edge.getSource( ref2 ); | ||
boolean firstVisit = visited.add( previousNode ); | ||
if ( firstVisit ) | ||
stack.add( previousNode ); | ||
} | ||
} | ||
return visited; | ||
} | ||
finally | ||
{ | ||
graph.releaseRef( ref ); | ||
graph.releaseRef( ref2 ); | ||
} | ||
} | ||
|
||
/** | ||
* @return a subset of the given {@code nodes} that contains only those | ||
* nodes that are roots of the {@code graph}. (A note is considered a root | ||
* if it has no incoming edges.) | ||
*/ | ||
private static < V extends Vertex<E>, E extends Edge< V > > RefSet< V > filterRoots( Graph< V, E > graph, Collection< V > nodes ) | ||
{ | ||
final RefSet< V > roots = RefCollections.createRefSet( graph.vertices() ); | ||
for ( V node : nodes ) | ||
if ( node.incomingEdges().isEmpty() ) | ||
roots.add( node ); | ||
return roots; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
mastodon-graph
lib there is RootFinder:https://github.com/mastodon-sc/mastodon-graph/blob/master/src/main/java/org/mastodon/graph/algorithm/RootFinder.java
Maybe we could merge these two?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you would like to suggest. Would you like to have a method
TreeUtils.getRoots(graph)
that replacesnew RootFinder(graph).get()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly. Do you think it is a good fit? One fewer class.... that would be nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make sense organize the utility classes, but maybe not as a part of this PR.
There is already a ticket mastodon-sc/mastodon-tomancak#13, we should have a look at the RootFinder as part of this.