Algorithm Search⌘K

Algorithmadvanced

Dorfler marking

Also called bulk marking.

Marks a smallest set of elements whose squared indicators sum to at least a fraction theta of the total, the marking rule behind the convergence proofs of AFEM.

Time
Θ(M) average
Space
Θ(M) extra
Where
M = the number of elements
Properties
randomizedexact with exact arithmeticminimal cardinality
Origin
Willy Dorfler, 1996

Dorfler marking, also called bulk marking, is the MARK step of the adaptive finite element method. Given squared error indicators ηK2\eta_K^2 and a parameter θ∈(0,1]\theta \in (0, 1], it marks a set M\mathcal{M} of elements with ∑K∈MηK2≥θ∑KηK2\sum_{K \in \mathcal{M}}\eta_K^2 \ge \theta\sum_K\eta_K^2, and among all such sets it takes one of the smallest size. So the marked elements carry a fixed share of the estimated error. This one inequality is what the convergence proofs need: with it, the error plus a multiple of the estimator contracts by a fixed factor in each step. With a small θ\theta and a set of minimal size, the loop also reaches the optimal rate of the AFEM optimality theorem. The obvious implementation sorts the indicators. A partition around pivots, as in quickselect, finds a minimal set in expected linear time.

In plain words

Each element has a number that says how much error it holds. Put the elements in order from the largest number to the smallest. Take elements from the top of the list until the ones you took hold at least a fixed share of the total, for example half of it. Those are the elements to refine. If a few elements hold most of the error, as near a corner singularity, only a few are marked. If the error is spread evenly, many are marked, and the refinement is close to uniform. A faster method gets the same answer without sorting the whole list, by splitting it around one value at a time and keeping only the part that holds the cut.

The problem

Input. Numbers η12,…,ηM2≥0\eta_1^2, \ldots, \eta_M^2 \ge 0, one for each element, and θ∈(0,1]\theta \in (0, 1].

Output. A set M⊆{1,…,M}\mathcal{M} \subseteq \{1, \ldots, M\} with ∑K∈MηK2≥θ∑K=1MηK2\sum_{K \in \mathcal{M}}\eta_K^2 \ge \theta\sum_{K = 1}^M\eta_K^2 (the bulk criterion, also called the Dorfler property) and with the smallest size among all nonempty sets with this property. The minimal size is unique. The set itself is unique only when there are no ties at the cut.

The algorithm

DORFLER-SORT is the direct method. DORFLER-SELECT keeps a candidate list and splits it around a random pivot value. The answer is a prefix of the sorted list, so the procedure only needs to find where the prefix ends.

  1. DORFLER-SORT(eta2, theta)
  2. 1goal <- theta * sum(eta2)
  3. 2order <- the indices sorted by eta2, largest first
  4. 3M <- empty; acc <- 0
  5. 4for each t in order, while acc < goal or M is empty do
  6. 5add t to M; acc <- acc + eta2[t]
  7. 6return M
  8. DORFLER-SELECT(eta2, theta)
  9. 7goal <- theta * sum(eta2); M <- empty
  10. 8cand <- all indices
  11. 9while cand is not empty do
  12. 10p <- eta2[t] for a random t in cand ▷ the pivot value
  13. 11split cand into high (> p), equal (= p) and low (< p)
  14. 12sh <- sum of eta2 over high
  15. 13if high is not empty and sh >= goal then cand <- high; continue ▷ the cut is inside high
  16. 14if sh + sum over equal >= goal then return M + high + enough of equal to reach goal
  17. 15M <- M + high + equal; goal <- goal - sh - sum over equal; cand <- low
  18. 16return M ▷ not reached

Line L14 may take the elements of equal in any order, because they all have the same value.

Example

Take seven elements with η2=(4,1,9,2,6,3,5)\eta^2 = (4, 1, 9, 2, 6, 3, 5), numbered 0 to 6. The total is 30.

With θ=12\theta = \tfrac12, the goal is 15. DORFLER-SORT takes element 2 (value 9, sum 9) and element 4 (value 6, sum 15). The sum reaches 15, so M={2,4}\mathcal{M} = \{2, 4\}. No single element has 15, so two is the minimal size.

