分支限界法最大团问题和旅行背包问题java源程序Word格式文档下载.docx

上传人:b****6 文档编号:21329774 上传时间:2023-01-29 格式:DOCX 页数:13 大小:36.55KB
下载 相关 举报
分支限界法最大团问题和旅行背包问题java源程序Word格式文档下载.docx_第1页
第1页 / 共13页
分支限界法最大团问题和旅行背包问题java源程序Word格式文档下载.docx_第2页
第2页 / 共13页
分支限界法最大团问题和旅行背包问题java源程序Word格式文档下载.docx_第3页
第3页 / 共13页
分支限界法最大团问题和旅行背包问题java源程序Word格式文档下载.docx_第4页
第4页 / 共13页
分支限界法最大团问题和旅行背包问题java源程序Word格式文档下载.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

分支限界法最大团问题和旅行背包问题java源程序Word格式文档下载.docx

《分支限界法最大团问题和旅行背包问题java源程序Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《分支限界法最大团问题和旅行背包问题java源程序Word格式文档下载.docx(13页珍藏版)》请在冰豆网上搜索。

分支限界法最大团问题和旅行背包问题java源程序Word格式文档下载.docx

{1,0,0,1,1},

{1,1,1,1,1}

};

graphg=newgraph(a);

g.find_answer();

g.show();

}

}

----------------------------------------------------------------------------------------------------------------------

importjava.util.ArrayList;

importjava.util.PriorityQueue;

