Graphs as a Data Model
A graph represents entities as nodes and relationships as edges. That sounds simple, but the useful model begins with semantics: what makes two node records the same entity, what event creates an edge, which way the edge points, and what an absent edge means. A graph is not automatically a truer version of a table.
Prerequisites
You should be comfortable reading CSV files and Python dictionaries. Review Python fundamentals if those are unfamiliar. No graph-theory background is required.
Learning goals
- Define node identity, edge identity, direction, weight, and observation window before analysis.
- Separate a graph-shaped question from its edge-list storage.
- Preserve declared nodes that have no valid incident relationship.
- Explain why the absence of an edge is not evidence that no real-world relationship exists.
Start with a graph-shaped question
Suppose a collaboration system logs replies, mentions, reviews, and private notes. “Who can information reach through recorded interactions?” is graph-shaped because paths and direction matter. “How many events occurred each day?” is naturally tabular; converting it into a graph adds no value.
Our local fixture uses this contract:
| Element | Meaning |
|---|---|
| node | one declared person, keyed by stable node_id |
| edge | one recorded interaction event, keyed by relationship_id |
| direction | actor source to recipient target |
| weight | positive interaction intensity for this teaching example |
| parallel edges | distinct events between the same ordered pair |
| self-loop | a retained self-directed event |
| observation window | timestamps present in the fixture, not all real activity |
Keeping a node table separate from valid edges is essential: Gia is a declared person with no accepted edge. Building nodes only from edge endpoints would silently erase that isolate.
Worked example
The example reads the repository-owned fixture, counts only relationships explicitly marked valid, and derives isolates from declared nodes rather than inventing them from the edge list.
import csv
from collections import Counter
from pathlib import Path
fixture = Path("tests/fixtures/graph-data-science")
with (fixture / "interaction-nodes.csv").open(encoding="utf-8") as handle:
nodes = list(csv.DictReader(handle))
with (fixture / "interaction-relationships.csv").open(encoding="utf-8") as handle:
relationships = list(csv.DictReader(handle))
valid = [row for row in relationships if row["record_status"] == "valid"]
incident = Counter(
node
for row in valid
for node in (row["source"], row["target"])
)
isolates = sorted(row["node_id"] for row in nodes if incident[row["node_id"]] == 0)
print(f"nodes={len(nodes)} valid_relationships={len(valid)} isolates={isolates}")
print("model=directed weighted repeated self-loop")
Expected output:
nodes=7 valid_relationships=10 isolates=['gia']
model=directed weighted repeated self-loop
The output describes the declared data contract. It does not claim that Gia has no relationships outside the observed system or time window.
Limitations
- A person, account, device, company, and document require different identity-resolution rules; merging them incorrectly changes every graph result.
- An edge can represent an event, a persistent state, similarity, or a claim. Those meanings are not interchangeable.
- A missing relationship can arise from access rules, instrumentation failure, sampling, or the observation window. The absence of an edge is not evidence of no real-world relationship.
- Paths in recorded data show possible routes under the model, not communication, influence, or causation.
Practice
Choose one relational question. Write a one-sentence definition for the node, edge, direction, weight, time window, and missing-edge assumption. Then name one ordinary table question that should stay tabular.
Next steps
Next, choose among directed, weighted, and multigraphs without discarding events accidentally.
Primary sources
Checked 2026-10-18: NetworkX, tutorial and graph construction; NetworkX, graph types reference; W3C, Data Quality Vocabulary.