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

Divide and conquer algorithm with a random pivot

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

What is Dijkstra’s Formula?

A

Dijkstra’s algorithm is a greedy algorithm used to find the shortest path from a starting node to all other nodes in a graph, particularly when edge weights are non-negative. It’s widely used in networking, routing, and GPS applications.

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

What is the Space/Time of Dijkstra’s Formula?

A

Time: O((V + E) log V)

  • Using min-heap
  • With min_by? it’s O(V^2) because we iterate through the queue..

Space: O(V + E)

  • V is the number of vertices (for storing distances and the priority queue).
  • E is the number of edges (for storing the graph’s adjacency list).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the key steps of Dijkstra’s Formula?

A

Initialization:

distances = Hash.new(Float::Infinity)
distances[start_node] = 0
priority_queue = [[start_node, 0]] # or min-heap
visited = Set.new

Processing Nodes:

Get the node with the smallest distance
current_node, distance = priority_queue.min_by{|node, dist| dist}
priority_queue.delete([current_node, distance])

# Skip if the node is already visited
next if visited[current_node]

# Mark the current node as visited
visited[current_node] = true

Further Optional Node Processing Here..

Iterate through the neighbors

If a shorter path is found, update and push to the priority queue
if new_distance < distances[neighbor]
distances[neighbor] = new_distance
priority_queue &laquo_space;[neighbor, new_distance]

Termination:

Once all nodes are processed, the shortest distances from the start node to all other nodes are finalized.

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

What is the Knapsack problem?

A

The Knapsack Problem is a classic optimization problem where you have a container with a weight limit, a set of items that have both a weight and a value. The Knapsack Problem evaluates how to have the most value given the capacity of the bag.

17
Q

What are the key steps of the Knapsack Problem?

A
  1. Initialization

n = weights.length
# Create a 2D dp array, where dp[i][w] is the max value for i items and capacity w
dp = Array.new(n + 1) { Array.new(capacity + 1, 0) }

  1. Build the Table

Build the dp table
(1..n).each do |i|
(1..capacity).each do |w|
if weights[i-1] <= w
# max of not taking the item or taking the item
dp[i][w] = [dp[i-1][w], values[i-1] + dp[i-1][w - weights[i-1]]].max
else
# Can’t take the item, so carry forward the previous value
dp[i][w] = dp[i-1][w]
end
end
end

  1. Termination

Maximum value that can be carried
dp[n][capacity]

18
Q

What is the Space / Time complexity of the Knapsack Problem?

A

Time : O(n * W)
- where n is the number of items and W is the knapsack capacity.

Space: O(n * W)
- You can optimize the space complexity to O(W) by using a single-dimensional array if you only care about the previous row’s results.

19
Q

Adjacency List type

Undirected Unweighted:

A

Each node points to a list of adjacent nodes.

graph = {
0 => [1, 2],
1 => [0, 3],
2 => [0, 3],
3 => [1, 2]
}

20
Q

Adjacency List type

Undirected Weighted:

A

Each node points to a dictionary where the keys are adjacent nodes and the values are edge weights.

graph = {
0 => { 1 => 4, 2 => 3 },
1 => { 0 => 4, 3 => 2 },
2 => { 0 => 3, 3 => 5 },
3 => { 1 => 2, 2 => 5 }
}

21
Q

Adjacency List type

Directed Unweighted:

A

Each node points to a list of nodes it directs to.

graph = {
0 => [1, 2],
1 => [3],
2 => [3],
3 => []
}

22
Q

Adjacency List type

Directed Weighted:

A

Each node points to a dictionary where the keys are the nodes it directs to and the values are the edge weights.

graph = {
0 => { 1 => 4, 2 => 3 },
1 => { 3 => 2 },
2 => { 3 => 5 },
3 => {}
}