Linking data with dots and lines, the graph
Say you write friendships in a table. Listing who is friends with whom row by row, finding a friend of a friend means digging through the table again and again. But what if you draw each person as a dot and each friendship as a line? From one dot you just follow a line to reach a friend, and follow that friend's line to see a friend of a friend right away. Holding relations with dots and lines like this is a graph.
Hold relations as dots and lines
Earlier we learned to hold values
lined up by slot numbers.
But for some data,
the relation between things matters more than each value.
Like who is friends with whom.
Lining such data up feels awkward.
So we draw each person as a dot,
and each friendship as a line.
With just dots and lines, the relation is held as is.
Tap two people in turn to link them with a line. Dots are people, lines are friendships.
Once dots were linked, the relation became visible.
Who is connected to whom,
you can tell just by looking at the lines.
It lands far more like a picture
than rows written one by one in a table.
Dots are things, lines are relations.
Data held with these two is called a graph.
Then if we follow these lines,
what can we find out?
Along the lines to a friend of a friend
Let's start from one person.
Follow the lines tied to that dot,
and right away that person's friends appear.
Without stopping there,
follow the lines tied to a friend's dot too,
and a friend of a friend appears.
Stepping one hop farther like this,
you reach acquaintances of acquaintances.
This is how recommendations are made.
Tap one person to light up friends, tap again for friends of friends, step by step.
One tap lit up friends,
another tap lit up friends of friends.
How far someone is from the start
is set by how many lines you crossed.
One line away is a friend,
two lines away is a friend of a friend.
With a table you would have dug
again and again to find this.
Following lines, it is this easy.
Follow neighbors to tour the group
Dots linked by a line straight to one dot
are called that dot's neighbors.
From one dot to its neighbors,
then to the neighbors of those,
let's keep spreading along the lines.
In time there is nowhere left to go.
The dots reached by then
are one group connected to each other.
A dot with no line in does not join it.
Tap a neighbor touching the lit group's edge to spread one hop. Only connected dots light up.
Spreading along neighbors,
the connected dots lit up one by one.
Wherever a line reached, you could tour.
The separate dots, on the other hand,
never lit up no matter how far you went.
Whether a line connects them is what splits the groups.
Following neighbors like this
is easy in a graph, since you just watch the lines.
But what if you did the same with a table?
By table, or by graph
Say friendships are written in a table.
To find a friend of a friend,
first look up friends in the table and note them,
then for each friend dig through the table again.
Every time you go one step farther,
you scan the table from the top once more.
With a graph, follow a line from a dot,
then follow a line from that dot, and you are done.
When relations are the point, a graph is far handier.
Solve the same "friend of a friend" question by table and by graph. Compare how many taps each takes.
By table, finding the same answer
took far more taps.
Because it meant looking up again and again.
The graph followed lines twice and was done.
Of course a table is not bad.
For writing things straight, row by row, a table is great.
But for tasks that follow relations,
a graph that holds them as dots and lines
is far more natural and fast.
Let's wrap up
Gathered on one line, it is this.
Hold things as dots and relations as lines.
That is a graph.
From one dot, following a line gives a neighbor,
and following the neighbor gives a friend of a friend.
Spread along lines and a connected group appears.
For tasks where relations are the point,
what a table makes you dig for, you reach by just following lines.
Dots are things, lines are relations, follow neighbors. That is a graph.
Tap the key points in order to review. (dots are things, lines are relations -> friend of a friend -> follow neighbors -> graphs for relations)
Now you know how to hold relations as dots and lines
and roam by following lines.
A friend of a friend, awkward by table,
was reached in two lines on a graph.
Depending on what shape you give data,
the same task gets easier or harder.
The eye for choosing that shape
is the power of handling data.
Let's keep exploring together next time too.