Loading…
DAA · Backtracking & B&B · Program 22 · In the record
Write a program in c to solve the job assignment problem using branch and bound and find its execution time using the time function.
Algorithm: JOB-ASSIGNMENT(C, n) (Least Cost Branch and Bound)
(lb(X) = cost of the jobs assigned so far in X
+ for every worker not yet assigned, his cheapest job among the jobs still free)
1. Start
2. Create the root node: no worker assigned, cost <- 0
3. lb(root) <- lower bound of root
4. Insert root into the list of live nodes
5. While the list of live nodes is not empty do
6. E <- the live node with minimum lb
7. Remove E from the list of live nodes
8. if all n workers are assigned in E then
9. print the assignment in E and its cost as the optimal solution
10. Stop
11. i <- the next worker to be assigned in E
12. For each job j not yet assigned in E do
13. Create a child X of E with worker i assigned job j
14. cost(X) <- cost(E) + C[i][j]
15. lb(X) <- lower bound of X
16. Insert X into the list of live nodes
17. Stop1#include<stdio.h>2#include<time.h>3#define INF 999994#define MAX 1000005 6int n,c[15][15],count=0,total=0;7int parent[MAX],level[MAX],job[MAX],used[MAX],cost[MAX],lb[MAX],live[MAX];8 9int bound(int k) {10 int i,j,min,b=cost[k];11 for(i=level[k]+1;i<=n;i++) {12 min=INF;13 for(j=1;j<=n;j++) {14 count++;15 if(!(used[k]&(1<<j)) && c[i][j]<min)16 min=c[i][j];17 }18 b+=min;19 }20 return b;21}22 23void new_node(int p,int j) {24 int k=total++;25 parent[k]=p;26 if(p==-1) {27 level[k]=0;28 job[k]=0;29 used[k]=0;30 cost[k]=0;31 }32 else {33 level[k]=level[p]+1;34 job[k]=j;35 used[k]=used[p]|(1<<j);36 cost[k]=cost[p]+c[level[k]][j];37 }38 lb[k]=bound(k);39 live[k]=1;40}41 42void assignment(int k) {43 if(parent[k]==-1) {44 printf("root");45 return;46 }47 if(parent[parent[k]]!=-1) {48 assignment(parent[k]);49 printf(", ");50 }51 printf("W%d->J%d",level[k],job[k]);52}53 54int main() {55 int i,j,k,E,ans=-1,expanded=0;56 printf("Enter the number of workers/jobs:");57 scanf("%d",&n);58 printf("Enter the %dx%d cost matrix (row = worker, column = job):\n",n,n);59 for(i=1;i<=n;i++)60 for(j=1;j<=n;j++)61 scanf("%d",&c[i][j]);62 63 int order[MAX],first[MAX];64 65 clock_t start=clock();66 new_node(-1,0);67 while(1) {68 E=-1;69 for(k=0;k<total;k++) {70 if(live[k]) {71 count++;72 if(E==-1 || lb[k]<lb[E])73 E=k;74 }75 }76 if(E==-1)77 break;78 live[E]=0;79 if(level[E]==n) {80 ans=E;81 break;82 }83 order[expanded]=E;84 first[expanded]=total;85 expanded++;86 for(j=1;j<=n;j++)87 if(!(used[E]&(1<<j)))88 new_node(E,j);89 }90 clock_t end=clock();91 double time_taken=(double)(end-start)/CLOCKS_PER_SEC;92 93 printf("\nNodes expanded (least lb first):");94 for(i=0;i<expanded;i++) {95 E=order[i];96 printf("\nE = {");97 assignment(E);98 printf("} lb = %d",lb[E]);99 for(k=first[i];k<first[i]+n-level[E];k++)100 printf("\n W%d->J%d cost = %-4d lb = %d",level[k],job[k],cost[k],lb[k]);101 }102 103 printf("\n\nOptimal assignment:");104 for(i=1;i<=n;i++) {105 k=ans;106 while(level[k]!=i)107 k=parent[k];108 printf("\nWorker %d -> Job %d\tcost = %d",i,job[k],c[i][job[k]]);109 }110 printf("\nMinimum total cost:%d",cost[ans]);111 printf("\nNodes generated:%d",total);112 printf("\nNodes expanded:%d",expanded);113 printf("\nNumber of comparisons:%d",count);114 printf("\nTime taken:%f seconds",time_taken);115 printf("\nTime complexity: O(n!) in the worst case, far less when the bound prunes the tree");116 printf("\nSpace complexity: O(n!) in the worst case for the live nodes");117 printf("\n");118 return 0;119}4 ⏎ 9 2 7 8 ⏎ 6 4 3 7 ⏎ 5 8 1 8 ⏎ 7 6 9 4Enter the number of workers/jobs:4
Enter the 4x4 cost matrix (row = worker, column = job):
9 2 7 8
6 4 3 7
5 8 1 8
7 6 9 4
Nodes expanded (least lb first):
E = {root} lb = 10
W1->J1 cost = 9 lb = 17
W1->J2 cost = 2 lb = 10
W1->J3 cost = 7 lb = 20
W1->J4 cost = 8 lb = 18
E = {W1->J2} lb = 10
W2->J1 cost = 8 lb = 13
W2->J3 cost = 5 lb = 14
W2->J4 cost = 9 lb = 17
E = {W1->J2, W2->J1} lb = 13
W3->J3 cost = 9 lb = 13
W3->J4 cost = 16 lb = 25
E = {W1->J2, W2->J1, W3->J3} lb = 13
W4->J4 cost = 13 lb = 13
Optimal assignment:
Worker 1 -> Job 2 cost = 2
Worker 2 -> Job 1 cost = 6
Worker 3 -> Job 3 cost = 1
Worker 4 -> Job 4 cost = 4
Minimum total cost:13
Nodes generated:11
Nodes expanded:4
Number of comparisons:121
Time taken:0.000009 seconds
Time complexity: O(n!) in the worst case, far less when the bound prunes the tree
Space complexity: O(n!) in the worst case for the live nodesRun program 22 in your browser
C · clang → WebAssembly. Change the code or the input, run it, break it, and see what happens.