1 Introduction To Data Structures Algorithms

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.

Introduction to Data Structures & Algorithms: notes

Data Types, Data Structure and Abstract Data Type

A data type is an attribute of a variable that tells the compiler what kind of value it can hold, how much memory it occupies, and what operations are legal on it. C provides a small set of basic (primitive) data types: int for whole numbers, char for a single character (usually 1 byte), float for single precision decimal numbers (usually 4 bytes) and double for double precision decimal numbers (usually 8 bytes, giving roughly twice the precision of float). Alongside these, C lets a programmer build derived or user-defined data types: an array groups values of the same basic type under one name, a structure (struct) groups values of possibly different types into a single record, a union is similar to a structure except all its members share the same memory location so only one member holds a valid value at a time, and an enumeration (enum) assigns readable names to a set of integer constants. C++ adds the class, a user-defined type that bundles data members with the functions that operate on them.

A data structure is a way of organizing a collection of data items so that they can be stored and processed efficiently. Instead of holding a student's name and age as two unrelated variables, a data structure groups them into one record, for example a struct Student { char name[30]; int age; };, so the pair can be created, stored and retrieved as a single unit. Using an appropriate data structure reduces the complexity of a program, increases its efficiency, and gives clear internal organization to otherwise unrelated data. This relationship is often summarised as

$$\text{Algorithm} + \text{Data Structure} = \text{Program}$$

An abstract data type (ADT) is a mathematical model of a data type defined purely by the values it can hold and the operations allowed on those values, without saying how either is implemented. A stack, for example, is an ADT: its contract is "insert and remove from one end only", and that contract can be implemented equally well with an array or a linked list. Programming to an ADT rather than to a concrete representation gives modularity (the implementation can change without touching the code that uses it), information hiding (callers cannot depend on internal details they should not see), implementation independence, and precise specifications of what each operation guarantees.

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]

Big Oh notation is an asymptotic notation used to describe the upper bound on the growth rate of a function. In algorithm analysis, it tells us the worst-case rate at which the running time or space requirement of an algorithm grows as the input size increases. For two functions $f(x)$ and $g(x)$...

Open this answer on its own page →

2080 · 5 marks

What is asymptotic analysis? Explain theta notation with example. [5]

Show solved 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:

CaseDescription
Best CaseMinimum time required for program execution
Average CaseAverage time required for program execution
Worst CaseMaximum 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
Open this answer on its own page →

2079 · 5 marks

Why do we need asymptotic notation? Describe about Big oh notation with its curve. [5]

Show solved 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

PropertyDescription
NotationO(g(n))
MeaningUpper bound on running time
Use caseWorst-case analysis
Conditionf(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.

Open this answer on its own page →

2078 · 5 marks

How do you find complexity of algorithms? Explain. [5]

The complexity of an algorithm is a measure of the amount of time and/or space required by an algorithm as a function of the size of the input. It helps us evaluate and compare algorithms in terms of their efficiency. --- Time complexity measures the amount of time an algorithm takes to complete ...

Open this answer on its own page →

2075 · 5 marks

What do you mean by complexity of algorithm? How do you find time complexity? [5]

The complexity of an algorithm f(n) gives the running time and/or the storage space required by the algorithm in terms of n, where n is the size of the input data. It is used to measure the efficiency of an algorithm. There are two main factors that decide the efficiency of an algorithm X: Factor...

Open this answer on its own page →

2074 · 5 marks

Describe the Big 'O' notation. [5]

Big O notation is a mathematical notation that describes the limiting behaviour of a function when the argument tends towards a particular value or infinity. In computer science, Big O notation is used to classify algorithms according to how their running time or space requirement grows as the in...

Open this answer on its own page →

Data types, Data structure and Abstract date type

2080 · 5 marks

Write short notes on: a. Abstract data type b. Circular linked list [5]

Definition: An Abstract Data Type (ADT) is a type or a class for objects whose behaviour is defined by a set of values and a set of operations. It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations. Key Points: - ADT focuses on w...

Open this answer on its own page →

2077 · 5 marks

What is dynamic memory allocation? Compare data structure with abstract data type. [5]

--- Dynamic Memory Allocation is the process of allocating memory to variables or data structures at runtime (during program execution) rather than at compile time. In static memory allocation, the size of memory is fixed and determined before the program runs. In contrast, dynamic memory allocat...

Open this answer on its own page →

2074 · 5 marks

Differentiate between structure and union. [5]

Structure: A structure is a user-defined data type in C/C++ that creates a data type used to group items of possibly different types into a single type. Each member of a structure has its own separate memory location. Union: A union is also a user-defined data type similar to structure. However, ...

Open this answer on its own page →

Dynamic memory allocation in C

2075 · 5 marks

Write short notes on: a. Dynamic memory allocation b. Game tree [5]

--- Dynamic memory allocation is the process of allocating memory to variables and data structures at runtime (during program execution), rather than at compile time. It is a key component of the variable part of space complexity, meaning the memory required depends on the size and nature of the ...

Open this answer on its own page →