DORFLER-SELECT with the pivots 4 and then 6 runs as follows.

Pass cand Pivot high equal low shs_h Line Action
1 all 7 4 {2, 4, 6} {0} {1, 3, 5} 20 L13 20≥1520 \ge 15, so cand ←\leftarrow {2, 4, 6}
2 {2, 4, 6} 6 {2} {4} {6} 9 L14 9+6≥159 + 6 \ge 15, return {2, 4}

With θ=45\theta = \tfrac45, the goal is 24. In pass 1 with pivot 4, sh=20<24s_h = 20 < 24 and sh+4=24≥24s_h + 4 = 24 \ge 24, so line L14 returns {2, 4, 6, 0}, the four largest. DORFLER-SORT gives the same set. The test file checks both procedures on these inputs and on 300 random inputs with Fraction values against a search over all subsets.

Effect in the loop. On the L-shaped domain with θ=0.5\theta = 0.5, the AFEM reference run marks mostly triangles near the reentrant corner, and its error falls like N−1/2N^{-1/2} in the number NN of unknowns. With θ=1\theta = 1 every triangle is marked, the loop is uniform refinement, and the error falls only like N−1/3N^{-1/3}.

Correctness

The full proof is in the correctness proof. An exchange argument shows that among all sets of size LL, the LL largest indicators have the largest sum. So the minimal size L∗L^* is the length of the shortest prefix of the sorted list that reaches the goal, and DORFLER-SORT returns that prefix. For DORFLER-SELECT, the invariant is that marked is a prefix of some descending order, cand is the block right after it, goal is what marked still lacks, and the end of the shortest bulk prefix lies inside cand. Each of the three cases at lines L13 to L15 keeps the invariant and makes cand smaller, and line L14 returns exactly the shortest bulk prefix.

Proof: Both marking procedures return a bulk set of minimal sizefull

Setting. The input is a list of numbers η12,…,ηn2≥0\eta^2_1, \ldots, \eta^2_n \ge 0 with n≥1n \ge 1, and θ∈(0,1]\theta \in (0, 1]. Write Σ=∑iηi2\Sigma = \sum_i \eta^2_i and g=θΣg = \theta\Sigma. A set S⊆{1,…,n}S \subseteq \{1, \ldots, n\} is a bulk set if ∑i∈Sηi2≥g\sum_{i \in S}\eta^2_i \ge g. Let L∗L^* be the smallest size of a nonempty bulk set. It exists, because the full set is a bulk set (Σ≥θΣ\Sigma \ge \theta\Sigma). Fix a descending order π\pi of the indices, ηπ(1)2≥ηπ(2)2≥⋯≥ηπ(n)2\eta^2_{\pi(1)} \ge \eta^2_{\pi(2)} \ge \cdots \ge \eta^2_{\pi(n)}, and write PL={π(1),…,π(L)}P_L = \{\pi(1), \ldots, \pi(L)\} for its prefixes. Arithmetic is exact (real-RAM, or Fraction in the code).

Lemma 1 (prefixes are best). For every LL, the prefix PLP_L has the largest sum among all sets of size LL. So L∗L^* is the smallest L≥1L \ge 1 such that PLP_L is a bulk set, and every prefix of a descending order of length L∗L^* is a minimal bulk set.

Proof. Let ∣S∣=L|S| = L. List the values of SS in descending order as s1≥⋯≥sLs_1 \ge \cdots \ge s_L. The jj-th largest value of SS is at most the jj-th largest value of all indicators, sj≤ηπ(j)2s_j \le \eta^2_{\pi(j)}, because at least jj values of the whole list are at least sjs_j. Sum over jj. If some SS of size LL is a bulk set, so is PLP_L, and the converse is trivial. □\square

Claim A (DORFLER-SORT, lines L1 to L6). The output has size L∗L^* and is a bulk set.

