What you are looking for is the shortest path between two nodes in a graph. If your graph is unweighted this comes down to a breadth first search.
I assume your relevance edges are weighted so you need an algorithm like dijkstra. Since you are most likely interested in the all pair shortest path problem because you might want to query for the shortest path between any two nodes later on you could use the floyd Warshall algorithm. This creates a matrix with all the shortest path values between all pairs of nodes.
If you import your graph to neo4j you can use their floyd warshall implementation. Be aware that the implementation of this algorithm is cubic in the number of vertices. So it really depends on your use case which of the above algorithms to choose.
I have some sample code (in neo4j 1.4) attached which shows how easy it is:
CostEvaluator<Double> costEvaluator = new CostEvaluator<Double>() {
public Double getCost(Relationship relationship, Direction direction) {
return -Math.log((Double) relationship.getProperty(Messages
.getString("RelProperty.Cost")));
}
};
CostAccumulator<Double> costAccumulator = new CostAccumulator<Double>() {
public Double addCosts(Double c1, Double c2) {
// TODO Auto-generated method stub
return c1 + c2;
}
};
Comparator<Double> costComparator = new Comparator<Double>() {
public int compare(Double o1, Double o2) {
// TODO Auto-generated method stub
return o1.compareTo(o2);
}
};
FloydWarshall<Double> fw = new FloydWarshall<Double>(1.0,
Double.MIN_VALUE, Direction.OUTGOING, costEvaluator,
costAccumulator, costComparator, this.currencySet,
this.tradeSet);
fw.reset();
fw.calculate();
you can then query your graph for any two nodes node1 and node2 with:
fw.getCost(node1, node2)
btw breadth first search can also easily be implemented in neo4j matching exactly your query by using the traverser framework or a simple cypher query