UNIT I

 

UNIT: Algorithms & Complexity Analysis

1. Algorithm

Definition

An algorithm is a finite sequence of well-defined, unambiguous steps designed to solve a specific problem or perform a computation.

👉 In simple words:
An algorithm is a step-by-step procedure to convert input into output.

Example (Finding Maximum of Two Numbers)

  1. Start

  2. Read two numbers a and b

  3. If a > b, print a

  4. Else print b

  5. Stop

2. Characteristics of an Algorithm

A good algorithm must satisfy the following properties:

  1. Input

    • An algorithm can have zero or more inputs.

    • Inputs are externally supplied.

  2. Output

    • Produces at least one output.

    • Output is the solution to the problem.

  3. Definiteness

    • Each step must be clear and unambiguous.

    • No confusion in instructions.

  4. Finiteness

    • The algorithm must terminate after a finite number of steps.

  5. Effectiveness

    • Each operation must be simple and executable in finite time.

  6. Correctness

    • Produces the correct output for all valid inputs.

  7. Generality

    • Should solve all instances of the problem, not just a single case.

3. Complexity Analysis

Definition

Complexity analysis is the process of determining how much time and memory an algorithm requires as a function of the input size.

There are two main types:

  1. Time Complexity

  2. Space Complexity

4. Time Complexity

Definition

Time complexity measures the amount of time an algorithm takes to execute as the input size increases.

It is expressed as a function of input size n.

Cases of Time Complexity

  1. Best Case – Minimum time required

  2. Average Case – Average time taken

  3. Worst Case – Maximum time required (most important for analysis)

Example

Linear search in an array of size n:

  • Best case: O(1) (element found at first position)

  • Worst case: O(n) (element found at last position or not found)

5. Space Complexity

Definition

Space complexity measures the total memory used by an algorithm during execution.

Components of Space Complexity

  1. Fixed Part

    • Variables, constants, program code

  2. Variable Part

    • Dynamically allocated memory

    • Recursive stack space

Example

  • Iterative algorithm → less space

  • Recursive algorithm → extra stack space

6. Asymptotic Notations

Definition

Asymptotic notations describe the growth rate of time or space complexity as input size n becomes very large.

They help compare algorithms independent of machine or language.

6.1 Big-O Notation (O)

Definition

Big-O notation represents the upper bound (worst-case) time complexity of an algorithm.

Example

  • Linear search → O(n)

  • Bubble sort → O(n²)

👉 Meaning: The algorithm will not take more than this time.

6.2 Big-Omega Notation (Ω)

Definition

Big-Omega represents the lower bound (best-case) time complexity.

Example

  • Linear search (best case) → Ω(1)

  • Binary search → Ω(1)

👉 Meaning: The algorithm will take at least this much time.

6.3 Big-Theta Notation (Θ)

Definition

Big-Theta represents the tight bound, where best and worst cases grow at the same rate.

Example

  • Binary search → Θ(log n)

  • Merge sort → Θ(n log n)

👉 Meaning: Exact order of growth.

7. Common Time Complexities

ComplexityNameExample
O(1)ConstantAccessing array element
O(log n)LogarithmicBinary search
O(n)LinearLinear search
O(n log n)LinearithmicMerge sort
O(n²)QuadraticBubble sort
O(2ⁿ)ExponentialRecursive Fibonacci

8. Importance of Complexity Analysis

  • Helps select the best algorithm

  • Improves program efficiency

  • Saves time and memory

  • Essential for large-scale applications


1. Data Structure

Definition

A data structure is a way of organizing, storing, and managing data in a computer so that it can be accessed and modified efficiently.

👉 In simple words:
A data structure decides how data is stored and how operations are performed on it.

Example

  • Array → stores elements in contiguous memory

  • Linked List → stores elements using pointers

2. Types of Data Structures

2.1 Primitive Data Structures

Basic data types supported by programming languages.

Examples:

  • Integer

  • Float

  • Character

  • Boolean

2.2 Non-Primitive Data Structures

Used to store collections of data.

a) Linear Data Structures

Data elements are arranged sequentially.

Examples:

  • Array

  • Linked List

  • Stack

  • Queue

b) Non-Linear Data Structures

Data elements are arranged hierarchically or graph-like.

Examples:

  • Tree

  • Graph

  • Heap

  • Hash Table

2.3 Static vs Dynamic Data Structures

StaticDynamic
Fixed sizeSize can grow or shrink
Memory allocated at compile timeMemory allocated at run time
Example: ArrayExample: Linked List

3. Abstract Data Type (ADT)

Definition

An Abstract Data Type (ADT) is a logical description of a data structure, defining:

  • What operations are performed

  • Not how they are implemented

👉 ADT focuses on behavior, not implementation.

Examples of ADT

Stack ADT

  • Operations:

    • push() – insert element

    • pop() – remove element

    • peek() – view top element

    • isEmpty()

Queue ADT

  • Operations:

    • enqueue()

    • dequeue()

    • front()

ADT vs Data Structure

ADTData Structure
Logical modelPhysical implementation
Specifies operationsSpecifies memory structure
Language independentLanguage dependent

4. Introduction to Linked Lists

Definition

A linked list is a dynamic linear data structure where elements are stored in non-contiguous memory locations and are connected using pointers (links).

Each element is called a node.

Structure of a Node

A node consists of:

  1. Data field – stores the value

  2. Link field – stores address of next node

[ Data | Next ]

Types of Linked Lists

  1. Singly Linked List

  2. Doubly Linked List

  3. Circular Linked List

(Usually syllabus starts with Singly Linked List)

5. Representation of Linked List in Memory

Memory Representation

  • Nodes are stored anywhere in memory

  • Each node points to the next node

  • First node is pointed by HEAD

  • Last node points to NULL

HEAD → [10|•] → [20|•] → [30|NULL]

Key Points

  • Memory is allocated dynamically

  • No need for contiguous memory

  • Insertion and deletion are efficient

6. Linked List vs Array

Comparison Table

FeatureArrayLinked List
Memory allocationContiguousNon-contiguous
SizeFixedDynamic
AccessRandom accessSequential access
InsertionCostlyEfficient
DeletionCostlyEfficient
Memory usageLess overheadExtra memory for pointers
Cache friendlyYesNo

7. Advantages of Linked List

  • Dynamic size

  • Efficient insertion and deletion

  • No memory wastage due to fixed size

8. Disadvantages of Linked List

  • Extra memory for pointers

  • No direct access to elements

  • More complex implementation

9. Applications of Linked List

  • Implementation of stacks and queues

  • Dynamic memory allocation

  • Polynomial manipulation

  • Graph representation (Adjacency list)

10. Summary

  • Data structures organize data efficiently

  • ADT defines what operations are needed

  • Linked list is a dynamic data structure

  • Arrays are faster for access; linked lists are better for insertion/deletion

Comments

  1. Ruby assignments can be challenging when students are learning programming logic, syntax, and application development at the same time. Many learners find ruby assignment help experts useful for understanding difficult coding concepts and debugging problems. Good guidance focuses on explaining why a particular solution works rather than simply providing code. This approach helps students strengthen their programming skills, understand Ruby more effectively, and become more confident when handling future development tasks independently.

    ReplyDelete

Post a Comment