DEV Community

Erik
Erik

Posted on • Edited on

Everything you need to know about algorithms

Algorithm is an exact description of how to solve a problem. Algorithms are basically what programming is all about: we tell computers, in very exact ways with programming languages, how to solve problems -- we write algorithms. But algorithms don't have to be just computer programs, they are simply instruction for solving problems.

Cooking recipes are sometimes given as an example of a non-computer algorithm (though they rarely contain branching and loops, very typical features of algorithms). The so called wall-follower is a simple algorithm to get out of any maze: you just pick either a left-hand or right-hand wall and then keep following it. You may write a crazy algorithm for how to survive in a jungle, but it has to be exact; if there is any ambiguity, it is not considered an algorithm.

Interesting fact: contrary to intuition there are problems that are mathematically proven to be unsolvable by any algorithm, but for most practically encountered problems we can write an algorithm (though for some problems even our best algorithms can be unusably slow).

Algorithms are mostly (possibly declarative, depending on definitions) written as a series of steps (or instructions); these steps may be specific actions (such as adding two numbers or drawing a pixel to the screen) or conditional jumps to other steps ("if condition X holds then jump to step N, otherwise continue"). These jumps can be used to create branches (in programming known as if-then-else) and loops. Branches and loops are together known as control structures -- they don't express a direct action but control where we move in the algorithm itself. All in all, any algorithm can be written with only these three constructs:

  • sequence: A series of steps, one after another.
  • selection (branches, if-then-else): Two branches (sequences of steps) preceded by a condition; the first branch is executed only if the condition holds, the second ("else") branch is executed only if the condition doesn't hold (e.g. "If user password is correct, log the user in, otherwise print out an error.").
  • iteration (loops, repetition): Sequence of steps that's repeated as long as certain condition holds (e.g. "As long as end of file is not reached, read and print out next character from the file.").

Note: in a wider sense algorithms may be expressed in other ways than sequences of steps non-imperative ways, even mathematical equations are often called algorithms because they imply the steps towards solving a problem. But we'll stick to the common meaning of algorithm given above.

Additional constructs can be introduced to make programming more comfortable, e.g. subroutines/functions (kind of small subprograms that the main program uses for solving the problem) or switch statements (selection but with more than two branches). Loops are also commonly divided into several types: counted loops, loops with condition and the beginning and loops with condition at the end (for, while and do while in C, respectively). Similarly to mathematical equations, algorithms make use of variables, i.e. values which can change that have a specific name (such as x or myVariable).

Practical programming is based on expressing algorithm via text, but visual programming is also possible: flowcharts are a way of visually expressing algorithms, you have probably seen some. Decision trees are special cases of algorithms that have no loops, you have probably seen some too. Even though some languages (mostly educational such as Snap are visual and similar to flow charts, it is not practical to create big algorithms in this way -- serious programs are written as a text in programming language.

Example

Let's write a simple algorithm that counts the number of divisors of given number x and checks if the number is prime along the way. (Note that we'll do it in a naive, educational way -- it can be done better). Let's start by writing the steps in plain English:

  1. Read the number x from the input.
  2. Set the divisor counter to 0.
  3. Set currently checked number to 1.
  4. While currently checked number is lower or equal than x:
    • a: If currently checked number divides x, increase divisor counter by 1.
    • b: Increase currently checked number.
  5. Write out the divisor counter.
  6. If divisor counter is equal to 2, write out the number is a prime.

Notice that x, divisor counter and currently checked number are variables. Step 4 is a loop (iteration) and steps a and 6 are branches (selection). The flowchart of this algorithm is:

               START
                 |
                 V
               read x
                 |
                 V
       set divisor count to 0
                 |
                 V
       set checked number to 1
                 |
    ------------>|
    |            |
    |            V                no
    |    checked number <= x ? -------
    |            |                   |
    |            | yes               |
    |            V                   |
    |     checked number    no       |
    |       divides x ? --------     |
    |            |             |     |
    |            | yes         |     |
    |            V             |     |
    |     increase divisor     |     |
    |       count by 1         |     |
    |            |             |     |
    |            |             |     |
    |            |<-------------     |
    |            |                   |
    |            V                   |
    |     increase checked           V
    |       number by 1     print divisor count
    |            |                   |
    --------------                   |
                                     V             no
                             divisor count = 2 ? ------
                                     |                |
                                     | yes            |
                                     V                |
                           print "number is prime"    |
                                     |                |
                                     |<----------------
                                     V
                                    END

