Index
- #Dynamic Programming
- #Exhaustive Search and Brand-and-Bound
- #NP and NP-Complete Algorithms
- #Approximation Algorithms
- #Linear Programming
- #Integer Linear Programming
Dynamic Programming
Principle of Optimality and Recurrences
If a solution is optimal, any portion of it should be optimal
with respect to all the other possible choices of that particular portion. So, if a solution is not optimal, it shouldn't be further exploited.
Memoization Tables
Memoization tables are a way to store the information of a Dynamic Programming problem. They are used to speed up computing by saving the result of a certain operation, thus preventing running the same operation on the same input values more than once.
Shortest Path Using Dynamic Programming
is the cost of an optimal path from node at stage to sink is the cost associated with the edge
Why it works? It makes sure that a partial solution is optimal. Using that principle, we can reuse partial optimal solutions to build a global solution.
0/1 Knapsack
is the maximum value that is possible to transport with a weight limit of , allowing only objects numbered from to . - The optimal solution is defined by
Here,
This gives a complexity of
Longest Common Sub-Sequence (LCS)
Given 2 sequences
Example:
= abefcghd = eagbcfdh = abcd
A brute force solution is impractical ()
By using this, we get a complexity of
Floyd-Warshall Algorithm for APSP
The goal is to find the shortest path between 2 nodes, passing through every edge.
- If all the weights are non negative, we can use the Dijkstra's Algorithm , assuming each vertex as source. This has complexity
, and if the graph is dense the complexity is . - If the are negative weights, we can use the Bellman-Ford-Moore algorithm, assuming each vertex as source. This has complexity
, and if the graph is dense the complexity is . - Although these approaches work, we need to find algorithms with better performance.
Floyd-Warshall Algorithm:
- Start by creating an adjacency matrix following these rules:
In short, the diagonal is 0, if there is an edge from
to use the weight of that edge, otherwise use .
Now, we need a matrix to use as a memoization table. In this case, we will use a 3D matrix of size
In order to populate the matrix, use the following rules:
Lets break down the second case:
is the path from to without using is the path from to is the path from to
This gives a space complexity of
Since state
In doing so, we can also simplify the rules to:
Another matrix that is needed is a
How to run the algorithm?
for (int k = 0; k < n; k++){
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (dp[i][k] + dp[k][j] < dp[i][j]){
dp[i][j] = dp[i][k] + dp[k][j];
next[i][j] = next[i][k];
}
}
}
}
If the graph has negative cycles, run the algorithm again, but if a better path is detected, mark the edges as
This has a complexity of
Exhaustive Search and Brand-and-Bound
There is not much to say about this.
Exhaustive search is searching every possible solution, and we can use bounding (pruning) to stop searching a certain branch if it is impossible to reach a solution.
Example:
Sum of subsets
Given an integer
Using backtracking, we can build a binary tree of either using or not using a certain element.
We can use a bounding function to stop searching if we have already exceeded
NP and NP-Complete Algorithms
The Halting Problem
Let's imagine we have a machine (or program). Let's call this machine
Now, let's extend
Let's run
- If
says that the program halts, then will not halt, thus produced the wrong answer. - If
says that the program runs forever, then will halt, thus produced the wrong answer.
Decision vs Optimization Problems
In decision problems, the answer should be yes/no, but in optimization problems, the answer should be a subset of feasible answers.
Abstract example:
- Optimization problem: Find the minimum value
so that some property holds. - Decision problem: Assuming
is some value, does some property holds?
Their difficulty is similar, because if one of them is easier than the other, then we could use it to solve the other.
P Problems (Polynomial Time)
P Problems are decision problems that can be solved in polynomial time (
NP Problems (Nondeterministic Polynomial Time)
A decision problem is a NP problem if it is possible to check if a solution is valid in polynomial time (
NP Hard Problems
A problem is NP-hard if an algorithm for it can be transformed into one for solving any NP problem. So, NP problems are at least as hard as any NP problem.
NP Complete Problems
If a problem has a known non-deterministic algorithm then we can classify it as NP-Complete.
Polynomial time reductions
Suppose that
Satisfiability (SAT)
CNF Satisfiability serves as a base problem to relate all non-deterministic problems together, in a sense that if one of them is solved, then the others are also solved.
The CNF-SAT problem means taking a set and a propositional calculus formula and finding out which values make the formula true. It is a NP-Hard problem.
Now, we can relate the SAT with other problems, meaning that if we find a polynomial solution to SAT, then all the other problems can also be solved in polynomial time.
Can we relate the 0/1 Knapsack with the SAT?
At first glance, they might seem different, but in reality they are almost the same problem:
Let's imagine we have 3 items that we can use. So, the complexity is also
This means that SAT reduces 0/1 Knapsack:
Since SAT is also NP-Complete (because there is a know non-deterministic algorithm for it), 0/1 Knapsack also is NP-Complete.
Clique Problem
What is a clique?
A clique is a graph / subgraph where there is an edge connecting all pairs of vertices.
Knowing the maximum size of the clique or if there is a clique is a NP-Hard problem, but how do we prove it?
Abdul Bari explained it in a very simple way: https://www.youtube.com/watch?v=qZs767KQcvE&t=10s
Approximation Algorithms
I'm not going to prove the algorithms, the lecture slides have them.
The important concept is the approximation factor (
So, a 2-approximation algorithm means that the solution is at most 2 times worse than the optimal solution.
Linear Programming
I highly recommend this video: https://www.youtube.com/watch?v=E72DWgKP_1Y. It explains the concept of linear programming in a very easy way.
Integer Linear Programming
A linear programming program is an integer linear programming if our values (
We can start by solving it like a normal linear programming problem (ignoring the fact that the values need to be integers).
Then, the optimal solution will have values that are not integers (if that's not the case, then the solution found is the solution for the integer problem).
Now, we need to create different branches by either rounding one value up or down. Usually, we round the biggest value.
Example:
A further explanation of the problem can be found here: https://www.youtube.com/watch?v=upcsrgqdeNQ&t=2s