ArticleslgStudy

computer science

Lloyd's algorithm

Lloyd's algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Lloyd's algorithm rather than just read about it. In short: In electrical engineering and computer science, Lloyd's algorithm, also known as Voronoi iteration or relaxation, is an algorithm named after Stuart P. Lloyd for finding evenly spaced sets of points in subsets of Euclidean spaces and partitions of these subsets into well-shaped and uniformly sized convex cells.

Lloyd's algorithm — main illustration
Lloyd's algorithm — illustration

Key takeaways

  • Lloyd's algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Lloyd's algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Lloyd's algorithm from memory before moving on to harder problems.

Reference excerpt

In electrical engineering and computer science, Lloyd's algorithm, also known as Voronoi iteration or relaxation, is an algorithm named after Stuart P. Lloyd for finding evenly spaced sets of points in subsets of Euclidean spaces and partitions of these subsets into well-shaped and uniformly sized convex cells. Like the closely related k-means clustering algorithm, it repeatedly finds the centroid of each set in the partition and then re-partitions the input according to which of these centroids is closest. In this setting, the mean operation is an integral over a region of space, and the nearest centroid operation results in Voronoi diagrams. Although the algorithm may be applied most directly to the Euclidean plane, similar algorithms may also be applied to higher-dimensional spaces or to spaces with other non-Euclidean metrics. Lloyd's algorithm can be used to construct close approximations to centroidal Voronoi tessellations of the input, which can be used for quantization, dithering, and stippling. Other applications of Lloyd's algorithm include smoothing of triangle meshes in the finite element method.

History The algorithm was first proposed by Stuart P. Lloyd of Bell Labs in 1957 as a technique for pulse-code modulation. Lloyd's work became widely circulated but remained unpublished until 1982. A similar algorithm was developed independently by Joel Max and published in 1960, which is why the algorithm is sometimes referred as the Lloyd-Max algorithm.

Algorithm description Lloyd's algorithm starts by an initial placement of some number k of point sites in the input domain. In mesh-smoothing applications, these would be the vertices of the mesh to be smoothed; in other applications they may be placed at random or by intersecting a uniform triangular mesh of the appropriate size with the input domain. It then repeatedly executes the following relaxation step:

The Voronoi diagram of the k sites is computed. Each cell of the Voronoi diagram is integrated, and the centroid is computed. Each site is then moved to the centroid of its Voronoi cell.

Integration and centroid computation Because Voronoi diagram construction algorithms can be highly non-trivial, especially for inputs of dimension higher than two, the steps of calculating this diagram and finding the exact centroids of its cells may be replaced by an approximation.

Approximation A common simplification is to employ a suitable discretization of space like a fine pixel-grid, e.g. the texture buffer in graphics hardware. Cells are materialized as pixels, labeled with their corresponding site-ID. A cell's new center is approximated by averaging the positions of all pixels assigned with the same label. Alternatively, Monte Carlo methods may be used, in which random sample points are generated according to some fixed underlying probability distribution, assigned to the closest site, and averaged to approximate the centroid for each site.

Exact computation Although embedding in other spaces is also possible, this elaboration assumes Euclidean space using the L2 norm and discusses the two most relevant scenarios, which are two, and respectively three dimensions. Since a Voronoi cell is of convex shape and always encloses its site, there exist trivial decompositions into easy integratable simplices:

In two dimensions, the edges of the polygonal cell are connected with its site, creating an umbrella-shaped set of triangles. In three dimensions, the cell is enclosed by several planar polygons which have to be triangulated first: Compute a center for the polygon face, e.g. the average of all its vertices. Connecting the vertices of a polygon face with its center gives a planar umbrella-shaped triangulation. Trivially, a set of tetrahedra is obtained by connecting triangles of the cell's hull with the cell's site. Integration of a cell and computation of its centroid (center of mass) is now given as a weighted combination of its simplices' centroids (in the following called c i {\textstyle \mathbf {c} _{i}} ).

Two dimensions: For a triangle the centroid can be easily computed, e.g. using cartesian coordinates. Weighting computes as simplex-to-cell area ratios. Three dimensions: The centroid of a tetrahedron is found as the intersection of three bisector planes and can be expressed as a matrix-vector product. Weighting computes as simplex-to-cell volume ratios. For a 2D cell with n triangular simplices and an accumulated area A C = ∑ i = 0 n a i {\textstyle A_{C}=\sum _{i=0}^{n}a_{i}} (where a i {\textstyle a_{i}} is the area of a triangle simplex), the new cell centroid computes as:

C = 1 A C ∑ i = 0 n c i a i {\displaystyle C={\frac {1}{A_{C}}}\sum _{i=0}^{n}\mathbf {c} _{i}a_{i}}

Analogously, for a 3D cell with a volume of V C = ∑ i = 0 n v i {\textstyle V_{C}=\sum _{i=0}^{n}v_{i}} (where v i {\textstyle v_{i}} is the volume of a tetrahedron simplex), the centroid computes as:

… excerpt ends here. Continue reading the full article.

Illustrations

Lloyd's algorithm illustration
Lloyd's algorithm illustration
Lloyd's algorithm illustration
Lloyd's algorithm illustration

Worked examples

Example 1 — a first encounter with Lloyd's algorithm

Start with the simplest possible case. Write down what Lloyd's algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Lloyd's algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Lloyd's algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Lloyd's algorithm

In research
Lloyd's algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Lloyd's algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Lloyd's algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Geometric algorithms, Optimization algorithms and methods, so understanding it makes those chapters shorter.
In everyday life
Look for Lloyd's algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Lloyd's algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Lloyd's algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Lloyd's algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Lloyd's algorithm in simple terms?

In electrical engineering and computer science, Lloyd's algorithm, also known as Voronoi iteration or relaxation, is an algorithm named after Stuart P. Lloyd for finding evenly spaced sets of points in subsets of Euclidean spaces and partitions of these subsets into well-shaped and uniformly sized…

Why does Lloyd's algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Lloyd's algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Lloyd's algorithm.

Tags

  • Geometric algorithms
  • Optimization algorithms and methods

Keep exploring