Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
A. ALGORITHM
input.txt :
10 4
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
4 4 1 2
Before program examines jobs variables and matrix:
1 2 3 4 5 6 7 8 9 10
P1 0 0 0 0 0 0 0 1 1 1 4
P2 0 0 0 1 1 1 1 0 0 0 4
P3 0 1 1 0 0 1 1 0 0 1 1
P4 1 0 1 0 1 0 1 0 1 0 2
1 1 2 1 2 2 3 1 2 2
0 0 0 0 0 0 0 0 0 0
Processors
Jobs
1. Scan j array for all 1 values. That means that job can only be done by
only one processor:
1 2 3 4 5 6 7 8 9 10
P1 0 0 0 0 0 0 0 1 1 1 4
P2 0 0 0 1 1 1 1 0 0 0 4
P3 0 1 1 0 0 1 1 0 0 1 1
P4 1 0 1 0 1 0 1 0 1 0 2
1 1 2 1 2 2 3 1 2 2
0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10
P1 0 0 0 0 0 0 0 1 1 1 4
P2 0 0 0 1 1 1 1 0 0 0 4
P3 0 1 1 0 0 1 1 0 0 1 1
P4 1 0 1 0 1 0 1 0 1 0 2
1 1 2 1 2 2 3 1 2 2
0 0 0 0 0 0 0 0 0 0
3. Write that processor to result array and decrease processor’s capacity:
1 2 3 4 5 6 7 8 9 10
P1 0 0 0 0 0 0 0 1 1 1 4
P2 0 0 0 1 1 1 1 0 0 0 4
P3 0 1 1 0 0 1 1 0 0 1 1
P4 1 0 1 0 1 0 1 0 1 0 1
1 1 2 1 2 2 3 1 2 2
4 0 0 0 0 0 0 0 0 0
allSet = 1
1 2 3 4 5 6 7 8 9 10
P1 0 0 0 0 0 0 0 1 1 1 4
P2 0 0 0 1 1 1 1 0 0 0 4
P3 0 1 1 0 0 1 1 0 0 1 1
P4 1 0 1 0 1 0 1 0 1 0 1
1 1 2 1 2 2 3 1 2 2
4 0 0 0 0 0 0 0 0 0
.
.
.
1 2 3 4 5 6 7 8 9 10
P1 0 0 0 0 0 0 0 1 1 1 4
P2 0 0 0 1 1 1 1 0 0 0 4
P3 0 1 1 0 0 1 1 0 0 1 0
P4 1 0 1 0 1 0 1 0 1 0 2
1 1 2 1 2 2 3 1 2 2
4 3 0 0 0 0 0 0 0 0
allSet = 2
Capacity of processor-3 is full now. If there is a job that can be done by
processor-3, now cannot be done by processor-3:
1 2 3 4 5 6 7 8 9 10
P1 0 0 0 0 0 0 0 1 1 1 4
P2 0 0 0 1 1 1 1 0 0 0 4
P3 0 1 1 0 0 1 1 0 0 1 0
P4 1 0 1 0 1 0 1 0 1 0 2
1 1 2 1 2 2 3 1 2 2
4 3 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10
P1 0 0 0 0 0 0 0 1 1 1 4
P2 0 0 0 1 1 1 1 0 0 0 4
P3 0 1 0 0 0 0 0 0 0 0 0
P4 1 0 1 0 1 0 1 0 1 0 2
1 1 1 1 2 1 2 1 2 1
4 3 0 0 0 0 0 0 0 0
.
.
.
At the end of the first loop of the program our result will be:
1 2 3 4 5 6 7 8 9 10
P1 0 0 0 0 0 0 0 1 1 1 1
P2 0 0 0 1 1 1 1 0 0 0 0
P3 0 1 0 0 0 0 0 0 0 0 0
P4 1 0 1 0 1 0 1 0 1 0 0
1 1 1 1 2 1 2 1 2 1
4 3 4 2 2 2 2 1 1 1
allSet = 10
Because of allSet value reached to number of jobs. Program will not enter to
second loop. Program writes output file and ends.
input.txt
4 4
0 0 0 1
0 0 1 0
1 1 0 0
1 1 0 0
1 1 1 1
1 2 3 4
P1 0 0 1 1 1
P2 0 0 1 1 1
P3 0 1 0 0 1
P4 1 0 0 0 1
1 1 2 2
0 0 0 0
1 2 3 4
P1 0 0 1 1 1
P2 0 0 1 1 1
P3 0 1 0 0 0
P4 1 0 0 0 0
1 1 2 2
4 3 0 0
allSet = 2
Because of allSet value did not reach to number of jobs. It means number of
conflict jobs is equal to (jobs value – allSet value) . So a variable (g in the
program) is assigned to this value and second loop will be started:
1. Take the first 1 value of the first conflict job:
1 2 3 4
P1 0 0 1 1 1
P2 0 0 1 1 1
P3 0 1 0 0 0
P4 1 0 0 0 0
1 1 2 2
4 3 0 0
1 2 3 4
P1 0 0 1 1 1
P2 0 0 0 1 1
P3 0 1 0 0 0
P4 1 0 0 0 0
1 1 2 2
4 3 1 0
1 2 3 4
P1 0 0 1 0 0
P2 0 0 0 1 1
P3 0 1 0 0 0
P4 1 0 0 0 0
1 1 2 1
4 3 1 0
4. At the end of the program:
1 2 3 4
P1 0 0 1 0 0
P2 0 0 0 1 0
P3 0 1 0 0 0
P4 1 0 0 0 0
1 1 1 1
4 3 1 2
B. TIME COMPLEXITY
As I described in detail, my algorithm finds solution to given jobs and processors values in a time
complexity of: