旅行售货员问题(回溯法搜索排列树)
2006-12-02 11:29
399 查看
用回朔法解旅行售货员问题:
解法1:建立一个无向完全图,根据图建立一颗排列树,用深度优先搜索排列树,找到最优解
困难1:在建树的过程中,如何得知当前结点的祖先结点有哪些?(即如何得知哪些结点已经访问过)
解决办法:每个树结点的都含有这样一个信息域,负责记录还没有访问过的结点集合。具体做法是在创建每个结点时,结点的这个信息域的值等于从其父节点的这个信息域的值集合中删除自身结点得到的集合。
困难2:在遍历排列树的时候,需要记录结点间的路径和,我是从下至上记录的(遍历到叶结点才开始记录权值,每向上走一层,都比较同一父亲的子树中的路径,选取最短的,舍弃其它的),而不是采用从上至下(在遍历每一个结点时,立即计算从根节点到该结点的路径和,直至到达叶结点,再比较刚得到的路径是否为当前最短路径), 如何记录遍历的路径?
解决办法:从上至下的方法,设置一个栈,在遍历每一个结点前入栈,对该结点遍历结束后出栈;
然而,从下至上法,路径的记录比较麻烦,因为每向上走一层,都要在同一父亲的子树间选取最优的,舍弃其它的。用一个堆栈是不能解决这个问题的,还需要一个保留最优路径的栈时刻跟踪记录当前得到的最优子树,核心代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public int Traverse(TreeNode node,Graph g,ref Stack priPathStack)//自下而上计算权值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//找到该结点下的最小路径,并返回最小路径,priPathStack负责传出这个结点返回的最优路径的序列(不包括这个结点)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (node.firstchild == null)//到了叶结点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return (int)((ArrayList)g.AdjacentMatrix[node.data])[0];//叶结点与根结点之间的路径,因为需要回路
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int iTemp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode lastnode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int minPath;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stack emptyStack = new Stack();//!!!!!!!!!!!!!!!!!!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lastnode = node.firstchild;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
minPath = lastnode.upedgeweight + Traverse(lastnode,g,ref emptyStack);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
emptyStack.Push(lastnode.data);//!!!!!!!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
priPathStack = new Stack(emptyStack);//得到第一棵子树内的最优路径,并记下!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lastnode = lastnode.nextsib;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while (lastnode != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
emptyStack.Clear();//开始查找下一棵子树,emptyStack准备存放下一棵子树的最优路径!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
iTemp = lastnode.upedgeweight + Traverse(lastnode,g,ref emptyStack);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (iTemp < minPath)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
minPath = iTemp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
emptyStack.Push(lastnode.data);//!!!!!!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
priPathStack = new Stack(emptyStack);//!!!!!!!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lastnode = lastnode.nextsib;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
priPathStack = new Stack(priPathStack);//因为堆栈的复制结果会颠倒顺序,所以需要这样倒一下!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return minPath;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
解法2:(巧妙)不需要建立排列树,用一个数组实现结点的排列遍历,并存放当前遍历的路径,核心代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void Backtrack(int t)//t从1到n
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (t > g.NodeNumber)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Output();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = t; i <= g.NodeNumber; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Swap(ref alnode,t-1,i-1);//注意,层次从1开始,结点编号从0开始
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Constraint(t) && Bound(t))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Backtrack(t+1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Swap(ref alnode,t-1,i-1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
每趟遍历到最后一个元素,数组alnode中存放的就是当前遍历的结点顺序。
解法1:建立一个无向完全图,根据图建立一颗排列树,用深度优先搜索排列树,找到最优解
困难1:在建树的过程中,如何得知当前结点的祖先结点有哪些?(即如何得知哪些结点已经访问过)
解决办法:每个树结点的都含有这样一个信息域,负责记录还没有访问过的结点集合。具体做法是在创建每个结点时,结点的这个信息域的值等于从其父节点的这个信息域的值集合中删除自身结点得到的集合。
困难2:在遍历排列树的时候,需要记录结点间的路径和,我是从下至上记录的(遍历到叶结点才开始记录权值,每向上走一层,都比较同一父亲的子树中的路径,选取最短的,舍弃其它的),而不是采用从上至下(在遍历每一个结点时,立即计算从根节点到该结点的路径和,直至到达叶结点,再比较刚得到的路径是否为当前最短路径), 如何记录遍历的路径?
解决办法:从上至下的方法,设置一个栈,在遍历每一个结点前入栈,对该结点遍历结束后出栈;
然而,从下至上法,路径的记录比较麻烦,因为每向上走一层,都要在同一父亲的子树间选取最优的,舍弃其它的。用一个堆栈是不能解决这个问题的,还需要一个保留最优路径的栈时刻跟踪记录当前得到的最优子树,核心代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public int Traverse(TreeNode node,Graph g,ref Stack priPathStack)//自下而上计算权值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//找到该结点下的最小路径,并返回最小路径,priPathStack负责传出这个结点返回的最优路径的序列(不包括这个结点)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (node.firstchild == null)//到了叶结点
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return (int)((ArrayList)g.AdjacentMatrix[node.data])[0];//叶结点与根结点之间的路径,因为需要回路
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int iTemp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeNode lastnode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int minPath;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Stack emptyStack = new Stack();//!!!!!!!!!!!!!!!!!!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lastnode = node.firstchild;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
minPath = lastnode.upedgeweight + Traverse(lastnode,g,ref emptyStack);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
emptyStack.Push(lastnode.data);//!!!!!!!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
priPathStack = new Stack(emptyStack);//得到第一棵子树内的最优路径,并记下!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lastnode = lastnode.nextsib;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while (lastnode != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
emptyStack.Clear();//开始查找下一棵子树,emptyStack准备存放下一棵子树的最优路径!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
iTemp = lastnode.upedgeweight + Traverse(lastnode,g,ref emptyStack);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (iTemp < minPath)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
minPath = iTemp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
emptyStack.Push(lastnode.data);//!!!!!!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
priPathStack = new Stack(emptyStack);//!!!!!!!!!!!!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lastnode = lastnode.nextsib;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
priPathStack = new Stack(priPathStack);//因为堆栈的复制结果会颠倒顺序,所以需要这样倒一下!!!!!!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return minPath;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
解法2:(巧妙)不需要建立排列树,用一个数组实现结点的排列遍历,并存放当前遍历的路径,核心代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public void Backtrack(int t)//t从1到n
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (t > g.NodeNumber)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Output();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = t; i <= g.NodeNumber; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Swap(ref alnode,t-1,i-1);//注意,层次从1开始,结点编号从0开始
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (Constraint(t) && Bound(t))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Backtrack(t+1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Swap(ref alnode,t-1,i-1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
每趟遍历到最后一个元素,数组alnode中存放的就是当前遍历的结点顺序。
相关文章推荐
- 算法java实现--回溯法--旅行售货员问题--排列树
- 旅行售货员问题(回溯法实现)
- C语言使用回溯法解旅行售货员问题与图的m着色问题
- 旅行售货员问题 回溯法 与 01背包的区别
- 旅行售货员问题(回溯法)
- 回溯算法;旅行售货员问题;排列树;需要访问的是各个地点,不是路径;第一个排列点是出发点;O(n!)
- 旅行售货员问题-回溯法
- 回溯子集树与排列树——装载问题&旅行售货员问题(算法设计课题)
- 分支限界法----旅行售货员问题
- 旅行售货员问题的回溯法求解
- 旅行售货员(回溯法)
- 算法java实现--分支限界法--旅行售货员问题
- 旅行售货员问题1
- 旅行售货员问题(c++)
- 分支限界法----旅行售货员问题
- 采用openMP并行方法,实现用分支界限法解决的旅行售货员问题
- 回溯法_旅行售货员问题
- 回溯法搜索排列树的问题
- 分支限界法----旅行售货员问题
- 旅行商问题(深度优先搜索 回溯法 排列树)