(Example on the picture above, with root in W4)That’s all for the theory, now let’s look at the algorithm: First let’s have a look on the scheme of the Hungarian algorithm: Step 0. (See Picture 1)Picture 1Here are the global variables that will be used in the code:#define N 55 //max number of vertices in one part#define INF 100000000 //just infinityint cost[N][N]; //cost matrixint n, max_match; //n workers and n jobsint lx[N], ly[N]; //labels of X and Y partsint xy[N]; //xy[x] - vertex that is matched with x,int yx[N]; //yx[y] - vertex that is matched with ybool S[N], T[N]; //sets S and T in algorithmint slack[N]; //as in the algorithm descriptionint slackx[N]; //slackx[y] such a vertex, that// l(slackx[y]) l(y) - w(slackx[y],y) = slack[y]int prev[N]; //array for memorizing alternating paths It’s easy to see that next initial labeling will be feasible: And as an initial matching we’ll use an empty one. The code for initializing is quite easy, but I’ll paste it for completeness: The next three steps will be implemented in one function, which will correspond to a single iteration of the algorithm.
Find some initial feasible vertex labeling and some initial matching. If M is perfect, then it’s optimal, so problem is solved. (x – is a root of the alternating tree we’re going to build). When the algorithm halts, we will have a perfect matching, that's why we'll have n iterations of the algorithm and therefore (n 1) calls of the function.
We’ll not touch these approaches, because it’s less practical for Top Coder needs.
As mentioned above, we are dealing with a bipartite graph.
In other words, it only includes those edges from the bipartite matching which allow the vertices to be perfectly feasible.
Steps To Solve Assignment Problem Clever Ways To Start A College Essay
Now we’re ready for the theorem which provides the connection between equality subgraphs and maximum-weighted matching: is called alternating if its edges alternate between M and E\M.The only thing in code that hasn't been explained yet is the procedure that goes after labels are updated.Say we've updated labels and now we need to complete our alternating tree; to do this and to keep algorithm in O(n3) time (it's only possible if we use each edge no more than one time per iteration) we need to know what edges should be added without iterating through all of them, and the answer for this question is to use BFS to add edges only from those vertices in Y, that are not in T and for which slack[y] = 0 (it's easy to prove that in such way we'll add all edges and keep algorithm to be O(n3)).In order to avoid this, on each step we can just modify the matching from the previous step, which only takes O(n2) operations.It’s easy to see that no more than n2 iterations will occur, because every time at least one edge becomes 0-weight. This is simply a function (for each vertex we assign some number called a label).Our goal is to complete all jobs minimizing total inputs, while assigning each worker to exactly one job and vice versa. Small example just to make things clearer: This problem is known as the assignment problem.The assignment problem is a special case of the transportation problem, which in turn is a special case of the min-cost flow problem, so it can be solved using algorithms that solve the more general cases.If we can’t find perfect matching on the current step, then the Hungarian algorithm changes weights of the available edges in such a way that the new 0-weight edges appear and these changes do not influence the optimal solution.To clarify, let’s look at the step-by-step overview: Step 0)A. Apply the same procedure for the vertices in the right part (jobs). Otherwise find the minimum vertex cover (for the subgraph with 0-weight edges only), the best way to do this is to use Köning’s graph theorem.For each vertex from left part (workers) find the minimal outgoing edge and subtract its weight from all weights connected with this vertex. Actually, this step is not necessary, but it decreases the number of main cycle iterations. Find the maximum matching using only 0-weight edges (for this purpose you can use max-flow algorithm, augmenting path algorithm, etc.). Step 2) Let and adjust the weights using the following rule: Step 3) Repeat Step 1 until solved.But there is a nuance here; finding the maximum matching in step 1 on each iteration will cause the algorithm to become O(n5).