CSE Roadmap

Specialist tier · about 2 hours · 5 parts

Part 4 of 5 · 2 min read

Traversing the graph

A graph query follows a chain of edges from a starting vertex, forwards or backwards, rather than joining fixed tables.

Traversing the graph

Think of following a piece of string from one photo to the next, then to the next, tracing a path across the corkboard. The path can be followed in either direction, forwards along the string or backwards to where it came from.

In plain words: a graph query typically starts at one or more vertices and follows edges outward, a process called traversal. Because an edge records both its start and end vertex, a traversal can follow edges forwards, from tail to head, or backwards, from head to tail. A query can also chain several edge labels together, following a path of a specific shape through the graph. Declarative graph query languages, such as Cypher, express this chain directly using an arrow-like notation, rather than writing out the underlying joins by hand.

A worked example. Starting at author Maria Chen, a traversal follows her outgoing WROTE edges to find every book she wrote. From one of those book vertices, following incoming WROTE edges backwards finds every other author who co-wrote that same book, even though Maria Chen's vertex was never directly connected to theirs.

The key lines.

// Cypher-style pattern: find co-authors of Maria Chen
MATCH (maria:Author {name: 'Maria Chen'})-[:WROTE]->(b:Book)<-[:WROTE]-(coauthor:Author)
RETURN coauthor.name

Traversal

Traversal is following a chain of edges from a starting vertex to reach other vertices, in either direction. A query can chain several edge labels together to express a specific path shape through the graph, rather than writing out the equivalent relational joins directly.

Example: starting at one author, following a WROTE edge to a book, then following another WROTE edge backwards to a co-author, is a two-step traversal finding a connection no single edge expressed directly.

Remember this

A property graph stores vertices and edges, both carrying their own properties, with no fixed schema restricting what can connect to what. That makes it a strong fit for highly interconnected, many-to-many data, and for data whose relationships keep growing new kinds over time. The next page covers column-family stores, built for a very different problem: very high write throughput at scale.

Quick notes

  • A vertex holds a unique ID and properties; an edge is a labeled, directed connection between two vertices, also with its own properties.
  • A property graph has no fixed schema for what can connect to what, so adding a new relationship type needs no schema change.
  • A traversal follows a chain of edges from a starting vertex, forwards or backwards, to reach other vertices.
  • Declarative graph query languages express a chain of edges directly, rather than writing the equivalent relational joins by hand.
  • A graph fits highly interconnected, many-to-many data better than a document model, and often more naturally than a relational schema as connections multiply.