Assignment_DAA_Final
Assignment_DAA_Final
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
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
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
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.
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.
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)
**Algorithm complexity** measures the resources required by an algorithm to solve a problem, such
- **Time Complexity**: The amount of time taken by an algorithm to complete based on the size of
- **Space Complexity**: The amount of memory required to run the algorithm, including input
Complexity helps in comparing algorithms and determining their feasibility for practical use.
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**:
- **Fixed Size**: The size of an array must be defined at the time of its declaration and cannot be
changed later.
**Advantages**:
**Disadvantages**:
1. **Fixed Size**: Limited flexibility as the size cannot be changed once declared.
**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
**Operations on a Stack**:
**Applications**:
1. **Big-O (O)**: Represents the upper bound of an algorithm's running time. It defines the
worst-case complexity.
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
These notations describe the performance of algorithms as the input size grows, ignoring constants
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.
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**:
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. Start.
3. Compute c = a + b.
4. Display c.
5. Stop.
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 determines how the execution time of an algorithm depends on the size of
*Example*: Push(5).