问题
How to print all the parents of a given node.
回答1:
Graham's answer is okay, but uses expensive path computations, which are not really required for your use-case.
This is how I've set up your graph:
g = TinkerGraph.open().traversal()
g.addV().property(id, 1).as("v1").
addV().property(id, 2).as("v2").
addV().property(id, 3).as("v3").
addV().property(id, 4).as("v4").
addV().property(id, 5).as("v5").
addV().property(id, 6).as("v6").
addV().property(id, 7).as("v7").
addV().property(id, 8).as("v8").
addE("black").from("v2").to("v1").
addE("black").from("v7").to("v2").
addE("black").from("v8").to("v7").
addE("orange").from("v8").to("v7").
addE("black").from("v3").to("v2").
addE("black").from("v6").to("v3").
addE("black").from("v4").to("v3").
addE("black").from("v5").to("v4").
addE("orange").from("v5").to("v4").iterate()
Now, to get all ancestors, all you need is this:
gremlin> g.V(5).repeat(out().dedup()).emit()
==>v[4]
==>v[3]
==>v[2]
==>v[1]
Likewise you won't need path computations to determine the maximum depth:
gremlin> g.V(5).emit().repeat(out().dedup()).count()
==>5
回答2:
Here's a sample graph that I think matches the branch you want to traverse.
graph=TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
g=graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
v1 = graph.addVertex(T.label, "vtx", T.id, 1, "name", "alpha")
==>v[1]
v2 = graph.addVertex(T.label, "vtx", T.id, 2, "name", "beta")
==>v[2]
v3 = graph.addVertex(T.label, "vtx", T.id, 3, "name", "gamma")
==>v[3]
v4 = graph.addVertex(T.label, "vtx", T.id, 4, "name", "delta")
==>v[4]
v5 = graph.addVertex(T.label, "vtx", T.id, 5, "name", "epsilon")
==>v[5]
v5.addEdge("parent", v4, T.id, 101)
==>e[101][5-parent->4]
v4.addEdge("parent", v3, T.id, 102)
==>e[102][4-parent->3]
v3.addEdge("parent", v2, T.id, 103)
==>e[103][3-parent->2]
v2.addEdge("parent", v1, T.id, 104)
==>e[104][2-parent->1]
A repeat() loop taking outbound 'parent' edges will traverse 'up' the hierarchy. In the following example, we've told it to start at v[5] and take two 'hops':
g.V(5).repeat(out('parent')).times(2).path()
==>[v[5],v[4],v[3]]
In practice you probably want to keep traversing (i.e. repeating) until you hit some terminating condition which will depend on what you want to achieve.
You might know you want to stop at a particular vertex, e.g. if you know the root of the hierarchy is v[1], or has a particular property:
g.V(5).repeat(out('parent')).until(hasId(1)).path()
==>[v[5],v[4],v[3],v[2],v[1]]
Or you may want to traverse until you hit the top of the hierarchy (i.e. until there are no more outgoing edges):
g.V(5).repeat(out('parent')).until(outE().count().is(0)).path()
==>[v[5],v[4],v[3],v[2],v[1]]
To get the max depth from the root to the furthest leaf node:
Let's add some more vertices to the graph, to match your graph.
v7 = graph.addVertex(T.label, "vtx", T.id, 7, "name", "eta")
==>v[7]
v8 = graph.addVertex(T.label, "vtx", T.id, 8, "name", "theta")
==>v[8]
v6 = graph.addVertex(T.label, "vtx", T.id, 6, "name", "zeta")
==>v[6]
v8.addEdge("parent", v7, T.id, 105)
==>e[105][8-parent->7]
v7.addEdge("parent", v2, T.id, 106)
==>e[106][7-parent->2]
v6.addEdge("parent", v3, T.id, 107)
==>e[107][6-parent->3]
A traversal from the root toward the leaf vertices will now result in 3 paths:
g.V(1).repeat(__.in('parent')).until(inE().count().is(0)).path()
==>[v[1],v[2],v[3],v[6]]
==>[v[1],v[2],v[7],v[8]]
==>[v[1],v[2],v[3],v[4],v[5]]
But you only want the length of the longest path:
g.V(1).repeat(__.in('parent')).until(inE().count().is(0)).path().
......1> tail(1).unfold().count()
==>5
Hope that helps, Graham
来源:https://stackoverflow.com/questions/40645066/print-hierarchical-vertices-in-a-graph