Practical Viva Voce 1
Practical Viva Voce 1
Definition of Algorithm:
Characteristics of Algorithms:
1. Finiteness: An algorithm must always terminate after a finite number of steps. It should not go into an infinite loop.
2. Definiteness: Each step of the algorithm must be clear and unambiguous. The operations to be performed must be
precisely defined.
3. Input: An algorithm should have zero or more inputs, which are the data given to the algorithm for processing.
4. Output: An algorithm should have one or more outputs, which are the results of the processing of the inputs.
5. Effectiveness: The steps in the algorithm should be basic enough to be carried out, in principle, by a human using a
pen and paper. They should be realizable.
6. Generality: An algorithm should solve a general class of problems, not just a specific problem instance. It should be
applicable to different sets of input values.
Definition of Stack:
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. In a stack, the last element added to
the stack is the first one to be removed. It's analogous to a stack of plates where you can only take the top plate off the
stack.
Operations on Stacks:
3. Peek/Top: Returns the top element of the stack without removing it.
5. isFull: Checks if the stack is full (in case of a bounded stack with limited capacity).
- Each node contains data and a reference (or pointer) to the next node in the sequence.
- Traversal is possible only in one direction, from the head (starting node) to the end.
- Each node contains data, a reference to the next node, and a reference to the previous node.
- Similar to a singly linked list, but the last node points back to the first node, forming a circle.
- Can be either singly or doubly circular linked, enabling traversal to loop back to the starting point.
- Each node has references to both the next and previous nodes, and the last node points to the first node, forming a
circle.
Graph traversal refers to the process of visiting all the nodes or vertices in a graph. The primary graph traversal
techniques are:
- Suitable for solving problems related to connectivity, pathfinding, and cycle detection.
- Explores all the nodes at the present depth level before moving on to the nodes at the next depth level.
- Suitable for finding the shortest path in unweighted graphs and for level-order traversal.
| Size | Fixed size (static array) or dynamic size (dynamic array, but resizing is costly) | Dynamic size, can grow or
shrink during runtime |
| Access Time | O(1) – Direct access using index | O(n) – Sequential access, requires traversal |
| Memory Utilization | May have unused memory (if array size is overestimated) | Memory is used as needed, efficient
for varying sizes |
| Insertion/Deletion | Costly, O(n) for insertion/deletion (requires shifting elements) | Efficient, O(1) if
insertion/deletion is at the beginning; O(n) for general case |
| Search | O(n) for unsorted arrays; O(log n) for sorted arrays (using binary search) | O(n) – Requires traversal, no
direct access |
| Overhead | No overhead, except for unused space | Extra memory is needed for storing pointers |
| Use Cases | Best for scenarios where quick access is needed and the size is known in advance | Best for scenarios
where the size of the data structure changes frequently and quick insertions/deletions are needed |
These differences highlight when to use arrays or linked lists based on the problem requirements and constraints.