PROJECTENGRAM

[HO] 01 - INTRO

Intro

Tags: #◇Computer Science #◇CCDSALG #◇Intro #◇Conceptual #◇M1


◇Overview

An algorithm is any well-defined computational procedure that takes value(s) as input and produces some value(s) as output in a finite amount of time. The main design issues to consider are correctness and efficiency (performance in terms of time and space).

Analyzing an algorithm involves studying its behavior to predict its performance. This includes understanding what happens as the input size gets larger and how much resources (time and memory) are used. The purpose of this analysis is to predict the behavior of algorithms without implementing them, and to compare different algorithms to determine the best one for a given purpose. While exact prediction is not possible due to numerous factors, the analysis provides a useful approximation.

There are two primary methods for analysis:

  • A Posteriori Analysis: An empirical analysis where the program is implemented using a programming language, executed on an actual machine, and exact time and space usage are measured.
  • A Priori Analysis: A theoretical analysis where the program is analyzed assuming all factors are constant and independent of specific implementation details.

Assumptions for A Priori Analysis (RAM Model)

A Priori Analysis relies on the Random-Access Machine (RAM) model with the following assumptions:

  • Algorithms are implemented as computer programs.
  • They run on a generic, single-processor, random-access machine model.
  • Instructions are executed sequentially, one at a time (no concurrent operations).
  • Each instruction or data access takes a constant amount of time.
  • The RAM model contains standard instructions commonly found in real computers.

Order of Growth

The rate of growth, or order of growth, of an algorithm is an approximation of the time required by the algorithm as the input size increases.

  • Input size usually refers to the number of items in the input.
  • Running time is the number of instructions or data accesses executed, independent of a specific computer but within the RAM model framework. The running time T(n)T(n) for an input size nn is the sum of the running times of each statement executed.
  • It considers only the leading term of the running time polynomial (e.g., in an2+bn+can^2 + bn + c, it focuses on an2an^2).

◇Important Formulas

Running time T(n)T(n) is represented by order of growth classes (Big-O notation). If an algorithm takes t1t_1 time for input size n1n_1, you can estimate the time t2t_2 for a larger input size n2n_2 using ratios, depending on the complexity class:

  • Constant: O(1)O(1) →t2=t1\rightarrow t_2 = t_1
  • Logarithmic: O(log⁡2n)O(\log_2 n) →t2=t1×log⁡2n2log⁡2n1\rightarrow t_2 = t_1 \times \frac{\log_2 n_2}{\log_2 n_1}
  • Linear: O(n)O(n) →t2=t1×n2n1\rightarrow t_2 = t_1 \times \frac{n_2}{n_1}
  • Linearithmic: O(nlog⁡2n)O(n \log_2 n) →t2=t1×n2log⁡2n2n1log⁡2n1\rightarrow t_2 = t_1 \times \frac{n_2 \log_2 n_2}{n_1 \log_2 n_1}
  • Quadratic: O(n2)O(n^2) →t2=t1×(n2n1)2\rightarrow t_2 = t_1 \times \left(\frac{n_2}{n_1}\right)^2
  • Cubic: O(n3)O(n^3) →t2=t1×(n2n1)3\rightarrow t_2 = t_1 \times \left(\frac{n_2}{n_1}\right)^3
  • Exponential: O(2n)O(2^n) →t2=t1×2n22n1\rightarrow t_2 = t_1 \times \frac{2^{n_2}}{2^{n_1}}

◇Worked Examples

Example 1: Scaling Input Size
If an algorithm takes 1 second to run a problem with an input size of 88 (n1=8,t1=1n_1=8, t_1=1), what is the approximate time for that algorithm to run a problem with an input size of 1616 (n2=16n_2=16)?

  • O(1)O(1): T(16)=1T(16) = 1 second
  • O(log⁡2n)O(\log_2 n): T(16)=1×log⁡216log⁡28=43T(16) = 1 \times \frac{\log_2 16}{\log_2 8} = \frac{4}{3} seconds
  • O(n)O(n): T(16)=1×168=2T(16) = 1 \times \frac{16}{8} = 2 seconds
  • O(nlog⁡2n)O(n \log_2 n): T(16)=1×16log⁡2168log⁡28=16×48×3=6424=83T(16) = 1 \times \frac{16 \log_2 16}{8 \log_2 8} = \frac{16 \times 4}{8 \times 3} = \frac{64}{24} = \frac{8}{3} seconds
  • O(n2)O(n^2): T(16)=1×(168)2=22=4T(16) = 1 \times \left(\frac{16}{8}\right)^2 = 2^2 = 4 seconds
  • O(n3)O(n^3): T(16)=1×(168)3=23=8T(16) = 1 \times \left(\frac{16}{8}\right)^3 = 2^3 = 8 seconds
  • O(2n)O(2^n): T(16)=1×21628=28=256T(16) = 1 \times \frac{2^{16}}{2^8} = 2^8 = 256 seconds

◇References & Navigation

[HO] 01 - Intro | PROJECT ENGRAM