旅行商问题的n种解法

问题描述:

旅行商问题(Traveling Salesman Problem,TSP)是旅行商要到若干个城市旅行,各城市之间的费用是已知的,为了节省费用,旅行商决定从所在城市出发,到每个城市旅行一次后返回初始城市,问他应选择什么样的路线才能使所走的总费用最短?此问题可描述如下:设G=(V,E)是一个具有边成本cij的有向图,cij的定义如下,对于所有的i和j,cij>0,若<i,j>不属于E,则cij=∞。令|V|=n,并假设n>1。 G的一条周游路线是包含V中每个结点的一个有向环,周游路线的成本是此路线上所有边的成本和。

问题分析:

旅行商问题要从图G的所有周游路线中求取最小成本的周游路线,而从初始点出发的周游路线一共有(n-1)!条,即等于除初始结点外的n-1个结点的排列数,因此旅行商问题是一个排列问题。排列问题比子集合的选择问题通常要难于求解得多,这是因为n个物体有n!种排列,只有 个子集合(n!>O( ))。通过枚举(n-1)!条周游路线,从中找出一条具有最小成本的周游路线的算法,其计算时间显然为O(n!)。

枚举法思想:程序中采用深度优先策略。(采用隐式和显式两种形式)

枚举算法的特点是算法简单,但运算量大,当问题的规模变大,循环的阶数越大,执行的速度越慢。如果枚举范围太大(一般以不超过两百万次为限),在时间上就难以承受。在解决旅行商问题时,以顶点1为起点和终点,然后求{2…N}的一个全排列,使路程1→{2…N}的一个全排列→1上所有边的权(代价)之和最小。所有可能解由(2,3,4,…,N)的不同排列决定。

核心代码(完整源代码见源代码)

  

为便于讨论,介绍一些关于解空间树结构的术语。在下面分析回溯法和分支限界法时都直接或间接用到解空间树。在解空间树中的每一个结点确定所求问题的一个问题状态(problem state)。由根结点到其它结点的所有路径则确定了这个问题的状态空间(state space)。解状态(solution states)表示一些问题状态S,对于这些问题状态,由根到S的那条路径确定了这解空间中的一个元组。答案状态(answer states)表示一些解状态S,对于这些解状态而言,由根到S的这条路径确定了这问题的一个解(即,它满足隐式约束条件)。解空间的树结构称为状态空间树(state space tree)。

对于旅行商问题,一旦设想出一种状态空间树,那么就可以先系统地生成问题状态,接着确定这些问题状态中的哪些状态是解状态,最后确定哪些解状态是答案状态,从而将问题解出。为了生成问题状态,采用两种根本不同的方法。如果已生成一个结点而它的所有儿子结点还没有全部生成,则这个结点叫做活结点。当前正在生成其儿子结点的活结点叫E-结点。不再进一步扩展或者其儿子结点已全部生成的生成结点就是死结点。在生成问题状态的两种方法中,都要用一张活结点表。在第一种方法中,当前的E-结点R一旦生成一个新的儿子C,这个儿子结点就变成一个新的E-结点,当完全检测了子树C之后,R结点就再次成为E-结点。这相当与问题状态的深度优先生成。在第二种状态生成方法中,一个E-结点一直保持到死结点为止。这两种方法中,将用限界函数去杀死还没有全部生成其儿子结点的那些活结点。如果旅行商问题要求找出全部解,则要生成所有的答案结点。使用限界函数的深度优先结点生成方法称为回溯法。E-结点一直保持到死为止的状态生成方法称为分支限界法

回溯法思想:

       为了应用回溯法,所要求的解必须能表示成一个n- 元组(x1,…,Xn),其中x1是取自某个有穷集Si。通常,所求解的问题需要求取一个使某一规范函数P(x1,…,Xn)取极大值(或取极小值或满足该规范函数条件)的向量。

       假定集合Si的大小是mi,于是就有m=m1m2…Mn个n-元组可能满足函数P。所谓硬性处理是构造这m个n-元组并逐一测试它们是否满足P,从而找出该问题的所有最优解。而回溯法的基本思想是,不断地用修改过的函数Pi(x1,…Xi)(即限界函数)去测试正在构造中的n-元组的部分向量(x1,…,Xi),看其是否可能导致最优解。如果判定(x1,…,Xi)不可能导致最优解,那么就可能要测试的后n-i个元素组成的向量一概略去。因此回溯法作的次数比硬性处理作的测试次数(m次)要少得多。用回溯法求解的旅行商问题,即在枚举法的基础上多了一个约束条件,约束条件可以分为两种类型:显示约束和隐式约束。

