2 Algorithm Analysis And Complexity

Data Structures and Algorithms Ā· Unit 2

Algorithm Analysis and Complexity

Exam-focused notes for Algorithm Analysis and Complexity (Data Structures and Algorithms, BIT201): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

Algorithm Analysis and Complexity in short

There are often many ways to solve one problem. Algorithm analysis lets us compare them by the time and memory they need, before we run any code. This chapter explains how we measure and describe that cost.

Algorithm Analysis and Complexity: notes

Definition of algorithm analysis

An algorithm is a finite list of clear steps that solves a problem. Algorithm analysis is the study of how much time and memory an algorithm needs as the input size grows.

We do not measure seconds on a real machine, because that depends on the computer and the language. Instead we count the basic operations as a function of the input size $n$. This gives a result that is true on any machine.

Analysis is done for three cases:

  • Best case: the input that makes the algorithm fastest.
  • Worst case: the input that makes it slowest. This is the most used case because it is a safe limit.
  • Average case: the expected cost over all inputs.

For linear search in an array of $n$ items, the best case is 1 comparison (item at the front). The worst case is $n$ comparisons (item at the end or missing).

Asymptotic analysis fundamentals

Asymptotic analysis studies how the cost grows when $n$ becomes very large. Small terms and constants matter little for large $n$, so we ignore them.

For example, if an algorithm does $3n^2 + 5n + 7$ operations, then for large $n$ the term $n^2$ dominates. We say the growth is of order $n^2$.

Common growth rates, from slowest to fastest growing:

$$O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(n^3) < O(2^n) < O(n!)$$

NotationNameExample
$O(1)$constantreading a[i]
$O(\log n)$logarithmicbinary search
$O(n)$linearlinear search
$O(n \log n)$linearithmicmerge sort
$O(n^2)$quadraticbubble sort
$O(2^n)$exponentialTower of Hanoi moves

What this unit covers

  • Definition of algorithm analysis
  • Big O notation with examples
  • Omega notation with examples
  • Time complexity definition and measurement
  • Space complexity definition and measurement
  • Asymptotic analysis fundamentals

Time complexity definition and measurement

2082 Ā· 5 marks

Define time and space complexity. Discuss about Round Robin Algorithm for MST. [1+4]

Show solved answer

Time and Space Complexity + Round Robin Algorithm for MST


(a) Time and Space Complexity

Time Complexity

Time complexity is a measure of the amount of time (number of basic operations) an algorithm takes to complete as a function of the input size n. It describes how the running time grows with increasing input.

Example: Linear search has time complexity O(n).

Space Complexity

Space complexity is a measure of the amount of memory space an algorithm requires as a function of the input size n. It includes both auxiliary space and space used by the input.

Example: An algorithm storing an n x n matrix has space complexity O(n²).


(b) Round Robin Algorithm for MST

Background

The Round Robin (also called Boruvka's Algorithm) is one of the oldest algorithms for finding a Minimum Spanning Tree (MST) of a weighted, connected, undirected graph.

  • Proposed by Otakar Boruvka in 1926.
  • It works by simultaneously growing multiple components and merging them.

Key Idea

In each round (phase), every component (initially each vertex is its own component) selects the minimum weight edge that connects it to a different component. All such edges are added to the MST, and components are merged.


Algorithm Steps

Input: Graph G = (V, E) with weighted edges
Output: Minimum Spanning Tree T

1. Initialize each vertex as its own component.
   T = {} (empty set of MST edges)

2. Repeat until only one component remains:
   a. For each component C:
      - Find the minimum weight edge (u, v) such that
        u ∈ C and v āˆ‰ C (cheapest outgoing edge).
   
   b. Add all such selected edges to T
      (avoid duplicates if two components select the same edge).
   
   c. Merge the connected components.

3. Return T as the MST.

Example

Consider the graph:

    4       2
A ------B ------C
|       |       |
6|      |3      |1
|       |       |
D ------E ------F
    5       8

Edges: A-B(4), B-C(2), A-D(6), B-E(3), C-F(1), D-E(5), E-F(8)

Round 1: Each vertex is its own component {A},{B},{C},{D},{E},{F}

ComponentCheapest Edge
AA-B (4)
BB-C (2)
CC-F (1)
DD-E (5)
EB-E (3)
FC-F (1)

Selected edges (removing duplicates): C-F(1), B-C(2), B-E(3), A-B(4), D-E(5)

After merging: {A,B,C,E,F} and {D} merge further -> one component.

MST edges: C-F, B-C, B-E, A-B, D-E
Total Weight: 1+2+3+4+5 = 15


Complexity Analysis

MeasureValue
Time ComplexityO(E log V)
Space ComplexityO(V + E)
  • Each phase reduces the number of components by at least half.
  • So there are at most O(log V) phases.
  • Each phase scans all E edges.

Advantages

  • Naturally parallelizable (all components work simultaneously).
  • Efficient for dense graphs.
  • Works well in distributed computing environments.

Summary

Boruvka's Round Robin Algorithm builds the MST by repeatedly selecting the minimum cost edge from each component and merging components until a single spanning tree is formed, achieving O(E log V) time complexity.

Open this answer on its own page →

2079 Ā· 5 marks

What is time complexity? Explain big oh notation with example. [5]

Time complexity is a measure of the amount of time (or number of basic operations) an algorithm takes to complete as a function of the size of its input n. - It does not measure actual clock time, but rather the growth rate of operations relative to input size. - It helps compare algorithms and p...

Open this answer on its own page →

Space complexity definition and measurement

2080 Ā· 5 marks

What is space complexity? Explain omega notation with example. [5]

--- Space complexity is the amount of memory space required by an algorithm to run as a function of the input size n. It includes: - Instruction space - space required to store the compiled version of the program instructions. - Data space - space required to store constants, variables, and dynam...

Open this answer on its own page →

Big O notation with examples

2078 Ā· 5 marks

Explain big oh(O) notation with suitable example. [5]

Big Oh notation (O) is used to describe the upper bound of an algorithm's running time or space complexity. It represents the worst-case scenario of an algorithm's growth rate. Formally, a function f(n) = O(g(n)) if and only if there exist positive constants c and nā‚€ such that: f(n) ≤ c Ā· g(n) fo...

Open this answer on its own page →

Definition of algorithm analysis

0 Ā· 5 marks

Write short notes on a.) Analysis of Algorithm Write short notes on b.) Representation of Graph [2.5+2.5]

Analysis of Algorithm refers to the process of evaluating the efficiency of an algorithm in terms of the resources it consumes, primarily time and space. - To predict the behavior of an algorithm before implementation. - To compare multiple algorithms solving the same problem. - To choose the mos...

Open this answer on its own page →