8Graphs
What is a graph?
In a tree every node had a single parent and there was one path down from the root. Real-world links are seldom that tidy: roads run from one city to many, a person has many friends, and friendships form cycles. The structure that keeps relations like these is called a graph. This lesson explains the terms used in the rest of this part on one sample graph.
- What a graph is and how it differs from a tree
- Directed, undirected and weighted graphs
- Degree, path, cycle and connected graphs, with the formulas exams ask about
1Vertices and edges
A graph is made of two sets: vertices and edges. Every edge joins two vertices. Vertices are also called nodes. In code they are numbered from 0 to n-1, so a vertex can be used as an array index. The graph itself is often written G = (V, E): V is the set of vertices and E the set of edges.
A graph is a more general structure than a tree. A graph has no root, a vertex can have any number of neighbours and the edges can form cycles. A tree is in fact a special graph: a connected graph with no cycles. That is why a tree with n vertices has exactly n - 1 edges; the sample graph has 6 vertices and 7 edges, so it must contain at least one cycle.
Graphs are everywhere: cities and roads on a map, people and friendships in a social network, web pages and links, courses and their prerequisites. A navigation app finding the shortest route and a search engine crawling pages are algorithms running on graphs.
2Undirected and directed graphs
In an undirected graph an edge has no direction: if there is an edge between 0 and 1, you can go from 0 to 1 and from 1 to 0. Friendship works like this. In a directed graph every edge is an arrow: an edge from 0 to 1 does not mean you can go from 1 to 0. Following someone on Instagram, one-way streets and course prerequisites are directed.
3Weighted graphs
In a weighted graph every edge carries a number: the kilometres between two cities, or the time or cost of a link. Without weights every edge counts the same and the length of a path is its number of edges. Finding shortest paths in a weighted graph (with Dijkstra's algorithm, for example) belongs to the algorithms field; this part works with unweighted graphs.
4Neighbours and degree
Two vertices with an edge between them are adjacent, or neighbours. The degree of a vertex is the number of edges joined to it. In the sample graph vertices 1 and 4 have degree 3 and the others degree 2. A directed graph has two separate numbers: the number of edges coming into a vertex is its in-degree, the number going out is its out-degree.
The formula exams ask about most is the handshake rule: in an undirected graph the degrees of all vertices add up to twice the number of edges, because every edge adds one to the degree of each of its two ends. In the example 2 + 3 + 2 + 2 + 3 + 2 = 14 = 2 × 7. It follows that the number of vertices with an odd degree is always even.
| Formula | Meaning | In the example |
|---|---|---|
| Σ degree = 2 × E | The degrees add up to twice the number of edges | 14 = 2 × 7 |
| E ≤ n(n - 1) / 2 | Most edges in an undirected simple graph | at most 15 for 6 vertices |
| E ≤ n(n - 1) | Most edges in a directed simple graph | at most 30 for 6 vertices |
| E = n - 1 | Number of edges in a tree | 5 edges in a tree with 6 vertices |
The limits in the table are for a simple graph: there is no edge from a vertex to itself (self-loop) and no more than one edge between two vertices. Every graph in this part is simple. A graph whose number of edges is close to the maximum is dense, one far below it is sparse; real graphs are mostly sparse.
5Paths and cycles
A path is a sequence of vertices in which each one is joined to the next by an edge. The length of a path is its number of edges. In the example 0, 1, 2, 5 is a path of length 3. Unlike in a tree, a graph can have more than one path between two vertices: 0, 3, 4, 5 also leads from 0 to 5.
A path that ends at the vertex where it started without repeating an edge is a cycle. In the example 1, 2, 5, 4, 1 is a cycle. A graph with no cycle is acyclic. Directed acyclic graphs (DAGs) are used to order tasks; finding a cycle in a graph and ordering the vertices are covered in the graph applications subtopic.
6Connected graphs
An undirected graph is connected if there is a path between any two of its vertices. The sample graph is connected. Remove some edges and it can fall apart into pieces; each piece is called a connected component. Finding how many pieces a graph has is the subject of the connected components lesson.
7How is a graph kept in memory?
There are two common ways to keep a graph in memory, and this part codes both with the same operations. An adjacency matrix is an n × n table: cell adj[u][v] is 1 when there is an edge. An adjacency list is a linked list of neighbours for each vertex. Then come the two basic traversals: breadth-first search (BFS) and depth-first search (DFS).
8Try it
The 5 vertices of an undirected graph have degrees 3, 2, 2, 2, 1. How many edges does the graph have?
The answer is B. The degrees add up to 3 + 2 + 2 + 2 + 1 = 10, which is twice the number of edges: 10 / 2 = 5 edges.
Show the answer
The answer is B. The degrees add up to 3 + 2 + 2 + 2 + 1 = 10, which is twice the number of edges: 10 / 2 = 5 edges.
At most how many edges can an undirected simple graph with 8 vertices have?
The answer is B. Each pair of vertices is joined by at most one edge: 8 × 7 / 2 = 28. 56 is the limit for a directed graph.
Show the answer
The answer is B. Each pair of vertices is joined by at most one edge: 8 × 7 / 2 = 28. 56 is the limit for a directed graph.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.