Proof. Line L2 gives a descending order. The loop at line L4 adds π(1),π(2),…\pi(1), \pi(2), \ldots and stops before adding π(L+1)\pi(L+1) exactly when the sum of PLP_L is at least gg and L≥1L \ge 1. So it returns PLP_L for the smallest L≥1L \ge 1 with PLP_L a bulk set, which is L∗L^* by Lemma 1. □\square

Claim B (DORFLER-SELECT, lines L7 to L16). The procedure stops, and its output has size L∗L^* and is a bulk set.

Proof. We prove the following invariant at the start of each pass of line L9. Write a=∣marked∣a = |\text{marked}| and c=∣cand∣c = |\text{cand}|. There is a descending order π\pi of all indices with

  1. marked ={π(1),…,π(a)}= \{\pi(1), \ldots, \pi(a)\} and cand ={π(a+1),…,π(a+c)}= \{\pi(a+1), \ldots, \pi(a+c)\};
  2. goal =g−∑i∈markedηi2= g - \sum_{i \in \text{marked}}\eta^2_i;
  3. a<L∗≤a+ca < L^* \le a + c.

At the start a=0a = 0, cand is everything, and 1≤L∗≤n1 \le L^* \le n, so the invariant holds.

In one pass, line L10 picks a value pp of cand, and line L11 splits cand into high (>p> p), equal (=p= p) and low (<p< p), which are not empty in the case of equal. Choose π\pi so that it lists marked, then high, then equal, then low, each in descending order. It is still a descending order, and it keeps property 1 of the invariant. Let shs_h and ses_e be the sums of high and equal. By property 2, the prefix Pa+jP_{a + j} is a bulk set exactly when the sum of its first jj elements after marked is at least goal.

Case L13: high is not empty and sh≥s_h \ge goal. Then Pa+∣high∣P_{a + |\text{high}|} is a bulk set, so L∗≤a+∣high∣L^* \le a + |\text{high}|. The new cand is high, which is {π(a+1),…,π(a+∣high∣)}\{\pi(a+1), \ldots, \pi(a + |\text{high}|)\}. Properties 1 and 2 still hold, and property 3 holds with the new c=∣high∣c = |\text{high}|. The new cand is smaller, because the pivot index is not in high.

Case L14: not case L13, and sh+se≥s_h + s_e \ge goal. If high is not empty, then sh<s_h < goal, so Pa+∣high∣P_{a + |\text{high}|} is not a bulk set and L∗>a+∣high∣L^* > a + |\text{high}|. If high is empty, then L∗>a=a+∣high∣L^* > a = a + |\text{high}| by property 3. Also Pa+∣high∣+∣equal∣P_{a + |\text{high}| + |\text{equal}|} is a bulk set, so L∗L^* lies in the equal block. The code takes high and then the elements of equal one at a time, until the sum reaches goal and at least one element is marked. All elements of equal have the same value, so any order of them is a descending order, and the code returns the prefix PLP_{L} of the smallest L>a+∣high∣L > a + |\text{high}| that is a bulk set (with L≥1L \ge 1). This is L∗L^* by Lemma 1.

Case L15: sh+se<s_h + s_e < goal. Then Pa+∣high∣+∣equal∣P_{a + |\text{high}| + |\text{equal}|} is not a bulk set, so L∗>a+∣high∣+∣equal∣L^* > a + |\text{high}| + |\text{equal}|. Line L15 moves high and equal into marked, lowers goal by sh+ses_h + s_e and sets cand to low. All three properties hold for the new values. The new cand is smaller, because equal contains the pivot.

Each pass either returns or makes cand strictly smaller, so the loop ends after at most nn passes. It cannot end with cand empty: then c=0c = 0, and property 3 would give a<L∗≤aa < L^* \le a. So it ends at line L14 with a bulk set of size L∗L^*. □\square

Claim C (expected cost). With the pivot chosen uniformly at random from cand, the expected number of operations on indicators is at most 16n16n, in the real-RAM model.

Proof. One pass costs at most 4c4c operations (three filters and one sum over cand, plus a sum over equal). Let c0=n,c1,c2,…c_0 = n, c_1, c_2, \ldots be the sizes of cand. If the pivot has rank rr in cand (in descending order, ties counted once), the next cand is high or low, of size at most max⁡(r−1,c−r)\max(r - 1, c - r). For rr uniform in {1,…,c}\{1, \ldots, c\}, the mean of max⁡(r−1,c−r)\max(r - 1, c - r) is at most 3c/43c/4, and ties only make the parts smaller. So E[ci+1∣ci]≤34ci\mathbb{E}[c_{i+1} \mid c_i] \le \tfrac34 c_i, and E[ci]≤(34)in\mathbb{E}[c_i] \le (\tfrac34)^i n. The expected total cost is at most 4∑iE[ci]≤4n∑i(34)i=16n4\sum_i \mathbb{E}[c_i] \le 4n\sum_i(\tfrac34)^i = 16n. □\square

Complexity

DORFLER-SORT costs Θ(Mlog⁡M)\Theta(M\log M) comparisons in the worst case for the sort and Θ(M)\Theta(M) for the scan. DORFLER-SELECT costs Θ(M)\Theta(M) expected time in the real-RAM model: one pass is linear in the size of cand, and a random pivot makes the expected size of the next cand at most 34\tfrac34 of the current one, so the expected total is at most 16M16M operations. Its worst case is Θ(M2)\Theta(M^2), with bad pivots every time. A median of medians pivot gives worst-case linear time, and Pfeiler and Praetorius proved that marking with minimal size is a linear time problem. Both procedures need Θ(M)\Theta(M) extra memory.

In the adaptive loop the cost of marking is small next to the solve. The sort is often fast enough in practice. The linear methods matter when the theory counts every step, as in proofs that the whole loop has optimal cost.

Properties

  • Minimal size. The output has the smallest size among all bulk sets. The optimality theorem allows a set up to a constant factor larger than the minimum.
  • Largest values. Every marked indicator is at least as large as every unmarked one.
  • Monotone in theta. A larger θ\theta never gives a smaller set.
  • Extremes. θ=1\theta = 1 marks every element with a nonzero indicator (and at least one element), and a small θ\theta marks the few largest.
  • Contraction. With an exact solve, newest vertex bisection and the residual estimator, Dorfler marking gives ∣∣∣u−Uk+1∣∣∣2+γηk+12≤α2(∣∣∣u−Uk∣∣∣2+γηk2)|||u - U_{k+1}|||^2 + \gamma\eta_{k+1}^2 \le \alpha^2(|||u - U_k|||^2 + \gamma\eta_k^2) with α<1\alpha < 1 for every θ∈(0,1]\theta \in (0, 1].
  • Optimal marking lemma. The converse also holds. If a refinement reduces the total error by a fixed factor, then the refined elements satisfy the bulk criterion for every θ\theta below a threshold θ∗\theta_* that depends on the stability and discrete reliability constants. This is why the optimality theorem needs a small θ\theta.

Variants and improvements

  • Binning. Stevenson puts the indicators into bins by powers of 2 of their size relative to the largest one and takes whole bins from the top. This costs linear time and gives a bulk set at most twice the minimal size, which the optimality theorem allows.
  • Maximum strategy. Mark every KK with ηK≥θmax⁡K′ηK′\eta_K \ge \theta\max_{K'}\eta_{K'}. It needs no sort and often works well, but it does not satisfy the bulk criterion in general, and the classical rate proofs do not cover it.
  • Marking for oscillation. Morin, Nochetto and Siebert (2000) also marked by the data oscillation with a second bulk criterion. Cascon, Kreuzer, Nochetto and Siebert showed this is not needed for the model problem.
  • Separate marking for hp. For hp-FEM, the marked elements are then split between h and p refinement by a smoothness test.
  • Goal-oriented marking. With a primal and a dual estimator, one marks a bulk set for each, or for their product, and takes the smaller set. The rates are then proved for the goal error.

When to use it

Use Dorfler marking in any adaptive loop where a convergence guarantee matters, and whenever the code should follow the theory of rates. It is also the safe default in practice: it adapts the number of marked elements to how concentrated the error is. When the indicators come from the Zienkiewicz-Zhu estimator or the dual weighted residual method, the same rule applies, but the convergence theory is less complete.

