Tail Step
The tail
step navigates from edges to their source (origin) vertices, allowing traversal back to where the edges
start from. It transforms a stream of edges into a stream of vertices.
In this diagram:
- An Input Stream contains edge elements (e.g., A->B, C->D).
- The
.tail()
step processes each edge. - The Output Stream contains the corresponding source (tail) vertices (A, C) for each input edge.
Syntax
walker.tail()
Parameters
This step takes no parameters.
Return Value
Returns a new walker positioned at the source vertices of the edges in the current traversal.
Examples
Basic Tail Step
Find people who created projects by getting back to the source vertex:
// Find projects created by people
// 1. Start with all people
// 2. Find "created" edges they made
// 3. Use head() to get to the projects (the target vertices)
let projects = graph
.walk()
.vertices(Vertex::person())
.edges(Edge::created().outgoing())
.head() // Move to the target vertex of the edge
.collect::<Vec<_>>();
println!("Found {} projects created by people", projects.len());
Multi-Step Traversal
Traverse multiple relationships to find indirect connections:
// Find people who follow someone who created a project
// This demonstrates chaining head and tail multiple times
let indirect_creators = graph
.walk()
.vertices(Vertex::person())
.edges(Edge::follows().outgoing())
.tail() // Move to the people they follow
.edges(Edge::created().outgoing())
.tail() // Move to the projects created by those people
.collect::<Vec<_>>();
println!(
"Found {} projects created by people the original people know",
indirect_creators.len()
);
Best Practices
- Always follow edge traversals with either
head()
ortail()
to return to vertices. - Use contexts to retain information about the edge when moving back to the source vertex via
tail()
. - Remember that
tail()
returns the source vertex (where the edge starts). - For retrieving both endpoints, consider using context to store one while visiting the other.
Common Use Cases
- Author identification: Finding who created something by looking at edge sources (
tail()
). - Relationship sources: Identifying the initiators of connections (
tail()
). - Backtracking: Returning to source vertices after examining edges (
tail()
). - Edge-based filtering: Finding vertices that have specific incoming edges (by traversing the edge and using
tail()
).