0% found this document useful (0 votes)
7 views

Assignment_DAA_Final

The document outlines the characteristics of algorithms, including input, output, finiteness, definiteness, and effectiveness. It discusses various types of algorithm analysis, such as worst-case, best-case, average-case, and amortized analysis, along with algorithm complexity in terms of time and space. Additionally, it covers data structures like arrays, stacks, and lists, their features, advantages, disadvantages, applications, and provides examples of algorithms and their operations.

Uploaded by

Shaista Zarrin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Assignment_DAA_Final

The document outlines the characteristics of algorithms, including input, output, finiteness, definiteness, and effectiveness. It discusses various types of algorithm analysis, such as worst-case, best-case, average-case, and amortized analysis, along with algorithm complexity in terms of time and space. Additionally, it covers data structures like arrays, stacks, and lists, their features, advantages, disadvantages, applications, and provides examples of algorithms and their operations.

Uploaded by

Shaista Zarrin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment No.

1: Design and Analysis of Algorithms (DAA)

### Characteristics of an Algorithm

1. **Input**:

An algorithm can take zero or more inputs. These inputs are usually provided at the beginning of

the algorithm to process the desired computation. For example, in an algorithm to sort numbers, the

unsorted list is the input.

2. **Output**:

The algorithm should produce at least one output as the result of the process. The output must

relate to the problem being solved. For instance, a sorting algorithm will output a sorted list.

3. **Finiteness**:

An algorithm must complete its task in a finite number of steps. It cannot run indefinitely and must

terminate after a limited time.

4. **Definiteness**:

Each step in the algorithm must be precisely defined and clear. This ensures there is no ambiguity

in the logic, allowing any person or machine to follow the process without confusion.

5. **Effectiveness**:

All operations in the algorithm must be simple enough to be executed within a reasonable time

using basic computational resources.

### Types of Analysis in Algorithms

1. **Worst-Case Analysis**:

This evaluates the maximum amount of time an algorithm takes to complete, considering the least

favorable input scenario. It is useful for ensuring the algorithm works efficiently under any condition.

*Example*: Searching for an element in an unsorted array may take O(n) in the worst case.

2. **Best-Case Analysis**:
This measures the minimum time an algorithm takes, considering the most favorable input

scenario. While it highlights the best performance, it is less reliable for practical use.

*Example*: Finding an element in the first position of a list takes O(1).

3. **Average-Case Analysis**:

This evaluates the expected performance of an algorithm across all possible inputs. It considers

the distribution of input cases and is more practical for understanding typical algorithm behavior.

*Example*: Searching an element in a randomly distributed array takes O(n/2).

4. **Amortized Analysis**:

This focuses on the average performance of a sequence of operations, rather than individual

operations. It is often used for dynamic data structures where the cost of some operations might

vary.

*Example*: Inserting elements into a dynamic array has an amortized time complexity of O(1)

despite occasional costly resizing operations.

### Algorithm Complexity

**Algorithm complexity** measures the resources required by an algorithm to solve a problem, such

as **time** (execution speed) and **space** (memory usage).

- **Time Complexity**: The amount of time taken by an algorithm to complete based on the size of

the input. Expressed using Big-O notation (e.g., O(n), O(n^2)).

- **Space Complexity**: The amount of memory required to run the algorithm, including input

storage, temporary variables, and additional data structures.

Complexity helps in comparing algorithms and determining their feasibility for practical use.

### Short Note on Arrays

An **array** is a linear data structure that stores a collection of elements of the same data type in

contiguous memory locations. Each element in an array can be accessed directly using its index,

which makes arrays very efficient for tasks like searching or modifying elements.
**Key Features**:

- **Indexing**: Elements are accessed by their index, starting from 0.

- **Fixed Size**: The size of an array must be defined at the time of its declaration and cannot be

changed later.

**Advantages**:

1. **Random Access**: Accessing elements is fast and takes O(1) time.

2. **Simple Implementation**: Arrays are easy to declare and use.

**Disadvantages**:

1. **Fixed Size**: Limited flexibility as the size cannot be changed once declared.

2. **Inefficient Insertion/Deletion**: Operations like insertion or deletion require shifting elements,

