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.
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.
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.
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.)
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-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.
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.
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.
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.
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.
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.
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.
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.
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.
11 either way. A linked list of 12 nodes has 11 links. Any tree on n vertices has n − 1 edges, whatever its shape.
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.
Coming next: to write graph algorithms, a program has to store the graph. Representing graphs: adjacency lists and matrices ↗ shows the two standard ways, and lets you build graphs and watch each structure change.