C2java Greedy Job Scheduling

Source: Internet
Author: User

This is what I did when I debug a java project that is not expected to output:
A1 comment out the thrown exception; A2 and print compare Abnormal Input and normal input; A3 and print further to narrow the scope.
In fact, you only need to: B1 calm down and carefully observe the exception stack frame.

A1 is intended to make the program complete the normal process and get the expected results. This is a local optimal choice.
In some cases, we have no choice but to make judgments based on the information we have obtained. To put it bluntly, it is a process of continuous trial and error. For example, on a night in the wild, we need to find some firewood for fire, which is a single-source multi-destination shortest path problem.

Feasible example:
Find min C = \ sum Ci s. t. V = \ sum Vi * Ci.
Hoffman code min BitLength = \ sum Freq (c) * depth (c ).
Score backpack max V = \ sum Vi * Fi s. t. \ sum Wi * Fi <= W.
Select A (I, j) = max {A (I, k) + a (k) + A (k, j)} for an activity with A start and end time )}
Unfeasible example: n tasks are assigned to n people, an integer backpack.


Matroid is (S, I) satisfied
1. S is a finite set.
2. I is A set of independent subsets of S, that is, if B \ in I, A \ subset B, A \ in I.
3. exchange properties: If A, B \ in I, | A | <| B |, an x \ in B-A exists, making A \ nion {x} \ in I.
This concept comes from:
Hassler Whitney's matric matroids, where the S element is the row of the matrix,
Matrix rows are independent if they are normally linear.
Undirected graphs (V, E), S = E, the subset A of E is independent if A is non-ring.
(S, I) is weighted, if each element in S is assigned a positive number.


GREEDY (S, I, W) // returns the subset of the weighted maximum.
A = \ empty
Sort S in descending order based on W
For x \ in S
If A \ nion {x} \ in I
A = A \ nion {x}
Return
It can be proved that the quasi-array has the optimal substructure property, and the greedy algorithm above is correct.
F (n) is required for detection. The entire complexity is O (n * log (n) + n * f (n ))

Scheduling of time-based tasks with deadline and expiration penalty on a single processor.
Scheduling unit-time tasks with deadlines and penalties for a single processsor.
How to arrange tasks in the task set S to minimize the penalty value.

Conversion to a subset:
For a given arrangement, the penalty value can always be maintained, and the expired a is placed after B without expiration, because after a is adjusted to a, B is still not expired; similarly, in non-expired tasks, the total number of tasks can be sorted in descending order by the expiration time.
Because the sum of the penalty values is a constant, the minimum penalty for expiration is <=> the maximum penalty for non-expiration.
Solution to the original problem:
Sort the non-expiration date in ascending order of the deadline, and then sort the expiration date at the end.


Application quasi-array framework:
Subset A is called an independent task. If A scheduling task exists, the task does not expire. Note that all independent subsets are I.
How can we determine if A subset A is independent?
N (t, A) indicates the number of tasks whose cutoff time does not exceed t in. Since the deadline starts from 1, N (0, A) = 0.
Theorem: A is independent <=> N (t, A) <= t, for t = 0, 1,..., n. ()

Theorem: the system (S, I) is a quasi-array.
The GREEDY algorithm is applied. If you press (A) directly, the total computing complexity is O (n ^ 3 ).

The following example shows how to reduce the computing workload.
Task No. a: 1, 2, 3, 4, 5, 6, 7
Penalty value: p: 70, 60, 50, 40, 30, 20, 10
Expiration value d: 4, 2, 4, 3, 1, 4, 6
Number of <= 1 in N (1, {4}) = {4} = 0 <1
Number of <= 1 in N (1, {}) = {} = 0
Number of <= 2 in N (2, {}) = {} = 1
A = {4, 2, 4}: N (1, A) = 0, N (2, A) = 1, N (3, A) = 1.
A = {4, 2, 4, 3}: N (1, A) = 0, N (2, A) = 1, N (3, A} = 2, N (4, a) = 4.
A = {4, 2, 4, 3, 1}: N (1, A) = 1, N (2, A) = 2, N (3, A) = 3, N (4, A) = 5, N (5, A) = 6.
Rule:
N (t, A + {x}) = N (t, A) + (x <= t? 1: 0) for t = 1, 2,..., | A |
"N (t, A + {x}) = N (t-1, A + {x}) + (x <= t? 1: 0) for t = | A | + 1
Open an array to record the N value corresponding to the current partial decomposition A. Since 1 in the above example cannot be added, you have to find A solution.
How to maintain the semantics of this array. Obviously N (t, A) is monotonic increasing about t, if we start from

T = | A | + 1 can be excluded. If not, the array is updated. "This is wrong. It is not true for the last two groups.


With the idea of counting and sorting, Calculating N (t, A) from left to right is A process of partial and accumulation.

Another solution:
An ideal scenario is that the end time of the I-th task is exactly I, for all I =,..., n.
Obviously, the answer is to directly place the I-th task on the time slot I, and the penalty value is zero.
Expansion is normal. For the I-th task, the end time slot d [I] is either empty or not empty.
For the former, the direct intuitive idea is to put it directly. If it is left blank, it is better to put it in front of d [I]. For the latter, start from it and find the first vacant space. If there is no vacant space in front of it, you have to be fined. put it at the end.
The calculation result is as follows: 4, 2, 3, 1, 5, 7, 6, and 5 times out. The penalty value is 50. Is it a coincidence that it is exactly the same as the above math solution?
If this scheme is directly calculated, the total complexity is n ^ 2, where n is the time for finding the vacant space each time. We can use the query set to optimize it to a constant smaller than 10.
It is omitted here...

Finally, I will share some philosophical comments.
In the secular sense, greed is a derogatory term used to describe a person's character. Especially bankers who have broken down many labor companies.
From this point of view, the moral Scriptures should not be full, but have some shortcomings.
However, from the two examples of task scheduling and the shortest circuit, they seem to be internal physical properties, just like the water trend, where the total number of atoms is from the high energy orbit to the Low Energy steady state.
And once it is put on a math coat, it will feel that it is no longer people's subjective will, but an objective law.

Ref
[1] System Scheduling Summary section "Since Linux 2.6.23"

Http://en.wikipedia.org/wiki/Scheduling_ (computing)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.