Algorithms Flashcards

1
Q

What is Breadth-First Search (BFS)

A

A graph traversal algorithm that explores all neighbors of a node before moving on to the next level of neighbors.

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

Space and Time Complexity of BFS?

A

O ( V + E )

O ( V ) - Each node is visited once
O ( E ) - Each edge is explored once

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

Key Steps of BFS

A
  1. Initialization
    - Queue containing starting node
    - Mark the starting node as visited
  2. Main Loop
    - While the queue is not empty..
    – 1. dequeue front node of queue
    – 2. process current node
    – 3. for each unvisited neighbor of current node, A. mark neighbor visited, B. add neighbor to queue
  3. Termination
    - The algo ends when all reachable nodes have been visited
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is DFS ( Depth First Search ) ?

A

A graph traversal algorithm that explores each branch before backtracking.

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

DFS Space/Time complexity

A

O ( E + V )

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

Key Steps of DFS

A
  1. Start at a node ( Root or Arbitrary
  2. Mark Node as visited
  3. Explore Neighbors ( recursively, with same method )
  4. Backtrack, when all neighbors have been visited, explore other unexplored paths.

IE. The way water might fill a hollowed out root system. The water travels to the absolute depth first, and then begins to fill in the branches from the furthest bottom first working its way to the top.

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

What is Sliding Window Algorithm?

A

Technique used for finding a contiguous subarray (or sublist) of a fixed size in an array.

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

What is the Space/Time complexity of the Sliding Window Algorithm?

A

Time

O(n) traverses once for each shift.

Space

O(1) No additional space is required besides the initial input

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

Key Steps of the Sliding Window Algorithm?

A

Initialization :
- Setup the first window by [ performing window function ]

Sliding: Add the next element, remove the last element to keep window fixed size.

Update Result After each slide, update the result (min, max, etc. )

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

What is the Quick Sort Algo?

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

What are the Key Steps of the Quick Sort

A
  1. Picking a pivot element from the array.
  2. Partitioning the remaining elements into two groups: those less than the pivot and those greater than the pivot.
  3. Recursively sorting both groups.
  4. Finally, combining the sorted groups and the pivot into a single sorted array.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Space/Time complexity of Quick Sort

A

O(n log n) on average, but can degrade to O(n^2) if the pivot isn’t well chosen.

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