Pitfalls

  • Squares, not norms. The criterion sums the squared indicators ηK2\eta_K^2. Summing ηK\eta_K gives a different set and is not what the proofs use.
  • Theta near 1. A large θ\theta marks almost everything and loses the point of adaptivity. The optimality theorem needs θ<θ∗\theta < \theta_*, and θ∗\theta_* is not known in practice. Values from 0.25 to 0.6 are common, and they are a rule of thumb.
  • Floating point at the cut. In floating point, two procedures that add the values in different orders can disagree by one element when the sum is very close to the goal. Use exact arithmetic in tests, as the test file does with Fraction values.
  • All zero indicators. If all indicators are 0, the goal is 0 and any single element satisfies the criterion. The loop should stop before this, because the estimator is already 0.
  • Ties. With ties at the cut, the minimal set is not unique. Code that depends on a particular choice, for example in a parallel run, needs a fixed rule for ties.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

dorfler_marking.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/dorfler-marking/. The authors are listed in prov/G0MEX4.jsonl.

"""Dorfler (bulk) marking with a marked set of minimal size.

Input: a list eta2 of squared error indicators eta_K^2 >= 0, one per
element, and a parameter theta in (0, 1]. Output: a list M of element
numbers with sum(eta2[K] for K in M) >= theta * sum(eta2) and with the
smallest possible size. The functions work on float or Fraction data; with
Fraction the comparisons are exact.

dorfler_sort sorts and takes the largest indicators: O(M log M) for M
elements. dorfler_select finds the same size by partitions around random
pivots, as in quickselect: O(M) expected time (real-RAM).
"""

import random


def dorfler_sort(eta2, theta):
    """Return a minimal bulk set by sorting the indicators in decreasing order."""
    goal = theta * sum(eta2)  # @line L1
    order = sorted(range(len(eta2)), key=lambda t: -eta2[t])  # @line L2
    marked, acc = [], 0  # @line L3
    for t in order:  # @line L4
        if acc >= goal and marked:
            break
        marked.append(t)  # @line L5
        acc += eta2[t]
    return marked  # @line L6


def dorfler_select(eta2, theta, rng=None):
    """Return a minimal bulk set by partitions around random pivots (expected linear time)."""
    rng = rng or random.Random(0)
    goal = theta * sum(eta2)  # @line L7
    marked = []
    cand = list(range(len(eta2)))  # @line L8
    while cand:  # @line L9
        p = eta2[cand[rng.randrange(len(cand))]]  # @line L10
        high = [t for t in cand if eta2[t] > p]  # @line L11
        equal = [t for t in cand if eta2[t] == p]
        low = [t for t in cand if eta2[t] < p]
        sh = sum(eta2[t] for t in high)  # @line L12
        if high and sh >= goal:  # @line L13
            cand = high  # the answer ends inside high
            continue
        se = sum(eta2[t] for t in equal)
        if sh + se >= goal:  # @line L14
            marked.extend(high)
            acc = sh
            for t in equal:
                if acc >= goal and marked:
                    break
                marked.append(t)
                acc += eta2[t]
            return marked
        marked.extend(high + equal)  # @line L15
        goal -= sh + se
        cand = low
    return marked  # @line L16
test_dorfler_marking.py 71 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/dorfler-marking/. The authors are listed in prov/G0MEX4.jsonl.

import itertools
import random
from fractions import Fraction

from dorfler_marking import dorfler_select, dorfler_sort


def brute_force_size(eta2, theta):
    goal = theta * sum(eta2)
    n = len(eta2)
    for k in range(1, n + 1):
        for s in itertools.combinations(range(n), k):
            if sum(eta2[t] for t in s) >= goal:
                return k
    return None


def check(eta2, theta, marked):
    assert len(set(marked)) == len(marked)
    assert all(0 <= t < len(eta2) for t in marked)
    assert sum(eta2[t] for t in marked) >= theta * sum(eta2)


