Data Structures · Graphs

Introduction to graphs

Lists, trees and heaps each connect items in one fixed pattern. A graph lets anything connect to anything. This page defines graphs, names the common kinds, and gives you a lab where you build graphs and see which kinds they are.

What is a graph?

A graph is a non-linear data structure made of vertices (also called nodes) and edges (also called arcs). Each edge connects two vertices. Formally, a graph is a set of vertices V and a set of edges E, written G(V, E).

In an undirected graph, an edge is an unordered pair {A, B}: it goes both ways. In a directed graph, an edge is an ordered pair (A, B): it goes from A to B only, and is drawn as an arrow.


      
Undirected. Each edge is a set of two vertices, so {A, B} and {B, A} are the same edge.

      
Directed. Each edge is an ordered pair. (A, B) is in E, but (B, A) is not, so you can go from A to B but not back.

Graphs model relationships

Graphs are a tool for representing and analyzing relationships: social networks (people and friendships), recommendation systems (users and the items they liked), computer networks (machines and cables), and road maps (cities and roads).

Sports data science uses them too. Think of a football match as a web of connections: players are the vertices, and passes are the edges. Because a pass goes from one player to another, the edges are directed, and the number of passes can be stored on each edge as a weight.

A passing network. Each arrow is labeled with how many times one player passed to the other; thicker arrows mean more passes. The busiest route is CB → CM (15). CB and LB pass to each other, so there are two arrows between them.

Words you will need

  • Adjacent: two vertices joined by an edge. B is adjacent to A, C and D below.
  • Degree: the number of edges at a vertex. In a directed graph, the in-degree counts arrows coming in and the out-degree counts arrows going out.
  • Path: a sequence of vertices where each one is adjacent to the next, such as A – B – C.
  • Cycle: a path that comes back to where it started without reusing an edge, such as B – C – D – B (in red).
  • Simple graph: no edge from a vertex to itself, and at most one edge between two vertices. Every graph on this page is simple.
Degrees and a cycle. The number under each vertex is its degree. They add up to 10, twice the 5 edges, because every edge touches two vertices.
Stop and think
Add up the degrees of every vertex in any graph. Why is the total always 2 × E?

Each edge has two ends, and each end adds 1 to the degree of the vertex it touches. So every edge adds exactly 2 to the total. (In a directed graph, each edge adds 1 to an out-degree and 1 to an in-degree.)

Can a graph have exactly three vertices with an odd degree?

No. The degrees add up to 2E, an even number. If three of them were odd and the rest even, the total would be odd. The number of odd-degree vertices is always even.

In the passing network, what is CM's in-degree and out-degree? What does each mean?

In-degree 3 (from CB, LB and ST): three players passed to CM. Out-degree 1 (to ST): CM only passed to the striker. The weights tell you more: CM received 15 + 9 + 3 = 27 passes.

Kinds of graphs

These names describe a graph's shape. A graph can be several kinds at once: a cycle graph is also cyclic, connected and 2-regular. Click Open in the lab on any card to load it below and see every property it has.

Graph lab: what kind of graph is this?

Click empty space to add a vertex. Click one vertex and then another to add an edge. Drag to move a vertex. With Weighted on, click a weight to change it. The list on the right updates as you go; click a property to see the reason on the graph.


        
Changes to the graph are explained here.

Properties

Challenges

    Stop and think
    Is a trivial graph also an empty graph?

    Yes. It has one vertex and no edges, and "no edges" is all an empty graph needs. Load the trivial graph in the lab and both rows say yes.

    Every cycle graph is cyclic. Is every cyclic graph a cycle graph?

    No. A cyclic graph only needs one cycle somewhere; the rest of the graph can look like anything. A cycle graph must be exactly one loop, with every vertex of degree 2. The "Cyclic" card is cyclic but not a cycle graph.

    A complete graph on n vertices is regular. What is its degree, and how many edges does it have?

    Each vertex connects to the other n − 1, so it is (n − 1)-regular. The degrees add up to n(n − 1), which is 2E, so E = n(n − 1) / 2. For 5 vertices that is 10 edges. If everyone at a party of n people shakes hands with everyone else, that is the number of handshakes.

    Which cycle graphs are bipartite?

    The even ones. Going around the loop, the colors must alternate, which works only if you come back to the start after an even number of steps. In fact, a graph is bipartite exactly when it has no cycle of odd length.

    Why does a DAG have to be directed?

    An undirected edge can be walked both ways, so any undirected graph with three or more vertices in a loop has a cycle, and even a single edge A – B can be walked A, B, A. "Acyclic" only becomes a useful idea when the arrows restrict which way you can go.

    Trees are graphs too

    A tree is a graph with extra rules: it is connected and has no cycles. Every tree is a graph, but most graphs are not trees. A tree with n vertices always has exactly n − 1 edges. Remove any edge and it falls into two pieces; add any edge and it gets a cycle.

    The structures from earlier in the course are special cases of graphs. A linked list is a path. A binary tree and a heap are trees where each vertex has at most two children. When we draw them, the edges point from parent to child, but as graphs their shape is a tree.

    Stop and think
    A tree has 12 vertices. How many edges does it have? What if it is a linked list?

    11 either way. A linked list of 12 nodes has 11 links. Any tree on n vertices has n − 1 edges, whatever its shape.

    Why can a graph do things a tree cannot?

    A tree has exactly one path between any two vertices. A graph can have many paths, or none. A road map needs several routes between two cities, and a social network has cycles of friends. A tree cannot store either.