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.
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 answerHide 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}
| Component | Cheapest Edge |
|---|---|
| A | A-B (4) |
| B | B-C (2) |
| C | C-F (1) |
| D | D-E (5) |
| E | B-E (3) |
| F | C-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
| Measure | Value |
|---|---|
| Time Complexity | O(E log V) |
| Space Complexity | O(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.
2079 Ā· 5 marks
What is time complexity? Explain big oh notation with example. [5]
Space complexity definition and measurement
2080 Ā· 5 marks
What is space complexity? Explain omega notation with example. [5]
Big O notation with examples
2078 Ā· 5 marks
Explain big oh(O) notation with suitable example. [5]
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]
Make Unit 2 stick
Practice BIT201 with flashcards & quizzes