Data Structure and Algorithm 1 | Algorithm Analysis Theory

0_QXrDY4nVmhZ1umbX

0. Environment Installation

1. Basic Concepts

As soon as an Analytic Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will arise — By what course of calculation can these results be arrived at by the machine in the shortest time?

— Charles Babbage (1864)

In Java, we can time the running time of an algorithm by Stopwatch class, which is a part of the stdlib.jar. We should use Stopwatch() for creating a new stopwatch and then call elapsedTime() to get the time in seconds since the Stopwatch is created.

(1) Scientific Method

In the 1970s, Donald Knuth introduces the scientific method for understanding computing performance. Nowadays, the scientific method is explained in the following steps,

There are two principles for the scientific method,

2. Examples of Successful Algorithms

(1) Discrete Fourier Transform (DFT) Problem

Break down waveform of N samples into periodic components. The applications are on DVDs, JPEG files, MRI, astrophysics, etc.

(2) N-Body Simulation Problem

Simulate gravitational interactions among N bodies.

(3) Three Sum Problem

Given N distinct integers, how many triples sum to exactly zero?

For example,

The answer should be,

The Grid-search program should be,

We can download the testing files of 1Kint.txt, 2Kint.txt , 4Kint.txt , and 8Kint.txt (nKint means we have n thousand integers in that file) from these links. And then we are able to test these files by,

The results are,

If we plot this result with the standard plot and the log-log plot, we will have the following relationships,

img

This is because we have,

So,

where,

Because we have a slope in this plot of about 3, then we can say,

And the order of growth of the running time is about,

Again, we can simulate the time by,

The results are,

Here, we can spot a big improvement in the time complexity.

3. Algorithm Theory

(1) The Definition of Algorithm Analysis

The algorithm analysis theory is invented in the book The Art Of Computer Programming by Donald Knuth. It explains the mathematical models for calculating the total running time of a computer program, which should physically be the sum of cost times frequency for all operations.

(2) Cost of Basic Operations

img

(3) Brute-Force TwoSum Cost Example

Now, let’s analyze the cost of a TwoSum program. In this program, we have,

Then, the frequency for each operation should be,

The cost model is to use some basic operations as a proxy for running time and here we can simply choose array access with the cost model N(N-1).

(4) Tilde Notation

The tilde notation ~ is used when we are able to ignore lower order terms, so technically,

When,

Then, for the Brute-Force TwoSum problem, the can be cost model with N inputs can be simplified to N².

(5) Big-Oh Notation

The most commonly used notation in algorithm analysis is the big-oh notation O. It also has two friends, big-theta Θ and big-omega Ω.

(6) Common Order of Growth Classifications

img

(7) Problem Sizes Per Minute for Different Order of Growth in the 2000s

(8) Memory Cost for Common Data Type