Tuesday, 12 May 2026

Vertex Adjacency, Incidence, and Outgoing Traversals (out()) in Gremlin

  

1. Adjacency and Incidence in Property Graphs

In a property graph, relationships are first class citizens. Understanding how vertices and edges relate to one another is fundamental before exploring traversal steps such as out().

 

Vertex Adjacency

Two vertices are said to be adjacent if there is an edge connecting them.

 

·      If vertex A is connected to vertex B by an edge, then A and B are adjacent.

·      Adjacency does not imply direction by itself; direction becomes relevant only when the edge is directed.

 

For example, if an order vertex is connected to a product vertex via a contains edge, those two vertices are adjacent.

 

Vertex Edge Incidence

A vertex and an edge are considered incident if the edge is connected to that vertex.

 

·      An edge is incident to its out-vertex and its in-vertex.

·      This concept becomes important when reasoning about traversal direction and edge filtering.

 

Think of incidence as participation, a vertex participates in an edge if the edge starts from or ends at that vertex.

 

Direction Matters: Incoming and Outgoing Relationships

Apache TinkerPop follows the directed property graph model. Every edge has a direction:

 

·      Outgoing direction: from the out-vertex to the in-vertex

·      Incoming direction: from the in-vertex back to the out-vertex

 

Gremlin provides explicit steps to navigate these directions:

 

·      out() – move to outgoing adjacent vertices

·      in() – move to incoming adjacent vertices

·      both() – move to all adjacent vertices regardless of direction

 

This chapter focuses on out().

 

What out() Does?

The out() step traverses from a vertex to all vertices connected by outgoing edges.

 

In precise terms:

·      Starting from a vertex V

·      Follow all edges where V is the out-vertex

·      Emit the connected in-vertices

 

V --(edge)--> adjacent vertex

 

Only edges leaving the current vertex are considered.

 

Filtering by Edge Label

The out() step can optionally take one or more edge labels.

 

·      out(): follow all outgoing edges

·      out('placed'): follow only outgoing edges with the label placed

 

This makes out() both a navigation and filtering step.

 

2. Example: An E-Commerce Graph

To make the discussion concrete, consider an e-commerce domain modeled as a property graph. The graph contains:

 

Vertex Types

·      city: Bengaluru, Hyderabad, Chennai, Pune

·      category: Electronics, Books, Clothing

·      product: Laptop, Smartphone, Java Programming Book, T-Shirt

·      customer: Amit Sharma, Priya Iyer, Rohit Verma

·      order: O9001 to O9005

 

Edge Types

·      belongsTo: product → category

·      placed: customer → order

·      contains: order → product

·      deliveredTo: order → city

 

Each edge has a clear semantic direction that aligns with real-world meaning.

 

Gremlin Statements to build the Graph

graph = TinkerGraph.open()
g = graph.traversal()

blr = g.addV('city').property('name', 'Bengaluru').next()
hyd = g.addV('city').property('name', 'Hyderabad').next()
chn = g.addV('city').property('name', 'Chennai').next()
pne = g.addV('city').property('name', 'Pune').next()

catElectronics = g.addV('category').property('name', 'Electronics').next()
catBooks       = g.addV('category').property('name', 'Books').next()
catClothing    = g.addV('category').property('name', 'Clothing').next()

p1 = g.addV('product').property('sku', 'P1001').property('name', 'Laptop').next()
p2 = g.addV('product').property('sku', 'P1002').property('name', 'Smartphone').next()
p3 = g.addV('product').property('sku', 'P2001').property('name', 'Java Programming Book').next()
p4 = g.addV('product').property('sku', 'P3001').property('name', 'T-Shirt').next()

g.V(p1).addE('belongsTo').to(catElectronics).next()
g.V(p2).addE('belongsTo').to(catElectronics).next()
g.V(p3).addE('belongsTo').to(catBooks).next()
g.V(p4).addE('belongsTo').to(catClothing).next()


c1 = g.addV('customer').property('customerId', 'C101').property('name', 'Amit Sharma').next()
c2 = g.addV('customer').property('customerId', 'C102').property('name', 'Priya Iyer').next()
c3 = g.addV('customer').property('customerId', 'C103').property('name', 'Rohit Verma').next()

o1 = g.addV('order').property('orderId', 'O9001').next()
o2 = g.addV('order').property('orderId', 'O9002').next()
o3 = g.addV('order').property('orderId', 'O9003').next()
o4 = g.addV('order').property('orderId', 'O9004').next()
o5 = g.addV('order').property('orderId', 'O9005').next()

g.V(c1).addE('placed').to(o1).next()
g.V(c1).addE('placed').to(o2).next()

g.V(c2).addE('placed').to(o3).next()

g.V(c3).addE('placed').to(o4).next()
g.V(c3).addE('placed').to(o5).next()


g.V(o1).addE('contains').to(p1).next()
g.V(o1).addE('contains').to(p3).next()

g.V(o2).addE('contains').to(p2).next()

g.V(o3).addE('contains').to(p4).next()

g.V(o4).addE('contains').to(p1).next()
g.V(o4).addE('contains').to(p4).next()

