Back - optimality problem with the order processing Berman law

1. Analysis

In the previous blog in https://blog.csdn.net/Jayphone17/article/details/102996649

We solve the optimal processing machine parts processing sequence problems with backtracking

We analyze the complexity of the situation:

(1) time complexity, as shown:

The worst case, except for the last layer, there are 1 + n + n (n- 1) + ... + n (n-1) (n-2) ... ≤ nn! Nodes need to determine bounding function determines bounding function requires O (1) time, thus consuming O (nn!). Processed optimal solution in a leaf node records O (n), searches for the worst case for each leaf node, there is the number of leaves n !, Processed O (nn!). Finally, the time complexity is O (nn!) ≈O (( n + 1)!).

Complexity (2) space. Use of X [] array longest path recorded as a feasible solution space complexity is O (n-) .

Bellman using optimization rules, the time complexity can be achieved O (nlogn) .

Suppose the S set processing sequence, one of the most only two processing schemes schemes:

  • Part No. i before processing, reprocessing part number j, the other workpiece is optimal sequence
  • First processing part number j, i reprocessing number of parts, the other workpiece is optimal sequence

The Behrman derived formula: https://blog.csdn.net/Jayphone17/article/details/103013335

Scheme 1 Scheme preferred ratio is necessary and sufficient: max {t1j, t2i} ≥ max {t1i, t2j}.

After the analysis continues:

Berman rules can be obtained:

  • The first machine to the workpiece shorter processing times
  • After the second stage machine the workpiece shorter processing times
  • The processing time is less than the first machine to a second machine processing Processing Time
  • Machining the first post-processing time is greater than or equal to the second machine processing time

2. Algorithm Design

One,

(1) According to Baermann rule set can be divided into two parts: N1 = {i | t1i <t2i}, i.e., the processing time is less than the first machine processing time on the second machine;

         N2 = {i | t1i> = t2i}, i.e. the first processing machine time is greater than or equal processing time on the second machine

(2) The non-N1 t1i descending order according to the workpiece, the workpiece according to the N2 non t2i ascending order.

(3) N1 N2 workpiece contact the workpiece, i.e., the optimal processing sequence N1N2 rule is satisfied Berman asked for.

two,

Because C ++ can be self-defined priority ordering function, and therefore define priority cmp, then you can call the sort function.

bool cmp(node a,node b)
{
    return min(b.x,a.y)>=min(a.x,b.y);
}
sort (T,T+n,cmp);

This specific priority What does it mean?

There are, for example, a, b two parts, the first machining time is x, the second-machine processing time y.

min (bx, ay) = min (10,7) = 7.

min (city, ax) = min (2,3) = 2.

min (bx, ay) ≥ min (by, ax). // Bellman rule! ! ! !

Thus processed ahead of a part b.

3. Source Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

 const int MX=11111;
 int n;
 struct node
 {
     int x;
     int y;
     int id;
 }T[MX];

 bool cmp(node a,node b)
 {
     return min(b.x,a.y) >=min(b.y,a.x);//贝尔曼规则条件的排序
 }

int main()
{
    cout << "请输入机器零件的个数n:"<< endl;
    cin>> n;
    cout << "请依次输入每个机器零件在第一台机器上的加工时间x和第二台机器上的加工时间y:"<< endl;
    for (int i=0;i<n;i++)
    {
        cin >> T[i].x >> T[i].y;
        T[i].id=i+1;
    }
    sort(T,T+n,cmp);
    int f1=0;
    int f2=0;
    for (int i=0;i<n;i++)
    {
        f1+=T[i].x;
        f2=max(f1,f2)+T[i].y;
    }
    cout << "最优的机器零件加工顺序是:"<< endl;
    for (int i=0;i<n;i++)
    {
        cout << T[i].id ;
    }
    cout << endl;
    cout << "最优的零件加工时间是:"<< endl;
    cout << f2 << endl;
    return 0;
}

 

4. The results of the tests 

The last time complexity is O (nlogn).

The last space complexity is O (n).

Published 57 original articles · won praise 9 · views 3604

Guess you like

Origin blog.csdn.net/Jayphone17/article/details/103038988
law