Elephant in Cairo

An elephant in Cairo is a term used in computer programming to describe a piece of data inserted at the end of a search space, which matches the search criteria, in order to make sure the search algorithm terminates. The term derives from a humorous essay circulated on the Internet and published in Byte magazine in September 1989 that described how various professions would go about hunting elephants, with programmers following the algorithm:

  1. Go to Africa.
  2. Start at the Cape of Good Hope.
  3. Work northward in an orderly manner, traversing the continent alternately east and west,
  4. During each traverse pass:
  5. Check whether we are at the end of Africa. If not:
    • Catch each animal seen.
    • Compare each animal caught to a known elephant.
    • Stop when a match is detected
  6. If you are at the end of Africa, there are no elephants in Africa.

Experienced programmers modify the above algorithm by placing a known elephant in Cairo to ensure that the algorithm will terminate. In technical terms, they use the elephant in Cairo as a sentinel value. The modified algorithm is therefore as follows:

  1. Go to Africa.
  2. Put an elephant in Cairo.
  3. Start at the Cape of Good Hope.
  4. Work northward in an orderly manner, traversing the continent alternately east and west,
  5. During each traverse pass:
    • Catch each animal seen.
    • Compare each animal caught to a known elephant.
    • Stop when a match is detected.
  6. If you are in Cairo, then there are no elephants in Africa (other than the one you placed there).

The removal of the requirement to check for the end of Africa each time speeds up the algorithm (although with modern processors implementing branch prediction, the speed-up is likely to be negligible) and simplifies the code.

Famous quotes containing the words elephant and/or cairo:

    I asked my mother for fifty cents
    To see the elephant jump the fence.
    He jumped so high he reached the sky,
    And didn’t get back till the Fourth of July.
    —Unknown. I Asked My Mother (l. 1–4)

    The Cairo conference ... is about a complicated web of education and employment, consumption and poverty, development and health care. It is also about whether governments will follow where women have so clearly led them, toward safe, simple and reliable choices in family planning. While Cairo crackles with conflict, in the homes of the world the orthodoxies have been duly heard, and roundly ignored.
    Anna Quindlen (b. 1952)