Graphentheorie Schritt für Schritt

Kostenloser Kurs
Zuerst das Quiz zu Kapitel 4

Zuerst Kapitel 4 prüfen

Mach das Quiz zu Kapitel 4: 10 Fragen, bestanden mit 7 richtigen. Du kannst es beliebig oft wiederholen.

Colours That Never Touch

Kapitel 5, Lektion 1 von 3

Graph colouring: give neighbours different colours, with as few colours as possible.

Auf Englisch gesprochen. Oben kannst du englische, arabische, deutsche, spanische oder französische Untertitel einschalten.

Schriftliche Lektion

9 Min. LesezeitAuf Englisch verfasst.

In 1852 a young man named Francis Guthrie was colouring a map of the counties of England. He noticed that four colours were enough to give every pair of neighbouring counties different colours. Was that true for every map? He asked his brother, a student of Augustus De Morgan. De Morgan could not answer, and that same day he wrote about it to his friend William Rowan Hamilton.

It sounds like a puzzle for children. It took 124 years, a famous wrong proof and more than a thousand hours of computer time to settle, and the answer is yes: four colours are always enough. Behind that puzzle sits one of the most useful problems in graph theory, colouring a graph. This lesson covers how maps become graphs, how many colours a graph needs, the story of the four colour theorem, why colouring is hard, and how it schedules exams, radio stations and even your processor.

Maps to graphs

Take a map of the sixteen states of Germany. Turn each state into a vertex, and join two vertices whenever their states share a border.

Every state becomes a vertex, every shared border an edge.Every state becomes a vertex, every shared border an edge.

Two states that meet at a single point do not count as neighbours. Otherwise a map cut like a pie could need any number of colours, since every slice touches every other one in the middle.

Now the rule is simple:

A proper colouring gives every vertex a colour so that the two ends of every edge get different colours.

Giving every vertex its own colour always works, but it is lazy. The real question is the smallest number of colours that works: the chromatic number, written χ(G) (chi).

  • A graph with no edges needs one colour.
  • A star needs two: one for the centre, one for all the tips.
  • A triangle needs three, because every vertex touches the other two.

One, two and three colours.One, two and three colours.

How many colours?

Two quick facts give a range.

  • Lower bound: cliques. If some k vertices all touch each other (a clique), each needs its own colour, so χ ≥ k.
  • Upper bound: degrees. If no vertex has more than d neighbours, d + 1 colours are always enough. Colour the vertices one at a time: each has at most d coloured neighbours, so one of the d + 1 colours is always free.

In 1941 Leonard Brooks sharpened this: d colours are enough, except for complete graphs and odd cycles.

The two bounds can be far apart. The Petersen graph has ten vertices, three neighbours each, and not a single triangle, so the clique bound only says 2. Yet it needs three colours, because it contains a cycle of five.

That points to the most important dividing line: two colours.

A graph can be coloured with two colours exactly when it has no cycle of odd length. Such graphs are called bipartite.

Why no odd cycle is allowed. Walk around any cycle, and the two colours must alternate. After an odd number of steps you arrive home with the wrong colour.

Why that is enough. When there is no odd cycle, a breadth-first search colours the levels in turn: even levels one colour, odd levels the other. An edge inside one level would close an odd cycle. So two-colourability can be tested in linear time.

A 6-cycle takes two colours. A 5-cycle leaves an edge inside one BFS level.A 6-cycle takes two colours. A 5-cycle leaves an edge inside one BFS level.

When four are needed

Can a map ever need four colours? Here is a small one: a region X in the middle, surrounded by a ring of five regions A, B, C, D, E.

  1. Try two colours, and three borders clash at once. The ring is a cycle of five, an odd cycle, so the ring alone needs three colours.
  2. Every proper 3-colouring of the ring uses all three colours. X touches all five regions of the ring, so whatever three colours the ring uses, X cannot take any of them.
  3. So four colours are necessary.

A region inside a ring of five needs a fourth colour.A region inside a ring of five needs a fourth colour.

So four is sometimes needed. The famous question was whether four is always enough, for every map that can ever be drawn.

Four colours

1879: Kempe's proof. Alfred Kempe, a London barrister and amateur mathematician, published a proof, and it was accepted for eleven years. His starting point still matters:

  • Every map has a region with five neighbours or fewer. That follows from Euler's formula: a map's graph with V vertices has at most 3V − 6 edges, so the average degree is below 6.
  • Take that region out, colour the rest of the map, and put it back. If its neighbours leave a colour free, you are done.
  • If they use every colour, swap. Pick two colours and follow a chain of neighbouring regions that use only those two (a Kempe chain). Swapping the two colours along the whole chain keeps the map proper, and can free a colour.

Kempe's idea: remove, colour, swap along a two-colour chain.Kempe's idea: remove, colour, swap along a two-colour chain.

1890: the flaw. Percy Heawood found a map where Kempe's argument breaks: two swaps, made together, can get in each other's way. The proof was wrong. But Heawood saved something: with five colours, one swap is always enough, so five colours suffice for every map. For four, the question stayed open for another 86 years.

1976: a computer proof. Kenneth Appel and Wolfgang Haken, at the University of Illinois, reduced every possible map to 1,834 special configurations, and a computer checked them all in more than a thousand hours. It was the first major theorem proved with a computer, and many mathematicians were uneasy. The department celebrated with a postmark: Four colors suffice.

The University of Illinois postmark, 1976.The University of Illinois postmark, 1976.

In 1996 a new proof by Robertson, Sanders, Seymour and Thomas cut the list to 633 configurations, and in 2005 the entire proof was checked, step by step, by a computer proof assistant.

Colouring fast

