The Hungarian algorithm solves the assignment problem in polynomial time. Given workers and tasks, and an n×n matrix containing the cost of assigning each worker to a task, it can find the cost minimizing assignment.
I want to find the choice for which cost is max? Can I do it using Hungarian or any similar method? Or this can only be done exponentially?
Wikipedia says:
If the goal is to find the assignment that yields the maximum cost,
the problem can be altered to fit the setting by replacing each cost
with the maximum cost subtracted by the cost.
So if I understand correctly: among all the costs you have as input, you find the maximum value. Then you replace each cost x by max - x. This way you still have positive costs and you can run the Hungarian algorithm.
Said differently: Hungarian tries to minimize the assignment cost. So, if you are looking for the maximum, you can reverse the costs: x -> -x. However, some implementations (don't know if all or any) require positive numbers. So the idea is to add a constant value to each cost in order to have positive numbers. This constant value does not change the resulting affectation.
As David said in the comment:
Multiply the cost matrix by -1 for maximization.
Related
I'm reading CLRS Algorithms Edition 3 and I have two problems for my homework (I'm not asking for answers, I promise!). They are essentially the same question, just applied to Kruskal or to Prim. They are as follows:
Suppose that all edge weights in a graph are integers in the range from 1 to |V|. How fast can you make [Prim/Kruskal]'s algorithm run? What if the edge weights are integers in the range from 1 to W for some constant W?
I can see the logic behind the answers I'm thinking of and what I'm finding online (ie sort the edge weights using a linear sort, change the data structure being used, etc), so I don't need help answering it. But I'm wondering why there is a difference between the answer if the range is 1 to |V| and 1 to W. Why ask the same question twice? If it's some constant W, it could literally be anything. But honestly, so could |V| - we could have a crazy large graph, or a very small one. I'm not sure how the two questions posed in this problem are different, and why I need two separate approaches for both of them.
There's a difference in complexity between an algorithm that runs in O(V) time and O(W) time for constant W. Sure, V could be anything, as could W, but that's not really the point: one is linear, one, is O(1). The question is then for which algorithms could having a restricted range of edge-weights impact complexity (based, as you suggest on edge-weight sort time and choice in data-structure), and what would the actual new optimal complexity be for linearly bounded edge-weights vs. for edge-weights bounded by a constant, W.
Having bounded edge-weights could open up new possibilities for sorting algorithms for Kruskal's, and might change the data structure you'd want to use to implement the queue for Prim's along with the most optimal way you could implement extract-min and update-key operations for that queue. The extent to which edge-weights are bounded can impact whether a particular change in data structure or implementation is even beneficial to make in terms of final complexity.
For example, knowing that the n elements of a list are bounded in value by a constant W makes it so that a switch to radix sort would improve the asymptotic complexity of sorting them, but if I instead only knew that they were bounded in value by 2^n there would be no advantage in changing to radix sort over the traditional methods and their O(n*logn) sorting complexity.
1)Suppose we have a common 0-1 knapsack problem. Given a set of n items numbered from 1 up to n, each with a weight w_i and a value v_i, along with a maximum weight capacity W. Here we need to select some of the objects, so that maximize sum of v_i, such that sum of w_i of selected objects will not exceed the given W number.
maximize∑(v_i*x_i), such that ∑(w_i*x_i)≤ W
2)Now suppose we have just the same problem, but we need to select objects so that sum of their values will be minimal, and sum of their weights can't be less than the given number.
minimize∑(v_i*x_i), such that ∑(w_i*x_i)≥ W.
Knowing that first problem is NP complete, how can I prove that second one has the same complexity, in other words is NP complete too?
Knowing that first problem is NP complete, how can I prove that second one has the same complexity, in other words is NP complete too?
If you want to prove that problem B is NP-complete, you have to prove that there exists a polynomial time reduction from A to B, where A is known to be a NP-complete problem.
A polynomial-time reduction from a problem A to a problem B is an algorithm that solves problem A using a polynomial number of calls to a subroutine for problem B, and polynomial time outside of those subroutine calls.(source).
So in your case, your can easily make a polynomial time reduction from the knapsack problem to the inversed knapsack problem.
These two problems are equivalent (finding an optimal solution to one immediately solves the other).
Let S be the set of objects, M be the sum of the weights of the objects of S, and W the capacity of the knapsack.
Then, we have:
(i) finding a subset of objects such that the sum of their weight does not exceed W and the sum of their value is maximal
is equivalent to
(ii) finding a subset of objects such that the sum of their weight is of at least M-W and the sum of their value is minimal.
That is because if S' is an optimal solution of (i), then S\S' is an optimal solution of (ii) (and vice-versa).
This is a polynomial time reduction (O(1) calls to the subroutine, polynomial number of operations), so the inversed knapsack is indeed NP-complete.
The key idea seems to be to exchange value and weight and use binary search on the second problem to construct the reduction.
Given an instance I of the first formulation with values v_i and weights w_i, construct an instance of the second problem by exchanging profits and weights. The sum of all weights (which now are the profits) is bounded by
n * w_max
where w_max is the maximum weight. This number itself is exponential in the encoding length of the input; however, we can use binary search to determine the maximum attainable profit such that the initial capacity W is not exceeded. This can be done in
log( n * w_max )
iterations, a number which is polynomially bounded in the encoding size of the input, using the same number of calls to an algorithm for the second problem. The described algorithm is the polynomial rediction from the first problem to the second problem.
The inverse knapsack is one of my favorites. While I have never explicitly proven that it is NP complete, I do know how to reformulate the question to the knapsack problem itself, which should do the trick:
Rather than adding objects to an empty bag, consider the problem of choosing objects to be removed from a bag that is full. Then, since the number of weights can't be less than a given number, we must remove only up to total (weight-minimum weight) in terms of objects.
Since the price is to be minimized, then the price of objects to be removed must be maximized.
We are left with the initial knapsack problem, where we have to select a group of items (to be removed) such that we maximize their price, and their total weight does not exceed weight-minimum weight. (At the end, we take the items we did NOT remove as the solution)
We have reformed the problem to be exactly the original knapsack problem, therefore it must be NP-complete as well.
The beauty of this method is that I personally have no idea what NP complete it; I just proved that inverse knapsack and knapsack are perfectly equivalent.
Given n checks, each of arbitrary (integer) monetary value, decide if the checks can be partitioned into two parts that have the same monetary value.
I'm beyond mind blown on how to solve this. Is there an algorithm to solve this in polynomial time or is this NP-Complete?
Yes it's an NP complete problem. It's a variation of the subset sum problem.
Actually you can solve this in O(n*sum/2) using dynamic programming, first sum up all checks into a varaibel sum, then you can perform dp on the checks values (either take it or leave it or take it) and check at the end if that sum is equal to s/2.
I would like to quote from Wikipedia
In mathematics, the minimum k-cut, is a combinatorial optimization
problem that requires finding a set of edges whose removal would
partition the graph to k connected components.
It is said to be the minimum cut if the set of edges is minimal.
For a k = 2, It would mean Finding the set of edges whose removal would Disconnect the graph into 2 connected components.
However, The same article of Wikipedia says that:
For a fixed k, the problem is polynomial time solvable in O(|V|^(k^2))
My question is Does this mean that minimum 2-cut is a problem that belongs to complexity class P?
The min-cut problem is solvable in polynomial time and thus yes it is true that it belongs to complexity class P. Another article related to this particular problem is the Max-flow min-cut theorem.
First of all, the time complexity an algorithm should be evaluated by expressing the number of steps the algorithm requires to finish as a function of the length of the input (see Time complexity). More or less formally, if you vary the length of the input, how would the number of steps required by the algorithm to finish vary?
Second of all, the time complexity of an algorithm is not exactly the same thing as to what complexity class does the problem the algorithm solves belong to. For one problem there can be multiple algorithms to solve it. The primality test problem (i.e. testing if a number is a prime or not) is in P, but some (most) of the algorithms used in practice are actually not polynomial.
Third of all, in the case of most algorithms you'll find on the Internet evaluating the time complexity is not done by definition (i.e. not as a function of the length of the input, at least not expressed directly as such). Lets take the good old naive primality test algorithm (the one in which you take n as input and you check for division by 2,3...n-1). How many steps does this algo take? One way to put it is O(n) steps. This is correct. So is this algorithm polynomial? Well, it is linear in n, so it is polynomial in n. But, if you take a look at what time complexity means, the algorithm is actually exponential. First, what is the length of the input to your problem? Well, if you provide the input n as an array of bits (the usual in practice) then the length of the input is, roughly said, L = log n. Your algorithm thus takes O(n)=O(2^log n)=O(2^L) steps, so exponential in L. So the naive primality test is in the same time linear in n, but exponential in the length of the input L. Both correct. Btw, the AKS primality test algorithm is polynomial in the size of input (thus, the primality test problem is in P).
Fourth of all, what is P in the first place? Well, it is a class of problems that contains all decision problems that can be solved in polynomial time. What is a decision problem? A problem that can be answered with yes or no. Check these two Wikipedia pages for more details: P (complexity) and decision problems.
Coming back to your question, the answer is no (but pretty close to yes :p). The minimum 2-cut problem is in P if formulated as a decision problem (your formulation requires an answer that is not just a yes-or-no). In the same time the algorithm that solves the problem in O(|V|^4) steps is a polynomial algorithm in the size of the input. Why? Well, the input to the problem is the graph (i.e. vertices, edges and weights), to keep it simple lets assume we use an adjacency/weights matrix (i.e. the length of the input is at least quadratic in |V|). So solving the problem in O(|V|^4) steps means polynomial in the size of the input. The algorithm that accomplishes this is a proof that the minimum 2-cut problem (if formulated as decision problem) is in P.
A class related to P is FP and your problem (as you formulated it) belongs to this class.
What is the most efficient algorithm for detecting if there exists a perfect matching in a graph with even number of vertices ?
EDIT: As NiklasB pointed out, the first answer I gave is incorrect.
To correct what I said below (see 1 for a more in depth discussion of the following paragraph):
First, the Tutte matrix is a matrix of formal variables, so its determinant is a polynomial in terms of formal variables, and not a number. Since T is a formal matrix, computing its determinant could take exponential time. However, we don't need to really compute det(T) just test whether it is identically the 0 polynomial. This means that for every numerical assignment of variables in T, det(T) will yield 0. It can further be shown that if there doesn't exist a perfect matching, for most assignments of variables, det(T) will evaluate to a non-zero variable. Therfore, we can substitute random values for variables in T and compute the determinant. If we evaluate to a non-zero value we can safely say that there doesn't exist a matching. Otherwise, if we repeat this a sufficient number of times, we achieve a desired confidence to say there does exist a perfect matching.
This algorithm is still bounded by the runtime of taking a determinant. The current most efficient known algorithm has a runtime of O(n^2.373), which in this case is O(|V|^2.373). What does this mean?
Edmond's has O(|E|sqrt(|V|)).
For certain types of graphs, particularly dense ones, this could actually beat Edmond's, asymptotically. But like I mentioned, this is still a randomized algorithm not an exact one.
Editorial Comment: The one benefit it has is that its significantly easier to wrap your head around an actually implement than Edmonds. Implementing Edmond's in C was one of the saddest nights of my undergraduate career.
ORIGINAL WRONG ANSWER:
You can find existence of perfect matching using the Tutte matrix: http://en.wikipedia.org/wiki/Tutte_matrix
If A(t) is the tutte matrix of a graph G(V,E), then there exists a perfect matching iff the det(A) != 0. You can find a determinant in O(n^3) operations by gaussian elimination, which, in this case would be O(|V|sqrt(|V|)). This is an improvement over the O(|E|sqrt(|V|)) runtime of Edmonds if O(|E|) > O(|V|).