g.V(o5).addE('contains').to(p3).next()


g.V(o1).addE('deliveredTo').to(blr).next()
g.V(o2).addE('deliveredTo').to(blr).next()

g.V(o3).addE('deliveredTo').to(hyd).next()

g.V(o4).addE('deliveredTo').to(chn).next()

g.V(o5).addE('deliveredTo').to(pne).next()

Example 1: Print all vertices and edges

gremlin> g.V().valueMap(true)
==>[id:0,label:city,name:[Bengaluru]]
==>[id:33,label:customer,customerId:[C102],name:[Priya Iyer]]
==>[id:2,label:city,name:[Hyderabad]]
==>[id:4,label:city,name:[Chennai]]
==>[id:36,label:customer,customerId:[C103],name:[Rohit Verma]]
==>[id:6,label:city,name:[Pune]]
==>[id:39,label:order,orderId:[O9001]]
==>[id:8,label:category,name:[Electronics]]
==>[id:41,label:order,orderId:[O9002]]
==>[id:10,label:category,name:[Books]]
==>[id:43,label:order,orderId:[O9003]]
==>[id:12,label:category,name:[Clothing]]
==>[id:45,label:order,orderId:[O9004]]
==>[id:14,label:product,name:[Laptop],sku:[P1001]]
==>[id:47,label:order,orderId:[O9005]]
==>[id:17,label:product,name:[Smartphone],sku:[P1002]]
==>[id:20,label:product,name:[Java Programming Book],sku:[P2001]]
==>[id:23,label:product,name:[T-Shirt],sku:[P3001]]
==>[id:30,label:customer,customerId:[C101],name:[Amit Sharma]]
gremlin> 
gremlin> 
gremlin> 
gremlin> g.E().valueMap(true)
==>[id:64,label:deliveredTo]
==>[id:65,label:deliveredTo]
==>[id:49,label:placed]
==>[id:50,label:placed]
==>[id:51,label:placed]
==>[id:52,label:placed]
==>[id:53,label:placed]
==>[id:54,label:contains]
==>[id:55,label:contains]
==>[id:56,label:contains]
==>[id:57,label:contains]
==>[id:26,label:belongsTo]
==>[id:58,label:contains]
==>[id:27,label:belongsTo]
==>[id:59,label:contains]
==>[id:28,label:belongsTo]
==>[id:60,label:contains]
==>[id:29,label:belongsTo]
==>[id:61,label:deliveredTo]
==>[id:62,label:deliveredTo]
==>[id:63,label:deliveredTo]

Example 2: Get all the orders placed by a customer.

g.V().
  has('customer', 'name', 'Amit Sharma').
  out('placed').
  valueMap(true)

gremlin> g.V().
......1>   has('customer', 'name', 'Amit Sharma').
......2>   out('placed').
......3>   valueMap(true)
==>[id:39,label:order,orderId:[O9001]]
==>[id:41,label:order,orderId:[O9002]]

   

Example 3: Products Contained in an Order

 

g.V().
  has('order', 'orderId', 'O9001').
  out('contains').
  valueMap(true)

gremlin> g.V().
......1>   has('order', 'orderId', 'O9001').
......2>   out('contains').
......3>   valueMap(true)
==>[id:14,label:product,name:[Laptop],sku:[P1001]]
==>[id:20,label:product,name:[Java Programming Book],sku:[P2001]]

   

Example 4: Get me the city where the order id delivered to

 

g.V().
  has('order', 'orderId', 'O9001').
  out('deliveredTo').
  valueMap(true)

gremlin> g.V().
......1>   has('order', 'orderId', 'O9001').
......2>   out('deliveredTo').
......3>   valueMap(true)
==>[id:0,label:city,name:[Bengaluru]]

   

In well-modeled graphs, most business questions read left to right using out():

 

·      Customers → orders → products

·      Orders → delivery locations

·      Products → categories

 

This clarity is a direct result of choosing correct edge directions during modeling.

 

In summary:

·      Adjacency describes vertices connected by an edge

·      Incidence describes the relationship between a vertex and an edge

·      out() traverses to outgoing adjacent vertices only

·      Edge direction is not optional—it defines traversal semantics

·      Clear edge naming and direction make out() traversals intuitive and readable

 


  

Previous                                                    Next                                                    Home

Grouped Counting in Gremlin: Understanding Distributions, Not Just Totals

  

Counting individual elements such as the total number of vertices or edges is often only the starting point in graph analysis. While such totals provide a high-level sense of scale, they rarely answer the questions that matter most in real systems.

 

In practice, analysts and engineers are far more interested in distributions than in raw totals. Instead of asking how many exist, the more meaningful question is often how many exist per category.

 

Consider the following examples drawn from common business scenarios:

 

·      How many orders has each customer placed?

·      How many products exist in each category?

·      How many deliveries are made to each city?

·      How many items are sold per brand?

 

Each of these questions requires grouped counting, counting elements after partitioning them into logical groups.

 

Apache TinkerPop provides first-class support for this pattern through two closely related traversal constructs:

 

