Introduction to data structures and algorithms

Published

2023-08-05

So here you are, taking a course on data structures and algorithms. But what is a “data structure”? What is an “algorithm”? The textbook gives these definitions: “An algorithm is a recipe for performing a certain task.” and “a data structure is a way of arranging data to make solving a particular problem easier.” Let’s look at these in turn.

What is an algorithm?

It makes sense to expand on the textbook’s definition. An algorithm must be a recipe that you can write down (either in code or in words), and thus it must be finite. Moreover, it should be a complete recipe—there should be no guesswork involved—a complete set of instructions to perform some task or solve some kind of problem. It is also a common requirement that the process itself must terminate, not just its written form.

In this course, we’ll look at a wide variety of algorithms for sorting, managing, and retrieving data, and for analyzing graphs and networks.

Algorithms have been around a long time. The earliest algorithms for which we have evidence date back to ancient Egypt and Babylonia—around 4,500 years ago. These are instructions for multiplying numbers, finding areas, finding volumes of certain solids, and calculating square roots. The word “algorithm” comes from the name of a 9th century Persian polymath, Muhammad ibn Musa Al-Khwarizmi. He made substantial contributions in astronomy and mathematics (especially with regard to algebra and trigonometry). The last part of his name “Al-Khwarizmi” became our word “algorithm.

Statue of al-Khwarizmi in Uzbekistan. Source: Getty Images.

You may be interested to know that the first algorithm written to run on a computer was written before computers existed! Back in the 19th century, Charles Babbage drafted designs for a mechanical computer, the so-called “Analytical Engine.” Upon hearing of this Lady Ada Lovelace became intrigued, and among her works from the 1840s we find an algorithm for calculating Bernoulli numbers (see: https://en.wikipedia.org/wiki/Bernoulli_number if you’re curious about Bernoulli numbers). So the world’s first computer programmer is, arguably, Lady Ada Lovelace.

Lady Ada Lovelace

What is a data structure?

Frankly, I like the definition given in the textbook: “a data structure is a way of arranging data to make solving a particular problem easier.” Data always have structure, but arranging your data in the right structure for a given problem can make solving the problem much, much easier. We’ll see a variety of data structures in this course. Some are linear data structures, like vectors, linked lists, stacks, and queues. Others are not linear, like trees, heaps, and graphs. Sometimes a data structure is equipped with methods. Methods are, in a sense, actions that can be performed on the data. For example, a stack is a last-in first-out (LIFO) data structure that has—at minimum—methods to push a new element onto the stack, and to pop an element off the top of the stack. A more complete implementation of a stack might also have a peek method (to get the value of the element at the top of the stack) or an “is empty” method, to find out whether the stack is empty. Other, more complex data structures may have more complex methods. For example, we’ll learn about trees, and some tree-based data structures have the property that they modify themselves when values are searched, added or deleted. These are the so-called self-balancing trees. We don’t have the terminology or understand these in detail yet, but we will soon. The point is that data structures aren’t static things. They are dynamic objects that provide structure and methods for working with data. One of the most important things I want you to learn in this course how to determine is which data structures are suitable for a given use case. Picking the correct data structure can make your problem much easier. Picking a data structure that is not appropriate can cause all manner of difficulty. So this is an important skill to acquire.

Additional reading (optional)

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

All materials copyright © 2020–2023, The University of Vermont. All rights reserved.