Introduction to Graphs
Table of Contents + −
So far you have seen data that lines up in a row, like an array. You have also seen data that branches down, like a tree. But some things in real life do not fit either shape. Think about your friends on WhatsApp. You know Riya. Riya knows Arjun. Arjun also knows you. That is not a line. It is not a tree either. It loops back. To store relationships like this, we use a structure called a graph.
🤔 The Pain First
Picture this. You want to write a program that finds the shortest road from your home to the airport. Your city has hundreds of roads. Some roads connect two places directly. Some places have no direct road at all. A few roads are one-way.
Now try to fit that into an array. You can’t. An array is a straight line. Try a tree. A tree never loops back on itself. But roads do loop. So both break.
The thing is, the pain is not the data. The pain is the relationships between the data. A relationship is just a connection between two things. When connections go in many directions and even form loops, you need a structure built for exactly that. That structure is the graph.
🌐 A Real-World Picture
Look at a map of metro stations in your city. Each station is a dot. Each track between two stations is a line that joins two dots.
That is literally a graph. The dots are the data. The lines are the connections. You do not care how the dots are arranged on paper. You only care about which dot connects to which dot.
This same picture shows up everywhere:
- Google Maps — each place is a dot, each road is a line joining two places.
- Social networks — each person is a dot, each friendship is a line joining two people.
- The web — each web page is a dot, each link from one page to another is a line.
📚 What Is a Graph?
A graph is a set of dots joined by lines. In computer science we give those dots and lines proper names.
- A vertex is one dot in the graph. It holds one item, like a person or a city. The plural of vertex is vertices. People also call it a node.
- An edge is one line that joins two vertices. It says “these two are connected”. Like a friendship between two people. Or a road between two cities.
So a graph is just a bunch of vertices and the edges between them. Nothing more.
Here is a small graph with four people. An edge means they are friends.
Read it like this. Riya is friends with Arjun and Alex. Arjun is friends with Alex too. Alex is friends with Meera. See how Riya, Arjun, and Alex form a little loop? A tree could never do that. A graph does it easily.
➡️ Directed vs Undirected
Now, not every connection works both ways. So graphs come in two flavours.
An undirected graph has edges with no direction. The connection goes both ways. Friendship on Facebook is like this. If Riya is friends with Arjun, then Arjun is friends with Riya too. There is no one-way friendship. We draw these edges as plain lines.
A directed graph has edges that point one way. The connection has a direction. It goes from one vertex to another. We draw these as arrows.
Think about Instagram. You can follow a celebrity. But the celebrity may not follow you back. So the “follows” connection points one way only. That is a directed edge.
Here you follow the celebrity, but no arrow comes back. Riya and Arjun follow each other, so there are two arrows.
Tip
Here is an easy way to remember it. Undirected edges are like a phone call where both people can talk. Directed edges are like a letter you post. It goes one way to one address.
⚖️ Weighted vs Unweighted
There is one more thing an edge can carry. A number.
In an unweighted graph, an edge just says “connected or not”. That’s all. Every edge is treated the same.
In a weighted graph, each edge carries a number called a weight. A weight is a value attached to an edge. It tells you the cost of using that edge. On a road map the weight could be the distance in kilometres. Or the time in minutes. Or the toll you pay.
This is why Google Maps can tell you the shortest route. The roads are edges. The distances are weights. The app just adds up weights along different paths and picks the smallest total.
Going Home to Airport directly is 9 km. Going Home to Mall to Airport is 5 plus 3, which is 8 km. So the second path is shorter, even though it has more stops. Without weights you could never figure that out.
🧮 The Four Combinations
Put the two ideas together and you get four kinds of graphs. Here is where each one shows up.
| Type | Edges point one way? | Edges carry a number? | Real example |
|---|---|---|---|
| Undirected, unweighted | No | No | Facebook friends |
| Directed, unweighted | Yes | No | Instagram follows |
| Undirected, weighted | No | Yes | Roads with distances |
| Directed, weighted | Yes | Yes | Flight routes with prices |
💻 A Tiny Graph in Code
You do not need a fancy class to hold a graph. A simple way is to list, for each vertex, the vertices it connects to. This list is called an adjacency list. It just means “for each dot, here are the dots it touches”.
Let us store the small friendship graph from earlier and print each person’s friends. This shows that a graph is really just data you already know how to handle.
# Print the friends of each person in an undirected graph
# Adjacency list: each person maps to a list of their friendsfriends = { "Riya": ["Arjun", "Alex"], "Arjun": ["Riya", "Alex"], "Alex": ["Riya", "Arjun", "Meera"], "Meera": ["Alex"],}
for person, their_friends in friends.items(): print(person, "is friends with:", " ".join(their_friends))The output of the above code will be:
Riya is friends with: Arjun AlexArjun is friends with: Riya AlexAlex is friends with: Riya Arjun MeeraMeera is friends with: AlexNote
This is just one way to store a graph. There are other ways too, like a grid of true/false values. We will go deeper into storing graphs in the Graph Representation tutorial.
⚠️ Common Mistakes
A few things trip up beginners when they first meet graphs. Watch out for these.
- Mixing up directed and undirected. In an undirected graph, one edge means both people are connected. You do not add it twice.
- Forgetting that a graph can have loops. This is fine and normal. A tree forbids loops, but a graph welcomes them.
- Thinking every vertex must connect to every other one. Not true. A vertex can sit alone with zero edges, and the graph is still valid.
- Ignoring weights when the problem clearly has costs. If the question talks about distance, time, or money, you almost always need a weighted graph.
🧩 What You’ve Learned
- ✅ A graph stores data as vertices (dots) joined by edges (lines), perfect for relationships that loop and branch in any direction.
- ✅ A vertex holds one item; an edge connects two vertices.
- ✅ Undirected edges go both ways, directed edges point one way.
- ✅ Unweighted edges only say “connected”, weighted edges carry a number like distance or cost.
- ✅ Real systems like Google Maps, social networks, and the web are all graphs underneath.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
In graph terms, what is a vertex?
Why: A vertex (also called a node) is one dot in the graph that holds a single item, like a person or a city.
- 2
Instagram lets you follow a celebrity who does not follow you back. What kind of graph is this?
Why: The 'follows' connection points one way only, so it is a directed edge in a directed graph.
- 3
What does the weight on an edge usually represent?
Why: A weight is a value attached to an edge that gives the cost of using it, such as distance, time, or money.
- 4
Why can't a simple tree represent a road map of a city?
Why: Roads often loop back to earlier places, and a tree forbids loops, so a graph is needed instead.