which takes O(n) time in the worst case.

**Applications**:

- Used to implement other data structures like stacks, queues, and matrices.

- Ideal for storing and processing datasets like temperature readings, marks, etc.

### Stack

A **stack** is a linear data structure that follows the **LIFO** (Last In, First Out) principle. The last

element added to the stack is the first to be removed.

**Operations on a Stack**:

1. **Push**: Add an element to the top.

2. **Pop**: Remove the top element.

3. **Peek/Top**: Retrieve the top element without removing it.


4. **IsEmpty**: Check if the stack is empty.

**Applications**:

- Expression evaluation and conversion (e.g., infix to postfix).

- Backtracking algorithms (e.g., maze solving).

- Managing function calls in recursion.

### Asymptotic Notations

1. **Big-O (O)**: Represents the upper bound of an algorithm's running time. It defines the

worst-case complexity.

*Example*: O(n^2) for a nested loop.

2. **Omega (Omega)**: Represents the lower bound of an algorithm's running time. It defines the

best-case complexity.

*Example*: Omega(n).

3. **Theta (Theta)**: Represents the tight bound of an algorithm, where both upper and lower

bounds are the same.

*Example*: Theta(n log n).

These notations describe the performance of algorithms as the input size grows, ignoring constants

and lower-order terms.

### Data Structure

A **data structure** is a way of organizing and storing data to perform operations efficiently. It

defines the relationship between data elements and how they can be processed.

**Types of Data Structures**:

1. **Linear**: Arrays, Lists, Stacks, Queues.

2. **Non-Linear**: Trees, Graphs.


### Short Note on List

A **list** is a collection of ordered elements, which can be of the same or different data types. Unlike

arrays, lists are dynamic, meaning they can grow or shrink during runtime, making them more

flexible.

**Features**:

1. **Dynamic Size**: Can resize itself automatically.

2. **Non-Contiguous Memory Allocation**: Elements are not stored in consecutive memory

locations, unlike arrays.

### Features of an Algorithm

- **Scalability**: Works efficiently for varying input sizes.

- **Generality**: Solves a broad range of problems.

- **Efficiency**: Optimizes time and space.

- **Reliability**: Produces correct results.

### Comparing Algorithms

Algorithms are compared based on:

1. **Time Complexity**: How fast the algorithm executes.

2. **Space Complexity**: Memory usage during execution.

3. **Scalability**: Behavior with increasing input size.

4. **Implementation Ease**: How easy it is to implement and maintain.

### Applications of Data Structures

1. **Stacks**: Expression evaluation, recursion.

2. **Queues**: Scheduling, buffering.


3. **Trees**: Hierarchical data, file systems.

4. **Graphs**: Networks, shortest path algorithms.

5. **Hash Tables**: Database indexing, caching.

### Tower of Hanoi

The **Tower of Hanoi** is a mathematical puzzle involving three rods and n disks of different sizes.

The goal is to move all disks from one rod to another while adhering to these rules:

1. Only one disk can be moved at a time.

2. Larger disks cannot be placed on smaller disks.

3. A disk can only be moved to the top of another rod.

The minimum number of moves required is 2^n - 1.

### Definition of Algorithm and Example

An **algorithm** is a step-by-step procedure to solve a problem or perform a computation.

**Algorithm to Add Two Numbers**:

1. Start.

2. Take two inputs: a and b.

3. Compute c = a + b.

4. Display c.

5. Stop.

### Postfix Conversion of Expression

Given: a + b * c + d.

**Postfix Form**: a b c * + d +.
Steps:

1. Process b * c as b c *.

2. Add a, resulting in a b c * +.

3. Add d, yielding a b c * + d +.

### Running Time Analysis

Running time analysis determines how the execution time of an algorithm depends on the size of

the input. It is expressed as a function T(n), where n is the input size.

### Stack Operations

1. **Push**: Adds an element to the stack.

*Example*: Push(5).

2. **Pop**: Removes and returns the top element.

*Example*: Pop() -> 5.

3. **Peek**: Returns the top element without removing it.

*Example*: Peek() -> 5.

4. **IsEmpty**: Checks if the stack is empty.

You might also like