·      groupCount(): a concise and optimized step dedicated to counting

·      group() combined with count(): a more expressive and flexible alternative

 

This chapter explains how grouped counting works in Gremlin, why it is essential for analytical queries, and how to apply it effectively using a familiar e-commerce domain.

 

Example: A Simplified E-Commerce Graph

To help the discussion, consider a simplified e-commerce platform similar to a large online marketplace operating across Indian cities.

 

Vertex Labels

·      customer: represents a customer (for example, Ravi or Ananya)

·      order: represents an order placed on the platform

·      product: represents a product for sale

·      category: represents a product category

·      city: represents a delivery city (such as Bengaluru or Hyderabad)

 

Edge Labels

·      customer - placed  → order

·      order — contains → product

·      product — belongsTo → category

·      order - deliveredTo → city

 

This graph structure enables questions about customer behavior, product distribution, and geographic demand to be expressed naturally as traversals.

 

Step 1: Create Graph and Traversal Source

graph = TinkerGraph.open()
g = graph.traversal()

Step 2: Create City Vertices

blr = g.addV('city').property('name', 'Bengaluru').next()
hyd = g.addV('city').property('name', 'Hyderabad').next()
chn = g.addV('city').property('name', 'Chennai').next()
pne = g.addV('city').property('name', 'Pune').next()

Step 3: Create Category Vertices

catElectronics = g.addV('category').property('name', 'Electronics').next()
catBooks       = g.addV('category').property('name', 'Books').next()
catClothing    = g.addV('category').property('name', 'Clothing').next()

   

Step 4: Create Product Vertices

 

p1 = g.addV('product').property('sku', 'P1001').property('name', 'Laptop').next()
p2 = g.addV('product').property('sku', 'P1002').property('name', 'Smartphone').next()
p3 = g.addV('product').property('sku', 'P2001').property('name', 'Java Programming Book').next()
p4 = g.addV('product').property('sku', 'P3001').property('name', 'T-Shirt').next()

   

Step 5: Link Products to Categories

 

g.V(p1).addE('belongsTo').to(catElectronics).next()
g.V(p2).addE('belongsTo').to(catElectronics).next()
g.V(p3).addE('belongsTo').to(catBooks).next()
g.V(p4).addE('belongsTo').to(catClothing).next()

   

Step 6: Create Customer Vertices

 

c1 = g.addV('customer').property('customerId', 'C101').property('name', 'Amit Sharma').next()
c2 = g.addV('customer').property('customerId', 'C102').property('name', 'Priya Iyer').next()
c3 = g.addV('customer').property('customerId', 'C103').property('name', 'Rohit Verma').next()

   

Step 7: Create Order Vertices

 

o1 = g.addV('order').property('orderId', 'O9001').next()
o2 = g.addV('order').property('orderId', 'O9002').next()
o3 = g.addV('order').property('orderId', 'O9003').next()
o4 = g.addV('order').property('orderId', 'O9004').next()
o5 = g.addV('order').property('orderId', 'O9005').next()

   

Step 8: Connect Customers to Orders (placed)

 

g.V(c1).addE('placed').to(o1).next()
g.V(c1).addE('placed').to(o2).next()

g.V(c2).addE('placed').to(o3).next()

g.V(c3).addE('placed').to(o4).next()
g.V(c3).addE('placed').to(o5).next()

   

Step 9: Connect Orders to Products (contains)

 

g.V(o1).addE('contains').to(p1).next()
g.V(o1).addE('contains').to(p3).next()

g.V(o2).addE('contains').to(p2).next()

g.V(o3).addE('contains').to(p4).next()

g.V(o4).addE('contains').to(p1).next()
g.V(o4).addE('contains').to(p4).next()

g.V(o5).addE('contains').to(p3).next()

   

Step 10: Connect Orders to Cities (deliveredTo)

 

g.V(o1).addE('deliveredTo').to(blr).next()
g.V(o2).addE('deliveredTo').to(blr).next()

g.V(o3).addE('deliveredTo').to(hyd).next()

g.V(o4).addE('deliveredTo').to(chn).next()

g.V(o5).addE('deliveredTo').to(pne).next()

   

How Many of Each Type of Vertex Exist?

A common diagnostic task when working with a graph is understanding its overall composition. Grouped counting can quickly reveal how many vertices exist for each label.

 

g.V().groupCount().by(label)

gremlin> g.V().groupCount().by(label)
==>[product:4,city:4,category:3,customer:3,order:5]

   

Here:

·      The keys in the result map are vertex labels.

·      The values represent the number of vertices with each label.

 

The same result can be produced using an explicit label extraction:

 

gremlin> g.V().label().groupCount()
==>[product:4,city:4,category:3,customer:3,order:5]

   

Both traversals are valid. The choice between them is often stylistic, though by(label) makes the grouping criterion more explicit and readable.

 

Note

As with simple count() traversals, grouped counting over all vertices or edges is not recommended for very large graphs in production workloads. However, for reporting, analytics, diagnostics, and queries scoped to subgraphs, grouped counting is both practical and extremely valuable.

  

Previous                                                    Next                                                    Home