Fucking Algorithms
Fucking Algorithms
Clique
1. The problem is about finding a subset of vertices in a given graph such that every pair of
vertices in the subset is connected by an edge.
2. The problem belongs to the NP-Complete complexity class.
3. The decision variant is Clique(k), which asks whether a clique of size k exists in the
graph.
4. Clique can be reduced to problems like Vertex Cover, Independent Set, and other
NP-Complete problems.
SAT
1. The problem is about determining the satisfiability of a given propositional logic formula,
i.e., whether there exists a variable assignment that makes the formula true.
2. The problem belongs to the NP-Complete complexity class.
3. The decision variant is deciding whether the input formula is satisfiable.
4. SAT can be reduced to many other problems, as it was the first problem proven to be
NP-Complete. Examples include 3SAT, Vertex Cover, and Clique.
2SAT
1. The problem is about determining the satisfiability of a given propositional logic formula,
where each clause has exactly two literals.
2. The problem belongs to the P complexity class, as there are polynomial-time algorithms
like the 2SAT algorithm based on strongly connected components.
3. The decision variant is deciding whether the input 2SAT formula is satisfiable.
4. 2SAT cannot be reduced to NP-Complete problems in general, as it is in P.
3SAT
1. The problem is about determining the satisfiability of a given propositional logic formula,
where each clause has exactly three literals.
2. The problem belongs to the NP-Complete complexity class.
3. The decision variant is deciding whether the input 3SAT formula is satisfiable.
4. 3SAT can be reduced to many other NP-Complete problems, like Vertex Cover, Clique,
and Hamilton Cycle.
P:
2SAT:
2SAT can be solved in polynomial time using an algorithm based on finding strongly connected
components in the implication graph. The algorithm consists of the following steps: (1) construct
the implication graph, (2) find its strongly connected components using Tarjan's or Kosaraju's
algorithm, both of which are polynomial-time, (3) check the satisfiability condition. The overall
time complexity is polynomial.
NP-Complete:
SAT:
The proof of SAT being NP-Complete is based on Cook-Levin theorem. The theorem states that
SAT is NP-Complete, and the proof demonstrates that any problem in NP can be reduced to
SAT in polynomial time. The reduction involves encoding the computation of a non-deterministic
Turing machine as a SAT formula.
3SAT:
3SAT is a special case of SAT, where each clause has exactly three literals. To show that 3SAT
is NP-Complete, it is sufficient to reduce SAT to 3SAT in polynomial time. This reduction
involves transforming any given SAT formula into an equivalent 3SAT formula using additional
variables and clauses.
For the remaining NP-Complete problems (HC, HP, Clique, IS, VC, CN, and SGI), the proofs of
their NP-Completeness typically involve reducing another known NP-Complete problem to the
given problem in polynomial time. For example:
Hamilton Cycle (HC) and Hamilton Path (HP):
To show that HC is NP-Complete, we can reduce the NP-Complete problem of 3SAT to it. The
reduction involves constructing a graph based on the 3SAT instance, such that the graph has a
Hamiltonian cycle if and only if the 3SAT instance is satisfiable. A similar reduction can be
constructed for HP.
NP:
1. IS to clique
To reduce the independent set (IS) problem to the clique problem, we construct a complement
graph G'=(V, E') where E' consists of all possible edges that are not present in G. Then, any set of
vertices that forms an independent set in G is a set of vertices that forms a clique in G', and vice
versa. Thus, we can solve the independent set problem in G by finding a maximum clique in G'. The
set of vertices corresponding to the maximum clique in G' will be a maximum independent set in G.
Pseudo code:
Input: A graph G = (V, E)
2. Clique to IS
To reduce the clique problem to the independent set problem, we can construct the
complement graph of the input graph and find a maximum independent set in it. The
largest independent set in the complement graph will correspond to the largest clique in
the original graph.
Here's the pseudocode for this reduction
3. Redusicg 2-Sat to 3-Sat:
To reduce a 2-SAT problem to a 3-SAT problem in polynomial time, you need to convert
each clause containing two literals into one or more clauses containing three literals.
You can accomplish this by introducing a new variable for each clause and using it to
create two new 3-SAT clauses.
Example:
2-sat: (a v b)^ (-a v b)
3-sat: (a V b V x1) ^ (a V b V -x1) ^ (-a V b V x2) ^ (-a V b V -x2)
This transformation can be done in polynomial time, as it only needs to iterate through the
clauses once and create two new clauses for each original clause.
4. sat to 3-sat
The reduction from SAT to 3SAT aims to transform any given Boolean formula in conjunctive
normal form (CNF) to an equivalent formula where each clause has exactly three literals. Here's
a pseudocode for the reduction:
Given a Boolean formula in CNF form for the SAT problem, we need to convert it into an
equivalent 3-SAT formula. To do this, we can perform the following steps for each clause in the
SAT formula:
If a clause has only one literal, say x, we can introduce two new variables, say a and b, and
rewrite the clause as (x OR a OR NOT a) and (x OR b OR NOT b). This maintains the original
satisfiability because the newly introduced variables essentially cancel each other out.
If a clause has two literals, say (x OR y), we can introduce a new variable, say a, and rewrite the
clause as (x OR y OR a) and (x OR y OR NOT a). This maintains the original satisfiability as at
least one of the new clauses will always be true, regardless of the value of a.
If a clause has more than three literals, say (x1 OR x2 OR x3 OR x4 OR ... OR xn), we can
introduce new variables to break it down into multiple 3-literal clauses. For example, we can
rewrite the clause as:
Given the SAT problem in CNF form: (x1 OR x2) AND (NOT x1 OR x3 OR NOT x2) AND (x3)
The first clause has two literals, so we introduce a new variable a and rewrite it as (x1 OR x2 OR
a) AND (x1 OR x2 OR NOT a).
The second clause already has three literals, so we keep it as is: (NOT x1 OR x3 OR NOT x2).
The third clause has only one literal, so we introduce two new variables b and c and rewrite it as
(x3 OR b OR NOT b) AND (x3 OR c OR NOT c).
The final 3-SAT formula is: (x1 OR x2 OR a) AND (x1 OR x2 OR NOT a) AND (NOT x1 OR x3 OR
NOT x2) AND (x3 OR b OR NOT b) AND (x3 OR c OR NOT c).
Suppose we have a SAT problem with a clause containing four literals: (x1 OR x2 OR x3 OR x4).
We want to convert this clause into an equivalent set of clauses, each containing exactly three
literals.
To do this, we'll introduce new variables to break the original clause into a series of smaller
clauses. Let's introduce a new variable a1. We'll use this variable to connect two 3-literal
clauses:
Thus, we've reduced the original 4-literal clause into two 3-literal clauses while maintaining its
satisfiability.
In general, for a clause with n literals (x1 OR x2 OR ... OR xn), we can introduce n-3 new variables
a1, a2, ..., a(n-3) and create a series of 3-literal clauses like this:
6. Reduce CN to IS
7. Reduce IS to Clique
8. cReduce IS to CN
3SAT to NPC
1. 3SAT to HC
Proof
Thus, any problem A in P can also be verified in polynomial time, and therefore belongs
to NP. This demonstrates that P is a subset of NP (P ⊆ NP).
Proof that for example 3sat can be reduced to 4sat and etc
3.