Sub topic 4
Vocabulary: degree, path, cycle, connectivity
This chapter assumes you know what vertices and edges are, and the difference between directed and undirected edges. It also assumes you can read basic Python. The terms below appear in every later chapter, so the goal is to use each one precisely.
Degree
The degree of a vertex is the number of edges that touch it. In the graph below, A has degree 2 and E has degree 1.
Directed graphs split degree in two:
- In-degree is the number of edges arriving at a vertex.
- Out-degree is the number of edges leaving it.
You can compute both while scanning an edge list.
from collections import Counter
edges = [("A", "B"), ("A", "C"), ("B", "D"), ("C", "D"), ("E", "F")]
degree = Counter()
for u, v in edges:
degree[u] += 1
degree[v] += 1
print(sorted(degree.items())) # [('A', 2), ('B', 2), ('C', 2), ('D', 2), ('E', 1), ('F', 1)]
print(sum(degree.values()), 2 * len(edges)) # 10 10
arcs = [("A", "B"), ("A", "C"), ("C", "B")]
out_degree = Counter(u for u, v in arcs)
in_degree = Counter(v for u, v in arcs)
print(dict(out_degree)) # {'A': 2, 'C': 1}
print(dict(in_degree)) # {'B': 2, 'C': 1}The second print shows the handshake rule: in an undirected graph, the degrees add up to twice the number of edges, because each edge has two endpoints.
Paths and cycles
A path is a sequence of vertices in which each consecutive pair is joined by an edge. In a directed graph, each edge must be followed in its own direction. The length of a path is its number of edges. A simple path never repeats a vertex.
In the diagram, A, B, D is a path of length 2. A, B, D, C is a simple path of length 3.
A cycle is a path that starts and ends at the same vertex and repeats no other vertex. A, B, D, C, A is a cycle of length 4. A graph with no cycles is acyclic.
Connectivity
Two vertices are connected if some path joins them. An undirected graph is connected if every pair of vertices is connected. Otherwise it splits into connected components, which are the maximal groups of mutually reachable vertices. The graph above has two components: {A, B, C, D} and {E, F}.
Directed graphs need two levels:
- Strongly connected: every vertex can reach every other by following edge directions.
- Weakly connected: the graph becomes connected once you ignore directions.
A directed graph with edges A→B and B→C is weakly connected but not strongly connected, because C cannot reach A.
| Term | Applies to | Meaning |
|---|---|---|
| Degree | Undirected | Number of edges at a vertex |
| In-degree / out-degree | Directed | Edges arriving / leaving |
| Path | Both | Vertices joined by consecutive edges |
| Cycle | Both | Path that returns to its start |
| Connected component | Undirected | Maximal set of mutually reachable vertices |
| Strongly connected | Directed | Every vertex reaches every other |
Common mistakes
- Mixing up path length (edges) with the number of vertices on the path. A path with 4 vertices has length 3.
- Calling a graph connected when only most of it is. One isolated vertex makes it disconnected.
- Applying undirected cycle rules to directed graphs. In a directed graph, A→B and B→A form a cycle of length 2.
Exercise
A directed graph has edges A→B, B→C and C→A. What are the in-degree and out-degree of B, does the graph contain a cycle, and is it strongly connected?
Show answer
B has in-degree 1 (from A) and out-degree 1 (to C). The path A, B, C, A is a cycle. The graph is strongly connected, because following the edges around the cycle lets every vertex reach every other.
Exercise
An undirected graph has 6 vertices and its degrees are 3, 3, 2, 2, 1, 1. How many edges does it have?
Show answer
The degrees sum to 12. By the handshake rule that equals twice the edge count, so the graph has 6 edges.