L18-19 (Prim's, Set Cover, Dynamic Programming) Flashcards
Define Prim’s algorithm
Prim’s algorithm is a greedy algorithm for finding the minimum spanning tree (MST) of an undirected, connected, and weighted graph.
The algorithm starts with an arbitrary vertex and iteratively adds the lightest edge that connects the MST to the remaining vertices, ensuring that no cycles are formed.
What makes Prim’s algorithm a greedy algorithm?
At each step, prim’s algorithm selects the lightest edge that connects the MST to the remaining vertices.
This choice is always part of an optimal solution (i.e., the MST) because it adheres to the cut property, ensuring that the algorithm converges to the global minimum.
What’s the cut property in MSTs? Why does it make the greedy MST algorithms correct?
The cut property states that given a cut (a partition of the graph’s vertices into two non-empty sets), the lightest edge crossing the cut (connecting the two sets) must be part of the MST.
This property ensures that the greedy choice of selecting the lightest edge at each step is always part of an optimal solution. The cut property is the foundation for the correctness of greedy MST algorithms, such as Kruskal’s and Prim’s algorithms.
How does Prim’s algorithm use a priority queue? How is it updated?
Prim’s algorithm uses a priority queue to efficiently select the lightest edge connecting the MST to the remaining vertices.
The priority queue is initialized with all vertices (except the starting vertex) with their keys set to infinity and the starting vertex key set to 0.
During the algorithm, the priority queue stores the vertices not yet in the MST, with their keys being the minimum edge weight connecting them to the MST.
Give detailed algorithm steps on how prim’s algorithm runs?
- Initialize an empty set for the MST, a set for visited vertices, and a priority queue with all vertices (except the starting vertex) with their keys set to infinity and the starting vertex key set to 0.
- While the priority queue is not empty, perform the following steps:
a. Select the vertex (u) with the smallest key that has not been visited, mark it as visited, and add the corresponding edge (if any) to the MST.
b. For each neighbor (v) of u, if v is not visited AND the weight of the edge (u, v) is less than the current key of v, update the key of v in the priority queue. - Repeat step 2 until all vertices are visited and the MST contains exactly |V| - 1 edges, where |V| is the number of vertices in the graph.
Give time complexity analysis for Prim’s
The time complexity of Prim’s algorithm depends on the data structure used for the priority queue:
Array: O(V^2)
Binary Heap: O((V+E) log V)
Fibonacci Heap: O(V Log V + E)
What are Horn formulas
Formally: Horn formulas are conjunctions of horn clauses
A Horn formula is a propositional formula in conjunctive normal form (CNF), where each clause contains at most one positive literal. Horn formulas are significant in the context of logic programming and automated theorem proving, as they can be efficiently checked for satisfiability using linear-time algorithms.
What is the satisfiability problem for Horn formulas?
Given a horn formula, can I assign true/false to each variable such that all clauses resolve to true?
In Horn formulas, there are two types of clauses. What are they?
- Implications (A => B)
- Pure negative clauses which is an “or” of any number of negative literals
For Horn formulas, the Unit propagation algorithm is used to solve the logical satisfiability problem in O(n) time.
Describe this step by step
- Set all variables to false
- While there is an implication clause that is not satisfied:
a. set the right-hand variable of the implication to true (only if the formula forces it to be true) - If all pure negative clauses are satisfied, then return the satisfying assignment. Otherwise return “formula unsatisfiable”
How do we know that the unit propagation algorithm works?
The algorithm works because it follows the basic rules of Boolean algebra and propositional logic. By iteratively applying the rules, it simplifies the formula and helps identify whether it is satisfiable or not.
If a variable is set to true by the greedy algorithm, then it must be true in any satisfying assignment
What is the Set Cover problem?
The Set Cover problem involves selecting a minimum number of sets from a collection of sets, such that the union of these selected sets contains all the elements in the universal set.
Give the formal definition of the Set Cover problem
Given a universe U of elements and a collection S of subsets of U (S is a set of sets), find the smallest sub-collection C (subset of S) such that the union of the sets in C contains all elements in U.
In other words, whats the minimum number of sets within S that you can choose such that all elements in U are included
Give the greedy algorithm for the set cover problem. What’s the runtime? Does this give the optimal solution?
- Initialize an empty set C for the chosen sets.
- While there are uncovered elements in U:
a. Select the set S in the collection with the maximum number of uncovered elements.
b. Add S to C and remove the covered elements from U. - Return the set C as the approximate set cover.
The runtime is O(k ln n)
where k
is the number of sets in the solution
This gives an approximation algorithm, which is ln(n) away from the optimal (n is the number of points)
What is the framework for Dynamic Programming in three steps?
- Define the structure of the subproblems:
Identify the subproblems that make up the original problem. Break the problem down into smaller, overlapping subproblems that can be solved independently. A table, matrix, array, etc can be used to store solutions to the subproblems - Find the recurrence relation:
Determine the relationship between the subproblems and how the solution to the larger problem depends on the solutions to the smaller subproblems. - Determine the order of computation and solve the problem:
This is typically done by either a bottom-up (iterative) approach or a top-down (memoization) approach. In the bottom-up approach, you start by solving the smallest subproblems and iteratively build up the solution to the larger problem. In the top-down approach, you start with the original problem and recursively break it down into smaller subproblems, storing the solutions to these subproblems to avoid redundant computation.