Greedy algorithm-----Single thread: Active scheduling problem Multithreading: Multi-Machine scheduling problem

Source: Internet
Author: User

First, the characteristics of the greedy algorithm

As the name implies, the greedy algorithm always makes the best choice in the current view. Although the greedy algorithm is not considered from the overall optimal, the choice made by it is only a partial optimal choice in some sense.

The advantage of greedy algorithm is simpler, more direct and more efficient, even if the greedy algorithm can not get the whole optimal solution, but the final result is a good approximate solution of the optimal solution.

Second, the greedy algorithm understanding

Because it involves making the best choices at the moment, the sorting algorithm is often used, and a fast sorting algorithm is recommended, the complexity is O (NLGN), and the efficiency is the highest in the same complexity algorithm.

The sort involved in this article is sorted by bubbling, just stating that it needs to be sorted.

The greedy algorithm can divide the problem from the angle of "single thread" or "multithreading". For example:

(1) Single thread problem

"Scheduling problem": Because the provision of the same resources, and activities do not simultaneously, that is, only one activity can use this resource at the same time, the start and end time of the activity needs to be sorted, that is, single-threaded problem;

"Knapsack problem": note is not often seen in the 0-1 knapsack problem, it is characterized by the choice of items I part, rather than 0 (not selected) and 1 (selection). This case is based on the value of unit weight per item

As the sorted object. Similarly, there is only one backpack, so it belongs to single-threaded problem;

"Optimal Loading Problem": is a batch of containers loaded with a weight limit of ships, no volume restrictions, at this time need to weight from small to large arrangement, the same way only a ship, so it is a single-threaded problem.

(2) Multithreading issues

Multi-threaded problems are mostly multiple servers dealing with multiple tasks, seeking to handle all tasks in the case of the use of the least time problem. Because the task is not limited to processing on a particular server, or is it a comprehensive consideration

These servers, therefore, are multithreaded issues.

Third, the algorithm application example

(1) Single-threaded problem------activity scheduling issues

Problem Description:

A collection of n activities e={1,2,... n}, where each activity requires the use of the same resource, such as a lecture venue, while only one activity can use the resource at the same time. Each activity i has a requirement

The starting time for using the resource Si and fi, and Si<fi. If activity i is selected, it occupies resources within the half-open time interval [Si,fi]. If the interval [Si,fi] and [SJ,FJ] do not intersect, then the activity I and the active J are compatible.

The problem with event scheduling is to select the largest subset of compatible activities in the set of activities given.

Solution Ideas:

By using the greedy algorithm, the activities are arranged in the non-decreasing order of the end time, and the activity with the earliest completion time is selected each time because the input activity is arranged in a non-descending order of the completed time.
, as this would leave as much time as possible for the outstanding activities and maximize the remaining available time periods to arrange for more compatible activities.

Programming:

Example Description:

The algorithm is implemented in detail:

Program results:

The effect is consistent with the diagram.

(2) Multi-machine scheduling problem with multi-threading problem Type one: The machine is identical, the different job processing time is independent

Problem Description:

There are n separate jobs {,... n}, which are processed by the same machine as M-units. The processing time required for job I is ti. It is hereby stipulated that any operation may be
Processing on the machine, but no interruption is allowed until it is completed. Any job cannot be split into smaller sub-jobs.

Solution Ideas:

It is necessary to implement the order of n separate jobs from large to small in time, and here you need to consider:
If n<=m, then the time required is the maximum processing time t in n jobs.
If n>m, assign the job to each machine first, this trip down is assigned m job. Then for each job, choose the machine area with the shortest processing time.
The above is the idea of greedy algorithm.

Example Description:

Specific implementation process:

Programming:

Program results:

is consistent with the results shown.

(3) Multi-machine scheduling problem of multi-threaded problem type two: The task is identical, the execution time of each server is independent

Problem Description:

Assuming that there is a central dispatcher, there are n identical tasks that need to be dispatched to the M server for execution, because each server is configured differently, so the time it takes for the server to perform a task is different.
It is now assumed that the time required for the first server to perform a task is t[i].


For example: There are 2 actuators, A, B, to perform a task requires 7min,10min, there are 6 tasks to be scheduled. If you split the 6 tasks, that is, A, B, 3 tasks, the minimum required is 30min to execute all.
If A is 4 tasks and B is 2 tasks, the shortest 28min is executed.

Solution Ideas:

Use an array to record the time that each machine has been executed (initially 0), and when each task is scheduled, compare the time that each machine needs to execute (the time that is executed plus the time it takes to perform the task).
Select the machine with the shortest execution time for processing.

Programming:

Program results:

The result is consistent with the description of the example.

Four, greedy algorithm summary

From single-threaded and multi-threaded perspective analysis of the problem, and focus on the two different problems of multi-machine scheduling methods used, need to slowly digest understanding, but the overall idea is still very clear, this is the greedy algorithm

A big advantage. From the above problem, almost all of the questions are sorted, even if there is no direct order, it will be compared to make the best choice of the present.

Greedy algorithm-----Single thread: Active scheduling problem Multithreading: Multi-Machine scheduling problem

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.