TExcelTAD
Home › Discrete Mathematics › Paths and Connectivity

Paths and Connectivity

A graph is connected if every vertex can reach every other along edges. Networks are built to stay connected even when links fail, so we also ask how connected a graph is. This lesson covers walks and paths in simple graphs, connected components, Euler tours (the Königsberg bridges) and edge connectivity.

Walks, paths and components

In a simple graph a walk is a sequence of vertices with consecutive ones adjacent, and a path is a walk without repeated vertices. As in digraphs, a shortest walk is always a path. Two vertices are connected if a walk joins them. "Is connected to" is an equivalence relation, and its classes are the connected components.

Theorem. A graph with nn vertices and mm edges has at least n−mn - m connected components.

Proof by induction on mm. With no edges, there are nn components. Adding an edge merges at most two components into one, so it lowers the count by at most 1. ■\blacksquare

So a connected graph on nn vertices needs at least n−1n - 1 edges. Trees achieve this (next lesson).

python · try it yourself
def components(n, edges):
    adj = {v: [] for v in range(n)}
    for u, v in edges:
        adj[u].append(v); adj[v].append(u)
    comp, label = {}, 0
    for s in range(n):
        if s in comp:
            continue
        stack = [s]
        comp[s] = label
        while stack:
            u = stack.pop()
            for w in adj[u]:
                if w not in comp:
                    comp[w] = label; stack.append(w)
        label += 1
    return [sorted(v for v in comp if comp[v] == c) for c in range(label)]

edges = [(0, 1), (1, 2), (3, 4), (5, 6), (6, 7), (7, 5)]
cs = components(9, edges)
print("components:", cs)
print("count:", len(cs), " lower bound n - m =", 9 - len(edges))
components: [[0, 1, 2], [3, 4], [5, 6, 7], [8]]
count: 4  lower bound n - m = 3

Euler tours

An Euler tour is a closed walk that uses every edge exactly once. Königsberg's citizens asked whether they could cross each of the city's seven bridges once and return home. Euler (1736) answered no, founding graph theory.

Theorem. A connected graph has an Euler tour if and only if every vertex has even degree.

Necessity: each time the tour passes through a vertex it uses two edges, one in and one out. Sufficiency: start anywhere and walk along unused edges; with all degrees even you can only get stuck back at the start. If unused edges remain, some vertex on the closed walk has one; splice in a closed walk from there (Hierholzer's algorithm).

python · try it yourself
from collections import defaultdict

def euler_tour(edges):
    adj = defaultdict(list)
    for i, (u, v) in enumerate(edges):
        adj[u].append((v, i)); adj[v].append((u, i))
    if any(len(a) % 2 for a in adj.values()):
        return None
    used, stack, tour = set(), [edges[0][0]], []
    while stack:
        u = stack[-1]
        while adj[u] and adj[u][-1][1] in used:
            adj[u].pop()
        if adj[u]:
            v, i = adj[u].pop()
            used.add(i)
            stack.append(v)
        else:
            tour.append(stack.pop())
    return tour[::-1]

koenigsberg = [("A", "B"), ("A", "B"), ("A", "C"), ("A", "C"), ("A", "D"), ("B", "D"), ("C", "D")]
print("Koenigsberg:", euler_tour(koenigsberg))
house = [(0, 1), (1, 2), (2, 3), (3, 0), (0, 4), (4, 1), (1, 3), (0, 2)]
print("degrees:", {v: sum(v in e for e in house) for v in range(5)})
print("tour of the house:", euler_tour(house))
print("with a second 2-3 edge, tour:", euler_tour(house + [(2, 3)]))
Koenigsberg: None
degrees: {0: 4, 1: 4, 2: 3, 3: 3, 4: 2}
tour of the house: None
with a second 2-3 edge, tour: [0, 2, 3, 1, 4, 0, 3, 2, 1, 0]

Königsberg has vertices of degree 5, 3, 3, 3: no tour. (Euler's multigraph allows repeated edges between the same land masses; the theorem holds for multigraphs too.) A connected graph with exactly two odd vertices has an Euler path from one odd vertex to the other: the "draw the house without lifting the pen" puzzle. In the example, vertices 2 and 3 of the house are odd, so it has an Euler path from 2 to 3 but no tour until a second 2–3 edge is added.

Edge connectivity

A graph is kk-edge-connected if it stays connected after removing any k−1k - 1 edges. A tree is only 1-edge-connected: every edge is a bridge whose removal disconnects it. A cycle is 2-edge-connected; KnK_n is (n−1)(n-1)-edge-connected. Menger's theorem: the minimum number of edges whose removal separates uu from vv equals the maximum number of edge-disjoint paths from uu to vv. Network designers use it to guarantee redundancy.

In software

Connected components group users in a social network, pixels in an image (flood fill), and machines that can talk to each other. Detecting bridges finds single points of failure in a network. Euler paths plan routes that cover every street once (snow ploughs, mail delivery: the "Chinese postman problem") and assemble DNA fragments from overlapping reads (de Bruijn graphs).

Exercises

Exercise 1: Components

A graph has 20 vertices and 15 edges. What is the minimum number of components? Can it be connected?

Answer

At least 5 components; it cannot be connected.

Solution

By the theorem there are at least 20−15=520 - 15 = 5 components. A connected graph on 20 vertices needs at least 19 edges.

Exercise 2: Euler tours

Which have an Euler tour: (a) K5K_5; (b) K4K_4; (c) a cycle CnC_n; (d) the 3×33 \times 3 grid graph?

Answer

(a) yes; (b) no; (c) yes; (d) no.

Solution

(a) every degree is 4. (b) every degree is 3. (c) every degree is 2. (d) the 4 edge-midpoint vertices of the grid have degree 3.

Exercise 3: Bridges (code)

Find the bridges of the graph with edges 0–1, 1–2, 2–0, 2–3, 3–4, 4–5, 5–3, 5–6 by deleting each edge and counting components.

Answer

2–3 and 5–6.

Solution
python · try it yourself
edges = [(0, 1), (1, 2), (2, 0), (2, 3), (3, 4), (4, 5), (5, 3), (5, 6)]

def n_components(n, E):
    parent = list(range(n))
    def find(x):
        while parent[x] != x:
            x = parent[x]
        return x
    for u, v in E:
        parent[find(u)] = find(v)
    return len({find(v) for v in range(n)})

base = n_components(7, edges)
print([e for e in edges if n_components(7, [f for f in edges if f != e]) > base])
[(2, 3), (5, 6)]

Test yourself

A connected graph has an Euler tour exactly when…

  1. it has an even number of edges
  2. every vertex has even degree
  3. it is bipartite
  4. it is a tree

What is the minimum number of edges of a connected graph with 50 vertices?

  1. 25
  2. 49
  3. 50
  4. 1225

A bridge is an edge whose removal…

  1. creates a cycle
  2. disconnects the graph (increases the number of components)
  3. makes the graph bipartite
  4. lowers the maximum degree