旅行售货员问题分支界限法

package test;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

/**

* Created by saishangmingzhu on 2018/12/13.

* 旅行售货员问题

*/

public class TravellingSalesmanProblem {

//图

private int[][] pointIndex=new int[][]{

{0,30,6,4},

{30,0,5,10},

{6,5,0,20},

{4,10,20,0}};

public static void main(String[] arg){

new TravellingSalesmanProblem().branchAndBoundMethod();

}

/**

* 分支界限法-优先队列式

* 优先队列式求解时,到达第一个没有子结点的活结点时,即为最优解

*/

public void branchAndBoundMethod() {

List pointList=new ArrayList<>();

pointList.add(new Point(0,"1"));

pointList.add(new Point(1,"2"));

pointList.add(new Point(2,"3"));

pointList.add(new Point(3,"4"));

Node root=new Node();

root.point=pointList.get(0);

root.childPointList.addAll(pointList);

root.childPointList.remove(root.point);

Node minNode=new Node();

minNode.value=Integer.MAX_VALUE;

List currentLiveNodeList=new ArrayList<>();

currentLiveNodeList.add(root);

while (currentLiveNodeList.size()>0) {

Node liveNode = currentLiveNodeList.get(0);

List childPointList = liveNode.childPointList;

for (Point childPoint : childPointList) {

int value=pointIndex[childPoint.index][liveNode.point.index];

if (value!=0) {

Node childNode = new Node();

childNode.parentNode=liveNode;

childNode.point = childPoint;

childNode.childPointList.addAll(childPointList);

childNode.childPointList.remove(childPoint);

childNode.value = liveNode.value + value;

if (childNode.childPointList.size() == 0 ) {

if (pointIndex[0][childPoint.index] != 0) {

childNode.value += pointIndex[0][childPoint.index];

if (childNode.value

minNode.value=childNode.value;

minNode.point=childNode.point;

minNode.parentNode=childNode.parentNode;

}

} else {

continue;

}

}

currentLiveNodeList.add(childNode);

}

}

currentLiveNodeList.remove(liveNode);

Collections.sort(currentLiveNodeList, new Comparator() {

@Override

public int compare(Node o1, Node o2) {

return o2.value-o1.value;

}

});

}

System.out.println(minNode.value);

getParents(minNode);

}

private void getParents(Node node){

if (node==null){

return;

}

getParents(node.parentNode);

System.out.println(node.point.name);

}

class Node{

private Node parentNode;

private Point point;

private List childNodeList=new ArrayList<>();

private List childPointList=new ArrayList<>();

private int value;

}

class Point{

private int index;

private String name;

public Point(int index, String name) {

this.index = index;

this.name = name;

}

}

}

©著作权归作者所有:来自51CTO博客作者塞上名猪的原创作品,如需转载,请注明出处,否则将追究法律责任

