Safekipedia

Analysis of algorithms

Adapted from Wikipedia · Discoverer experience

In computer science, the analysis of algorithms is the study of how much time, space, or other resources an algorithm needs to work. This helps us understand how well an algorithm performs, especially when dealing with large amounts of data. By looking at how the size of the input affects the steps an algorithm takes, we can determine if it is efficient or not.

The idea of analyzing algorithms was started by Donald Knuth. This analysis is a key part of computational complexity theory, which looks at the resources needed to solve different problems. It helps researchers find better ways to create fast and effective algorithms.

When studying algorithms, scientists often use special ways to describe how they grow with larger inputs. They use Big O notation, Big-omega notation, and Big-theta notation to give a general idea of an algorithm’s performance. For example, binary search is known to work in logarithmic time, meaning it takes steps that relate to the logarithm of the number of items being searched. These descriptions help compare different ways to solve the same problem, even though the exact speed can change based on how the algorithm is put into practice.

Sometimes, we can calculate the exact efficiency of an algorithm, but this often depends on specific details about how it is implemented. This is studied using a model of computation, such as an abstract computer like a Turing machine, where certain operations are assumed to take a set amount of time. For example, with binary search, if each lookup in a list of n items takes one unit of time, we can precisely say how long the search will take.

Cost models

When we talk about how fast a computer program runs, we need to decide what counts as a single step. For this to match real-life timing, each step should take about the same amount of time. Sometimes, this isn’t simple. For example, adding two big numbers might take longer than adding two small ones.

There are two main ways to measure these steps:

  • the uniform cost model, which says every step takes the same time, no matter how big the numbers are
  • the logarithmic cost model, which says the time depends on how many digits are involved

The logarithmic model is trickier and is only used when needed, like in special math for cryptography. Sometimes, the basic guesses about how slow a problem can be don’t match the best real-life solutions because they use simpler rules than what we can actually do.

Run-time analysis

Run-time analysis is a way to guess how much time a computer program will take as the size of its input gets bigger. It's very important in computer science because a program might take seconds, hours, or even years to finish depending on the method it uses. While actual testing can show how long a program takes, it can't predict times for every possible input size. That's why scientists use special theories to understand run-time analysis.

Different computers and software can change how fast a program runs, so scientists need better ways to compare programs. For example, imagine a program that searches a list. One computer might use a simple search and be fast, but if the list gets very big, a smarter search method on a slower computer might become faster overall.

Scientists use a special way to describe how a program's time grows with bigger inputs, called Big O notation. It helps them understand the worst-case scenario. For example, some sorting programs might take much longer on very large lists, and Big O notation shows this clearly.

By looking at the steps in a program, scientists can predict how much time it will take. They break down each part and add up the time needed. This helps them see which parts might slow down the program the most. For example, some programs might need a lot of memory as the input grows, which can also be predicted using similar methods.

n (list size)Computer A run-time
(in nanoseconds)
Computer B run-time
(in nanoseconds)
168100,000
6332150,000
250125200,000
1,000500250,000
n (list size)Computer A run-time
(in nanoseconds)
Computer B run-time
(in nanoseconds)
168100,000
6332150,000
250125200,000
1,000500250,000
.........
1,000,000500,000500,000
4,000,0002,000,000550,000
16,000,0008,000,000600,000
.........
63,072 × 101231,536 × 1012 ns,
or 1 year
1,375,000 ns,
or 1.375 milliseconds
n (list size)Computer A run-time
(in nanoseconds)
Local order of growth
(n^_)
Computer B run-time
(in nanoseconds)
Local order of growth
(n^_)
157100,000
65321.04150,0000.28
2501251.01200,0000.21
1,0005001.00250,0000.16
.........
1,000,000500,0001.00500,0000.10
4,000,0002,000,0001.00550,0000.07
16,000,0008,000,0001.00600,0000.06
.........

Relevance

Analyzing algorithms is very important because using the wrong one can make a computer work much slower. In situations where time matters, a slow algorithm might give answers too late to be helpful. Also, some algorithms need too much memory or power to run, which can make them cost too much or hard to use.

Constant factors

When we study how fast computer programs run, we often look at how their speed changes with bigger and bigger data. But for real-world use, the actual speed also depends on small details, like how much memory the computer has.

Computers have a limit to how much data they can handle. For example, a computer with 32-bit processing can handle about 4 billion pieces of data, while one with 64-bit processing can handle much more.

Some types of calculations grow very slowly. For example, even for huge amounts of data, certain steps might only need to be done a few times. Because of this, for small or medium amounts of data, a program that usually needs more steps might actually run faster than one that needs fewer steps for very large data. This idea is used in special computer programs that change how they work depending on the size of the data they are handling.

Related articles

This article is a child-friendly adaptation of the Wikipedia article on Analysis of algorithms, available under CC BY-SA 4.0.