O'Reilly logo

A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Graphs

A graph is a data structure that specializes in relationships, as it easily conveys how data is connected.

Here is a visualization of our Facebook network:

images/chapter14/graphs_Part1.png

Each person is represented by a node, and each line indicates a friendship with another person. In graph jargon, each node is called a vertex, and each line is called an edge. Vertices that are connected by an edge are said to be adjacent to each other.

There are a number of ways that a graph can be implemented, but one of the simplest ways is using a hash table (see Chapter 7, Blazing Fast Lookup with Hash Tables). Here’s a bare-bones Ruby implementation of our social network:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required