旅行售货员问题 java_旅行售货员问题-分支界限法相关推荐

  1. 单源路径分支界限java_单源最短路径-分支界限法

    单源最短路径-分支界限法-优先队列式.这里使用无回路的有向图,便于构建树.构建好树后,从根结点作为起始源,加入结点队列,然后判断获取队列中最短的路径结点做为活结点,将活结点的所有子结点加入队列,移除活 ...

  2. 最大团问题-分支界限法

    最大团问题-分支界限法 遍历所有点构造二叉树: 广度遍历树,遍历过程中判断当前结点的点数据时,是否构成完全子图,如果不能则只将右结点加入队列,每次选取队列中完全子图最大的结点作为活结点,无子结点时到达 ...

  3. 从0-1背包问题学习回溯法、分支界限法、动态规划

    一.0-1背包问题的描述 下面将使用回溯法.分支界限法.动态规划法来分析和解决此问题. 二.回溯法 (1)算法步骤 (2)代码如下(没有裁剪函数): 用i和n来判断结束与否,是因为解空间结构是完全二叉 ...

  4. 算法分析与设计第二版(李春葆)第六章分支界限法

    分支界限法类似于回溯法,一般回溯法目标是找出所有解,二分支界限法是找出满足条件的一个解或者最优解 算法 解空间树搜索方式 存储结点的常用数据结构 结点存储特性 常用应用 回溯法 深度优先搜索 栈 活结 ...

  5. 五大常用算法——分治法,动态规划,回溯法,分支界限法,贪心算法

    (1) 分治法 将一个难以直接解决的大问题,分割成一些规模较小的相同问题 快速排序 快排也是分治的一个实例,快排每一趟会选定一个数,将比这个数小的放左面,比这个数大的放右面, 然后递归分治求解两个子区 ...

  6. 算法分析课设(十一)博物馆守卫问题、世界名画陈列馆问题(分支界限法)

    免责声明 不想打字了.. 题目 在某博物馆中摆放了非常重要的文物,为了节省人力,该博物馆专门购买了警卫机器人来看管这些文物.该博物馆的房间排列整齐,房间的大小相同.每个警卫机器人能够巡查的范围除本身所 ...

  7. 算法设计-分支界限法——装载问题

    算法介绍 分支界限法: 分支限界法常以广度优先或以最小耗费(最大效益)优先的方式搜索问题的解空间树. 在分支限界法中,每一个活结点只有一次机会成为扩展结点.活结点一旦成为扩展结点,就一次性产生其所有儿 ...

  8. TSP旅行商问题之分支界限法法求解(C语言)

    #include <stdio.h> #include <malloc.h>#define NoEdge 1000struct MinHeapNode {int lcost; ...

  9. 分支界限法0 1背包 c语言,分支限界法之布线问题(1)

    一.要求: 1.输入电路板区域n*m以及布线的起始位置和结束位置: 2.输出布线方案: 3.可以使用c或者vc实现 二.问题分析及实验原理: 在n*m的方格阵列中存在封锁区域(布线时必须绕开的区域), ...

最新文章

  1. 看完你会为自己哭,或者为他们哭
  2. 《leetcode》single-number-ii
  3. Linux命令替换字符串
  4. matlab6.0序列号,MFC软件获取USB设备的制造商、产品、序列号
  5. python math库函数源码_11. math库函数
  6. Bzoj 3196 Tyvj 1730 二逼平衡树
  7. python自动化六--操作mysql,redis,发送邮件,EXCEL,MD5加密
  8. codeforces 615B. Longtail Hedgehog
  9. 当Java枚举遇到位掩码,还能这么玩?
  10. 自适应输出表格(ASP版)
  11. Linux 怎么shell脚本定时备份mysql数据库
  12. Kronecker 定理
  13. Oracle ERP Interface堵住--Request Running too long time,查找Request执行的Sql
  14. 百度有啊 真的还有啊
  15. 虚拟机配置opc服务器,组态王怎么配置成opc服务器
  16. 为什么全球最好的大学大半在美国?看看美国的孩子是怎么念书的 ~~
  17. Android 小米全面屏手势底部虚拟按键的适配
  18. 【CVPR2020视频超分辨率】Zooming Slow-Mo: Fast and Accurate One-Stage Space-Time Video Super-Resolution 阅读笔记
  19. TIA博途中触摸屏仿真时画面字体变大,超出范围的处理方法
  20. HTML Table之展开收起

热门文章

  1. BLOCK层代码分析(6)IO下发之SGL聚散列表
  2. SqlServer: 汉字转拼音标量函数
  3. 电波暗室里面的吸波材料有哪些?
  4. 有关《快速软件开发》的几点不解 By Yiming Qian
  5. html中getElementById() 方法
  6. 地图坐标转换(GCJ-02转WGS-84)
  7. 八数码问题matlab实现,用A*算法解决八数码问题 MATLAB
  8. 求程序运行时间的函数clock()以及 CLOCKS_PER_SEC与CLK_TCK的区别,用法
  9. mySagasoft MIS与WebMIS架构权限管理功能
  10. 【解决方案】智慧校园建设如何通过国标GB/T28181级联打造县-市-省三级架构的视频集中管理平台?