Graph Theory for Machine Learning
Nodes, edges, adjacency matrix, graph types, graph features, social network representations.
Graph Theory for Machine Learning is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a Graph
A graph models entities and the relationships between them. Formally a graph is G = (V, E) where V is the set of vertices (nodes) and E is the set of edges connecting pairs of nodes. Many real systems are naturally graphs.
Nodes and Edges
Each node represents an entity (a user, an atom, a web page). Each edge represents a relationship (a friendship, a chemical bond, a hyperlink). Graph machine learning learns from this connective structure, not just isolated samples.
The Adjacency Matrix
The adjacency matrix A encodes which nodes are connected. For n nodes, A is n by n; A[i][j] = 1 if an edge connects node i and node j, else 0.
import numpy as np
# Triangle: 0-1, 1-2, 0-2
A = np.array([
[0, 1, 1],
[1, 0, 1],
[1, 1, 0],
])Node Features X
Beyond structure, each node usually carries a feature vector. Stacked together they form the feature matrix X of shape (num_nodes, num_features). For a social graph, a row might hold a user age, post count, and account age.
X = np.array([
[25, 120, 3.0], # node 0
[31, 45, 5.5], # node 1
[22, 300, 1.2], # node 2
])Degree Matrix
The degree of a node is its number of edges. The degree matrix D is diagonal, with each diagonal entry equal to that node degree. It is the building block of the graph Laplacian.
D = np.diag(A.sum(axis=1))
# Each diagonal entry = number of neighborsThe Graph Laplacian
The graph Laplacian is defined as L = D - A. It captures how values differ between connected nodes and is fundamental to spectral graph theory and many GNN formulations.
L = D - A
print(L)Why the Laplacian Matters
The Laplacian eigenvalues and eigenvectors reveal global structure: connectivity, clusters, and smoothness. Graph convolutions are often derived from the Laplacian, so understanding L = D - A is key to understanding GNNs.
Directed vs Undirected
In an undirected graph an edge has no direction (friendship), so A is symmetric. In a directed graph edges point one way (a follower, a citation), so A may be asymmetric: A[i][j] can differ from A[j][i].
# Directed: 0 -> 1 but not 1 -> 0
A_dir = np.array([
[0, 1, 0],
[0, 0, 1],
[0, 0, 0],
])Weighted Graphs
Edges can carry weights representing strength or distance. Then adjacency entries are real numbers, not just 0/1. A road network might weight edges by travel time; a similarity graph by cosine similarity.
Representing Edges Efficiently
For large sparse graphs the adjacency matrix wastes memory because most entries are zero. ML frameworks instead store edges as an edge list (COO format): two arrays giving the source and target of each edge. PyTorch Geometric calls this edge_index.
# Triangle edges as edge_index [2, num_edges]
edge_index = [
[0, 0, 1], # sources
[1, 2, 2], # targets
]Graph ML Applications
Graphs appear everywhere:
- Social: friend recommendation, community detection
- Molecular: predicting properties of molecules (atoms = nodes, bonds = edges)
- Knowledge graphs: link prediction over entities and relations
- Web/citation: ranking and classification
Quick Check
Test your graph theory knowledge.
Recap
You learned graph theory foundations for ML:
- A graph is
G = (V, E)with nodes and edges - The adjacency matrix A encodes connectivity; X holds node features
- The graph Laplacian is
L = D - A - Graphs may be directed or undirected, weighted or unweighted
- Applications span social, molecular, and knowledge graphs
Frequently asked questions
Is the “Graph Theory for Machine Learning” lesson free?
Yes — the full text of “Graph Theory for Machine Learning” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Graph Theory for Machine Learning”?
Nodes, edges, adjacency matrix, graph types, graph features, social network representations. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Graph Theory for Machine Learning” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Graph Theory for Machine Learning
- Graph Convolutional Networks (GCN)
- Node Classification with GNN
- Link Prediction and Graph Classification