publicclassgraph{

privateArrayList<

point>

points;

answer_point;

publicgraph(int[][]a){

this.points=newArrayList<

>

();

this.answer_point=newArrayList<

intn=a.length;

for(inti=0;

i<

n;

i++)

this.points.add(newpoint());

for(intj=i+1;

j<

j++)

if(a[i][j]==1){

pointpi=this.points.get(i);

pointpj=this.points.get(j);

pi.add_nearpoint(pj);

pj.add_nearpoint(pi);

}

publicvoidfind_answer(){

inttotal_points=this.points.size();

PriorityQueue<

Node>

enode=newPriorityQueue<

intnode_in=0;

pointnode_point=this.points.get(0);

ArrayList<

nowanswer=newArrayList<

intmaxnum_point=this.points.size();

Nodestartnode=newNode(node_in,node_point,nowanswer,maxnum_point);

enode.offer(startnode);

Nodenode=null;

while(true){

if(enode.isEmpty())

break;

node=enode.poll();

if(node.get_node_in()==maxnum_point)

if(node.ifleft()){

intnew_node_in=node.get_node_in()+1;

intnew_answer=node.get_maxnum_point();

pointnewpoint;

if(new_node_in==total_points)

newpoint=null;

else

newpoint=this.points.get(new_node_in);

ArrayList<

new_answer_point=newArrayList<

(node.get_answer_point());

new_answer_point.add(node.get_node_point());

Nodenew_node=newNode(new_node_in,newpoint,new_answer_point,new_answer);

enode.offer(new_node);

if(new_node.get_answer_point().size()>

this.answer_point.size())

this.answer_point=newArrayList<

(new_node.get_answer_point());

}

if(node.ifright(this.points.size(),this.answer_point.size())){

new_node_answer_points=node.get_answer_point();

intnew_node_max=node.get_maxnum_point()-1;

pointp=this.points.get(new_node_in);

Nodenewnode=newNode(new_node_in,p,new_node_answer_points,new_node_max);

enode.add(newnode);

}

publicvoidshow(){

System.out.println(this.answer_point);

publicclasspoint{

privatestaticintids=1;

privateintid;

nearpoints;

publicpoint(){

super();

this.id=ids++;

this.nearpoints=newArrayList<

publicStringtoString(){

return"

顶点"

+this.id;

publicvoidadd_nearpoint(pointp){

this.nearpoints.add(p);

publicbooleancheck_if_nearpoint(pointp){

returnthis.nearpoints.contains(p);

publicclassNodeimplementsComparable<

{

privateintnode_in;

privatepointnode_point;

answer_points;

privateintmaxnum_point;

publicNode(intn,pointp,ArrayList<

a,intm){

this.node_in=n;

this.node_point=p;

this.answer_points=a;

this.maxnum_point=m;

publicintcompareTo(Nodeo){

if(this.maxnum_point>

o.maxnum_point)return-1;

if(this.maxnum_point<

o.maxnum_point)return1;

return0;

publicintget_node_in(){returnthis.node_in;

publicArrayList<

get_answer_point(){returnthis.answer_points;

publicbooleanifleft(){

for(pointp:

this.answer_points)

if(!

p.check_if_nearpoint(this.node_point))

returnfalse;

returntrue;

publicbooleanifright(inttotal_point,intnowanswer){

intleftpoint=total_point-this.node_in;

if(this.answer_points.size()+leftpoint>

nowanswer)

returntrue;

returnfalse;

publicintget_maxnum_point(){returnthis.maxnum_point;

publicpointget_node_point(){returnthis.node_point;

-----------------------------------------------------------------------

2、分支限界法求旅行售货员

package旅行售货员问题;

int[][]a={

{0,30,6,4},

{30,0,5,10},

{6,5,0,20},

{4,10,20,0},

};

graphg=newgraph(a);

g.findanswer();

g.show();

-------------------------------------------------------------------------------

importjava.util.Collections;

privateint[][]a;

privateintanswerpathlong;

publicgraph(int[][]b){

this.points=newArrayList<

intn=b.length;

for(inti=0;

i<

n;

i++)

this.a=b;

this.answerpathlong=Integer.MAX_VALUE;

this.answer_points=null;

this.set_point_short_long();

publicintget_value(pointpx,pointpy){

intp1id=px.getid();

intp2id=py.getid();

returnthis.a[p1id][p2id];

publicvoidfindanswer(){

enode=newPriorityQueue<

root_list=this.points;

introot_answer=0;

this.points)

root_answer+=p.getshortestlong();

introot_in=0;

introot_path_long=0;

Noderoot_node=newNode(root_in,root_list,root_path_long,root_answer);

enode.offer(root_node);

if(enode.isEmpty())break;

Nodeold_node=enode.poll();

inttotal_point=this.points.size();

if(old_node.if_end(total_point)){

this.answer_points=newArrayList<

(old_node.get_now_sort());

this.answerpathlong=old_node.get_now_path_long();

if(old_node.if_end_father(total_point)){

intnew_node_in=old_node.get_node_in()+1;

new_node_set=old_node.get_now_sort();

pointold_point_node=old_node.get_node_point();

pointnew_point_node=new_node_set.get(new_node_in);

pointstartpoint=this.points.get(0);

intnew_node_nowpath=old_node.get_now_path_long()+this.get_value(old_point_node,new_point_node)+this.get_value(new_point_node,startpoint);

intmaybe_the_best=new_node_nowpath;

Nodenew_node=newNode(new_node_in,new_node_set,new_node_nowpath,maybe_the_best);

if(new_node.get_now_path_long()<

this.answerpathlong){

this.answerpathlong=new_node.get_now_path_long();

enode.offer(new_node);

continue;

for(inti=old_node.get_node_in()+1;

total_point;

i++){//尝试原节点的各个分支

if(old_node.if_in_i(i,this.a,this.answerpathlong)){

intnew_node_in=old_node.get_node_in()+1;

ArrayList<

newnode_nowset=newArrayList<

intj=new_node_in;

this.changeij(newnode_nowset,i,j);

pointold_node_to_point=old_node.get_node_point();

pointnew_node_to_point=newnode_nowset.get(new_node_in);

intnew_maybe_the_best=old_node.get_node_answer()+this.get_value(old_node_to_point,new_node_to_point)-old_node_to_point.getshortestlong();

intnew_node_path_long=old_node.get_now_path_long()+this.get_value(old_node_to_point,new_node_to_point);

Nodenew_node=newNode(new_node_in,newnode_nowset,new_node_path_long,new_maybe_the_best);

privatevoidchangeij(ArrayList<

points,inti,intj){

Collections.swap(points,i,j);

System.out.println(this.answer_points);

System.out.println("

最优路径长度为:

"

+this.answerpathlong);

privatevoidset_point_short_long(){

pointp;

int[]value;

this.points.size();

i++){

p=this.points.get(i);

value=this.a[i];

for(intx:

value){

if(x==0)continue;

if(x<

p.getshortestlong())p.setshortestlong(x);

privatestaticintids=0;

privateintshortestlong;

publicpoint(){

this.id=point.ids++;

this.shortestlong=Integer.MAX_VALUE;

publicintgetid(){

returnid;

@Override

publicStringtoString(){

顶点:

+this.id+"

;

publicintgetshortestlong(){

returnshortestlong;

publicvoidsetshortestlong(ints){

this.shortestlong=s;

 

{

now_sort;

privateintnow_path_long;

privateintnode_answer;

publicNode(intin,ArrayList<

set,intp_l,intn_a){

this.node_in=in;

this.now_sort=set;

this.now_path_long=p_l;

this.node_answer=n_a;

this.node_point=this.now_sort.get(in);

publicbooleanif_in_i(inti,int[][]a,intnow_long){

pointnow_point=now_sort.get(node_in);

pointpointi=now_sort.get(i);

intnow_id=now_point.getid();

intpointi_id=pointi.get

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 天文地理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1