Small Group Tutorials

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

How to Learn Maximum Cardinality Search: Vertex Labels, Perfect Elimination Orderings, Chordal Graph Recognition and Linear-Time Structure

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

Wait, What?

A graph search can recognise a deep structural property without calculating shortest paths, spanning trees or connected components first.

Maximum Cardinality Search (MCS) is a graph-search method introduced by Robert Tarjan and Mihalis Yannakakis in their linear-time work on chordal graphs and hypergraph acyclicity. Instead of choosing the next vertex by queue order, stack order or distance, MCS repeatedly selects an unnumbered vertex with the largest number of already numbered neighbours.

That tiny change creates a remarkably useful ordering. On chordal graphs, the ordering connects directly to perfect elimination structure, maximal cliques, sparse elimination and efficient recognition algorithms.

Quick Answer

Learn MCS through graph ordering → labels as numbered-neighbour counts → selection rule → hand trace → perfect elimination ordering → chordal graphs → recognition test → clique extraction → data structures → linear-time implementation → sparse-matrix and database connections. The beginner question is “which vertex has the most already chosen neighbours?” The professional question is “what structural certificate does the resulting ordering provide?”

1. First separate MCS from BFS and DFS

Breadth-first search chooses by distance layers. Depth-first search follows one branch deeply before backtracking. Maximum Cardinality Search chooses according to how strongly each unnumbered vertex is connected to the set already numbered.

At each step, every unnumbered vertex v has a label:

label[v] = number of already numbered neighbours of v

Pick a vertex with maximum label, number it, then increase the label of each still-unnumbered neighbour by one.

Ties may be broken arbitrarily unless a particular application specifies otherwise.

2. Trace MCS on a tiny graph

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

A—B, A—C, B—C, B—D, C—D, C—E, D—E

Initially all labels are zero. Choose A. Now B and C each have label 1. Choose C. Then B rises to 2, while D and E become 1. Choose B. D rises to 2. Continue by choosing D, then E.

The important learning move is to keep a small table after every selection:

step | chosen | remaining labels
1    | A      | B:1 C:1 D:0 E:0
2    | C      | B:2 D:1 E:1
3    | B      | D:2 E:1
4    | D      | E:2
5    | E      | —

This is enough to understand the mechanism before introducing chordality.

3. What is a chordal graph?

An undirected graph is chordal if every cycle of length at least four has a chord: an edge joining two nonconsecutive vertices on that cycle.

A four-cycle A–B–C–D–A without A–C or B–D is not chordal. Add either diagonal, and that particular cycle is chorded.

Chordal graphs matter because many difficult graph problems become easier on them, and because they appear naturally in sparse Gaussian elimination, probabilistic graphical models, database structure and clique-based reasoning.

4. Perfect elimination orderings

A perfect elimination ordering (PEO) is an ordering of vertices such that, when each vertex is eliminated, its later neighbours form a clique.

Equivalently, for every vertex v, the neighbours of v that appear after v in the ordering must all be pairwise adjacent.

Chordal graphs are exactly the graphs that have a perfect elimination ordering. MCS provides an efficient route to such an ordering and to recognising when the graph fails the required structure.

5. Why the MCS ordering is so informative

The label of a vertex tells you how many selected vertices already “agree” on it as a neighbour. In a chordal graph, the maximum-cardinality rule tends to expose clique structure in a way that can be certified locally.

Tarjan and Yannakakis showed that MCS can support a linear-time chordality test. The algorithm is not merely a heuristic that “often finds cliques.” It participates in a structural theorem linking the search order to chordal elimination properties.

6. Numbering direction can confuse beginners

Some descriptions number vertices from 1 upward in the order they are selected. Others assign numbers from n downward. Some define the PEO as the reverse of the MCS visitation order. These are convention differences, not algorithm differences.

Before proving anything, write one sentence declaring your convention. For example:

“We record vertices in the order MCS selects them, then test the reverse order as the elimination ordering.”

This prevents many apparent contradictions.

7. Conceptual MCS pseudocode

for each vertex v:
    label[v] = 0
    unnumbered[v] = true

ordering = []

repeat |V| times:
    v = an unnumbered vertex with maximum label
    append v to ordering
    unnumbered[v] = false

    for each neighbor w of v:
        if unnumbered[w]:
            label[w] += 1

return ordering

A simple implementation may scan all vertices to find the maximum label and take O(V²+E). The theoretical linear-time result requires a more careful bucket structure.

8. The bucket-queue idea

Labels are integers from 0 to at most V−1. That makes a bucket data structure natural. Maintain a bucket for each possible label and a pointer to the highest nonempty bucket. When a neighbour’s label increases from k to k+1, move it one bucket upward.

With appropriate linked-list or set operations, each edge causes only a constant number of label updates, giving O(V+E) overall work.

This is a useful design lesson: when priorities are small bounded integers, a general comparison heap may be unnecessary.

9. Turning an ordering into a chordality test

Producing an ordering is only phase one. To recognise a chordal graph, verify the clique condition efficiently.

One common viewpoint is: for each vertex v, among the later neighbours of v choose the earliest one p(v) in the later ordering. Then every other later neighbour of v should also be adjacent to p(v). If this fails, the ordering exposes a structural violation consistent with a chordless cycle.

The exact implementation details depend on numbering convention, so keep the mathematics and the index direction aligned.

10. A small non-chordal counterexample

