Small Group Tutorials

Here to help students catch up, keep up, and move ahead. Book a consultation here.

How to Learn the Matula–Beck Algorithm: Smallest-Last Ordering, Graph Degeneracy, k-Cores and Greedy Coloring

Three students studying together in an eduKate small-group classroom.

Quick read: The Matula–Beck smallest-last algorithm repeatedly removes a currently minimum-degree vertex, records the removal order, and then reads that order backwards. With the right bucket data structure, this reveals graph degeneracy and supports k-core decomposition and strong greedy-coloring orderings in O(|V| + |E|) time.

One-sentence answer: peel the graph from its sparsest exposed vertex inward, remember the highest minimum degree encountered, then use the reverse peeling order as a structural map of the graph.

Why learn smallest-last ordering?

Graph algorithms often become difficult because a picture of a graph does not tell you which vertex to process first. Matula–Beck gives a principled answer: repeatedly choose a vertex with the smallest current degree. That simple local decision exposes a global property called degeneracy.

The learning journey is unusually rich. A beginner can understand “remove the least-connected vertex.” An intermediate learner can trace how degrees change. An advanced learner can connect the maximum removed degree to k-cores and coloring bounds. A professional can implement the procedure with degree buckets, apply it to large sparse networks, and understand where the ordering helps—and where it does not.

1. Begin with the peeling picture

Suppose a graph has vertices with different numbers of neighbours. Remove one of the currently least-connected vertices. Once it disappears, every surviving neighbour loses one edge, so some of their degrees decrease. Then again remove a vertex of minimum current degree. Continue until the graph is empty.

The removal order is a smallest-first ordering. Reverse it and you obtain a smallest-last ordering. The name is literal: the vertex that was smallest at each peeling step appears as late as possible when the order is reversed.

2. A small example

Consider vertices A, B, C, D, E with edges:

A-B, A-C, B-C, B-D, C-D, D-E

The initial degrees are:

  • A: 2
  • B: 3
  • C: 3
  • D: 3
  • E: 1

Remove E first because it has degree 1. Then D loses one neighbour. The next minimum degree may be 2, and we continue. At each step, record the degree of the vertex at the moment it is removed, not its original degree.

That changing degree is the key. The algorithm is not sorting vertices by their starting degrees. It is repeatedly recomputing what remains possible inside the surviving graph.

3. Degeneracy: the number hidden by the peeling process

A graph is k-degenerate if every non-empty subgraph contains a vertex of degree at most k. The graph’s degeneracy is the smallest such k.

The smallest-first peeling process reveals this number directly: the graph degeneracy is the maximum current degree of any vertex at the moment it is removed.

This can feel surprising. Why should repeatedly choosing the minimum degree reveal a maximum? Because each peeling step asks, “How dense is the surviving graph forced to be at this moment?” The hardest point in the peeling sequence—the largest minimum degree encountered—is exactly the degeneracy.

4. Connection to k-cores

A k-core is a maximal subgraph in which every vertex has degree at least k within that subgraph. Cores are nested: a (k+1)-core lies inside the k-core when it exists.

The same peeling idea supports core decomposition. Repeatedly remove vertices whose current degree is too small for the next core level. The core number of a vertex records the largest k-core containing it.

This connection matters because “degree” and “coreness” answer different questions. Degree is local: how many neighbours does this vertex have now? Coreness is structural: how deeply does this vertex sit inside a mutually well-connected region?

5. The simple implementation first

Before optimizing, write the version whose meaning is obvious. Maintain a set of remaining vertices, compute their current degrees, remove a minimum-degree vertex, and update the graph.

def smallest_last_slow(graph):
    remaining = {v: set(nbrs) for v, nbrs in graph.items()}
    removed = []
    degeneracy = 0

    while remaining:
        v = min(remaining, key=lambda x: len(remaining[x]))
        d = len(remaining[v])
        degeneracy = max(degeneracy, d)
        removed.append(v)

        for u in list(remaining[v]):
            remaining[u].remove(v)
        del remaining[v]

    return list(reversed(removed)), degeneracy

This version is pedagogically valuable but inefficient because repeatedly finding a minimum by scanning all remaining vertices can make the total cost quadratic or worse depending on representation.

6. How the linear-time implementation works

The professional improvement is to avoid searching from scratch for the next minimum-degree vertex. Since an undirected simple graph has integer degrees between 0 and the maximum degree, maintain buckets indexed by current degree.

  • Bucket 0 stores current degree-0 vertices.
  • Bucket 1 stores current degree-1 vertices.
  • Bucket 2 stores current degree-2 vertices.
  • and so on.

When removing vertex v, each surviving neighbour u drops from bucket d to bucket d - 1. Each edge causes only a constant amount of bucket-update work across the run. With careful arrays or linked bucket structures, the ordering can be computed in O(|V| + |E|) time for a sparse adjacency-list representation.

