Welcome to the World of Graphs
Have you ever stopped to think about all the different ways things in your life are connected? Think about your best friend at school. How did you meet them? Maybe you both joined the same soccer team, or you sat next to each other in class, or your parents are friends with their parents. Now think about your other friends. Some of them probably know each other too—maybe your soccer teammate is also in your math class, or your cousin is friends with your neighbor. These connections between people form a complex web that scientists and mathematicians love to study.
Here's something amazing: almost everything in our world is connected in similar ways. The roads you travel on connect different places together. The websites you visit connect to each other through links. Even the cells in your body are connected in intricate networks! When we want computers to understand these connections and learn from them, we use something called graphs. And when we want computers to think and learn using graphs, we use a special kind of artificial intelligence called Graph Neural Networks.
In this first chapter, we are going to explore the wonderful world of graphs together. You will discover that graphs are everywhere around you—hiding in plain sight in the most ordinary places. You will meet our friendly guides, Node Nelly and Edge Eddie, who will help us understand even the trickiest ideas. And you will start to see why scientists and engineers are so excited about teaching computers to understand connections.
But before we dive in, let me ask you a question: Can you draw a picture of your friendship group? Not just a list of your friends, but a picture that shows how they are connected to each other? Some friends might be very close and spend lots of time together, while others might be friends-of-friends who you only know through your other friends. If you can draw these connections, congratulations—you have already started working with graphs!
The Hidden Graphs All Around Us
Let us take a journey through a typical day and discover the graphs hiding in plain sight. When you wake up in the morning, the first thing you might do is check your phone. Have you ever noticed how social media apps show you who your friends are talking to, or suggest people you might know? Behind the scenes, these apps are using graphs to represent all the friendships and connections between people. When you have 100 friends on a social media platform, that is not just 100 separate people—it is a network where everyone is connected to everyone else they know, forming an intricate web of relationships.
Think about your morning commute to school. Whether you walk, take a bus, or get a ride from your parents, you are traveling on roads that form a network. Each intersection is like a point where roads meet, and each road is like a connection between those points. If you use a navigation app to find the fastest route, that app is using graph algorithms to calculate the best way to get you from one place to another. It might consider many different paths and find the one that is quickest, shortest, or avoids traffic.
Now think about your family. Your family tree is another kind of graph! Each person is a point, and the connections between parents and children, between siblings, between cousins—these are all the edges that connect your family together. When you look at an old photograph and wonder how someone is related to you, you are essentially navigating through your family graph.
Even your school is full of graphs. The classes you take are connected by prerequisites—you might need to learn basic math before you can take algebra, and algebra before calculus. These connections form an educational graph that helps teachers plan the best order for you to learn things.
Let us not forget about the internet itself! When you click on a link on a website, you are following a connection to another page. Each webpage can link to many other pages, and those pages link to more pages, forming the massive graph that we call the World Wide Web. Search engines like Google use this structure to help you find information—treating the internet as a graph where the connections between pages tell us about the importance and relevance of different websites.
```mermaid graph LR A[You] --> B[Best Friend] A --> C[Soccer Teammate] A --> D[Cousin] B --> C B --> E[Friend's Friend] C --> D C --> F[Classmate] D --> G[Uncle] E --> F F --> G style A fill:#ff9999 style B fill:#99ccff style C fill:#99ccff style D fill:#99ccff style E fill:#99ccff style F fill:#99ccff style G fill:#99ccff ```
This diagram shows a simple friendship graph. The big red circle represents you, and the blue circles represent your friends and their connections. Notice how some friends are directly connected to you, while others are connected to you through your other friends. This is exactly how social media apps see your social network!
Understanding the Building Blocks: Nodes and Edges
Now that we have discovered graphs all around us, let us learn what makes a graph actually a graph. Every graph is made up of two key ingredients: nodes (also called vertices) and edges (also called links or connections).
Think of nodes as the "things" in your graph. In a friendship graph, each node is a person. In a road map graph, each node is a location—an intersection, a city, or a landmark. In a family tree, each node is a family member. Nodes are the individual entities that we want to represent.
Edges are the connections between nodes. In a friendship graph, edges connect people who are friends. In a road map, edges are the actual roads that connect different locations. In a family tree, edges connect parents to their children. Edges represent the relationships or connections that exist between the things in our graph.
Let us look at a very simple example. Imagine you have three friends: Alice, Ben, and Charlie. Alice and Ben are friends with each other. Ben and Charlie are friends with each other. But Alice and Charlie do not know each other. We can represent this as a graph with three nodes (Alice, Ben, Charlie) and two edges (Alice-Ben and Ben-Charlie).
```mermaid graph TD A["👧 Alice"] <--> B["👦 Ben"] B["👦 Ben"] <--> C["👧 Charlie"] style A fill:#ffcccc style B fill:#ccffcc style C fill:#ccccff ```
Simple, right? Now here is where it gets interesting. Sometimes edges can have different types or strengths. In our friendship example, maybe Alice and Ben are best friends who talk every day, while Ben and Charlie are just casual acquaintances. We might want our graph to show this difference. There are several ways to do this.
First, we can use weighted edges. A weight is a number that represents the strength of a connection. Maybe Alice and Ben's friendship has a weight of 10 (because they are best friends), while Ben and Charlie's connection has a weight of 2 (because they only see each other occasionally). In a road map, weights could represent the distance between cities or the travel time between locations.
Second, edges can be directed or undirected. An undirected edge means the relationship goes both ways—Alice is friends with Ben, and Ben is friends with Alice. A directed edge means the relationship has a direction. Think about following someone on social media: you might follow a famous person, but they probably do not follow you back! In this case, we use a directed edge with an arrow showing that you follow them, but they do not follow you.
Third, edges can have different types. In a family tree, we have parent-child relationships. In a computer network, we might have different types of connections—wired connections, wireless connections, or different speeds of connection. We can represent these different types using different colors or labels on our edges.
```mermaid graph TD A["👧 Alice"] -.->|"follows