欢迎来到图的世界 | Welcome to the World of Graphs
欢迎来到图的世界
Welcome to the World of Graphs
想象一下,你正在刷朋友圈,看到了老同学分享的一张照片。你顺手点了个赞,又顺便看了看评论区里其他人的互动。也许你会想,这个人和那个人的关系怎么样?他们是通过谁认识的?如果你有这样的好奇心,那么恭喜你——你已经具备了理解图数据的天赋。
Imagine this: you're scrolling through your social media feed, and you see a photo shared by an old classmate. You tap the like button, and then casually glance at the interactions in the comments. Maybe you wonder about the relationship between this person and that person—how did they get to know each other? If you've ever had such curiosity, congratulations—you already possess the innate talent for understanding graph data.
在我们每天接触的信息海洋中,有一种数据结构无处不在,却又常常被忽视——那就是图(Graph)。社交网络是图,推荐系统是图,交通网络是图,甚至连我们的大脑神经网络,在某种意义上也是一个庞大而精密的图结构。理解图,就是理解这个世界运作方式的一把钥匙。
In the vast ocean of information we encounter daily, there exists a data structure that is omnipresent yet often overlooked—the graph. Social networks are graphs, recommendation systems are graphs, transportation networks are graphs, and even our own brain's neural network is, in a sense, a grand and intricate graph structure. Understanding graphs is understanding a key to how the world operates.
本章将带你走进图的世界。我们会从身边的实例出发,让你直观感受图数据的无处不在;然后深入浅出地讲解图的核心概念和术语;最后,你会明白为什么传统的深度学习方法在处理图数据时显得力不从心,以及图神经网络(GNN)如何应运而生,成为AI领域的一颗璀璨新星。
In this chapter, we'll take you into the world of graphs. We'll start from real-world examples around us, letting you intuitively feel the omnipresence of graph data; then we'll explain the core concepts and terminology of graphs in an accessible way; finally, you'll understand why traditional deep learning methods struggle with graph data, and how Graph Neural Networks (GNN) emerged as a brilliant new star in the AI field.
---
你身边的图:从社交网络到推荐系统
Graphs Around You: From Social Networks to Recommendation Systems
让我们先来做一个小小的实验。拿出你的手机,打开任意一个社交软件,把你的好友列表想象成一幅画。在那幅画里,每个人是一个小圆点(我们称之为节点,Node),而如果你和某个人是好友,你们之间就画一条线(我们称之为边,Edge)。恭喜,你现在已经在脑海中构建了一个社交网络图!
Let's start with a little experiment. Take out your phone, open any social media app, and imagine your friend list as a painting. In that painting, each person is a small dot (what we call a node), and if you're friends with someone, you draw a line between you (what we call an edge). Congratulations—you've now constructed a social network graph in your mind!
社交网络:六度分隔理论的可视化
Social Networks: Visualizing the Six Degrees of Separation
社交网络是图最经典的应用场景之一。1967年,社会心理学家斯坦利·米尔格拉姆(Stanley Milgram)提出了著名的"六度分隔理论"(Six Degrees of Separation),他认为世界上任意两个人之间的平均距离不超过六个人。换句话说,你的朋友的朋友的朋友的朋友的朋友的朋友,很可能就认识好莱坞明星汤姆·克鲁斯。这个理论在当时听起来像是天方夜谭,但当我们用图的视角来看时,一切都变得清晰起来。
Social networks are one of the most classic application scenarios for graphs. In 1967, social psychologist Stanley Milgram proposed the famous "Six Degrees of Separation" theory, suggesting that any two people in the world are connected through an average of no more than six intermediaries. In other words, your friend's friend's friend's friend's friend's friend might well know Hollywood star Tom Cruise. This theory sounded like a fantasy at the time, but when we view it through the lens of graphs, everything becomes clear.
```mermaid graph LR A["你 (You)"] --> B["朋友A (Friend A)"] A --> C["朋友B (Friend B)"] B --> D["朋友的朋友 (Friend of Friend)"] C --> E["朋友的朋友 (Friend of Friend)"] D --> F["...") E --> G["汤姆·克鲁斯 (Tom Cruise)"] F --> G style A fill:#e1f5fe style G fill:#ffcc80 ```
在这个图中,每个节点代表一个人,每条边代表"认识"关系。米尔格拉姆的实验实际上就是在探索这个图的路径长度(Path Length)——从你到汤姆·克鲁斯,最短需要多少条边?
In this graph, each node represents a person, and each edge represents the "knowing" relationship. Milgram's experiment was essentially exploring the path length of this graph—how many edges at minimum are needed to connect you to Tom Cruise?
如今,Facebook的研究团队在2016年发表了一篇论文,他们分析了21.9亿用户的数据,发现任意两个用户之间的平均距离只有3.57!也就是说,在Facebook这个巨大的社交网络图中,99%的节点对之间的路径长度不超过4。这个发现让六度分隔理论得到了实打实的数据支持。
Today, Facebook's research team published a paper in 2016 analyzing data from 2.19 billion users and discovered that the average distance between any two users is only 3.57! In other words, in Facebook's massive social network graph, 99% of node pairs have a path length of no more than 4. This finding provided solid data support for the Six Degrees of Separation theory.
推荐系统:图结构的力量
Recommendation Systems: The Power of Graph Structures
如果你觉得社交网络图还不够直观,那么让我们看看另一个无处不在的图——推荐系统。打开淘宝、京东、亚马逊,或者Netflix、Spotify,你会发现这些平台都在"猜你喜欢"。他们是怎么做到的呢?答案就藏在用户-商品交互图中。
If you think social network graphs aren't intuitive enough, let's look at another ubiquitous graph—the recommendation system. Open Taobao, JD.com, Amazon, or Netflix, Spotify, and you'll find these platforms are all "guessing what you like." How do they manage this? The answer lies in the user-item interaction graph.
在这个图中,有两类不同的节点:用户(User)和商品(Item)。用户和商品之间的边代表"购买"、"浏览"或"收藏"等交互行为。这就是一个典型的异构图(Heterogeneous Graph)——因为图中存在多种类型的节点和边。
In this graph, there are two distinct types of nodes: users and items. Edges between users and items represent interactions like "purchase," "browse," or "add to favorites." This is a typical heterogeneous graph—because the graph contains multiple types of nodes and edges.
```mermaid graph TB subgraph "用户层 (User Layer)" U1["用户A (User A)"] U2["用户B (User B)"] U3["用户C (User C)"] end subgraph "商品层 (Item Layer)" I1["商品1 (Item 1)"] I2["商品2 (Item 2)"] I3["商品3 (Item 3)"] I4["商品4 (Item 4)"] end U1 --> I1 U1 --> I2 U2 --> I2 U2 --> I3 U3 --> I3 U3 --> I4 style U1 fill:#e1f5fe style U2 fill:#e1f5fe style U3 fill:#e1f5fe style I1 fill:#c8e6c9 style I2 fill:#c8e6c9 style I3 fill:#c8e6c9 style I4 fill:#c8e6c9 ```
推荐系统的核心问题是这样:给定用户A和商品X,我们如何预测用户A会不会喜欢商品X?
The core problem of recommendation systems is this: Given user A and item X, how do we predict whether user A will like item X?
在图的视角下,这个问题变得优雅起来。如果我们发现用户A和用户B都购买了商品1和商品2,同时用户B还购买了商品3,那么我们可以合理推测:用户A可能也会喜欢商品3。这种基于图的协同过滤(Collaborative Filtering)方法,是推荐系统最早也是最成功的算法之一。
Through the lens of graphs, this problem becomes elegant. If we discover that both user A and user B purchased items 1 and 2, and user B also purchased item 3, then we can reasonably hypothesize that user A might also like item 3. This graph-based collaborative filtering approach is one of the earliest and most successful algorithms in recommendation systems.
更进一步,现代的推荐系统还会考虑商品之间的知识图谱(Knowledge Graph)。什么是知识图谱?想象一下,商品1是iPhone 15,商品2是Apple,商品3是智能手机。它们之间存在"是...的品牌"(brand_of)、"是...的子类"(subclass_of)等关系。当用户A购买了iPhone 15,我们不仅知道她喜欢Apple产品,还知道她对智能手机感兴趣,从而可以推荐其他智能手机品牌——比如三星Galaxy。
Taking it further, modern recommendation systems also consider knowledge graphs between items. What's a knowledge graph? Imagine item 1 is iPhone 15, item 2 is Apple, item 3 is smartphone. They have relationships like "is the brand of" (brand_of), "is a subclass of" (subclass_of), etc. When user A purchases an iPhone 15, we not only know she likes Apple products, but also that she's interested in smartphones, enabling us to recommend other smartphone brands—like Samsung Galaxy.
交通网络:图的空间维度
Transportation Networks: The Spatial Dimension of Graphs
让我们再看看一个更加具象的图——交通网络。打开地图应用,规划从家到公司的路线,背后的算法正是图论的应用。每个路口是一个节点,每条道路是一条边。如果道路有长度(距离)或通行时间(时间),我们就得到了一个加权图(Weighted Graph),其中边的权重代表距离或时间。
Let's look at a more concrete graph—transportation networks. Open a map application and plan a route from home to work; the underlying algorithm is precisely an application of graph theory. Each intersection is a node, and each road is an edge. If roads have length (distance) or travel time (time), we get a weighted graph, where edge weights represent distance or time.
```mermaid graph LR A["家 (Home)"] -->|"5km, 10min"| B["地铁站 (Metro)"] B -->|"8km, 15min"| C["公司附近 (Near Office)"] A -->|"3km, 8min"| D["公交站 (Bus Stop)"] D -->|"6km, 12min"| C style A fill:#ffcc80 style C fill:#ffcc80 ```
最短路径问题(Shortest Path Problem)是图论中最经典的问题之一。Dijks