seegongsik
Algorithms

What is a graph in computer science

Think of a subway map. The stations are points, and the tracks connecting them are lines. Friendships and web links can be drawn the same way. Points and lines: that's a graph.

01

Points are things

A graph starts with points. Each point is one thing: a station, a person, a web page, anything at all.
We give each point a name so we can tell which one it is.
There are no lines yet, just points scattered around.

Tap an empty spot to place a dot

Press an empty spot to place a point. Each point is one thing. (point = node)

The more points you place, the more things there are. But points alone are just scattered dots.
It's like stations standing on their own, which doesn't make a subway map. What's missing is how the points connect to one another.
We'll draw those connections next.

02

Lines are relationships

Now let's connect the points. Joining two points with a line means "these two are related."
There's a track between two stations, two people are friends, this web page links to that one. Each line is one relationship.
Put points and lines together and you have a graph.

ABCDE
Tap a dot, then another to link them

Press one point and then another, and a line appears. Each line is one relationship. (line = edge)

As you draw lines, the points get woven together, and the scattered dots finally become a subway map.
Points are things; lines are relationships. With just these two, you can draw countless things in the world.
Among them hides a shape we already know.

03

A tree is a special graph

A graph can contain loops. If you start at point A, follow the lines, and come back to A, that's a loop.
A graph with no loops at all is exactly a tree: a shape that only branches outward, with no way back around.
So a tree is one kind of graph.

ABCDE
The red dashed line is the loop. Tap to cut it

Press the line that makes the loop to cut it. With the loop gone, it becomes a tree. Tree ⊂ graph.

Cutting one loop removed the way back around, and the tangled-looking graph became a neat tree.
The folders and family trees we saw earlier were trees just like this.
A tree is a special case of a graph; in other words, the graph is the bigger umbrella.

04

Graphs are everywhere

Put on the glasses of points and lines, and the world looks different.
The subway is stations (points) and routes (lines), a friend network is people (points) and friendships (lines), and the web is pages (points) and links (lines).
Things that seemed completely different were really the same picture. So solve one, and you've solved the others too.

City HallGangnamJamsilSinchon
dot = station, line = line

Press an example to switch. Subway, friend network, web: all the same points and lines.

Switching examples doesn't change the skeleton of the picture. Points are things, lines are relationships; only the labels change.
That's exactly why graphs are so useful: a single tool can draw countless problems in the world.

05

Summary

In one line: points are things, lines are relationships, and a picture drawn with points and lines is a graph. A special graph with no loops is a tree.
Subways, friend networks, and the web can all be drawn with points and lines.
Now that the points and lines are in place, one question remains: how do we move around between these points?

1. dot = thing
2. line = relationship
3. dots + lines = graph
4. tree = graph with no loop
Press the button to review the key points in order

Press the key points in order to review. (point = thing → line = relationship → graph → a tree is a graph with no loops)

Now that you can draw graphs, the real fun begins.
Starting from one point and following the lines to visit every point is called graph traversal, and finding the shortest route comes after that.
In the next lesson, we'll follow how to move across points and lines.

In one lineA graph is "a picture drawn with points (things) and lines (relationships)." A point can be anything, and a line is a relationship between two of them. Subways, friend networks, and the web are all graphs. A tree is the special case with no loops (cycles), so a tree is one kind of graph too. See the world as points and lines, and things that looked complicated become clear at a glance.
← Algorithms
Was this helpful? Support seegongsik