核心代码(完整源代码见源代码)

 

分支限界法思想:本题采用FIFO分支限界法。

如前所述,分支限界法是在生成当前E-结点全部儿子之后再生成其它活结点的儿子,且用限界函数帮助避免生成不包含答案结点子树的状态空间的检索方法。在总的原则下,根据对状态控件树中结点检索的次序的不同又将分支限界设计策路分为数种不同的检索方法。在求解旅行商问题时,程序中采用FIFO检索(First In First Out),它的活结点表采用一张先进先出表(即队列)。可以看出,分支限界法在两个方面加速了算法的搜索速度,一是选择要扩展的节点时,总是选择选择一个最小成本的结点,尽可能早的进入最有可能成为最优解的分支;二是扩展节点的过程中,舍弃导致不可行解或导致非最优解的子结点。

核心代码(完整源代码见源代码)

 

贪心法思想:

贪心法是一种改进了的分级处理方法。它首先旅行商问题描述,选取一种度量标准。然后按这种度量标准对n个输入城市排序,并按序一次输入一个城市。如果这个输入和当前已构成在这种量度意义下的部分最优解加在一起不能产生一个可行解,则不把这个城市加入到这部分解中。这种能够得到某种量度意义下的最优解的分级处理方法成为谈心方法。

获得最优路径的贪心法应一条边一条边地构造这棵树。根据某种量度来选择将要计入的下一条边。最简单的量度标准是选择使得迄今为止计入的那些边的成本的和有最小增量的那条边。

核心代码(完整源代码见源代码)

 

源代码:

在程序执行目录下建立data.txt文件,用于存放城市节点信息,格式如下:

5  5

0  7  6  1  3  

7  0  3  7  8 

6  3  0  12 11

1  7  12 0  2

3  8  11 2  0

第一行表示为5行5列,之后为各个节点的权值;

