The Language of Graphs: Foundations and Representations
Graphs are everywhere. From the social connections that define our digital interactions to the molecular structures that underpin life itself, from transportation networks that move people and goods to the neural pathways that enable thought and consciousness, the language of graphs provides a universal framework for describing relationships and interactions. Understanding this language is not merely an academic exercise—it is an essential foundation for anyone seeking to harness the power of modern machine learning techniques that operate on structured data. This chapter establishes that foundation, introducing the fundamental concepts of graph theory that will serve as the bedrock for everything that follows in this book.
The study of graphs, known as graph theory, has a rich history stretching back to Euler's famous problem of the Seven Bridges of Königsberg in 1736. Yet despite this long heritage, the field has experienced a remarkable renaissance in recent decades, driven largely by the explosion of data from social media platforms, biological databases, and interconnected systems of all kinds. Traditional machine learning approaches, designed primarily for data residing in Euclidean spaces—images as grids of pixels, text as sequences of words—were not equipped to handle the irregular, non-Euclidean geometry of graph-structured data. The development of Graph Neural Networks represents a fundamental breakthrough in our ability to learn from such data, but to appreciate the elegance and power of these methods, we must first develop fluency in the language of graphs themselves.
Defining Graphs: Vertices, Edges, and the Structure of Relationships
At its core, a graph is a mathematical object that captures the notion of objects and the relationships between them. Formally, we define a graph as an ordered pair $G = (V, E)$, where $V$ is a set of vertices (also called nodes) and $E$ is a set of edges (also called links or arcs) that connect pairs of vertices. This deceptively simple definition encompasses an enormous variety of structures, from the trivial case of a single isolated vertex to the vast, complex networks that characterize modern data.
The vertices of a graph represent the entities in whatever system we are modeling. In a social network, these entities are people. In a molecular graph, they are atoms. In a transportation network, they are locations such as airports, train stations, or intersections. The edges represent the relationships or connections between these entities—friendships between people, chemical bonds between atoms, routes between locations. The power of graph representation lies in this abstraction: by focusing on which entities are related rather than on the specific nature of those entities, we can apply the same mathematical framework to vastly different domains.
Consider a simple example to solidify these concepts. Suppose we are modeling a small research collaboration network consisting of four researchers: Alice, Bob, Carol, and David. If Alice and Bob have co-authored a paper, we include an edge between their corresponding vertices. Similarly, if Bob and Carol have collaborated, we include an edge there. If David has not collaborated with anyone in this particular network, his vertex would exist in $V$ but would have no edges connecting it to other vertices. This graph captures the collaboration structure without concerning itself with the researchers' names, institutions, publication histories, or any other attributes—those can be added later as vertex or edge features if needed.
The formal definition of edges varies depending on the type of graph we are considering. In the simplest case, an undirected graph, an edge is an unordered pair $\{u, v\}$ of vertices, meaning that the relationship is symmetric: if $u$ is connected to $v$, then $v$ is connected to $u$. This model fits social networks where "friendship" is typically mutual, or molecular structures where chemical bonds connect atoms without directionality. In a directed graph, or digraph, edges are ordered pairs $(u, v)$, introducing asymmetry: there may be an edge from $u$ to $v$ without an edge from $v$ to $u$. This model fits citation networks where paper A may cite paper B without B citing A, or web link graphs where one webpage links to another.
The cardinality of the vertex set, denoted $|V|$, tells us the number of entities in our graph, while $|E|$ indicates the number of relationships. These quantities are fundamental to understanding the scale and density of graph-structured data. In real-world graphs, we often encounter significant imbalance: a social network might contain billions of vertices (users) and hundreds of billions of edges (friendships), while a small molecule might contain only a few dozen atoms and bonds. Understanding this scale is crucial for selecting appropriate algorithms and data structures.
The Taxonomy of Graphs: Understanding Different Structures
Beyond the fundamental distinction between directed and undirected graphs, several important variations exist that we must understand to properly model real-world phenomena. Each variation captures different aspects of relationships and imposes different constraints on what structures are possible.
Undirected Graphs
Undirected graphs form the simplest and most common category in many applications. Here, edges represent symmetric relationships with no inherent directionality. The friendship relation in most social networks exemplifies this: if Alice is friends with Bob, then Bob is friends with Alice. The mathematical representation uses unordered pairs $\{u, v\} \in E$, and we say that vertices $u$ and $v$ are adjacent or neighbors if such an edge exists.
In undirected graphs, we often speak of the degree of a vertex, denoted $d(v)$ or $\deg(v)$, which counts the number of edges incident to that vertex. In the collaboration network example, Alice's degree is 1 (she has collaborated with only Bob), while Bob's degree is 2 (he has collaborated with both Alice and Carol). The sum of all vertex degrees in a graph equals twice the number of edges—a fundamental result known as the Handshaking Lemma: $\sum_{v \in V} \deg(v) = 2|E|$. This relationship follows from the fact that each edge contributes exactly 1 to the degree of each of its two endpoints.
The concept of degree distribution is particularly important in network analysis. Many real-world graphs exhibit highly skewed degree distributions, with most vertices having few connections while a small number of vertices, called hubs, have many connections. The social network graph, the web graph, and citation networks all display this characteristic. Understanding degree distributions helps us design algorithms that can handle the heterogeneity inherent in many graph-structured datasets.
Directed Graphs
Directed graphs, or digraphs, introduce directionality into relationships. Each edge is an ordered pair $(u, v)$, indicating a relationship from $u$ to $v$ that may or may not be reciprocated. The directionality of edges adds substantial expressive power for modeling asymmetric relationships.
In directed graphs, we distinguish between in-degree and out-degree. The in-degree of a vertex $v$, denoted $d_{in}(v)$, counts edges pointing TO $v$, while the out-degree $d_{out}(v)$ counts edges pointing FROM $v$. In a citation network, a highly cited paper would have a high in-degree, while a paper that cites many references would have a high out-degree. In a web graph, a popular website linked to by many other pages has high in-degree, while a portal page linking to many resources has high out-degree.
The Twitter "follow" relationship provides an intuitive example of directed graphs in social media. User A can follow User B without User B following User A. This asymmetric relationship is naturally represented by a directed edge from A to B. The resulting graph captures follower counts, influence patterns, and infor