Introduction to algorithms-Dynamic Planning (2) Case Study of assembly line Scheduling

Source: Internet
Author: User

It was originally intended to get the introduction to algorithms done before March. It has been more than a month since now, and I only saw chapter 15th, but I only saw some details later, I don't know if I can watch it before the holiday... Sorry, I'm about to take the final exam. I got a degree warning when I had less than 2 GPA in the last semester. Although I don't want to study this major in the future, I can't go into the transcript at least, right... If you don't know it at ordinary times, you won't be able to use chunge, Zeng Ge, or Guan Gong Ge before the test... This progress is depressing!

Try your best!

By the way, there are two sentences:

1. some books are quite well analyzed, and I don't want to be a superfluous person, so I will omit some places as appropriate. Of course, it doesn't mean that what I summarize is a bad place in the book, no one has a set of ways to understand it. I summed it up in my own words. At that time, it was the most suitable knowledge for me! Therefore, it is recommended that you write more algorithm summaries and you will appreciate the benefits!

2. in addition, the nature of this is a summary. It is not a specific explanation of an algorithm. Therefore, if you do not read a book first, you should not understand it. For example, I did not post the question below, if you don't read the data, you certainly don't understand it. My goal is to thank you for your summary after reading the book, or you may find something wrong with your understanding, where it is not understood, or where it is worth exploring.

This is mainly an example of section 15.1-assembly line scheduling.

The question is a little long. First, you must read the question.

In this example, we spent six pieces of paper for detailed analysis, which shows its importance.

Speaking of DP, we have to talk about the violence law. Everyone knows that if we solve similar problems with violence, the time complexity is usually very high. At this time, the savior DP will come out, DP greatly reduces the time complexity by avoiding many repeated computations.

According to the four steps in the book, I would like to extract some key points here. We recommend that you set P194 ~ 196 complete analysis on the four steps. You will discover the beauty of "Introduction to algorithms" Only when you taste it slowly.

Step 1:

Analyze the problem. For example, if a chassis needs to reach S [1] [j], there are two situations, the first is from S [1] [J-1] To S [1] [j], and the second is from S [2] [J-1] To S [1] [j]. Find the minimum time spent by the two, that is, the minimum time required for S [1] [j.

This is to find the global optimal solution with the local optimal solution. That is to say, the optimal solution of a problem includes an optimal solution of a subproblem. We call this property an optimal sub-structure. This is one of the signs that DP can be applied.

Step 2:

Find out the recursive relationship. Step 1 shows the recursive relationship:

Step 3:

Because of the recursive relationship, it can always be converted to non-recursive algorithms.

From the known f1 [1], f2 [1], we gradually launched unknown quantities, pushed, pushed, and finally pushed to the results ~~~~

Step 4:

Return the path from the known results.

This section provides the following examples and corresponding graphs:

Pick up the pen and use the example in the book to analyze the graph!

The following code is used:

 

/*
Author: Tanky Woo
Blog: www.WuTianQi.com
About: algorithm introduction 15.1 assembly line Scheduling
*/
# Include <iostream>
Using namespace std;
 
Int n; // There are n assembly stations on an assembly line
Int e1, e2; // time required to enter assembly line 1, 2
Int x1, x2; // time required to exit assembly line 1 and 2
Int t [3] [100]; // t [1] [j] indicates the time required to move the chassis from S [1] [j] To S [2] [j + 1]. Likewise, t [2] [j]
Int a [3] [100]; // a [1] [j] indicates the time required in the assembly station S [1] [j]
Int f1 [100], f2 [100]; // f1 [j], f2 [j] indicates the optimal solution of the first assembly station on the first or second assembly line.
Int ln1 [100], ln2 [100]; // ln1 [j] records the first assembly line, when the optimal solution is used, the first assembly station of the first assembly station is the first line or the second line.
Int f, ln; // The optimal solution is. f indicates the minimum time spent, and ln indicates whether to start from assembly line 1 or assembly line 2.
 
Void DP ()
{
F1 [1] = e1 + a [1] [1];
F2 [1] = e2 + a [2] [1];
For (int j = 2; j <= n; ++ j)
{
// Process the optimal sub-structure of the First Assembly Line
If (f1 [J-1] + a [1] [j] <= f2 [J-1] + t [2] [J-1] + a [1] [j])
{
F1 [j] = f1 [J-1] + a [1] [j];
Ln1 [j] = 1;
}
Else
{
F1 [j] = f2 [J-1] + t [2] [J-1] + a [1] [j];
Ln1 [j] = 2;
}
// Process the optimal sub-structure of the Second Assembly Line
If (f2 [J-1] + a [2] [j] <= f1 [J-1] + t [1] [J-1] + a [2] [j])
{
F2 [j] = f2 [J-1] + a [2] [j];
Ln2 [j] = 2;
}
Else
{
F2 [j] = f1 [J-1] + t [1] [J-1] + a [2] [j];
Ln2 [j] = 1;
}
}
If (f1 [n] + x1 <= f2 [n] + x2)
{
F = f1 [n] + x1;
Ln = 1;
}
Else
{
F = f2 [n] + x2;
Ln = 2;
}
}
 
Void PrintStation ()
{
Int I = ln;
Cout <"line" <I <", station" <n <endl;
For (int j = n; j> = 2; -- j)
{
If (I = 1)
I = ln1 [j];
Else
I = ln2 [j];
Cout <"line" <I <, station "<J-1 <endl;
}
}
 
Int main ()
{
// Freopen ("input.txt", "r", stdin );
Cout <"Number of input assembly stations :";
Cin> n;
Cout <"Enter the time required to enter assembly line 1, 2, e1, e2 :";
Cin> e1> e2;
Cout <"Enter the time required to exit assembly line 1, 2 x1, x2 :";
Cin> x1> x2;
Cout <"Enter the processing time of each station on assembly line 1. a [1] [j]:";
For (int I = 1; I <= n; ++ I)
Cin> a [1] [I];
Cout <"Enter the processing time of each station on assembly line 2. a [2] [j]:";
For (int I = 1; I <= n; ++ I)
Cin> a [2] [I];
Cout <"Enter the time required for the station on assembly line 1 to the station on assembly line 2 t [1] [j]:";
// Note that I <n, not I <= n
For (int I = 1; I <n; ++ I)
Cin> t [1] [I];
Cout <"Enter the time required for the station on assembly line 2 to the station on assembly line 1 t [2] [j]:";
For (int I = 1; I <n; ++ I)
Cin> t [2] [I];
DP ();
Cout <"the fastest time required:" <f <endl;
Cout <"route:" <endl;
PrintStation ();
Cout <endl;
}
 

Finally, I would like to lament that the introduction to algorithms is really great. I hope you can take a look at this chapter.

Related Article

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.