With the following graph:
g.addV("entity").property(id, "A")
.addV("entity").property(id, "B")
.addV("entity").property(id, "C")
.addV("entity").property(id, "D")
.addV("entity").property(id, "E")
.addV("entity").property(id, "F")
.addV("entity").property(id, "G")
.addV("entity").property(id, "H")
.addE("needs").from(g.V("A")).to(g.V("B"))
.addE("needs").from(g.V("A")).to(g.V("C"))
.addE("needs").from(g.V("A")).to(g.V("D"))
.addE("needs").from(g.V("A")).to(g.V("E"))
.addE("needs").from(g.V("C")).to(g.V("F"))
.addE("needs").from(g.V("D")).to(g.V("F"))
.addE("needs").from(g.V("E")).to(g.V("G"))
.addE("needs").from(g.V("F")).to(g.V("H"))
One variant of a correct order would be
[[B, H, G], [F, E], [C, D], [A]]
or just
[B, H, G, F, E, C, D, A]
The order within a "group" (e.g. [B, H, G] vs [H, G, B]) is not important.
How do I retrieve the correct order to resolve these nodes if they were to live in a dependency graph?
My thinking of the algorithm would be along the lines of one of these two solutions:
A) Start at leaf nodes and traverse to parents
- Retrieve all leaf vertices (no outgoing edges), append to result set, and put into
current
andresolved
. - Loop through all
current
and see if all of their children (the vertices they reference to) have are contained withinresolved
. If they are, save the current vertex intoresolved
and append to result set. Save the vertex' parents intocurrent
. - If
current
has elements, go to 2, else finish.
B) Simplified start at leaf nodes but look through whole graph every iteration
- Retrieve all leaf vertices (no outgoing edges), append to result set, and put into
resolved
. - Find all vertices that that are not contained within
resolved
and that do not have any referenced vertices withinresolved
. Append those to the result set. If there are no vertices, return result set, else repeat 2.
Is this possible? If so, how? Can I use these current
and resolved
collections while traversing?