For a general graph, no fast method is known that always finds the chromatic number. Even deciding whether three colours are enough is NP-complete, and it stays that hard for maps. Trying everything explodes: twenty vertices and three colours give 3²⁰, about 3.5 billion colourings; fifty vertices give more than 7 × 10²³.

So in practice we use greedy colouring: take the vertices one by one, and give each the smallest colour that none of its neighbours uses. It never uses more than d + 1 colours, but the order matters.

On this graph from our guide (A1 joined to B2 and B3, A2 to B1 and B3, A3 to B1 and B2), colouring one side first, A1, A2, A3, B1, B2, B3, uses two colours. Alternating, A1, B1, A2, B2, A3, B3, uses three.

Same graph, two orders: 2 colours or 3.Same graph, two orders: 2 colours or 3.

It can get much worse. On the crown graph with four vertices on each side (every a joined to every b except its own partner), the order a1, b1, a2, b2, ... uses four colours when two would do. With a hundred on each side, it would use a hundred.

Yet some order always makes greedy perfect: list the vertices of a best colouring, one colour class at a time. The hard part is finding that order. Good heuristics choose it cleverly:

  • Welsh-Powell (1967) colours the busiest vertices first.
  • DSatur (Brélaz, 1979) always picks the vertex whose neighbours already use the most different colours.
from collections import deque

def greedy_colouring(adj, order):
    colour = {}
    for v in order:
        used = {colour[u] for u in adj[v] if u in colour}
        c = 0
        while c in used:
            c += 1
        colour[v] = c
    return colour

def two_colour(adj):
    # BFS levels; returns None if an edge joins two vertices of the same colour
    colour = {}
    for s in adj:
        if s in colour:
            continue
        colour[s] = 0
        queue = deque([s])
        while queue:
            u = queue.popleft()
            for v in adj[u]:
                if v not in colour:
                    colour[v] = 1 - colour[u]
                    queue.append(v)
                elif colour[v] == colour[u]:
                    return None  # odd cycle
    return colour

adj = {"A1": ["B2", "B3"], "A2": ["B1", "B3"], "A3": ["B1", "B2"],
       "B1": ["A2", "A3"], "B2": ["A1", "A3"], "B3": ["A1", "A2"]}
max(greedy_colouring(adj, ["A1", "A2", "A3", "B1", "B2", "B3"]).values()) + 1  # 2
max(greedy_colouring(adj, ["A1", "B1", "A2", "B2", "A3", "B3"]).values()) + 1  # 3
five_cycle = {i: [(i - 1) % 5, (i + 1) % 5] for i in range(5)}
two_colour(five_cycle)  # None

In the world

Exam timetables. Each course is a vertex, and two courses are joined when some student takes both. A colour is a time slot.

StudentCourses
AminaMaths, Physics, Chemistry
BrahimMaths, Biology, History
ChloePhysics, Biology, Art
DrissChemistry, History, Music
EvaArt, Music, Maths

Greedy, busiest course first, needs five slots. An exact search finds four: Maths; Physics and Music; Chemistry and Biology; History and Art. And three can never work: Maths, Physics, Biology and Art all clash with each other, a clique of four.

Seven courses, and four exam slots.Seven courses, and four exam slots.

Radio. Transmitters close enough to interfere must use different frequencies, so each frequency is a colour.

Registers. A processor has only a few fast registers, and a program has many variables. Two variables that are alive at the same time cannot share a register. So the compiler colours an interference graph, with registers as colours, an idea put to work by Gregory Chaitin in 1981. In this short program, five variables need only three registers:

a = read()
b = a + 1
c = a * 2
d = b + c
e = c - 1
return d + e

a, b and c are all alive at line 3, so they need three different registers; d can then reuse a's register and e can reuse b's.

The interference graph: five variables, three registers.The interference graph: five variables, three registers.

Sudoku. Every Sudoku is a colouring puzzle. Each of the 81 cells is a vertex, joined to the 20 cells in its row, column and box: 81 × 20 / 2 = 810 edges. The nine digits are the colours. Puzzles like it appeared in French newspapers as early as 1895; the graph was hiding there all along.

Key ideas

  • A proper colouring gives the two ends of every edge different colours, and the chromatic number χ is the fewest colours that work.
  • Cliques push χ up, the largest degree caps it (d + 1, and d by Brooks except for complete graphs and odd cycles), and two colours work exactly when there is no odd cycle.
  • Every map needs at most four colours (Appel and Haken, 1976), but finding the fewest colours for a general graph is hard: even "are three enough?" is NP-complete.
  • Greedy colouring is fast, and its quality depends entirely on the order.

Check yourself

1. What is the chromatic number of a cycle of 7 vertices? And of 8?

2. A graph has one vertex of degree 10, and every other vertex has degree at most 4. Greedy colours the big vertex last. At most how many colours can it use?

3. In the exam timetable, Chloe drops Art. Is three slots enough now?

Practice on the platform

The graph colouring guide covers the chromatic number, the four colour theorem and Sudoku. In the colouring visualizer, import the ring map and run the greedy colouring, which colours the busiest vertex first: X gets the first colour, the ring takes three more, and it finishes with four colours, exactly what this map needs.

Als Nächstes

Kursinhalt

0 von 20 angesehen

Kapitel 1Graph Theory Foundations

Kapitel 2Exploring a GraphZuerst das Quiz zu Kapitel 1

Kapitel 3Shortest PathsZuerst das Quiz zu Kapitel 2

Kapitel 4Connecting CheaplyZuerst das Quiz zu Kapitel 3

Kapitel 5Hard ProblemsZuerst das Quiz zu Kapitel 4

Kapitel 6Network FlowsZuerst das Quiz zu Kapitel 5