回溯法(2)

來源:互聯網
上載者:User

二、批次工作調度

問題表述:給定n個作業的集合{J1,J2,…,Jn}。每個作業必須先由機器1處理,然後由機器2處理。作業Ji需要機器j的處理時間為tji。對於一個確定的作業調度,設Fji是作業i在機器j上完成處理的時間。所有作業在機器2上完成處理的時間和稱為該作業調度的完成時間和。

批次工作調度問題要求對於給定的n個作業,制定最佳作業調度方案,使其完成時間和達到最小。

顯然,1,3,2是最佳調度方案。

解空間:排列樹(將作業順序進行全排列,分別算出各種情況的完成時間和,取最佳調度方案)

實現:

/* 主題:批次工作調度演算法* 作者:chinazhangjie* 郵箱:chinajiezhang@gmail.com* 開發語言:C++* 開發環境:Mircosoft Virsual Studio 2008* 時間: 2010.10.24*/#include <iostream>#include <vector>using namespace std;class flowshop {public:flowshop(vector<vector<int> >& rhs) {task_count = rhs.size() ;each_t = rhs ;best_t.resize (task_count) ;machine2_t.resize (task_count,0) ; machine1_t = 0 ;total_t = 0 ;best_total_t = 0 ;current_t.resize (task_count,0) ;for (int i = 0 ;i < task_count; ++ i) {current_t[i] = i; // 為了實現全排列}}void backtrack () {__backtrack (0);// 顯示最佳調度方案和最優完成時間和cout << "the best flowshop scheme is : ";copy (best_t.begin(),best_t.end(),ostream_iterator<int> (cout, " "));cout << endl;cout << "the best total time is : " << best_total_t << endl;}private:void __backtrack (int i) {if (i >= task_count) {if (total_t < best_total_t || best_total_t == 0) {// 儲存當前最優調度方式copy (current_t.begin(),current_t.end(),best_t.begin()) ;best_total_t = total_t;}return ;}for (int j = i; j < task_count; ++ j) {// 機器1上結束的時間machine1_t += each_t[current_t[j]][0] ;if (i == 0) {machine2_t[i] = machine1_t + each_t[current_t[j]][1] ;}else {// 機器2上結束的時間machine2_t[i] = ((machine2_t[i - 1] > machine1_t) ? machine2_t[i - 1] : machine1_t) + each_t[current_t[j]][1] ;}total_t += machine2_t[i];// 剪枝if (total_t < best_total_t || best_total_t == 0) {// 全排列swap (current_t[i],current_t[j]) ;__backtrack (i + 1) ;swap (current_t[i],current_t[j]) ;}machine1_t -= each_t[current_t[j]][0] ;total_t -= machine2_t[i] ;}}public :int task_count ;// 作業數vector<vector<int> >  each_t ;// 各作業所需的處理時間vector<int>current_t ;// 當前作業調度vector<int> best_t ;// 當前最優時間調度vector<int> machine2_t ;// 機器2完成處理的時間int machine1_t ;// 機器1完成處理的時間int total_t ;// 完成時間和int best_total_t ;// 當前最優完成時間和};int main(){// const int task_count = 4;const int task_count = 3 ;vector<vector<int> > each_t(task_count) ;for (int i = 0;i < task_count; ++ i) {each_t[i].resize (2) ;}each_t[0][0] = 2 ;each_t[0][1] = 1 ;each_t[1][0] = 3 ;each_t[1][1] = 1 ;each_t[2][0] = 2 ;each_t[2][1] = 3 ;// each_t[3][0] = 1 ;// each_t[3][1] = 1 ;flowshop fs(each_t) ;fs.backtrack () ;}

三、n後問題 

問題表述:在n×n格的棋盤上放置彼此不受攻擊的n個皇后。按照國際象棋的規則,皇后可以攻擊與之處在同一行或同一列或同一斜線上的棋子。n後問題等價於在n×n格的棋盤上放置n個皇后,任何2個皇后不放在同一行或同一列或同一斜線上。求不同的解的個數。

 

解向量:(x1, x2, … , xn)

顯約束:xi = 1,2, … ,n

隱約束:

    1)不同列:xi != xj

    2)不處於同一正、反對角線:|i-j| != |x(i)-x(j)|

解空間:滿N叉樹

實現:

/* 主題:n後問題* 作者:chinazhangjie* 郵箱:chinajiezhang@gmail.com* 開發語言:C++* 開發環境:Mircosoft Virsual Studio 2008* 時間: 2010.10.24*/#include <iostream>#include <vector>using namespace std;class queen{// 皇后在棋盤上的位置struct q_place {int x;int y;q_place () : x(0),y(0) {}};public:queen(int qc) : q_count (qc), sum_solution (0) {curr_solution.resize (q_count);}void backtrack () {__backtrack (0);}private:void __backtrack (int t) {if (t >= q_count) {// 找到一個解決方案++ sum_solution ;for (size_t i = 0;i < curr_solution.size(); ++ i) {cout << "x = " << curr_solution[i].x << " y = " << curr_solution[i].y << endl;}cout << "sum_solution = " << sum_solution << endl;}else {for (int i = 0;i < q_count; ++ i) {curr_solution[t].x = i;curr_solution[t].y = t;if (__place(t)) {__backtrack (t + 1);}}}}// 判斷第k個皇后的位置是否與前面的皇后相衝突bool __place (int k) {for (int i = 0; i < k; ++ i) {if ((abs(curr_solution[i].x - curr_solution[k].x) == abs(curr_solution[i].y - curr_solution[k].y))|| curr_solution[i].x == curr_solution[k].x) {return false;}}return true;}private:vector<q_place> curr_solution;// 當前解決方案const int q_count;// 皇后個數int sum_solution;// 當前找到的解決方案的個數};int main() {queen q(5);q.backtrack ();return 0;}

 

參考資料  《演算法設計與分析》王曉東編著

授課教師 張陽教授

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.