Use a cycle of four vertices A–B–C–D–A with no diagonal. Run MCS. Ties can produce several possible orders, but the graph has no PEO. During verification, one vertex will have two later neighbours that are not adjacent. That failed clique test is the signal that the graph is not chordal.

This is a powerful teaching contrast: first trace a chordal example where the later-neighbour sets are cliques, then trace the plain four-cycle and locate the exact failure.

11. MCS and maximal cliques

Because chordal graphs have clique structure aligned with elimination order, MCS can support efficient maximal-clique extraction. Current NetworkX documentation for chordal graphs explicitly uses maximum cardinality search when generating maximal cliques of chordal components.

This is another important boundary: MCS produces an ordering and structural information; a clique-enumeration routine uses that information for a separate output contract.

12. MCS and sparse Gaussian elimination

Chordal graphs are closely linked to sparse matrix elimination. Eliminating a variable can introduce fill edges among its neighbours. Perfect elimination orderings describe cases where the required neighbour connections are already present, avoiding additional fill.

This is why chordal graph theory appears in numerical linear algebra. MCS is not itself a numeric solver; it is a graph-structural tool that helps reveal elimination-friendly orderings.

13. MCS and database acyclicity

The original Tarjan–Yannakakis paper also connects the method to acyclic hypergraphs and relational database schemes. This historical connection is useful because it shows the algorithm’s real conceptual reach: a search ordering can expose whether complex relation structures admit efficient elimination-like processing.

For students, keep hypergraphs as an extension after the graph case is secure.

14. How to teach MCS from beginner to professional

A strong teaching sequence mirrors current programming-education guidance: read and reason before asking learners to build a complete implementation.

  • Predict: give a labelled graph and ask which vertex MCS could legally choose next.
  • Run: execute a short reference implementation and compare.
  • Investigate: maintain the label table by hand after each step.
  • Modify: add or delete one edge and predict how the ordering may change.
  • Make: implement MCS, then add a chordality verification phase.

A Parsons problem works well for the bucket-update phase: learners order the operations “remove chosen vertex,” “scan neighbours,” “increment label,” and “move bucket.”

15. Ties are a feature, not necessarily a bug

Two vertices can have the same maximum label. The search may choose either. This means the exact ordering can vary while still being a valid MCS ordering.

For deterministic testing, define a secondary rule such as smallest vertex ID. But do not accidentally make a theorem depend on that arbitrary tie-break unless the theorem explicitly requires it.

16. Common implementation mistakes

  • Incrementing labels of already numbered vertices.
  • Using total degree instead of number of numbered neighbours.
  • Recomputing labels from scratch after every step and then claiming linear time.
  • Confusing the MCS visitation order with the PEO direction.
  • Assuming an MCS ordering alone proves chordality without the required structural check.
  • Using directed graphs even though the standard chordal formulation is undirected.
  • Ignoring isolated vertices, which are perfectly valid graph vertices.

17. Professional validation strategy

Test the search separately from chordality recognition. First verify that every selected vertex has maximal current label. Then test recognition on known families:

  • trees, which are chordal;
  • cliques, which are chordal;
  • plain cycles Cₙ for n≥4, which are not chordal;
  • graphs produced by adding random fill edges to cycles;
  • disconnected graphs containing both chordal and non-chordal components.

Compare results with a trusted library such as NetworkX. For performance, generate sparse graphs and verify that edge-processing counts scale linearly when using buckets.

18. Complexity and engineering choices

The theoretical MCS search can run in O(V+E) with bucket structures. A classroom version that scans every unnumbered vertex each round is simpler but slower. Both are useful if their contracts are stated honestly.

Professional code should distinguish:

  • algorithmic complexity of the search;
  • cost of chordality verification;
  • memory layout for adjacency sets;
  • deterministic versus arbitrary tie-breaking;
  • whether graph labels must be remapped to dense integer IDs.

19. Practice ladder

  • Beginner: run MCS manually on a six-vertex graph.
  • Foundation: distinguish MCS order, reverse order and perfect elimination order.
  • Intermediate: implement MCS with a priority scan, then add chordality checking.
  • Advanced: replace the scan with integer-label buckets and prove O(V+E) update work.
  • Professional: use MCS in a pipeline that extracts chordal cliques or studies sparse elimination, with independent structural validation.

20. Ownership boundary

This article owns Maximum Cardinality Search as a graph-ordering algorithm and its direct relationship with chordal recognition and elimination structure. It does not replace BFS/DFS, generic graph colouring, clique enumeration, sparse-matrix reordering such as Reverse Cuthill–McKee, or treewidth algorithms.

Sources and further reading

  • Robert E. Tarjan and Mihalis Yannakakis, “Simple Linear-Time Algorithms to Test Chordality of Graphs, Test Acyclicity of Hypergraphs, and Selectively Reduce Acyclic Hypergraphs,” SIAM Journal on Computing, 13(3), 1984. DOI: 10.1137/0213035.
  • NetworkX 3.6.1 chordal-graph documentation, including is_chordal and chordal_graph_cliques, which use maximum-cardinality-search ideas.
  • Current programming-education guidance on PRIMM emphasises prediction, code reading, investigation and modification before independent construction.
  • Research on Parsons and faded Parsons problems supports reducing syntax burden while learners acquire algorithmic structure.

Professional rule: you understand Maximum Cardinality Search when you can trace every label change, explain why the ordering is structurally meaningful on chordal graphs, and distinguish “producing an order” from “certifying the graph property.”