程序执行前先建立如下头文件,用于存储和表示节点信息:

 

  1. //------------------------------------- AdjtwGraph.h文件--------------------------------------------------  
  2. #ifndef AdjTWGraph_H  
  3. #define AdjTWGraph_H  
  4. #include <vector>  
  5. #include <iostream>  
  6. using namespace std;  
  7. const int MaxV=100;  
  8. struct Edge  
  9.  {  
  10.     int dest;  
  11.     int weight;  
  12.     Edge * next;  
  13.      Edge(){}  
  14.      Edge(int d,int w):dest(d),weight(w),next(NULL){}  
  15. };  
  16. struct item  
  17.  {    int data;  
  18.     Edge * adj;  
  19. };  
  20. class AdjTWGraph  
  21.  {  
  22. private:  
  23.     item vertices[MaxV];  
  24.     int numV,numE;  
  25. public :  
  26.     AdjTWGraph();  
  27.     ~AdjTWGraph();  
  28.      int NumV(){return numV;}  
  29.      int NumE(){return numE;}  
  30.     int GetValue(const int i);  
  31.     int GetWeight(const int v1,const int v2);  
  32.     void InsertV(const int & vertex);  
  33.     void InsertE(const int v1,const int v2,int weight);  
  34.     friend ostream& operator<<(ostream& os,  AdjTWGraph & m)  
  35.      {     for (int i = 0; i < m.numV ; i++)     {  
  36.             for (int j = 0; j < m.numV; j++)  
  37.                 os << right << m.GetWeight(i,j) << " ";  
  38.             os << endl;  
  39.         }  
  40.         return os;  
  41.     }  
  42.     friend istream& operator>>(istream& is, AdjTWGraph & m)  
  43.      {    int t;  
  44.         for (int i = 0; i < m.NumV(); i++)  
  45.             for (int j = 0; j < m.NumV(); j++)  
  46.              {  
  47.                 is >> t;     m.InsertE(i,j,t);  
  48.             }  
  49.         return is;  
  50.     }  
  51. };  
  52. AdjTWGraph::AdjTWGraph()  
  53.  {  
  54.     for(int i=0;i<MaxV;i++)     vertices[i].adj=NULL;  
  55.     numV=0;numE=0;  
  56. }  
  57. AdjTWGraph::~AdjTWGraph()  
  58.  {  
  59.     for(int i=0;i<numV;i++)  
  60.      {  
  61.         Edge * p=vertices[i].adj,*q;  
  62.         while(p!=NULL)  
  63.          {  
  64.             q=p->next;delete p;p=q;  
  65.         }  
  66.     }  
  67. }  
  68.  int AdjTWGraph::GetValue(const int i){    return vertices[i].data;  }  
  69. int AdjTWGraph::GetWeight(const int v1,const int v2)  
  70.  {  
  71.     Edge *p=vertices[v1].adj;  
  72.     while(p!=NULL && p->dest<v2) p=p->next;  
  73.      if(v2!=p->dest)    {    return 0;    }  
  74.     return p->weight;  
  75. }  
  76.  void AdjTWGraph::InsertV(const int & v) { vertices[numV].data=v; numV++;  }  
  77. void AdjTWGraph::InsertE(const int v1,const int v2,int weight)  
  78.  {  
  79.     Edge * q=new Edge(v2,weight);  
  80.     if(vertices[v1].adj==NULL) vertices[v1].adj=q;  
  81.     else  
  82.      {  
  83.         Edge *curr=vertices[v1].adj,*pre=NULL;  
  84.          while(curr!=NULL && curr->dest<v2)    {    pre=curr;curr=curr->next;    }  
  85.          if(pre==NULL){    q->next=vertices[v1].adj;vertices[v1].adj=q;        }  
  86.          else    {    q->next=pre->next;pre->next=q;    }  
  87.     }  
  88.     numE++;  
  89. }  
  90. #endif  
  91.   
  92. //------------------------------------- tsp.cpp文件--------------------------------------------------  
  93. #include "AdjtwGraph.h"  
  94. #include <fstream>  
  95. #include <vector>  
  96. #include <algorithm>  
  97. #include <ctime>  
  98. #include <queue>  
  99. using namespace std;  
  100. ofstream fout("out.txt");  
  101. int N;  
  102. AdjTWGraph g;  
  103. struct Node      
  104.  {   int currentIndex;  
  105.     int level;  
  106.     Node * previous;  
  107.      Node(int L = 0, int V = 0, Node *p = NULL):level(L),currentIndex(V), previous(p) {}  
  108. };  
  109. class TspBase  
  110.  {  
  111. protected:      
  112.     vector<int> currentPath;  
  113.     vector<int> bestPath;  
  114.     int cv;  
  115.     int bestV;  
  116.     Node * root;  
  117.   
  118.     int SumV();     
  119.     void EnumImplicit(int k);  
  120.     void BackTrackImplicit(int k);  
  121.   
  122.     void EnumExplicit(Node * r);  
  123.     void BackTrackExplicit(Node * r);  
  124.     void FIFOBB();  
  125.   
  126.     bool Valid(Node *p,int v)  //  
  127.          {    bool flag = true;  
  128.             for(Node *r = p; r->level > 0 && V; r = r->previous)  flag = r->currentIndex !=v;  
  129.             return flag;  
  130.         }  
  131.     void StoreX(Node * p) //  
  132.          {for(Node *r = p; r->level >0 ; r = r->previous )  
  133.  {    currentPath[r->level-1] = r->currentIndex;    }  
  134.         }  
  135.     void Print();  
  136. public:  
  137.      TspBase(){currentPath.resize(N);    bestPath.resize(N);    }  
  138.  ~TspBase(){currentPath.resize(0);bestPath.resize(0);}  
  139.   
  140.     void TspEnumImplicit();  
  141.     void TspBackTrackImplicit();  
  142.   
  143.     void TspEnumExplicit();          
  144.     void TspBackTrackExplicit();  
  145.     
  146.     void TspBB();  
  147.   
  148.     void TspGreedy();       
  149.       
  150.     void DataClear(bool flag)  
  151.      {   currentPath.resize(N);        bestPath.resize(N);  
  152.          if(flag)        { Node * p=root,*q;  
  153.                       while(p!=NULL) {q=p->previous; delete p; p=q;}      
  154.         }  
  155.     }  
  156. };  
  157. void TspBase::TspEnumImplicit()  //         枚举隐式  
  158.  {    fout<<"TspEnumImplicit ..."<<endl;  
  159. cv=0; bestV=10000;  
  160.     for(int i=0;i<N;i++)    currentPath[i]=i;  
  161.     EnumImplicit(1);  
  162.     Print();  
  163. }  
  164. void TspBase::EnumImplicit(int k)  
  165.  {    if(k == N)  
  166.      {    if((cv + g.GetWeight(currentPath[N-1],0)) < bestV)  
  167.          {  
  168.             bestV = cv + g.GetWeight(currentPath[N-1],0);  
  169.             for(int i = 0; i < N; i++)  
  170.               bestPath[i] = currentPath[i];  
  171.         }          
  172.     }  
  173.     else  
  174.         for(int j = k; j < N; j++)  
  175.          {    swap(currentPath[k],currentPath[j]);  
  176.             cv += g.GetWeight(currentPath[k-1],currentPath[k]);  
  177.             EnumImplicit(k+1);  
  178.             cv -= g.GetWeight(currentPath[k-1],currentPath[k]);  
  179.             swap(currentPath[k],currentPath[j]);  
  180.         }  
  181. }  
  182. void TspBase::TspEnumExplicit()    //  枚举显式  
  183.  {   fout<<"TspEnumExplicit  ..."<<endl;  
  184. cv=0;     bestV=10000;  
  185.      for(int i=0;i<N;i++)     currentPath[i]=i;  
  186.      root=new Node(0,-1,NULL);  
  187.      EnumExplicit(root);  
  188.      Print();  
  189. }  
  190.   
  191. void TspBase::EnumExplicit(Node * r)  
  192.  {    if(r->level == N)  
  193.      {    StoreX(r);    cv = SumV();  
  194.         if(cv  < bestV)      
  195.          {    bestV = cv  ;  
  196.             for(int i = 0; i < N; i++)  
  197.               bestPath[i] = currentPath[i];  
  198.         }  
  199.     }  
  200.     else  
  201.         for(int i = 0; i < N; i ++)  
  202.          { if(Valid(r,i))   
  203.              {  Node *q = new Node(r->level+1,i,r);    EnumExplicit(q);    }  
  204.         }  
  205. }  
  206. void TspBase::TspBackTrackImplicit()     //回溯隐式  
  207.  {    fout<<"TspBackTrackImplicit ..."<<endl;  
  208. cv=0;  bestV=10000;  
  209.     for(int i=0;i<N;i++)    currentPath[i]=i;  
  210.     BackTrackImplicit(1);  
  211.     Print();  
  212. }  
  213. void TspBase::BackTrackImplicit(int k)  
  214.  {    if(k == N)  
  215.      {    if((cv + g.GetWeight(currentPath[N-1],0)) < bestV)  
  216.          {  
  217.             bestV = cv + g.GetWeight(currentPath[N-1],0);  
  218.             for(int i = 0; i < N; i++)  
  219.               bestPath[i] = currentPath[i];  
  220.         }          
  221.     }  
  222.     else  
  223.         for(int j = k; j < N; j++)  
  224.          { if((cv + g.GetWeight(currentPath[k-1],currentPath[j])) < bestV)  
  225.            {    swap(currentPath[k],currentPath[j]);  
  226.                cv += g.GetWeight(currentPath[k-1],currentPath[k]);  
  227.             BackTrackImplicit(k+1);  
  228.             cv -= g.GetWeight(currentPath[k-1],currentPath[k]);  
  229.             swap(currentPath[k],currentPath[j]);  
  230.           }  
  231.         }  
  232. }  
  233. void TspBase::TspBackTrackExplicit()      // 回溯显式  
  234.  {    fout<<"TspBackTrackExplicit  ..."<<endl;   
  235. cv=0;     bestV=10000;  
  236.      for(int i=0;i<N;i++)     currentPath[i]=i;  
  237.      root=new Node(0,-1,NULL);  
  238.      BackTrackExplicit(root);  
  239.      Print();  
  240. }  
  241. void TspBase::BackTrackExplicit(Node * r)  
  242.  {    int w=0;  //初值  
  243.     if(r->level == N)  
  244.      {    StoreX(r);  
  245.         cv = SumV();  
  246.         if(cv  < bestV)      
  247.          {   bestV = cv  ;  
  248.             for(int i = 0; i < N; i++)        bestPath[i] = currentPath[i];  
  249.         }  
  250.     }  
  251.     else  
  252.         for(int i = 0; i < N; i ++)  
  253.          {  if(Valid(r,i))   
  254.              {    Node *q = new Node(r->level+1,i,r);  
  255.                 w += g.GetWeight(q->currentIndex,i);  
  256.                 if(w < bestV)       BackTrackExplicit(q);  
  257.                 w -= g.GetWeight(q->currentIndex,i);      
  258.             }  
  259.         }  
  260. }  
  261.   
  262.   
  263. void TspBase::Print() //  
  264.  {       fout<<"the shortest path is  ";  
  265.        for(unsigned i = 0; i < N; i++)  
  266.              fout<<bestPath[i] + 1<<"--";  
  267.        fout<<"1"<<endl;  
  268.        fout<<"minimum distance is  "<<bestV<<endl;         
  269. }  
  270.   
  271. void TspBase::TspBB()       // 分支限界法  
  272.  {        fout<<"TspBB(FIFOBB)  ........"<<endl;  
  273. cv = 0;        bestV = 100000;  
  274.         for(unsigned i = 0; i < N; i++)    currentPath[i] = i;  
  275.         root=new Node(0,-1,NULL);  
  276.         FIFOBB();  
  277.         Print();  
  278. }  
  279. void TspBase::FIFOBB()  
  280.  { queue<Node*> q;   Node *r;  
  281.   q.push(root);  
  282.   int w=0;  //初值  
  283.   while(!q.empty())  
  284.    {      r = q.front();      q.pop();  
  285.       if(r->level == N)  
  286.        { StoreX(r);  
  287.         cv = SumV();  
  288.         if(cv  < bestV)      
  289.          {   bestV = cv  ;  
  290.             for(int i = 0; i < N; i++)     bestPath[i] = currentPath[i];  
  291.         }  
  292.       }  
  293.       else  
  294.         for(int i = 0; i < N; i ++)  
  295.          {    if(Valid(r,i))   
  296.              {   Node *s = new Node(r->level+1,i,r);  
  297.                 w += g.GetWeight(s->currentIndex,i);  
  298.                 if(w < bestV)       q.push(s);  
  299.                 w -=  g.GetWeight(s->currentIndex,i);  
  300.             }  
  301.         }  
  302.   }  
  303. }  
  304. int TspBase::SumV()           //用于FIFOBB  
  305.  {    int s = 0;  
  306.     for(int i = 0; i < N; i++)  
  307.         s += g.GetWeight(currentPath[i],currentPath[(i + 1)%N]);  
  308.     return s;  
  309. }  
  310. void TspBase::TspGreedy()  //TSP贪心算法  
  311.  {     fout<<"TspGreedy ........"<<endl;  
  312. bestV = 0;       
  313.     vector<int> NEAR(N); //   
  314.     NEAR[0] = -1;  
  315.     for (int i = 1; i < N; i++)  
  316.        NEAR[i] = 0;  
  317.     bestPath[0] = 1;  
  318.     int t;  
  319.     for (int s = 1; s < N; s++)  
  320.      {  
  321.       int j = 1;  
  322.       while (j < N && NEAR[j] < 0) /   
  323.           j++;  
  324.       int K = j;  
  325.       for (int k = j + 1; k < N; k++)  
  326.          if (NEAR[k] >= 0 &&  g.GetWeight(k,NEAR[k]) < g.GetWeight(j,NEAR[j]))  
  327.                j = k;  
  328.       bestPath[s] = j + 1;  
  329.       bestV +=g.GetWeight(j,NEAR[j]);  
  330.       NEAR[j] = -1;  
  331.       for (k = K; k < N; k++) //调整NEAR值  
  332.          if (NEAR[k] >= 0)  
  333.              NEAR[k] = j;  
  334.       t = j;  
  335.     }  
  336.     bestV += g.GetWeight(t,0);  
  337.     fout<<"the shortest path is  ";  
  338.     for(unsigned w = 0; w < N; w++)  
  339.        fout<<bestPath[w] <<"--";  
  340.     fout<<"1"<<endl;  
  341.     fout<<"minimum distance is  "<<bestV<<endl;     
  342. }  
  343.   
  344. int main(int argc, char* argv[])  
  345.  {   int m,n;  
  346.     ifstream fin("data.txt");  
  347.     if(fin.bad()) return 1;  
  348.     fin >> m >> n;  
  349.     N = n;  
  350.     for(int i=0;i<N;i++)  g.InsertV(i);  
  351.     fin >> g;  
  352.     TspBase it;  
  353.     it.TspEnumImplicit();    it.DataClear(false);  
  354.   
  355.     it.TspBackTrackImplicit();    it.DataClear(false);  
  356.   
  357.     it.TspEnumExplicit();    it.DataClear(true);  
  358.   
  359.     it.TspBackTrackExplicit();    it.DataClear(true);  
  360.   
  361.     it.TspBB();    it.DataClear(true);  
  362.   
  363.     it.TspGreedy();    it.DataClear(false);  
  364.     return 0;  
  365. }  

 

 

执行结果:
the shortest path is  1--3--2--5--4--1
minimum distance is  20

  • 9
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值