Unique middle-man in traversal

89 views
Skip to first unread message

hyperutila

unread,
Apr 27, 2012, 7:57:17 PM4/27/12
to Neo4j
I have a graph where there are sometimes multiple relationships
between the same two nodes. I want to perform a traversal wherein one
of those relationships can be visited once, but once one is visited,
none of the others can be.

In other words, I don't want to implement uniqueness of relationships,
if you were to define a relationship as a means of getting from node A
to node B. Once the traversal has gone from node A to node B at some
point, it would never do that again, even if there are other
relationships that connect those same two nodes. Does that make sense?
How do I do this? Can I implement my own version of Uniqueness in
order to specify this behavior?

Thanks!

Peter Neubauer

unread,
Apr 28, 2012, 3:52:11 AM4/28/12
to ne...@googlegroups.com
Hi there,
I think you should use the Java Traversal framework, see
http://docs.neo4j.org/chunked/snapshot/tutorials-java-embedded-traversal.html#examples-uniqueness-of-paths-in-traversals
but with a Uniqueness of

Uniqueness.NODE,see
http://components.neo4j.org/neo4j/1.8-SNAPSHOT/apidocs/org/neo4j/kernel/Uniqueness.html

Will that work?

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j

Mattias Persson

unread,
Apr 29, 2012, 8:32:51 AM4/29/12
to ne...@googlegroups.com
It sounds like you'd need to implement your own UniquenessFilter, one which checks uniqueness on the last pair of nodes in a Path, right?

Look at Uniqueness class for an example of a factory for common UniquenessFilters. I would implement it something like this (type in this mail client only):

class NodePairUniqueness implements UniquenessFilter {
    private final Set<Pair<Long,Long>> pairs = new HashSet<Pair<Long,Long>>();

    public bollean checkFirst( TraversalBranch branch ) {
        return true;
    }

    public boolean check( TraversalBranch branch ) {
        Path position = branch.position();
        Node nextToLast = null;
        Node last = null;
        Iterator<Node> nodes = position.nodes().iterator();
        while ( nodes.hasNext() ) {
            nextToLast = last;
            last = nodes.next();
        }
        Pair<Long,Long> lastTwoNodes = Pair.of( nextToLast.getId(), last.getId() );
        return pairs.add( lastTwoNodes );
    }
}

I also realize that it would be grand with a Path#reversedNodes() or something this started from the end node and back to the start node. And memory-wise it would be more efficient with a custom PrimitiveLongPair class or similar instead of the Pair<Long,Long>.

hyperutila

unread,
Apr 30, 2012, 4:03:42 PM4/30/12
to Neo4j
Ah, perfect! I think this is exactly what I was looking for. I just
didn't know how to go about implementing your own Uniqueness class. At
first glance, it looked like they only have the built-in Uniqueness
filters that you can create from the Uniqueness class. But I got it
all set up with a UniquenessFactory that generates a UniquenessFilter
and does what I want. Thank you so much!

Mattias Persson

unread,
May 1, 2012, 5:06:53 AM5/1/12
to ne...@googlegroups.com
Excellent, cheers

2012/4/30 hyperutila <derrick...@gmail.com>



--
Mattias Persson, [mat...@neotechnology.com]
Hacker, Neo Technology
www.neotechnology.com
Reply all
Reply to author
Forward
0 new messages