This is a good example of a recurring professional lesson: an algorithmic idea and an asymptotic bound are often separated by the data structure used to support the next operation.

7. Why smallest-last helps greedy graph coloring

Greedy coloring processes vertices one by one and assigns each vertex the smallest color not already used by its colored neighbours. Its result can vary dramatically with ordering.

If we color in smallest-last order, then when a vertex is colored, the number of already-colored neighbours is bounded by the graph’s degeneracy. Therefore greedy coloring uses at most degeneracy + 1 colors.

This is not always the chromatic optimum. It is a guarantee tied to a structural parameter. That distinction is important: a heuristic ordering can be useful because it gives a strong bound and often good practical behavior without solving the NP-hard minimum-coloring problem exactly.

8. A proof idea worth learning

Why does the reverse removal order have the degeneracy property? When a vertex v was removed, it had at most k neighbours remaining, where k is the maximum removal degree. In the reversed order, exactly those surviving neighbours appear before v. So each vertex has at most k earlier neighbours in the smallest-last ordering.

That single observation powers the coloring bound. It also illustrates a broader technique: sometimes an ordering is easier to construct in the opposite direction from the direction in which it will later be used.

9. Common misconceptions

  • “Just sort by degree once.” Wrong. Degrees change after every removal.
  • “Degeneracy is maximum degree.” No. Degeneracy can be much smaller because it concerns the minimum degree present in every surviving subgraph.
  • “A high-degree vertex must have high coreness.” Not necessarily. A hub connected to many leaves can have high degree while the surrounding structure peels away quickly.
  • “Smallest-last gives optimal coloring.” It gives a useful bound, not a general optimum.
  • “The algorithm is linear no matter how I code it.” No. The bucket or equivalent priority structure is what makes the fast implementation possible.

10. Testing graph implementations professionally

  • Empty and singleton graphs: confirm boundary semantics.
  • Path graph: degeneracy should be 1 for a nontrivial path.
  • Tree: any non-empty tree with at least one edge has degeneracy 1.
  • Cycle: degeneracy 2.
  • Complete graph Kn: degeneracy n - 1.
  • Ordering property: in the returned smallest-last order, count each vertex’s earlier neighbours and verify none exceeds the reported degeneracy.
  • Cross-check: for small random graphs, compare core numbers against a trusted library such as igraph or NetworkX.

When the algorithm is used on real network data, also decide how to handle self-loops, parallel edges and directed edges. A mathematical theorem about simple undirected graphs does not automatically define the semantics of messy production data.

11. Where professionals use the idea

Degeneracy and core decomposition are useful in network analysis because they reveal nested dense regions efficiently. They appear in social and collaboration networks, graph mining, preprocessing for clique algorithms, sparse graph orientation, coloring heuristics and subgraph enumeration.

A smallest-last ordering can also make later algorithms cheaper. If edges are oriented according to a degeneracy ordering, each vertex has limited forward out-degree. That structural bound is valuable in algorithms that enumerate triangles, cliques or other local patterns.

12. How to learn it without drowning in graph notation

Use a staged progression. First draw a six-vertex graph and physically cross out the minimum-degree vertex at each step. Write the current degree beside every remaining vertex. Next, record the removal degrees and identify the maximum. Then reverse the order and color greedily. Only after those ideas are visible should you implement buckets.

This sequence follows a useful programming-education principle: start with a worked representation whose subgoals are explicit, then ask learners to predict updates, run or simulate them, investigate differences, modify a case and finally construct an implementation. Code comes after the state meaning is secure.

13. Practice ladder: beginner to professional

  • Beginner: peel a hand-drawn graph and record changing degrees.
  • Developing: explain in words why original degree order is insufficient.
  • Intermediate: implement the simple O(V² + E)-style version and compute degeneracy.
  • Advanced: implement degree buckets and core numbers, then prove the degeneracy + 1 coloring guarantee.
  • Professional: test on million-edge sparse graphs, measure memory layout and bucket-update costs, compare against a library implementation, and use the ordering as preprocessing for another graph workload.

14. The transferable algorithmic idea

Matula–Beck teaches peeling: repeatedly remove the element that currently violates or minimizes a structural condition, and let the changing residual problem reveal deeper layers. Similar thinking appears in topological elimination, constraint propagation, pruning, sparse matrix algorithms and iterative simplification.

The professional question is not merely “What is each vertex’s degree?” It is “What structure remains after the easy outer layer has been removed?” That shift—from static measurement to residual structure—is the reason the algorithm remains useful.

Sources and further reading

Final idea: smallest-last ordering is not “sort vertices by degree.” It is a dynamic peeling process. Once you see that the graph changes after every removal, degeneracy, k-cores and the coloring guarantee become parts of one coherent idea.