Data Structures and Algorithms · Unit 1 · 4 hrs
Introduction to Data Structures & Algorithms
Exam-focused notes for Introduction to Data Structures & Algorithms (Data Structures and Algorithms, CSC211): what the TU syllabus asks and how it has actually been tested, with 10 solved past questions from this unit.
Introduction to Data Structures & Algorithms in short
This chapter sets up the vocabulary the rest of the course is built on: what a data type and a data structure are, how dynamic memory lets a C program grow structures at runtime, what an algorithm is, and how asymptotic notation is used to measure and compare algorithm efficiency.
What this unit covers
- Data types, Data structure and Abstract date type
- Dynamic memory allocation in C
- Introduction to Algorithms
- Asymptotic notations and common functions
Asymptotic notations and common functions
2081 · 5 marks
Explain big oh notation in brief. Find big oh of the following function: $f(x) = 5x^4 + 9x^2 + 7x + 9$. [5]
2080 · 5 marks
What is asymptotic analysis? Explain theta notation with example. [5]
Show solved answerHide answer
Asymptotic Analysis and Theta Notation
Asymptotic Analysis
Asymptotic analysis of an algorithm refers to defining the mathematical framing of its run-time performance. It is a technique used to evaluate the performance of an algorithm in terms of input size, without measuring the actual running time.
Using asymptotic analysis, we can conclude three scenarios of an algorithm:
| Case | Description |
|---|---|
| Best Case | Minimum time required for program execution |
| Average Case | Average time required for program execution |
| Worst Case | Maximum time required for program execution |
Asymptotic analysis helps us compare algorithms independently of machine, language, or implementation details.
Theta Notation (Θ-Notation)
Theta notation is used to define the tight bound (both upper and lower bound) of an algorithm's running time. It represents the average case complexity.
Definition
A function f(n) is said to be Θ(g(n)) if there exist positive constants c1, c2, and n0 such that:
$$c_1 \cdot g(n) \leq f(n) \leq c_2 \cdot g(n) \quad \text{for all } n \geq n_0$$
This means:
- g(n) is both an upper bound and a lower bound for f(n)
- f(n) grows at the same rate as g(n)
Graphical Representation
f(n) is sandwiched between c1*g(n) and c2*g(n)
| c2*g(n)
| /
| / f(n)
| /
| / c1*g(n)
|___/________________
n0 n -->
Example
Let f(n) = 2n + 3
We want to show that f(n) = Θ(n), i.e., g(n) = n.
We need to find c1, c2, and n0 such that:
$$c_1 \cdot n \leq 2n + 3 \leq c_2 \cdot n$$
Finding c2 (upper bound):
$$2n + 3 \leq c_2 \cdot n$$
For n >= 1: $$2n + 3 \leq 2n + 3n = 5n$$
So c2 = 5 works.
Finding c1 (lower bound):
$$c_1 \cdot n \leq 2n + 3$$
Clearly: $$2n \leq 2n + 3$$
So c1 = 2 works.
Result:
$$2n \leq 2n + 3 \leq 5n \quad \text{for all } n \geq 1$$
Therefore, f(n) = 2n + 3 = Θ(n) with c1 = 2, c2 = 5, n0 = 1.
Key Points
- Theta notation gives an exact asymptotic behavior
- If f(n) = Θ(g(n)), then f(n) = O(g(n)) AND f(n) = Ω(g(n))
- It is the most precise of the three asymptotic notations
- Example: Linear search has Θ(n) average case complexity
2079 · 5 marks
Why do we need asymptotic notation? Describe about Big oh notation with its curve. [5]
Show solved answerHide answer
Asymptotic Notation and Big Oh Notation
Why Do We Need Asymptotic Notation? (2 marks)
When we analyze algorithms, we need a way to measure and compare their efficiency (time and space) without depending on machine speed, programming language, or hardware. Asymptotic notation provides a mathematical framework to describe the behavior of an algorithm as the input size grows toward infinity.
Key reasons we need asymptotic notation:
- To compare two algorithms independently of machine or implementation details.
- To describe the growth rate of an algorithm's running time or space requirement.
- To focus on the dominant term of a function and ignore constants and lower-order terms, which become insignificant for large inputs.
- To express best case, worst case, and average case complexity in a standard, concise way.
For example, if an algorithm takes T(n) = 5n² + 3n + 10 steps, asymptotic notation lets us simply say it grows as n², ignoring less significant terms.
Big Oh Notation O(g(n)) (3 marks)
Definition:
"When we have only asymptotic upper bound then we use O notation. If f and g are any two functions from set of integers then function f(x) is said to be big Oh of g(x)."
Formally:
f(n) = O(g(n)) if and only if there exist positive constants c and n₀ such that:
$$f(n) \leq c \cdot g(n) \quad \text{for all } n \geq n_0$$
- f(n) is the actual running time of the algorithm.
- g(n) is the bounding function (upper bound).
- c and n₀ are positive constants.
This means Big Oh gives the worst-case or upper bound on the growth of an algorithm.
Example
If f(n) = 4n + 2, then:
- 4n + 2 ≤ 5n for all n ≥ 2
- So f(n) = O(n), with c = 5 and n₀ = 2
Curve / Graphical Representation
Running
Time
| c * g(n)
| ./
| ./
| .../ <-- f(n) always stays
| .../ below c*g(n) after n₀
| .../
| .../
|../
|_________________________ n
n₀
- For all values of n ≥ n₀, the curve of f(n) lies at or below the curve of c * g(n).
- Big Oh notation thus represents the asymptotic upper bound.
Key Points
| Property | Description |
|---|---|
| Notation | O(g(n)) |
| Meaning | Upper bound on running time |
| Use case | Worst-case analysis |
| Condition | f(n) ≤ c * g(n) for all n ≥ n₀ |
Common Big Oh Complexities (Best to Worst)
$$O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n) < O(n!)$$
Conclusion: Big Oh notation is the most widely used asymptotic notation because it tells us the maximum time an algorithm will ever take, helping us guarantee performance in the worst case.
2078 · 5 marks
How do you find complexity of algorithms? Explain. [5]
2075 · 5 marks
What do you mean by complexity of algorithm? How do you find time complexity? [5]
Data types, Data structure and Abstract date type
2080 · 5 marks
Write short notes on: a. Abstract data type b. Circular linked list [5]
2077 · 5 marks
What is dynamic memory allocation? Compare data structure with abstract data type. [5]
Dynamic memory allocation in C
2075 · 5 marks
Write short notes on: a. Dynamic memory allocation b. Game tree [5]
Make Unit 1 stick
Practice CSC211 with flashcards & quizzes