Algorithms Flashcards

1
Q

Algorithm Definition

A

A step-by-step set of instructions or a well-defined computational procedure that takes an input, processes it, and produces an output in a finite number of steps. It’s a precise, unambiguous sequence of operations or a method for solving a particular problem or accomplishing a specific task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Use cases of Stacks in Algorithms

A
  • Expression Evaluation:
    • Stacks are commonly used to evaluate arithmetic expressions, including infix, postfix, and prefix notations. They can handle the precedence of operators and parentheses efficiently.
  • Balanced Parentheses Checking:
    • Stacks are used to check for balanced parentheses in expressions, ensuring that opening and closing parentheses, brackets, and braces are matched correctly.
  • Function Call and Recursion:
    • During function calls and recursion in programming, stacks are used to keep track of the calling sequence and local variables for each function, facilitating proper return values and memory management.
  • Depth-First Search (DFS):
    • In graph traversal algorithms like DFS, a stack is used to keep track of the vertices to visit, allowing the traversal to go deeper into the graph before backtracking.
  • Backtracking Algorithms:
    • Stacks are employed in backtracking algorithms, such as depth-first search for solving problems like the N-Queens puzzle and solving mazes.
  • Infix to Postfix Conversion:
    • Stacks play a crucial role in converting infix expressions to postfix notation, allowing for efficient evaluation of mathematical expressions.
  • Web Browser History:
    • Stacks are used to implement the back and forward buttons in web browsers, maintaining the history of visited pages.
  • Tower of Hanoi:
    • Stacks are used to implement the Tower of Hanoi problem, a classic problem in computer science and mathematics.
  • Parsing and Syntax Analysis:
    • Stacks are used in parsing and syntax analysis during the compilation process of programming languages to ensure proper syntax and construct parsing trees.
  • Algorithm Simulations:
    • Stacks are used to simulate algorithms and processes, such as simulating function call stacks or evaluating the postfix form of expressions.
  • Undo Functionality:
    • Stacks can be used to implement undo functionality in applications, allowing users to revert actions in a Last-In-First-Out manner.
  • Operating System Call Stack:
    • Stacks are used in operating systems to keep track of the execution context of processes and manage function calls.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Linear Search

A

Linear search, also known as sequential search, is a simple and straightforward search algorithm.
It starts at the beginning of the data collection and examines each element one by one until it finds the target element or reaches the end of the collection.
Linear search is typically used for unsorted or randomly ordered data because it does not rely on any specific order of elements.
It has a time complexity of O(n), where ‘n’ is the number of elements in the collection. This means that in the worst case, it may need to check all elements to find the target.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Binary Search

A

Binary search is a more efficient search algorithm, but it requires that the data collection be sorted in ascending or descending order.
It works by repeatedly dividing the search space in half, eliminating half of the elements at each step, until the target element is found or it’s determined that the element is not in the collection.
Binary search has a time complexity of O(log n), which makes it much faster than linear search for large datasets. It is a divide-and-conquer algorithm.
Binary search is most effective when dealing with ordered data, such as sorted arrays or lists. It is not suitable for unsorted data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Situations in which a linear search is a good choice

A

1) No Sorting Required: If your data is not sorted, or the cost of sorting it is high, then a linear search is the only viable option. Linear search can be used on unsorted data without any preprocessing.

2) Simplicity: Linear search is a straightforward algorithm that is easy to understand and implement. It doesn’t require complex logic or additional data structures, making it suitable for simple applications or when efficiency is not a primary concern.

3) Small Datasets: For very small datasets, the performance difference between linear search and binary search may not be significant. In such cases, the simplicity of a linear search can outweigh the efficiency gains of binary search.

4) Dynamic Data: If the data is frequently updated or modified, maintaining a sorted order can be challenging. In such cases, performing binary search on a constantly changing dataset might not be practical, and a linear search can be a more flexible choice.

5) Reduced Memory Usage: Binary search often requires additional memory for maintaining the search range or recursion stack. In contrast, linear search typically doesn’t require additional memory beyond the variables for iteration, making it memory-efficient.

6) Specific Use Cases: In some specific use cases, a linear search may be more suitable, especially if the data distribution or access patterns match its characteristics. For example, in some simple algorithms or educational scenarios, linear search may be preferred to teach basic search concepts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Most popular data structures used in search algorithms

A

1) Arrays: Arrays are often used for linear search algorithms. Although linear search has a time complexity of O(n), where n is the number of elements, arrays are straightforward and efficient for small datasets or unsorted data.

2) Binary Search Trees (BSTs): BSTs are used for binary search algorithms. They are particularly efficient for searching in sorted data. The search operation in BSTs has an average time complexity of O(log n) for balanced trees, where n is the number of nodes.

3) Hash Tables: Hash tables, which use a hash function to map keys to their associated values, provide fast average-case time complexity for search operations. In ideal circumstances, they offer constant-time complexity O(1) for search, but this can vary based on the quality of the hash function and potential collisions.

4) Heaps: Heaps, especially binary heaps, are used in priority queue implementations. While not directly used for search operations like binary search, heaps efficiently retrieve the highest or lowest priority element in constant time and are useful for priority-based searches.

5) Graphs: Graphs are used in search algorithms such as depth-first search (DFS) and breadth-first search (BFS). These algorithms traverse through the nodes of a graph to find a specific node, to discover paths, or to perform other graph-related searches.

6) Tries (Prefix Trees): Tries are used for fast prefix-based searches in strings or keys. They are commonly used in applications such as autocomplete and spell-checking systems.

7) Red-Black Trees, AVL Trees, and other balanced trees: These self-balancing trees are used for efficient search operations, especially when maintaining a balanced structure is critical.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly