{"id":26700,"date":"2017-12-22T19:59:52","date_gmt":"2017-12-22T14:29:52","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26700"},"modified":"2017-12-22T19:59:52","modified_gmt":"2017-12-22T14:29:52","slug":"branch-bound-job-assignment-problem","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/branch-bound-job-assignment-problem\/","title":{"rendered":"Branch And Bound | Set 4 (Job Assignment Problem)"},"content":{"rendered":"<p>Let there be N workers and N jobs. Any worker can be assigned to perform any job, incurring some cost that may vary depending on the work-job assignment. It is required to perform all jobs by assigning exactly one worker to each job and exactly one job to each agent in such a way that the total cost of the assignment is minimized.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26708\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment.png\" alt=\"Branch And Bound | Set 4 (Job Assignment Problem)\" width=\"350\" height=\"250\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment.png 350w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment-300x214.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>Let us explore all approaches for this problem.<\/p>\n<p><strong>Solution 1: Brute Force<\/strong><br \/>\nWe generate n! possible job assignments and for each such assignment, we compute its total cost and return the less expensive assignment. Since the solution is a permutation of the n jobs, its complexity is O(n!).<\/p>\n<p><strong><a href=\"http:\/\/www.geeksforgeeks.org\/hungarian-algorithm-assignment-problem-set-1-introduction\/\" target=\"_blank\" rel=\"noopener\">Solution 2: Hungarian Algorithm<\/a><\/strong><br \/>\nThe optimal assignment can be found using the Hungarian algorithm. The Hungarian algorithm has worst case run-time complexity of O(n^3).<\/p>\n<p><strong>Solution 3: DFS\/BFS on state space tree<\/strong><br \/>\nA state space tree is a N-ary tree with property that any path from root to leaf node holds one of many solutions to given problem. We can perform depth-first search on state space tree and but successive moves can take us away from the goal rather than bringing closer. The search of state space tree follows leftmost path from the root regardless of initial state. An answer node may never be found in this approach. We can also perform a Breadth-first search on state space tree. But no matter what the initial state is, the algorithm attempts the same sequence of moves like DFS.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Solution 4: Finding Optimal Solution using Branch and Bound<\/strong><br \/>\nThe selection rule for the next node in BFS and DFS is \u201cblind\u201d. i.e. the selection rule does not give any preference to a node that has a very good chance of getting the search to an answer node quickly. The search for an optimal solution can often be speeded by using an \u201cintelligent\u201d ranking function, also called an approximate cost function to avoid searching in sub-trees that do not contain an optimal solution. It is similar to BFS-like search but with one major optimization. Instead of following FIFO order, we choose a live node with least cost. We may not get optimal solution by following node with least promising cost, but it will provide very good chance of getting the search to an answer node quickly.<\/p>\n<p>There are two approaches to calculate the cost function:<\/p>\n<ol>\n<li>For each worker, we choose job with minimum cost from list of unassigned jobs (take minimum entry from each row).<\/li>\n<li>For each job, we choose a worker with lowest cost for that job from list of unassigned workers (take minimum entry from each column).<\/li>\n<\/ol>\n<p>In this article, the first approach is followed.<\/p>\n<p>Let\u2019s take below example and try to calculate promising cost when Job 2 is assigned to worker A.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26711\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment2.png\" alt=\"Branch And Bound | Set 4 (Job Assignment Problem)\" width=\"252\" height=\"163\" \/><\/p>\n<p>Since Job 2 is assigned to worker A (marked in green), cost becomes 2 and Job 2 and worker A becomes unavailable (marked in red).<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26714\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment3.png\" alt=\"Branch And Bound | Set 4 (Job Assignment Problem)\" width=\"239\" height=\"174\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment3.png 239w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment3-74x55.png 74w\" sizes=\"(max-width: 239px) 100vw, 239px\" \/><\/p>\n<p>Now we assign job 3 to worker B as it has minimum cost from list of unassigned jobs. Cost becomes 2 + 3 = 5 and Job 3 and worker B also becomes unavailable.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-26717\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment4.png\" alt=\"Branch And Bound | Set 4 (Job Assignment Problem)\" width=\"251\" height=\"165\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment4.png 251w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment4-130x86.png 130w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment4-187x124.png 187w\" sizes=\"(max-width: 251px) 100vw, 251px\" \/><\/p>\n<p>Finally, job 1 gets assigned to worker C as it has minimum cost among unassigned jobs and job 4 gets assigned to worker C as it is only Job left. Total cost becomes 2 + 3 + 5 + 4 = 14.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-26721\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment5.png\" alt=\"Branch And Bound | Set 4 (Job Assignment Problem)\" width=\"244\" height=\"176\" \/><\/p>\n<p>Below diagram shows complete search space diagram showing optimal solution path in green.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-26724\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment6.png\" alt=\"Branch And Bound | Set 4 (Job Assignment Problem)\" width=\"602\" height=\"460\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment6.png 602w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/jobassignment6-300x229.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/p>\n<p><strong>Complete Algorithm:<\/strong><\/p>\n<pre>\/* findMinCost uses Least() and Add() to maintain the\r\n   list of live nodes\r\n\r\n   Least() finds a live node with least cost, deletes\r\n   it from the list and returns it\r\n\r\n   Add(x) calculates cost of x and adds it to the list\r\n   of live nodes\r\n\r\n   Implements list of live nodes as a min heap *\/\r\n\r\n\r\n\/\/ Search Space Tree Node\r\nnode\r\n{\r\n   int job_number;\r\n   int worker_number;\r\n   node parent;\r\n   int cost;\r\n}\r\n\r\n\/\/ Input: Cost Matrix of Job Assignment problem\r\n\/\/ Output: Optimal cost and Assignment of Jobs\r\nalgorithm findMinCost (costMatrix mat[][])\r\n{\r\n   \/\/ Initialize list of live nodes(min-Heap)\r\n   \/\/ with root of search tree i.e. a Dummy node\r\n   while (true)\r\n   {\r\n      \/\/ Find a live node with least estimated cost\r\n      E = Least();\r\n\r\n      \/\/ The found node is deleted from the list\r\n      \/\/ of live nodes\r\n      if (E is a leaf node)\r\n      {\r\n         printSolution();\r\n         return;\r\n      }\r\n\r\n     for each child x of E\r\n     {\r\n         Add(x); \/\/ Add x to list of live nodes;\r\n         x-&gt;parent = E; \/\/ Pointer for path to root\r\n     }\r\n   }\r\n}<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p>Below is its C++ implementation.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ Program to solve Job Assignment problem<br\/>\/\/ using Branch and Bound<br\/>#include &lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/>#define N 4<br\/> <br\/>\/\/ state space tree node<br\/>struct Node<br\/>{<br\/>    \/\/ stores parent node of current node<br\/>    \/\/ helps in tracing path when answer is found<br\/>    Node* parent;<br\/> <br\/>    \/\/ contains cost for ancestors nodes<br\/>    \/\/ including current node<br\/>    int pathCost;<br\/> <br\/>    \/\/ contains least promising cost<br\/>    int cost;<br\/> <br\/>    \/\/ contain worker number<br\/>    int workerID;<br\/> <br\/>    \/\/ contains Job ID<br\/>    int jobID;<br\/> <br\/>    \/\/ Boolean array assigned will contains<br\/>    \/\/ info about available jobs<br\/>    bool assigned[N];<br\/>};<br\/> <br\/>\/\/ Function to allocate a new search tree node<br\/>\/\/ Here Person x is assigned to job y<br\/>Node* newNode(int x, int y, bool assigned[],<br\/>              Node* parent)<br\/>{<br\/>    Node* node = new Node;<br\/> <br\/>    for (int j = 0; j &lt; N; j++)<br\/>        node-&gt;assigned[j] = assigned[j];<br\/>    node-&gt;assigned[y] = true;<br\/> <br\/>    node-&gt;parent = parent;<br\/>    node-&gt;workerID = x;<br\/>    node-&gt;jobID = y;<br\/> <br\/>    return node;<br\/>}<br\/> <br\/>\/\/ Function to calculate the least promising cost<br\/>\/\/ of node after worker x is assigned to job y.<br\/>int calculateCost(int costMatrix[N][N], int x,<br\/>                  int y, bool assigned[])<br\/>{<br\/>    int cost = 0;<br\/> <br\/>    \/\/ to store unavailable jobs<br\/>    bool available[N] = {true};<br\/> <br\/>    \/\/ start from next worker<br\/>    for (int i = x + 1; i &lt; N; i++)<br\/>    {<br\/>        int min = INT_MAX, minIndex = -1;<br\/> <br\/>        \/\/ do for each job<br\/>        for (int j = 0; j &lt; N; j++)<br\/>        {<br\/>            \/\/ if job is unassigned<br\/>            if (!assigned[j] &amp;&amp; available[j] &amp;&amp;<br\/>                costMatrix[i][j] &lt; min)<br\/>            {<br\/>                \/\/ store job number<br\/>                minIndex = j;<br\/> <br\/>                \/\/ store cost<br\/>                min = costMatrix[i][j];<br\/>            }<br\/>        }<br\/> <br\/>        \/\/ add cost of next worker<br\/>        cost += min;<br\/> <br\/>        \/\/ job becomes unavailable<br\/>        available[minIndex] = false;<br\/>    }<br\/> <br\/>    return cost;<br\/>}<br\/> <br\/>\/\/ Comparison object to be used to order the heap<br\/>struct comp<br\/>{<br\/>    bool operator()(const Node* lhs,<br\/>                   const Node* rhs) const<br\/>    {<br\/>        return lhs-&gt;cost &gt; rhs-&gt;cost;<br\/>    }<br\/>};<br\/> <br\/>\/\/ print Assignments<br\/>void printAssignments(Node *min)<br\/>{<br\/>    if(min-&gt;parent==NULL)<br\/>        return;<br\/> <br\/>    printAssignments(min-&gt;parent);<br\/>    cout &lt;&lt; &quot;Assign Worker &quot; &lt;&lt; char(min-&gt;workerID + &#039;A&#039;)<br\/>         &lt;&lt; &quot; to Job &quot; &lt;&lt; min-&gt;jobID &lt;&lt; endl;<br\/> <br\/>}<br\/> <br\/>\/\/ Finds minimum cost using Branch and Bound.<br\/>int findMinCost(int costMatrix[N][N])<br\/>{<br\/>    \/\/ Create a priority queue to store live nodes of<br\/>    \/\/ search tree;<br\/>    priority_queue&lt;Node*, std::vector&lt;Node*&gt;, comp&gt; pq;<br\/> <br\/>    \/\/ initailize heap to dummy node with cost 0<br\/>    bool assigned[N] = {false};<br\/>    Node* root = newNode(-1, -1, assigned, NULL);<br\/>    root-&gt;pathCost = root-&gt;cost = 0;<br\/>    root-&gt;workerID = -1;<br\/> <br\/>    \/\/ Add dummy node to list of live nodes;<br\/>    pq.push(root);<br\/> <br\/>    \/\/ Finds a live node with least cost,<br\/>    \/\/ add its childrens to list of live nodes and<br\/>    \/\/ finally deletes it from the list.<br\/>    while (!pq.empty())<br\/>    {<br\/>      \/\/ Find a live node with least estimated cost<br\/>      Node* min = pq.top();<br\/> <br\/>      \/\/ The found node is deleted from the list of<br\/>      \/\/ live nodes<br\/>      pq.pop();<br\/> <br\/>      \/\/ i stores next worker<br\/>      int i = min-&gt;workerID + 1;<br\/> <br\/>      \/\/ if all workers are assigned a job<br\/>      if (i == N)<br\/>      {<br\/>          printAssignments(min);<br\/>          return min-&gt;cost;<br\/>      }<br\/> <br\/>      \/\/ do for each job<br\/>      for (int j = 0; j &lt; N; j++)<br\/>      {<br\/>        \/\/ If unassigned<br\/>        if (!min-&gt;assigned[j])<br\/>        {<br\/>          \/\/ create a new tree node<br\/>          Node* child = newNode(i, j, min-&gt;assigned, min);<br\/> <br\/>          \/\/ cost for ancestors nodes including current node<br\/>          child-&gt;pathCost = min-&gt;pathCost + costMatrix[i][j];<br\/> <br\/>          \/\/ calculate its lower bound<br\/>          child-&gt;cost = child-&gt;pathCost +<br\/>            calculateCost(costMatrix, i, j, child-&gt;assigned);<br\/> <br\/>          \/\/ Add child to list of live nodes;<br\/>          pq.push(child);<br\/>        }<br\/>      }<br\/>    }<br\/>}<br\/> <br\/>\/\/ Driver code<br\/>int main()<br\/>{<br\/>    \/\/ x-cordinate represents a Worker<br\/>    \/\/ y-cordinate represents a Job<br\/>    int costMatrix[N][N] =<br\/>    {<br\/>        {9, 2, 7, 8},<br\/>        {6, 4, 3, 7},<br\/>        {5, 8, 1, 8},<br\/>        {7, 6, 9, 4}<br\/>    };<br\/> <br\/> <br\/>    \/* int costMatrix[N][N] =<br\/>    {<br\/>        {82, 83, 69, 92},<br\/>        {77, 37, 49, 92},<br\/>        {11, 69,  5, 86},<br\/>        { 8,  9, 98, 23}<br\/>    };<br\/>    *\/<br\/> <br\/>    \/* int costMatrix[N][N] =<br\/>    {<br\/>        {2500, 4000, 3500},<br\/>        {4000, 6000, 3500},<br\/>        {2000, 4000, 2500}<br\/>    };*\/<br\/> <br\/>    \/*int costMatrix[N][N] =<br\/>    {<br\/>        {90, 75, 75, 80},<br\/>        {30, 85, 55, 65},<br\/>        {125, 95, 90, 105},<br\/>        {45, 110, 95, 115}<br\/>    };*\/<br\/> <br\/> <br\/>    cout &lt;&lt; &quot;\\nOptimal Cost is &quot;<br\/>        &lt;&lt; findMinCost(costMatrix);<br\/> <br\/>    return 0;<br\/>}<br\/>Run on IDE<\/code><\/pre> <\/div>\n<p>Output :<\/p>\n<pre>Assign Worker A to Job 1\r\nAssign Worker B to Job 0\r\nAssign Worker C to Job 2\r\nAssign Worker D to Job 3\r\n\r\nOptimal Cost is 13<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Branch And Bound (Job Assignment Problem) &#8211; Branch And Bound &#8211; It is required to perform all jobs by assigning exactly one worker to each job.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,79474],"tags":[79712,79715,79717,79713,79706,79708,79705,79709,79714,79710,79707,79711,79704,79718,79716],"class_list":["post-26700","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-branch-and-bound","tag-assignment-problem-definition","tag-assignment-problem-hungarian-method-maximization","tag-assignment-problem-ppt","tag-assignment-problem-solver","tag-assignment-problem-using-branch-and-bound-ppt","tag-branch-and-bound-method-for-assignment-problem","tag-branch-and-bound-problems-with-solutions","tag-c-code-for-job-assignment-problem-using-branch-and-bound","tag-how-to-solve-assignment-problem-in-operational-research","tag-job-assignment-problem-algorithm","tag-job-assignment-problem-example","tag-job-assignment-problem-in-c-program","tag-job-assignment-problem-using-branch-and-bound-algorithm","tag-solved-questions-assignment-problem","tag-unbalanced-assignment-problem-example"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26700"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26700\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}