I need to find an optimal distribution of some matching tasks on multiple jobs; each job is limited by a maximum workload and the grouped tasks should maximize the workload within the same job.

However, tasks with similar values within the same source should have a higher priority to be included in the same job.

```Task  Source1  Source2  Workload
1     A        A        40
2     A        B        15
3     B        A        48
4     B        B        18
5     C        A        32
6     C        B        24
7     A        C        8

```

In case the maximum workload is 50, the tasks should be distributed as follows.

```Job1
1     A        A        40
7     A        C        8

Job2
2     A        B        15
6     C        B        24

Job3
3     B        A        48

Job4
4     B        B        18
5     C        A        32
```

Any clues?

• "tasks with similar values" which values? The workload or...? Jul 23, 2016 at 20:55
• No, in my example, the A, B, C values which represents e.g. a type of the values to be matched together Jul 23, 2016 at 20:59
– gnat
Feb 2, 2018 at 13:16

In your example solution, jobs 1 and 2 manage to group similar values within the same source but jobs 3 and 4 don't. All jobs keep their total workload under 50.

This example solution appears to be the result of a greedy algorithm that grabs the first possible match.

In general, greedy algorithms have five components:

1. A candidate set, from which a solution is created