Created
November 24, 2011 15:51
-
-
Save pdobrev/1391654 to your computer and use it in GitHub Desktop.
Testing Neo4j shortestPath vs allSimplePaths
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
import java.util.Collection; | |
import org.neo4j.graphalgo.PathFinder; | |
import org.neo4j.graphalgo.impl.path.AllSimplePaths; | |
import org.neo4j.graphalgo.impl.path.ShortestPath; | |
import org.neo4j.graphdb.DynamicRelationshipType; | |
import org.neo4j.graphdb.Expander; | |
import org.neo4j.graphdb.GraphDatabaseService; | |
import org.neo4j.graphdb.Node; | |
import org.neo4j.graphdb.Path; | |
import org.neo4j.graphdb.Relationship; | |
import org.neo4j.graphdb.Transaction; | |
import org.neo4j.kernel.EmbeddedGraphDatabase; | |
import org.neo4j.kernel.Traversal; | |
import scala.Int; | |
public class Main { | |
public static void main(String[] args) { | |
GraphDatabaseService graphDb = new EmbeddedGraphDatabase("C:\\neo4j-community-1.5\\data\\graph.db"); | |
registerShutdownHook(graphDb); | |
Transaction tx = graphDb.beginTx(); | |
try { | |
// Setting up the experiment | |
Expander expander; | |
expander = Traversal.emptyExpander(); | |
expander = expander.add(DynamicRelationshipType.withName("PERSON_ORG")); | |
expander = expander.add(DynamicRelationshipType.withName("PERSON_PERSON")); | |
expander = expander.add(DynamicRelationshipType.withName("User_relation_6")); | |
int maxDepth = 4; | |
Node source = graphDb.getNodeById(2624016); | |
Node target = graphDb.getNodeById(2161112); | |
System.out.println("Source relationships:"); | |
for (Relationship r : source.getRelationships()) { | |
System.out.println(r.getEndNode().getId()); | |
} | |
System.out.println(); | |
////// | |
// First case: shortest path | |
System.out.println("Shortest path case"); | |
PathFinder<Path> shortestPath = new ShortestPath(maxDepth, expander); | |
Iterable<Path> paths = (Collection<Path>) shortestPath | |
.findAllPaths(source, target); | |
// get the shortest path length | |
int pathLength = paths.iterator().next().length(); | |
for (Path p : paths) { | |
System.out.println(p.toString()); | |
} | |
pathLength++; | |
while (pathLength <= maxDepth) { | |
PathFinder<Path> fixedLengthPaths = new ShortestPath( | |
pathLength, expander, Int.MaxValue(), true); | |
paths = fixedLengthPaths.findAllPaths(source, target); | |
for (Path p : paths) { | |
System.out.println(p.toString()); | |
} | |
pathLength++; | |
} | |
////// | |
// Second case: all simple paths | |
System.out.println("All simple paths case"); | |
PathFinder<Path> allSimplePaths = new AllSimplePaths(maxDepth, | |
expander); | |
for (Path p : allSimplePaths.findAllPaths(source, target)) { | |
System.out.println(p.toString()); | |
} | |
tx.success(); | |
} catch (Exception e) { | |
System.out.println(e.toString()); | |
} finally { | |
tx.finish(); | |
} | |
graphDb.shutdown(); | |
} | |
private static void registerShutdownHook(final GraphDatabaseService graphDb) { | |
// Registers a shutdown hook for the Neo4j instance so that it | |
// shuts down nicely when the VM exits (even if you "Ctrl-C" the | |
// running example before it's completed) | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
@Override | |
public void run() { | |
graphDb.shutdown(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment