CSE Roadmap

Specialist tier · about 2 hours · 5 parts

Part 2 of 5 · 1 min read

Vertices, edges, and properties

A graph stores two kinds of record, vertices and edges, each with its own properties, instead of forcing all data into rows and columns.

Think of a corkboard covered in photographs, pinned up and connected with pieces of string. Each photo is a thing. Each piece of string is a labeled connection between two specific photos, such as "co-wrote with."

In plain words: a property graph stores two kinds of record. A vertex represents one thing, such as a book or an author. It holds a unique ID and a set of properties, key-value pairs describing it. An edge represents a relationship between exactly two vertices. It holds a label naming the kind of relationship, and its own properties. An edge also records which vertex it starts at and which it ends at, so the direction of the relationship is explicit.

A worked example. A vertex represents author Maria Chen, with properties {"name": "Maria Chen", "country": "Canada"}. Another vertex represents the book "Deep Roots." An edge labeled WROTE connects the author vertex to the book vertex, recording that Maria Chen wrote that book.

The key lines.

-- A property graph can be stored as two relational tables underneath
CREATE TABLE vertices (
    vertex_id  INTEGER PRIMARY KEY,
    properties JSON
);

CREATE TABLE edges (
    edge_id     INTEGER PRIMARY KEY,
    tail_vertex INTEGER REFERENCES vertices (vertex_id),
    head_vertex INTEGER REFERENCES vertices (vertex_id),
    label       TEXT,
    properties  JSON
);

Vertex, edge, property graph

A vertex is one record in a graph, holding a unique ID and a set of properties. An edge is a labeled, directed connection between exactly two vertices, also holding its own properties. A property graph is a graph where both vertices and edges can carry properties.

Example: an author vertex connected to a book vertex by a WROTE edge, each carrying its own properties, is a property graph representing one small fact about the bookstore's catalog.