# Analysis of Algorithms

### Analysis of Algorithms Using Summation

The analysis of algorithms is often concerned with understanding their efficiency in terms of time and space complexity. One of the key techniques for quantifying this efficiency is summation, which helps determine the total number of operations performed by an algorithm. For example, the summation&#x20;

$$
\sum_{i=1}^{n}i
$$

can be used to represent the number of iterations in a loop running from `1` to `n`. This approach provides a concrete means to calculate the running time by aggregating all the individual operations. By converting these summation expressions into closed-form equations, it becomes easier to categorize algorithms into complexity classes, using Big O Notation such as $$O(n), O(n^2)$$ or $$O(n^3)$$. This summation analysis lays the foundation for deeper understanding and comparisons between algorithmic efficiencies.

***

### Big O Notation and Summation

In the realm of computer science, Big O notation serves as a shorthand for describing an algorithm's growth rate, which is often derived using summations. For instance, the sum&#x20;

$$
\sum_{I=1}^{n}i^2
$$

can be related to an $$O(n^3)$$ complexity (read **big O of n to the power of 3** or **big O of n cube**), providing a way to express the increasing number of operations as the input size `n` grows. Summation allows us to extract the leading term that dominates the growth rate, stripping away lower-order terms and constants, to focus on the algorithm's performance at scale. Through summation, Big O notation distills the essential characteristics of an algorithm's time or space requirements, which are crucial for algorithm selection and optimization in practice.

### Practical Example of Summation in Algorithm Analysis

To better understand how summation plays a role in algorithm analysis, consider an algorithm that contains a nested loop. The outer loop runs `n` times, while the inner loop runs `i` times (where `i` is the current iteration of the outer loop):

```c
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
        // Some constant-time operation
    }
}
```

The number of times the inner operation is executed can be expressed by the sum:&#x20;

$$
\sum_{i=1}^{n}\sum_{j=1}^{i} 1 = \sum_{i=1}^{n} i = \frac{n(n + 1)}{2}
$$

Here is the proof of the above summation:

$$\sum_{i=1}^{n} i = 1+2+\dots+(n-1)+n$$

$$\begin{align} S = 1+2+\dots+(n-1)+n \end{align}$$

You can now use a mathematical trick to help you simplify the process of calculating the summation by writing the sum in reverse order and adding it to the original sum. This creates pairs of terms that all have the same value.

$$\begin{align}  S &= n+(n-1)+\dots+2+1 \\  S+S &= (1+n)+(2+(n-1))+\dots+((n-1)+2)+(n+1)   \end{align}$$

$$\begin{align} \Rightarrow 2S = (n+1) +(n-1+2)+\dots+(n-1+2)+(n+1) \end{align}$$

$$\Rightarrow 2S=\overbrace{(n+1)+(n+1)+...+(n+1)+(n+1)}^{\text{n times}}$$

How many times do you have `(n+1)` in `2S`? `n` times. So now, you can combine those terms `n` times as they appear and solve for `S`.

$$\begin{align} 2S &= n(n+1) \\ \Rightarrow S &= \frac{n(n+1)}{2}  \end{align}$$

The sum of the first `n` natural numbers is $$\frac{n(n+1)}{2}$$, and this proof above shows how adding the sum forwards and backward leads to the final result.

From this, we can deduce, by reducing to the most dominant term, that the algorithm has a time complexity of $$O(n^2)$$, highlighting a quadratic growth with respect to the input of size `n`.

$$
\sum_{i=1}^{n} i = \frac{n(n + 1)}{2} = \frac{1}{2}(n^{2}+n)=n^{2} =O(n^{2})
$$

> Note that the leading constant $$\frac{1}{2}$$ does not affect the change in growth between $$n^{2}$$and $$n$$. Because $$n^{2}$$ still grow faster than $$n$$ with or without any leading constant the growth is still quadriatic.

### Summation Techniques and Time Complexity

Summations can sometimes be complex, requiring certain mathematical techniques to simplify them. For example, properties of arithmetic progressions or geometric series can be utilized to find closed-form expressions of summations. Additionally, we can evaluate summations that approximate certain continuous functions by applying integration or other analytical methods.

These techniques empower developers to predict and improve the performance of their algorithms by allowing for precise analysis beyond empirical testing. As a result, theoretical knowledge in summation and mathematical series becomes invaluable in computer science, especially in the study of algorithms.

***

### Formulas for Summations

#### Common Summation Formulas in Algorithm Analysis



$$
\sum_{i=1}^{n} 1 = n = O(n)
$$

$$
\sum_{i=1}^{n} i = \frac{n(n + 1)}{2}=O(n^2)
$$

$$
\sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6}=O(n^3)
$$

$$
\sum_{i=1}^{n} i^3 = \frac{n^{2}(n+1)^{2}}{4}=O(n^4)
$$

$$
\sum_{i=m}^{n} f(i) = \sum_{i=1}^{n} f(i) - \sum_{i=1}^{m-1} f(i)
$$

Recognizing these formulas can significantly expedite the analysis process by transforming complex summations into simpler closed-form expressions.