def test_hand_example():
    eta2 = [4, 1, 9, 2, 6, 3, 5]
    assert sorted(dorfler_sort(eta2, Fraction(1, 2))) == [2, 4]
    assert sorted(dorfler_select(eta2, Fraction(1, 2))) == [2, 4]
    assert sorted(dorfler_sort(eta2, Fraction(4, 5))) == [0, 2, 4, 6]
    assert sorted(dorfler_select(eta2, Fraction(4, 5))) == [0, 2, 4, 6]
    assert len(dorfler_sort(eta2, 1)) == 7


def test_minimal_against_brute_force_exact():
    rng = random.Random(2024)
    for _ in range(300):
        n = rng.randint(1, 9)
        eta2 = [Fraction(rng.randint(0, 12), rng.randint(1, 4)) for _ in range(n)]
        if sum(eta2) == 0:
            eta2[0] = Fraction(1)
        theta = Fraction(rng.randint(1, 10), 10)
        best = brute_force_size(eta2, theta)
        a = dorfler_sort(eta2, theta)
        b = dorfler_select(eta2, theta, random.Random(rng.randint(0, 10 ** 6)))
        check(eta2, theta, a)
        check(eta2, theta, b)
        assert len(a) == best
        assert len(b) == best


def test_same_size_on_large_float_input_with_ties():
    rng = random.Random(9)
    eta2 = [float(rng.randint(0, 50)) for _ in range(5000)]  # integers: sums are exact
    for theta in (0.1, 0.3, 0.5, 0.9, 1.0):
        a = dorfler_sort(eta2, theta)
        b = dorfler_select(eta2, theta, random.Random(1))
        check(eta2, theta, a)
        check(eta2, theta, b)
        assert len(a) == len(b)
        rest = set(range(len(eta2))) - set(b)
        if rest:
            assert min(eta2[t] for t in b) >= max(eta2[t] for t in rest)  # b holds the largest values


def test_all_zero_and_single():
    assert len(dorfler_sort([0.0, 0.0, 0.0], 0.5)) == 1
    assert len(dorfler_select([0.0, 0.0, 0.0], 0.5)) == 1
    assert dorfler_sort([2.0], 1.0) == [0]
    assert dorfler_select([2.0], 1.0) == [0]

History

Willy Dorfler introduced the criterion in 1996, in the first proof that an adaptive finite element method for Poisson's equation in 2D reduces the error by a fixed factor in each step. His proof needed a fine enough initial mesh to resolve the data. Pedro Morin, Ricardo Nochetto and Kunibert Siebert removed this condition in 2000. Peter Binev, Wolfgang Dahmen and Ronald DeVore in 2004, and Rob Stevenson in 2007, used bulk marking in the first proofs of optimal rates, and Stevenson gave the binning method with linear cost. Manuel Cascon, Christian Kreuzer, Nochetto and Siebert proved the optimal marking lemma in 2008. Carl-Martin Pfeiler and Dirk Praetorius showed in 2020 that a set of exactly minimal size can be found in linear time.

References

  1. Willy Dorfler, A convergent adaptive algorithm for Poisson's equation, SIAM Journal on Numerical Analysis 33(3) (1996) 1106-1124, the marking criterion and the error reduction proof
  2. Carl-Martin Pfeiler and Dirk Praetorius, Dorfler marking with minimal cardinality is a linear complexity problem, Mathematics of Computation 89(326) (2020) 2735-2752, the linear time algorithm for a minimal Dorfler set and its cost analysis
  3. Rob Stevenson, Optimality of a standard adaptive finite element method, Foundations of Computational Mathematics 7(2) (2007) 245-269, the marking by binning, linear cost with a set at most twice the minimal size, and the optimality proof
  4. J. Manuel Cascon, Christian Kreuzer, Ricardo H. Nochetto and Kunibert G. Siebert, Quasi-optimal convergence rate for an adaptive finite element method, SIAM Journal on Numerical Analysis 46(5) (2008) 2524-2550, the contraction with Dorfler marking, and the optimal marking lemma with its threshold on theta

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.average
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example