Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Introduction to Graphs: Nodes, Edges, and Direction

Recommended resources

Welcome to your first lesson in our course on Graph Neural Networks. Your goal is to build GNNs from the ground up, and this journey begins with the fundamental language of graphs. Before we can model complex networks or write a single line of PyTorch code for a GNN, we need to be fluent in the concepts that define what a graph is.

This lesson addresses the first and most crucial learning outcome: defining what graphs, nodes, and edges are, and understanding the critical distinction between directed and undirected graphs. We will start with an intuitive overview and then move to the formal definitions that you will use throughout your GNN implementations.

What is a Graph?

At its core, a graph is a mathematical structure used to model relationships between objects. In the world of data science and machine learning, this is an incredibly powerful concept. You can think of:

  • Social networks, where people are the objects and their friendships are the relationships.
  • Molecular structures, where atoms are the objects and chemical bonds are the relationships.
  • The internet, where web pages are the objects and hyperlinks are the relationships.

In graph theory, we have formal names for these components. The objects are called vertices (or more commonly, nodes), and the relationships are called edges.

To get a dynamic and visual introduction to these ideas, the following video provides a clear and concise overview.

Introduction to Graph Theory: A Computer Science Perspective

Watch the first part of the video "Introduction to Graph Theory: A Computer Science Perspective" from the Reducible channel. It will introduce the basic vocabulary and then show how a graph is formally defined using set notation.

Please watch from the beginning until timestamp 00:05:18. Pay close attention to these segments: The initial explanation of graphs as networks of vertices (nodes) and edges. The formal definition of a graph G as a set of vertices V and a set of edges E.

As the video explained, a graph is formally defined as an ordered pair , where is the set of all vertices and is the set of all edges. Each edge in represents a connection between a pair of vertices from . This simple notation, , is the bedrock of graph theory and will appear constantly as we discuss GNNs.

For a different perspective that frames these concepts within a machine learning context, you can watch this short segment.

Graph Neural Networks (GNN) | Nodes, Edges, Adjacency Matrix, Message Passing, Aggregation explained

This clip from the video by Vizuara uses a social network to explain nodes, edges, and features, which directly ties into how GNNs view graph data.

Watch from this segment. Notice how it introduces the idea of "features" associated with nodes (like age or hobbies). While our focus today is just on the graph's structure, this is a great preview of how we'll enrich graphs with data for machine learning tasks later.


Directed vs. Undirected Graphs

The nature of the relationships in a network is not always the same. This leads to a fundamental classification of graphs: directed and undirected. The distinction is simple but has profound implications for how information propagates through a network—a core mechanism in GNNs.

An undirected graph models symmetric, or two-way, relationships. If an edge connects node A to node B, it is implied that the connection also goes from B to A. A classic example is a friendship on Facebook; the relationship is mutual.

A directed graph, or digraph, models asymmetric, or one-way, relationships. An edge from node A to node B does not imply an edge from B to A. Think of following someone on Twitter; you can follow them without them following you back.

The following images clearly illustrate this difference.

A simple visual comparison between a directed graph, where edges have arrows indicating direction, and an undirected graph, where edges are simple lines representing a mutual connection.

To solidify your understanding, the next video segments provide clear explanations and helpful analogies.

Introduction to Graph Theory: A Computer Science Perspective

Continuing with the Reducible video, this part introduces the concepts of undirected and directed graphs.

Please watch from this section. It first defines an undirected graph and then contrasts it with a directed graph.

The school hierarchy example mentioned in the next video is a particularly intuitive way to understand directedness.

Graph Neural Networks (GNN) | Nodes, Edges, Adjacency Matrix, Message Passing, Aggregation explained

This clip from the Vizuara video offers a great real-world analogy for directed graphs.

Watch the segment on directed vs. undirected graphs. The example of information flowing from a principal to teachers to students clearly demonstrates a directed relationship.


Formalizing the Difference

This distinction between directed and undirected graphs is also reflected in the formal notation for edges.

  • In an undirected graph, an edge between vertices and is an unordered pair, typically written as a set . The order does not matter: is identical to .
  • In a directed graph, an edge from vertex to vertex is an ordered pair, written as . Here, the order is critical: is not the same as . represents an arrow pointing from to .

The following image provides a perfect summary, showing how the set of edges is written differently for undirected and directed graphs.

This image compares a 3-node undirected graph with a directed one. Note how the edge set E₁ for the undirected graph uses curly braces { } to denote unordered pairs (sets), while the edge set E₂ for the directed graph uses parentheses ( ) to denote ordered pairs, capturing directionality.

For a comprehensive textual reference that summarizes everything we've covered, the following article is an excellent resource.

An Introduction to Graph Theory

This article from DataCamp provides clear, written definitions of the fundamental concepts we've discussed.

Focus on these parts of the article: The section on fundamental concepts, which reinforces the formal definition G = (V, E). The definitions and use cases for Directed Graphs. The definitions and examples for Undirected Graphs.


Conclusion

In this introductory lesson, we have established the foundational vocabulary of graph theory. You can now define the core components of a graph and distinguish between its two primary forms.

Here are the key takeaways:

  • A graph is a structure used to model relationships, consisting of a set of vertices (, or nodes) and a set of edges (, or links).
  • Vertices represent the entities or objects in a network.
  • Edges represent the connections or relationships between pairs of vertices.
  • Undirected graphs model symmetric, bidirectional relationships, where an edge connects and mutually.
  • Directed graphs (digraphs) model asymmetric, one-way relationships, where an edge represents a connection from to .

This foundation is non-negotiable for understanding GNNs. The structure of a graph—particularly its directionality—dictates how information can be passed between nodes, which is the central mechanism of graph neural networks.

In our next lesson, we will build directly on this by exploring how to represent these abstract graph structures in a concrete way that a computer can process. We will dive into two essential data structures: adjacency matrices and adjacency lists.