Dutch National Flag

home1 home2
 Bib
 Algorithms
 Bioinfo
 FP
 Logic
 MML
 Prog.Lang
and the
 Book

Algorithms
 glossary
 Sorting
  Insertion
  Quick
  Merge
  Heap
  Dutch N.F.
  Radix

Dijkstra used the Dutch National Flag Problem* as a structured programming exercise in program derivation and program proof. Given `N' objects coloured red, white or blue, sort them so that objects of the same colour are adjacent, with the colours in the order red, white and blue.

The problem is closely related to the partition operation of quick sort: the attribute need not be a `colour' but can be `greater than the median', or `leading digit is zero', or whatever property you care to choose.

Two Colours

It is easiest to consider just two "colours", {zero,one}, first. The algorithm maintains three sections (possibly empty) in the array a[ ]:

  1. a[1..Lo-1] zeroes
  2. a[Lo..Hi] unknown
  3. a[Hi+1..N] ones
The unknown section is shrunk while maintaining these conditions:

  1. Lo := 1; Hi := N;
  2. while Lo <= Hi do
    1. Invariant: a[1..Lo-1] are all zero and a[Hi+1..N] are all one; a[Lo..Hi] are unknown.
    2. if a[Lo] = 0 then Lo++
    3. else swap a[Lo] and a[Hi]; Hi--
--- 2-way Partitioning ---

The data in the HTML FORM below consists of random (0|1)s, generated by Math.random() (beware there is a bug in MicroSoft Explorer3, you should be using Netscape3 or later). In the trace, the parts of the array known to the algorithm, i.e. a[1..Lo-1] and a[Hi+1..N] are printed and the unknown section is left blank.
Press the `go' button:

©
L
.
A
l
l
i
s
o
n
(0|1)*:
trace :

Three Colours

The problem was posed with three colours, here `0', `1' and `2'. The array is divided into four sections:

  1. a[1..Lo-1] zeroes (red)
  2. a[Lo..Mid-] ones (white)
  3. a[Mid..Hi] unknown
  4. a[Hi+1..N] twos (blue)
The unknown region is shrunk while maintaining these conditions

  1. Lo := 1; Mid := 1; Hi := N;
  2. while Mid <= Hi do
    1. Invariant: a[1..Lo-1]=0 and a[Lo..Mid-1]=1 and a[Hi+1..N]=2; a[Mid..Hi] are unknown.
    2. case a[Mid] in
      • 0: swap a[Lo] and a[Mid]; Lo++; Mid++
      • 1: Mid++
      • 2: swap a[Mid] and a[Hi]; Hi--
--- Dutch National Flag Algorithm, or 3-way Partitioning ---

Part way through the process, some red, white and blue elements are known and are in the "right" place. The section of unknown elements, a[Mid..Hi], is shrunk by examining a[Mid]:

            |
0 0 0 1 1 1 ? ? ? ? 2 2 2
      ^     ^     ^
      |     |     |
      Lo    Mid   Hi

Examine a[Mid]. There are three possibilities: a[Mid] is (0) red, (1) white or (2) blue.
Case (0) a[Mid] is red, swap a[Lo] and a[Mid]; Lo++; Mid++

0 0 0 0 1 1 1 ? ? ? 2 2 2
        ^     ^   ^
        |     |   |
        Lo    Mid Hi

Case (1) a[Mid] is white, Mid++

0 0 0 1 1 1 1 ? ? ? 2 2 2
      ^       ^   ^
      |       |   |
      Lo      Mid Hi

Case (2) a[Mid] is blue, swap a[Mid] and a[Hi]; Hi--

0 0 0 1 1 1 ? ? ? 2 2 2 2
      ^     ^   ^
      |     |   |
      Lo    Mid Hi

Continue until Mid>Hi.

Demonstration

The data in the HTML FORM below consists of random (0|1|2)s. In the trace, the parts of the array known to the algorithm, i.e. a[1..Mid-1] and a[Hi+1..N] are printed and the unknown section is left blank.
Press the `go' button:

©
L
.
A
l
l
i
s
o
n
(0..2)*:
trace :

Complexity

Time

Time complexity is O(n).

Space

Space complexity is O(1).

Notes

  • When the items being sorted are literally just integers in [0..1] or [0..2] one could just count the number of each value and overwrite the array contents accordingly. However the intent is that the array items are reasonably complex and that they happen to have an attribute (~colour) drawn from a 2-value or 3-value data-type.
  • The DNF algorithm can be extended to four, or even more colours but it grows more and more complex to write, and the constant of proportionality in its running time increases.
  • The DNF algorithm can be used to partition an array into sections that are (i) <x (red), (ii) =x (white), and (iii) >x (blue), where x is an estimate of the median, for example. Sections (i) and (iii) can be sorted recursively as in Quick Sort.
  • An alternative to the previous point is to find two different elements, v1 and v2, from the array such that v1<v2 (it's sorted if you can't find such v1 and v2), and partition the array into sections (i) <=v1 (red), (ii) >v1 and <=v2; (white), and (iii) >v2 (blue). Each of the three sections ((iii) might be empty) can then be sorted recursively giving a 3-way Quick Sort.
  • See [radix sort] and [quick sort].
  • * I fondly remember first hearing about the Dutch National Flag problem at the Institute of Computer Science (ICS), London University, c1973.

Exercises

  1. Write a 3-way Quick Sort program, using the DNF algorithm.
  2. Write a "flag" program for four "colours".


© L A,
School of Computer Sci & Software Eng., Monash U., Australia 1999.
Coding Ockham's Razor, L. Allison, Springer

A Practical Introduction to Denotational Semantics, L. Allison, CUP

Linux
 Ubuntu
free op. sys.
OpenOffice
free office suite
The GIMP
~ free photoshop
Firefox
web browser

© L. Allison   http://www.allisons.org/ll/   (or as otherwise indicated),
Faculty of Information Technology (Clayton), Monash University, Australia 3800 (6/'05 was School of Computer Science and Software Engineering, Fac. Info. Tech., Monash University,
was Department of Computer Science, Fac. Comp. & Info. Tech., '89 was Department of Computer Science, Fac. Sci., '68-'71 was Department of Information Science, Fac. Sci.)
Created with "vi (Linux + Solaris)",  charset=iso-8859-1,  fetched Friday, 29-Mar-2024 04:51:04 AEDT.