No history yet

Graph Basics

What is a Graph?

Think of a graph as a simple way to map relationships. It's not the kind of graph you see in a math class with x and y axes. In this context, a graph is a collection of dots and the lines connecting them. It's a visual language for showing how things are linked, whether it's friends in a social network, cities on a map, or steps in a process.

Every graph is made of two basic ingredients: vertices and edges.

Vertex

noun

A point or a node in a graph. It represents an individual object or entity.

Edge

noun

A line or link that connects two vertices. It represents the relationship or connection between them.

Together, these two parts form a graph. Let's look at a simple one. Here, the circles are vertices (let's call them A, B, C, and D), and the lines are the edges connecting them.

Direction Matters

Sometimes, the relationship between two vertices is a two-way street. If you're friends with someone on Facebook, they are also friends with you. The connection is mutual. This is an undirected graph. The edges are like simple lines; they don't have a specific direction.

Other times, the relationship is one-way. On Twitter, you might follow a celebrity, but that doesn't mean they automatically follow you back. This is a directed graph, or digraph. The edges are drawn as arrows to show which way the relationship goes.

In the directed graph, the arrow from A to B shows that A follows B. The arrow from C to B shows that C follows B. There's no arrow going from B to A, so the relationship isn't mutual in that case.

Describing Connections

We have specific terms to describe how vertices are connected. If an edge exists between two vertices, we say they are adjacent.

In our first example graph, vertex A is adjacent to B and C, but not to D.

We can also count the connections a vertex has. This count is called the degree of a vertex. In an undirected graph, it's simply the number of edges touching that vertex.

For directed graphs, we get more specific. The number of arrows pointing to a vertex is its in-degree. The number of arrows pointing away from it is its out-degree.

Think back to the Twitter example. If a user has 1,000 followers, their vertex has an in-degree of 1,000. If they follow 50 accounts, their vertex has an out-degree of 50.

Keeping it Simple (Or Not)

Most of the graphs we've seen so far are simple graphs. This means they follow two rules:

  1. There's at most one edge between any two vertices.
  2. No vertex has an edge that connects back to itself (this is called a loop).

But what if we need to represent more complex relationships? For that, we use a multigraph. A multigraph can have multiple edges between the same two vertices and can also have loops.

A multigraph might be useful for modeling a city's transportation network. Vertices could be locations, and edges could be different modes of transport. You might have two edges between 'Downtown' and 'Uptown': one for the subway and one for the bus. A loop could represent a sightseeing bus tour that starts and ends at the same landmark.

Now, let's test your understanding of these fundamental concepts.

Quiz Questions 1/5

What are the two fundamental components that make up any graph?

Quiz Questions 2/5

If you were modeling a social network like Instagram, where following someone doesn't automatically mean they follow you back, which type of graph would be most appropriate?