Enter fullscreen mode Exit fullscreen mode

This algorithm would be written in Python as:

x = int(input("enter a number: "))

divisors = 0

for i in range(1,x + 1):
  if x % i == 0: # i divides x?
    divisors = divisors + 1

print("divisors: " + str(divisors))

if divisors == 2:
  print("It is a prime!")
Enter fullscreen mode Exit fullscreen mode

and in C as:

#include <stdio.h>                                                              

int main(void)
{
  int x, divisors = 0;

  scanf("%d",&x); // read a number

  for (int i = 1; i <= x; ++i)
    if (x % i == 0) // i divides x?
      divisors = divisors + 1;

  printf("number of divisors: %d\n",divisors);

  if (divisors == 2)
    puts("It is a prime!");

  return 0;
} 
Enter fullscreen mode Exit fullscreen mode

Study of Algorithms

As algorithms are at the heart of [computer science, there's a lot of rich theory and knowledge about them.

Turing machine, a kind of mathematical bare-minimum computer, created by Alan Turing, is the traditional formal tool for studying algorithms, though many other models of computation exist. From theoretical computer science we know not all problems are computable, i.e. there are problems unsolvable by any algorithm (e.g. the [halting problem. Computational complexity is a theoretical study of resource consumption by algorithms, i.e. how fast and memory efficient algorithms are (see e.g. P vs NP. Mathematical programming is concerned, besides others, with optimizing algorithms so that their time and/or space complexity is as low as possible which gives rise to algorithm design methods such as dynamic programming (optimization is a less theoretical approach to making more efficient algorithms). Formal verification is a field that tries to mathematically (and sometimes automatically) prove correctness of algorithms (this is needed for critical software, e.g. in planes or medicine). Genetic programming and some other methods of artificial intelligence try to automatically create algorithms (algorithms that create algorithms). Quantum computingis concerned with creating new kind of algorithms algorithms for quantum computers (a new type of still-in-research computers). [Programming language design is an art of finding best ways of expressing algorithms.

Specific Algorithms

Following are some common algorithms classified into groups.

graphics

  • DDA : line drawing algorithm
  • Bresenham's algorithm : another line drawing algorithm
  • Midpoint algorithm : circle drawing algorithm
  • flood fill : algorithm for coloring continuous areas
  • Hough transform : finds shapes in pictures
  • painter's algorithm
  • path tracing
  • ray tracing
  • Boot'h algorithm : algorithm for multiplication
  • Dijkstra's algorithm
  • Euclidean algorithm: computes greatest common divisor
  • numerical algorithm : approximate mathematical functions
  • sieve of Eratosthenes : computes prime numbers
  • bubble sort : simple, kind of slow but still usable sorting algorithm
  • quick sort : one of the fastest sorting algorithms
    • searching
  • binary search
  • linear search
    • other
  • A* : path searching algorithm, used by AI in many games
  • fizzbuzz : problem/simple algorithm given in job interviews to filter out complete noobs
  • FFT: quickly converts signal (audio, image, ...) to its representation in frequencies, one of the most famous and important algorithms
  • Huffman coding : compression algorithm
  • k-means : clustering algorithm
  • MD5: hash function
  • minimax plus alpha-beta pruning : used by many AI that play turn based games
  • proof of work algorithms: used by some cryptocurrencies
  • Shor's algorithm: factorization algorithm
  • YouTube algorithm: secret algorithm YouTube uses to suggest videos to viewers, a lot of people hate it :)

Top comments (1)

Collapse
 
roniardynt profile image
Ron

Great article with clear explanation ✨