7

So I have a job assignment problem that doesn't have the traditional cost the Hungarian method requires.

For example:

I have 3 workers - A, B and C
I have 5 jobs -  1, 2, 3, 4 and 5

Each worker has a list of jobs he can perform, like so:

worker A can work on job 1, 2, 5
worker B can work on job 1, 2
worker C can work on job 1

The end result (since there's no cost) is the maximum number of assignments I can achieve. In this example, I can achieve a maximum of 3 assignments:

worker A on job 5
worker B on job 2
worker C on job 1

Is the Hungarian method a good way to solve this? Should I just use "dummy" costs? I was thinking maybe using the index of the job preference as the cost; is this a good idea?

Cœur
  • 37,241
  • 25
  • 195
  • 267
sap
  • 1,188
  • 1
  • 14
  • 25
  • Since there is no cost, how do you compare two different assignments? – zw324 May 09 '13 at 14:13
  • i was thinking to add "dummy" costs based on the job preference index, for example worker A for job 5 has a cost of 3 (because its the 3rd job in that worker list), is this a good idea? – sap May 09 '13 at 14:16

4 Answers4

6

The Hungarian algorithm could be made to work here, but an algorithm for unweighted maximum bipartite matching like Hopcroft–Karp would be faster.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
5

Assign the cost -1 to the job which they can, and the others is zero.

Then run the Hungarian algorithm and it will give you the answer(It will returns -answer,in fact).

Don't do it with some large numbers, it may cause overflow(unless you implement the Hungarian very carefully).

Indeed, it's Maximum matchings in bipartite graphs, and there are so many ways to solve this problem, see wiki pages:

http://en.wikipedia.org/wiki/Matching_(graph_theory)#Maximum_matchings_in_bipartite_graphs

PS:Hopcroft–Karp algorithm is faster than hungarian and is more simple also. It worth a try. Some compulicated method is faster than these two, but it's not recommanded to learn these algorithms at very first.

PSS: Your ID in stackoverflow is a method to solve this problem. It's a network flow way. It's called shortest argument path(sap). See:http://coral.ie.lehigh.edu/~ted/files/ie411/lectures/Lecture11.pdf

Sayakiss
  • 6,878
  • 8
  • 61
  • 107
  • You are ignoring the preferences. Probably you can do plumbing, but how happy would you be (joking)? – zw324 May 09 '13 at 14:23
  • sorry i should have said that the preferences are not important, as long as i can assign the maximum number of jobs possible. – sap May 09 '13 at 14:25
  • @ZiyaoWei I don't understand what is the preferences exactly. – Sayakiss May 09 '13 at 14:25
  • lol and now i have to learn the shortest argument path method, is a matter of honor :p – sap May 09 '13 at 15:03
  • @sap It's a little bit hard, you may learn some basic algorithm first then try to understand network flow and sap. – Sayakiss May 09 '13 at 15:05
  • @Sayakiss Darn! Now *I* have to learn about sap. I've been [nerd-sniped](https://xkcd.com/356/)! (And I was just passing by... ;-) – jpaugh May 08 '16 at 05:24
2

Dummy costs should do the trick. Assign a cost of 1 to any job they can do, and an infinite cost (if your system allows that) to jobs they can't. The Hungarian algorithm is designed to minimize the total cost across all tasks, so it'll figure things out naturally. There shouldn't be any need to account for what you think their job preferences are; that's the algorithm's job.

Jeremy Todd
  • 3,261
  • 1
  • 18
  • 17
  • thanks for the answer, i thought about that first but was afraid that the end matrix would be filled with 0s after row and column minimization (except on the positions with infinite cost), but maybe thats not a problem? I'm still new to the algorithm and trying to learn it. – sap May 09 '13 at 14:21
  • That shouldn't be a problem -- all that means is that there may be more than one equally good solution. – Jeremy Todd May 09 '13 at 14:43
1

Hungarian algorithm will give you an answer, but do not use infinity costs, since you cannot compare (infinity + infinity) and infinity (unless you compare the costs yourself).

A: 1, 2, 3

B: 1

C: 1

The matrix form:

  1   2   3

A 1   2   3

B 1   inf inf

C 1   inf inf

How can your computer compare 1, inf, inf and 2, 1, inf?

Instead, use some cost that is so large that it will guarantee to be not assigned (and yes, be careful with overflowing).

zw324
  • 26,764
  • 16
  • 85
  • 118
  • Unfortunately, it's impossible to determine such a cost in the general case. – Jeremy Todd May 09 '13 at 14:30
  • That's true - haven't look at Hungarian for quite some time, but with a not too clearly defined preferences, it's going to be hard to modify it (I doubt), unless like you and the other answer did, ignore the preferences. – zw324 May 09 '13 at 14:34