Exam points Flashcards

1
Q

What is the time complexity of BFS in a connected graph?

A

For a connected graph G(V, E), the number of vertices is in O(number of edges), there are a least E+1 nodes, so time complexity for BFS, O(V+E) can be reduced to O(E).

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

What are the key steps in formulating a dynamic programming solution?

A

The key steps are:

Define Subproblems (OPT): Clearly specify what each OPT(i), OPT(i, j), etc., represents.

Recurrence Relation: Determine how to compute OPT based on smaller subproblems.

Initial Conditions: Identify base cases to start the recursion.

Order of Computation: Decide the sequence to compute subproblems (often bottom-up).

Reconstruction (if needed): Keep track of choices to reconstruct the solution.

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

What properties must a problem have to be suitable for dynamic programming?

A

The problem must exhibit:

Optimal Substructure: An optimal solution contains optimal solutions to its subproblems.
Overlapping Subproblems: The problem can be broken down into subproblems that are reused multiple times.

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

How do you define the OPT function in the Knapsack problem for dynamic programming?

A

Define OPT(i, w) as the maximum total value achievable using the first i items without exceeding weight w. The recurrence is:

OPT(i, w) = max {

OPT(i - 1, w), (excluding item i)
vi + OPT(i - 1, w - wi) (including item i, if wi ≤ w) }

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

What is the exchange argument in greedy algorithms, and how is it used to prove correctness?

A

The exchange argument shows that any optimal solution can be transformed into the greedy solution through a series of exchanges without decreasing the optimality. This proves that the greedy choice leads to a globally optimal solution.

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

When does a greedy algorithm guarantee an optimal solution?

A

When the problem has:

Greedy Choice Property: A locally optimal choice leads to a globally optimal solution.
Optimal Substructure: An optimal solution to the problem contains optimal solutions to subproblems.

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

What is Dijkstra’s algorithm, and what are its limitations?

A

Dijkstra’s algorithm finds the shortest paths from a source node to all other nodes in a graph with non-negative edge weights by iteratively selecting the node with the smallest tentative distance. Limitation: It cannot handle graphs with negative edge weights.

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

How does the Bellman-Ford algorithm handle negative edge weights, and what is its time complexity?

A

The Bellman-Ford algorithm computes shortest paths in graphs with negative edge weights by relaxing all edges repeatedly. It also detects negative cycles. Time complexity: O(VE), where V is the number of vertices and E is the number of edges.

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

How do you prove that a problem is NP-complete?

A

To prove NP-completeness:

Show the problem is in NP: A solution can be verified in polynomial time.
Reduction: Choose a known NP-complete problem and provide a polynomial-time reduction from it to your problem.
Equivalence: Demonstrate that a solution to your problem corresponds to a solution of the known problem.

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

What is Big O notation, and why is it important in algorithm analysis?

A

Big O notation describes an upper bound on the time (or space) complexity of an algorithm, focusing on the growth rate as the input size increases. It’s important for classifying algorithms by their efficiency and for comparing their performance on large inputs.

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

What are the differences between Breadth-First Search (BFS) and Depth-First Search (DFS)?

A

BFS:
Explores neighbors level by level.
Used for shortest paths in unweighted graphs, testing bipartiteness.
Time complexity: O(V + E).

DFS:
Explores as far as possible along each branch before backtracking.
Used for cycle detection, topological sorting, finding connected components.
Time complexity: O(V + E).

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

What is a topological sort, and in what type of graph is it applicable?

A

A topological sort is an ordering of the vertices in a directed acyclic graph (DAG) such that for every directed edge (u, v), u comes before v in the ordering. It’s applicable in scheduling tasks where some tasks must precede others.

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

How can you detect negative cycles in a graph?

A

Use the Bellman-Ford algorithm. After |V| - 1 iterations, perform one more iteration. If any edge can still be relaxed, a negative cycle exists.

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

What is the divide and conquer approach in algorithm design?

A

Divide and conquer involves:

Dividing the problem into smaller subproblems of the same type.
Conquering by solving the subproblems recursively.
Combining the solutions of subproblems to solve the original problem.

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

How do you analyze the time complexity of a divide and conquer algorithm using the Master Theorem?

A

For a recurrence of the form T(n) = aT(n/b) + f(n):

Case 1: If f(n) = O(n^c) where c < log_b(a), then T(n) = Θ(n^{log_b(a)}).
Case 2: If f(n) = Θ(n^{log_b(a)} * log^k n), then T(n) = Θ(n^{log_b(a)} * log^{k+1} n).
Case 3: If f(n) = Ω(n^c) where c > log_b(a), and if af(n/b) ≤ kf(n) for some k < 1, then T(n) = Θ(f(n)).

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

Why is the standard dynamic programming solution for the Knapsack problem not considered polynomial time?

A

Because its time complexity is O(nW), where W is the capacity of the knapsack. If W is exponential in the size of the input (number of bits needed to represent W), then the algorithm is pseudo-polynomial, not polynomial.

17
Q

What is backtracking, and when is it useful?

A

Backtracking is a method for finding all (or some) solutions by exploring possible candidate solutions incrementally and abandoning a candidate (“backtracking”) as soon as it is determined that it cannot possibly lead to a valid solution. It’s useful in solving constraint satisfaction problems, puzzles, and combinatorial problems.

18
Q

How does inversion counting relate to sorting algorithms, and what is its time complexity?

A

Inversion counting measures how far an array is from being sorted by counting the number of inversions (pairs of elements that are out of order). Merge sort can be modified to count inversions while sorting, with a time complexity of O(n log n).

19
Q

What is the greedy choice property, and why is it important in greedy algorithms?

A

The greedy choice property means that making the locally optimal choice at each step leads to a globally optimal solution. It’s important because it justifies the use of a greedy algorithm for certain optimization problems.

20
Q

How do you compute all connected components of an undirected graph?

A

Perform BFS or DFS starting from each unvisited vertex. Each traversal marks all vertices reachable from the starting vertex as part of the same connected component. Time complexity: O(V + E).

21
Q

What is an induced subgraph?

A

An induced subgraph is formed from a subset of a graph’s vertices and all of the edges connecting pairs of vertices in that subset. It captures the exact relationships between those vertices as in the original graph.